jaroslav@314: /** jaroslav@314: * Back 2 Browser Bytecode Translator jaroslav@1787: * Copyright (C) 2012-2015 Jaroslav Tulach jaroslav@314: * jaroslav@314: * This program is free software: you can redistribute it and/or modify jaroslav@314: * it under the terms of the GNU General Public License as published by jaroslav@314: * the Free Software Foundation, version 2 of the License. jaroslav@314: * jaroslav@314: * This program is distributed in the hope that it will be useful, jaroslav@314: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@314: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@314: * GNU General Public License for more details. jaroslav@314: * jaroslav@314: * You should have received a copy of the GNU General Public License jaroslav@314: * along with this program. Look for COPYING file in the top folder. jaroslav@314: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@314: */ Martin@268: package org.apidesign.benchmark.matrixmul; Martin@268: jaroslav@381: import java.io.IOException; jaroslav@381: import java.util.Arrays; Martin@268: Martin@268: public class Matrix { Martin@268: private final int rank; jaroslav@381: private final float data[][]; Martin@268: Martin@268: public Matrix(int r) { jaroslav@381: this(r, new float[r][r]); jaroslav@381: } jaroslav@381: jaroslav@381: private Matrix(int r, float[][] data) { jaroslav@381: this.rank = r; jaroslav@381: this.data = data; Martin@268: } Martin@268: Martin@268: public void setElement(int i, int j, float value) { Martin@268: data[i][j] = value; Martin@268: } Martin@268: public float getElement(int i, int j) { Martin@268: return data[i][j]; Martin@268: } Martin@268: Martin@268: public void generateData() { Martin@268: //final Random rand = new Random(); Martin@268: //final int x = 10; Martin@268: for (int i = 0; i < rank; i++) { Martin@268: for (int j = 0; j < rank; j++) { jaroslav@520: data[i][j] = 1 / (1 + i + j); Martin@268: } Martin@268: } Martin@268: } Martin@268: Martin@268: public Matrix multiply(Matrix m) { Martin@268: if (rank != m.rank) { Martin@268: throw new IllegalArgumentException("Rank doesn't match"); Martin@268: } Martin@268: Martin@268: final float res[][] = new float[rank][rank]; Martin@268: for (int i = 0; i < rank; i++) { Martin@268: for (int j = 0; j < rank; j++) { Martin@268: float ij = 0; Martin@268: for (int q = 0; q < rank; q++) { Martin@268: ij += data[i][q] * m.data[q][j]; Martin@268: } Martin@268: res[i][j] = ij; Martin@268: } Martin@268: } jaroslav@381: return new Matrix(rank, res); Martin@268: } Martin@268: jaroslav@381: public void printOn(Appendable s) throws IOException { Martin@268: for (int i = 0; i < rank; i++) { jaroslav@381: String sep = ""; Martin@268: for (int j = 0; j < rank; j++) { jaroslav@381: s.append(sep + data[i][j]); jaroslav@381: sep = " "; Martin@268: } jaroslav@381: s.append("\n"); Martin@268: } Martin@268: } jaroslav@381: jaroslav@381: @Override jaroslav@381: public boolean equals(Object obj) { jaroslav@381: if (obj instanceof Matrix) { jaroslav@381: Matrix snd = (Matrix)obj; jaroslav@381: if (snd.rank != rank) { jaroslav@381: return false; jaroslav@381: } jaroslav@381: for (int i = 0; i < rank; i++) { jaroslav@381: for (int j = 0; j < rank; j++) { jaroslav@381: if (data[i][j] != snd.data[i][j]) { jaroslav@381: return false; jaroslav@381: } jaroslav@381: } jaroslav@381: } jaroslav@381: return true; jaroslav@381: } jaroslav@381: return false; jaroslav@381: } jaroslav@381: jaroslav@381: @Override jaroslav@381: public int hashCode() { jaroslav@381: int hash = 3; jaroslav@381: hash = 97 * hash + this.rank; jaroslav@409: hash = 97 * hash + Arrays.deepHashCode(this.data); jaroslav@381: return hash; jaroslav@381: } Martin@268: }