benchmarks/matrix-multiplication/src/main/java/org/apidesign/benchmark/matrixmul/Matrix.java
2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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.
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.
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.
18 package org.apidesign.benchmark.matrixmul;
20 import java.io.IOException;
21 import java.util.Arrays;
24 private final int rank;
25 private final float data[][];
27 public Matrix(int r) {
28 this(r, new float[r][r]);
31 private Matrix(int r, float[][] data) {
36 public void setElement(int i, int j, float value) {
39 public float getElement(int i, int j) {
43 public void generateData() {
44 //final Random rand = new Random();
46 for (int i = 0; i < rank; i++) {
47 for (int j = 0; j < rank; j++) {
53 public Matrix multiply(Matrix m) {
55 throw new IllegalArgumentException("Rank doesn't match");
58 final float res[][] = new float[rank][rank];
59 for (int i = 0; i < rank; i++) {
60 for (int j = 0; j < rank; j++) {
62 for (int q = 0; q < rank; q++) {
63 ij += data[i][q] * m.data[q][j];
68 return new Matrix(rank, res);
71 public void printOn(Appendable s) throws IOException {
72 for (int i = 0; i < rank; i++) {
74 for (int j = 0; j < rank; j++) {
75 s.append(sep + data[i][j]);
83 public boolean equals(Object obj) {
84 if (obj instanceof Matrix) {
85 Matrix snd = (Matrix)obj;
86 if (snd.rank != rank) {
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]) {
102 public int hashCode() {
104 hash = 97 * hash + this.rank;
105 hash = 97 * hash + Arrays.deepHashCode(this.data);