benchmarks/matrix-multiplication/src/main/java/org/apidesign/benchmark/matrixmul/Matrix.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 24 Feb 2015 11:12:53 +0100
changeset 1787 ea12a3bb4b33
parent 520 b03a2d2b1fdc
permissions -rw-r--r--
Using year range 2012-2015 in copyright header
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.benchmark.matrixmul;
    19 
    20 import java.io.IOException;
    21 import java.util.Arrays;
    22 
    23 public class Matrix {
    24     private final int rank;
    25     private final float data[][];
    26     
    27     public Matrix(int r) {
    28         this(r, new float[r][r]);
    29     }
    30     
    31     private Matrix(int r, float[][] data) {
    32         this.rank = r;
    33         this.data = data;
    34     }
    35     
    36     public void setElement(int i, int j, float value) {
    37         data[i][j] = value;
    38     }
    39     public float getElement(int i, int j) {
    40         return data[i][j];
    41     }
    42     
    43     public void generateData() {
    44         //final Random rand = new Random();
    45         //final int x = 10;
    46         for (int i = 0; i < rank; i++) {
    47             for (int j = 0; j < rank; j++) {
    48                 data[i][j] = 1 / (1 + i + j);
    49             }
    50         }
    51     }
    52 
    53     public Matrix multiply(Matrix m) {
    54         if (rank != m.rank) {
    55             throw new IllegalArgumentException("Rank doesn't match");
    56         }
    57         
    58         final float res[][] = new float[rank][rank];
    59         for (int i = 0; i < rank; i++) {
    60             for (int j = 0; j < rank; j++) {
    61                 float ij = 0;
    62                 for (int q = 0; q < rank; q++) {
    63                     ij += data[i][q] * m.data[q][j];
    64                 }
    65                 res[i][j] = ij;
    66             }
    67         }
    68         return new Matrix(rank, res);
    69     }
    70     
    71     public void printOn(Appendable s) throws IOException {
    72         for (int i = 0; i < rank; i++) {
    73             String sep = "";
    74             for (int j = 0; j < rank; j++) {
    75                 s.append(sep + data[i][j]);
    76                 sep = " ";
    77             }
    78             s.append("\n");
    79         }
    80     }
    81 
    82     @Override
    83     public boolean equals(Object obj) {
    84         if (obj instanceof Matrix) {
    85             Matrix snd = (Matrix)obj;
    86             if (snd.rank != rank) {
    87                 return false;
    88             }
    89             for (int i = 0; i < rank; i++) {
    90                 for (int j = 0; j < rank; j++) {
    91                     if (data[i][j] != snd.data[i][j]) {
    92                         return false;
    93                     }
    94                 }
    95             }
    96             return true;
    97         }
    98         return false;
    99     }
   100 
   101     @Override
   102     public int hashCode() {
   103         int hash = 3;
   104         hash = 97 * hash + this.rank;
   105         hash = 97 * hash + Arrays.deepHashCode(this.data);
   106         return hash;
   107     }
   108 }