benchmarks/matrix-multiplication/src/main/java/org/apidesign/benchmark/matrixmul/Matrix.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 13 Dec 2012 08:40:39 +0100
branchbenchmarks
changeset 314 1a3f102e5ea5
parent 268 e01b65623f72
child 381 70d15cf323ba
permissions -rw-r--r--
Fixing licenses for benchmarks module, so it can be merged to default branch
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 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.PrintStream;
    21 //import java.util.Random;
    22 
    23 public class Matrix {
    24 
    25     private final int rank;
    26     private float data[][];
    27     
    28     public Matrix(int r) {
    29         rank = r;
    30         data = new float[r][r];
    31     }
    32     
    33     public void setElement(int i, int j, float value) {
    34         data[i][j] = value;
    35     }
    36     public float getElement(int i, int j) {
    37         return data[i][j];
    38     }
    39     
    40     public void generateData() {
    41         //final Random rand = new Random();
    42         //final int x = 10;
    43         for (int i = 0; i < rank; i++) {
    44             for (int j = 0; j < rank; j++) {
    45                 data[i][j] = i + j;
    46             }
    47         }
    48     }
    49 
    50     public Matrix multiply(Matrix m) {
    51         if (rank != m.rank) {
    52             throw new IllegalArgumentException("Rank doesn't match");
    53         }
    54         
    55         final float res[][] = new float[rank][rank];
    56         for (int i = 0; i < rank; i++) {
    57             for (int j = 0; j < rank; j++) {
    58                 float ij = 0;
    59                 for (int q = 0; q < rank; q++) {
    60                     ij += data[i][q] * m.data[q][j];
    61                 }
    62                 res[i][j] = ij;
    63             }
    64         }
    65         data = res;
    66         
    67         return this;
    68     }
    69     
    70     /*
    71     public void printOn(PrintStream s) {
    72         for (int i = 0; i < rank; i++) {
    73             for (int j = 0; j < rank; j++) {
    74                 s.printf("%f ", data[i][j]);
    75             }
    76             s.println();
    77         }
    78     }
    79     */
    80     
    81 }