A.Maine
A.Maine

Reputation: 117

Implementing vector multiplication in java

Currently I'm trying to implement a way to be able to use vector- and matrix-multiplication in java, right now I have the code:

package ai2;

public class MyMatrix {
    int[][] alpha;
    int a;
    int b;
    int rowsB;
    int colsB;

    public MyMatrix(int a, int b) {
        this.a = a;
        this.b = b;
        alpha = new int[a][b];
        for (int k = 0; k < a; k++) {
            for (int l = 0; l < b; l++) {
                alpha[k][l] = 0;
            }
        }
    }

    public void insertValue(int o, int q, int z) {
        this.alpha[o][q] = z;
    }

    public void print() {
        for (int k = 0; k < a; k++) {
            for (int l = 0; l < b; l++) {
                System.out.print(this.alpha[k][l] + " ");
            }
            System.out.println();
        }
    }

    public void multiplyMatrix(MyMatrix B) {
        MyMatrix created = new MyMatrix(this.a, B.b);
        for (int m = 0; m < a; m++) {
            for (int k = 0; k < b; k++) {
                for (int l = 0; k < this.a; l++) {
                    myMatrixC[i][j] += myMatrixA[i][k] * myMatrixB[k][j];
                }
            }
        }
    }

    public static void main(String[] args) {
        MyMatrix a = new MyMatrix(2, 2);
        a.insertValue(0, 0, 1);
        a.insertValue(1, 1, 1);
        a.print();
        MyMatrix b = new MyMatrix(2, 2);
        b.insertValue(0, 0, 1);
        b.insertValue(1, 0, 1);
        // System.out.println(a);
    }
}

The problem is my multiplyMatrix method, it takes a MyMatrix object but I cant reach the values using for example:

MyMatrixA[k][l]

I need some sort of idea to reach those values or perhaps a smarter implementation, I cannot use packages outside of java, thankful for any help!

Upvotes: 0

Views: 1151

Answers (2)

Mena
Mena

Reputation: 48404

Square brackets in Java are only for accessing array elements.

Your syntax there will not compile, and you cannot access your matrix elements that way.

Why don't you just implement a getAlpha getter in your MyMatrix class that returns the value for alpha (or better, a copy thereof, to ensure immutability)?

You could then reference it with theMatrixInstance.getAlpha()[k][l].

You could also simplify a bit and implement a get method taking two indices.

That would allow you to check whether the given indices are within the bounds of your two-dimensional array and throw a custom exception (or return some default value) rather than the ArrayIndexOutOfBoundsException you'd otherwise get.

Upvotes: 1

RaffleBuffle
RaffleBuffle

Reputation: 5455

Replace this line

myMatrixC[i][j] += myMatrixA[i][k] * myMatrixB[k][j];

with

created.alpha[i][j] += this.alpha[i][k] * B.alpha[k][j];

Or better yet, replace

MyMatrix created = new MyMatrix(this.a, B.b);

with

MyMatrix A = this;
MyMatrix C = new MyMatrix(this.a, B.b);

then you can do

C.alpha[i][j] += A.alpha[i][k] * B.alpha[k][j];

Which reads a little more clearly.

Finally, no need to initialize alpha with 0's in your constructor, this happens automatically.

Upvotes: 1

Related Questions