robert trudel
robert trudel

Reputation: 5749

Multiplying two matrices with Java

I try to multiplying 2 matrices. I tried with R software: check this thread: Multiplying two matrices in R

Now i try to do the same thing in Java.

I use Jama library for matrix.

my power function

public Matrix power(Matrix M, double p) {
    EigenvalueDecomposition evd = M.eig();
    Matrix D = evd.getD();
    for (int i = 0; i < D.getColumnDimension(); i++) {
        D.set(i, i, Math.pow(D.get(i, i), p));
    }

    Matrix V = evd.getV();

    return V.times(D.times(V.transpose()));
}


double[][] matDouble1 = {{0.25, 0.25, 0.25, 0.25}};
double[][] matDouble2 = {{0, 0, 1, 0},
                       {0.333, 0, 0.333, 0.333},
                       {1, 0, 0, 0},
                       {0, 1, 0, 0}};
Matrix mat1 = new Matrix(matDouble1);
Matrix mat2 = new Matrix(matDouble2);
mat2 = power(mat2, 10000);
mat1 = mat1.times(mat2.transpose());

When is display mat1 i get:

0.25018740608813655 0.2498123125257854  0.2501874060881363  0.24981231252578548

instead of

0.5 0 0.5 0

with R i do

mpower = function(M,p) {
     A = as.matrix(M)

    if (dim(A)[1] != dim(A)[2]) stop("not a square matrix")

    # M^{-1} = the matrix inverse of M
    if (p==-1) return(solve(A))

    # M^0 = I
    if (p==0) return(diag(1,dim(A)[1],dim(A)[2]))

    # M^1 = M
    if (p==1) return(A)

    if (p < -1) stop("only powers >= -1 allowed")
    if (p != as.integer(p)) stop("only integer powers allowed")

    R = A
    for (i in 2:p) {
        R = R %*% A
    }
    return(R)

}

mat1<-matrix(c(0.25,0.25,0.25,0.25),nrow=1)
mat2<-matrix(c(0,0,1,0,0.3,0,0.3,0.3,1,0,0,0,0,1,0,0),nrow=4)
mat1%*%t(mpower(mat2,10000))

i get

[1,] 0.4996252    0 0.4996252    0

Upvotes: 0

Views: 1429

Answers (1)

trutheality
trutheality

Reputation: 23465

Just did this in MATLAB:

>> a = [0.25, 0.25, 0.25, 0.25]

a =

    0.2500    0.2500    0.2500    0.2500

>> b= [0 0 1 0; .333 0 .333 .333; 1 0 0 0; 0 1 0 0]

b =

         0         0    1.0000         0
    0.3330         0    0.3330    0.3330
    1.0000         0         0         0
         0    1.0000         0         0

>> c = b^10000

c =

    1.0000         0         0         0
    0.4993         0    0.4993         0
         0         0    1.0000         0
    0.4993         0    0.4993         0

>> a*c'

ans =

    0.2500    0.2496    0.2500    0.2496

The Java code is working correctly. However:

>> a*c

ans =

    0.4996         0    0.4996         0

So your matrix is getting transposed in the R code because you need byrow = TRUE in your as.matrix statements.

To clarify:

mat2<-matrix(c(0,0,1,0,0.3,0,0.3,0.3,1,0,0,0,0,1,0,0),nrow=4)

Creates the matrix

     0       0.3         1         0
     0         0         0         1
     1       0.3         0         0
     0       0.3         0         0

You want

mat2<-matrix(c(0,0,1,0,0.3,0,0.3,0.3,1,0,0,0,0,1,0,0),nrow=4,byrow=TRUE)

Which creates the matrix

     0         0         1         0
   0.3         0       0.3       0.3
     1         0         0         0
     0         1         0         0

Upvotes: 2

Related Questions