Difference between revisions of "Matlab to EJML"

From Efficient Java Matrix Library
Jump to: navigation, search
 
(4 intermediate revisions by the same user not shown)
Line 3: Line 3:
 
= Equations =
 
= Equations =
  
Equations is very similar to Matlab but there are a few differencesFor a description of the syntax and list of available functions checkout the [[Equations]] tutorial.
+
If you're a Matlab user you seriously might want to consider using the [[Equations]] interface in EJML.  It is similar to Matlab and can be mixed with the other interfaces.
 +
 
 +
<syntaxHighlight lang="java">
 +
eq.process("[A(5:10,:) , ones(5,5)] .* normF(B) \ C")
 +
</syntaxHighlight>
 +
 
 +
That equation would be horrendous to implement using SimpleMatrix or the operations interfaceTake a look at the [[Equations|Equations tutorial]] to learn more.
  
 
= SimpleMatrix =
 
= SimpleMatrix =
Line 15: Line 21:
 
| eye(3) || SimpleMatrix.identity(3)  
 
| eye(3) || SimpleMatrix.identity(3)  
 
|-
 
|-
| diag({{{[1 2 3]}}}) || SimpleMatrix.diag(1,2,3)  
+
| diag([1 2 3]) || SimpleMatrix.diag(1,2,3)  
 
|-
 
|-
 
| C(1,2) = 5 || A.set(0,1,5)  
 
| C(1,2) = 5 || A.set(0,1,5)  
Line 39: Line 45:
 
| C = -A || C = A.negative()  
 
| C = -A || C = A.negative()  
 
|-
 
|-
| C = A{{{*}}}B || C = A.mult(B)  
+
| C = A*B || C = A.mult(B)  
 
|-
 
|-
 
| C = A + B || C = A.plus(B)  
 
| C = A + B || C = A.plus(B)  
Line 45: Line 51:
 
| C = A - B || C = A.minus(B)  
 
| C = A - B || C = A.minus(B)  
 
|-
 
|-
| C = 2{{{*}}}A || C = A.scale(2)  
+
| C = 2*A || C = A.scale(2)  
 
|-
 
|-
 
| C = A / 2 || C = A.divide(2)  
 
| C = A / 2 || C = A.divide(2)  
Line 78: Line 84:
 
= Procedural =
 
= Procedural =
  
Functions and classes in the procedural interface use DenseMatrix64F as input.  Since SimpleMatrix is a wrapper around DenseMatrix64F its internal matrix can be extracted and passed into any of these functions.
+
Functions and classes in the procedural interface use DMatrixRMaj as input.  Since SimpleMatrix is a wrapper around DMatrixRMaj its internal matrix can be extracted and passed into any of these functions.
  
 
{| class="wikitable" align="center" style="font-size:120%; text-align:left; border-collapse:collapse;"
 
{| class="wikitable" align="center" style="font-size:120%; text-align:left; border-collapse:collapse;"
Line 84: Line 90:
 
! Matlab !! Procedural
 
! Matlab !! Procedural
 
|-
 
|-
| eye(3) || CommonOps.identity(3)  
+
| eye(3) || CommonOps_DDRM.identity(3)  
 
|-
 
|-
 
| C(1,2) = 5 || A.set(0,1,5)  
 
| C(1,2) = 5 || A.set(0,1,5)  
Line 90: Line 96:
 
| C(:) = A || C.setTo(A)  
 
| C(:) = A || C.setTo(A)  
 
|-
 
|-
| C(2,:) = [1,2,3] || CommonOps.insert(new DenseMatrix64F(1,3,true,1,2,3),C,1,0)  
+
| C(2,:) = [1,2,3] || CommonOps_DDRM.insert(new DMatrixRMaj(1,3,true,1,2,3),C,1,0)  
 
|-
 
|-
| C(:,2) = [1,2,3] || CommonOps.insert(new DenseMatrix64F(3,1,true,1,2,3),C,0,1)  
+
| C(:,2) = [1,2,3] || CommonOps_DDRM.insert(new DMatrixRMaj(3,1,true,1,2,3),C,0,1)  
 
|-
 
|-
| C = A(2:4,3:8) || CommonOps.extract(A,1,4,2,8)  
+
| C = A(2:4,3:8) || CommonOps_DDRM.extract(A,1,4,2,8)  
 
|-
 
|-
| diag({{{[1 2 3]}}}) || CommonOps.diag(1,2,3)  
+
| diag([1 2 3]) || CommonOps_DDRM.diag(1,2,3)  
 
|-
 
|-
| C = A' || CommonOps.transpose(A,C)  
+
| C = A' || CommonOps_DDRM.transpose(A,C)  
 
|-
 
|-
| A = A' || CommonOps.transpose(A)  
+
| A = A' || CommonOps_DDRM.transpose(A)  
 
|-
 
|-
| A = -A || CommonOps.changeSign(A)  
+
| A = -A || CommonOps_DDRM.changeSign(A)  
 
|-
 
|-
| C = A {{{*}}} B || CommonOps.mult(A,B,C)  
+
| C = A * B || CommonOps_DDRM.mult(A,B,C)  
 
|-
 
|-
| C = A .{{{*}}} B || CommonOps.elementMult(A,B,C)  
+
| C = A .* B || CommonOps_DDRM.elementMult(A,B,C)  
 
|-
 
|-
| A = A .{{{*}}} B || CommonOps.elementMult(A,B)  
+
| A = A .* B || CommonOps_DDRM.elementMult(A,B)  
 
|-
 
|-
| C = A ./ B || CommonOps.elementDiv(A,B,C)  
+
| C = A ./ B || CommonOps_DDRM.elementDiv(A,B,C)  
 
|-
 
|-
| A = A ./ B || CommonOps.elementDiv(A,B)  
+
| A = A ./ B || CommonOps_DDRM.elementDiv(A,B)  
 
|-
 
|-
| C = A + B || CommonOps.add(A,B,C)  
+
| C = A + B || CommonOps_DDRM.add(A,B,C)  
 
|-
 
|-
| C = A - B || CommonOps.sub(A,B,C)  
+
| C = A - B || CommonOps_DDRM.sub(A,B,C)  
 
|-
 
|-
| C = 2 {{{*}}} A || CommonOps.scale(2,A,C)  
+
| C = 2 * A || CommonOps_DDRM.scale(2,A,C)  
 
|-
 
|-
| A = 2 {{{*}}} A || CommonOps.scale(2,A)  
+
| A = 2 * A || CommonOps_DDRM.scale(2,A)  
 
|-
 
|-
| C = A / 2 || CommonOps.divide(2,A,C)  
+
| C = A / 2 || CommonOps_DDRM.divide(2,A,C)  
 
|-
 
|-
| A = A / 2 || CommonOps.divide(2,A)  
+
| A = A / 2 || CommonOps_DDRM.divide(2,A)  
 
|-
 
|-
| C = inv(A) || CommonOps.invert(A,C)  
+
| C = inv(A) || CommonOps_DDRM.invert(A,C)  
 
|-
 
|-
| A = inv(A) || CommonOps.invert(A)  
+
| A = inv(A) || CommonOps_DDRM.invert(A)  
 
|-
 
|-
| C = pinv(A) || CommonOps.pinv(A)  
+
| C = pinv(A) || CommonOps_DDRM.pinv(A)  
 
|-
 
|-
| C = trace(A) || C = CommonOps.trace(A)  
+
| C = trace(A) || C = CommonOps_DDRM.trace(A)  
 
|-
 
|-
| C = det(A) || C = CommonOps.det(A)  
+
| C = det(A) || C = CommonOps_DDRM.det(A)  
 
|-
 
|-
| C=kron(A,B) || CommonOps.kron(A,B,C)  
+
| C=kron(A,B) || CommonOps_DDRM.kron(A,B,C)  
 
|-
 
|-
| B=rref(A) || B = CommonOps.rref(A,-1,null)  
+
| B=rref(A) || B = CommonOps_DDRM.rref(A,-1,null)  
 
|-
 
|-
| norm(A,"fro") || NormOps.normf(A)  
+
| norm(A,"fro") || NormOps_DDRM.normf(A)  
 
|-
 
|-
| norm(A,1) || NormOps.normP1(A)  
+
| norm(A,1) || NormOps_DDRM.normP1(A)  
 
|-
 
|-
| norm(A,2) || NormOps.normP2(A)  
+
| norm(A,2) || NormOps_DDRM.normP2(A)  
 
|-
 
|-
| norm(A,Inf) || NormOps.normPInf(A)  
+
| norm(A,Inf) || NormOps_DDRM.normPInf(A)  
 
|-
 
|-
| max(abs(A(:))) || CommonOps.elementMaxAbs(A)  
+
| max(abs(A(:))) || CommonOps_DDRM.elementMaxAbs(A)  
 
|-
 
|-
| sum(A(:)) || CommonOps.elementSum(A)  
+
| sum(A(:)) || CommonOps_DDRM.elementSum(A)  
 
|-
 
|-
| rank(A,tol) || svd.decompose(A); SingularOps.rank(svd,tol)
+
| rank(A,tol) || svd.decompose(A); SingularOps_DDRM.rank(svd,tol)
 
|-
 
|-
| [U,S,V] = svd(A) || DecompositionFactory.svd(A.numRows,A.numCols,true,true,false)  
+
| [U,S,V] = svd(A) || DecompositionFactory_DDRM.svd(A.numRows,A.numCols,true,true,false)  
 
|-
 
|-
| || SingularOps.descendingOrder(U,false,S,V,false)  
+
| || SingularOps_DDRM.descendingOrder(U,false,S,V,false)  
 
|-
 
|-
| [U,S,V] = svd(A,0) || DecompositionFactory.svd(A.numRows,A.numCols,true,true,true)  
+
| [U,S,V] = svd(A,0) || DecompositionFactory_DDRM.svd(A.numRows,A.numCols,true,true,true)  
 
|-
 
|-
| || SingularOps.descendingOrder(U,false,S,V,false)  
+
| || SingularOps_DDRM.descendingOrder(U,false,S,V,false)  
 
|-
 
|-
| S = svd(A) || DecompositionFactory.svd(A.numRows,A.numCols,false,false,true)  
+
| S = svd(A) || DecompositionFactory_DDRM.svd(A.numRows,A.numCols,false,false,true)  
 
|-
 
|-
| [V,D] = eig(A) ||  eig = DecompositionFactory.eig(A.numCols); eig.decompose(A)
+
| [V,D] = eig(A) ||  eig = DecompositionFactory_DDRM.eig(A.numCols); eig.decompose(A)
 
|-  
 
|-  
| || V = EigenOps.createMatrixV(eig); D = EigenOps.createMatrixD(eig)
+
| || V = EigenOps_DDRM.createMatrixV(eig); D = EigenOps_DDRM.createMatrixD(eig)
 
|-
 
|-
| [Q,R] = qr(A) || decomp = DecompositionFactory.qr(A.numRows,A.numCols)  
+
| [Q,R] = qr(A) || decomp = DecompositionFactory_DDRM.qr(A.numRows,A.numCols)  
 
|-
 
|-
 
|  || Q = decomp.getQ(null,false); R = decomp.getR(null,false)
 
|  || Q = decomp.getQ(null,false); R = decomp.getR(null,false)
 
|-
 
|-
| [Q,R] = qr(A,0) || decomp = DecompositionFactory.qr(A.numRows,A.numCols)  
+
| [Q,R] = qr(A,0) || decomp = DecompositionFactory_DDRM.qr(A.numRows,A.numCols)  
 
|-
 
|-
 
|  || Q = decomp.getQ(null,true); R = decomp.getR(null,true)
 
|  || Q = decomp.getQ(null,true); R = decomp.getR(null,true)
 
|-
 
|-
| [Q,R,P] = qr(A) || decomp = DecompositionFactory.qrp(A.numRows,A.numCols)  
+
| [Q,R,P] = qr(A) || decomp = DecompositionFactory_DDRM.qrp(A.numRows,A.numCols)  
 
|-
 
|-
 
|  || Q = decomp.getQ(null,false); R = decomp.getR(null,false)
 
|  || Q = decomp.getQ(null,false); R = decomp.getR(null,false)
Line 182: Line 188:
 
|  || P = decomp.getPivotMatrix(null)
 
|  || P = decomp.getPivotMatrix(null)
 
|-
 
|-
| [Q,R,P] = qr(A,0) || decomp = DecompositionFactory.qrp(A.numRows,A.numCols)  
+
| [Q,R,P] = qr(A,0) || decomp = DecompositionFactory_DDRM.qrp(A.numRows,A.numCols)  
 
|-
 
|-
 
|  || Q = decomp.getQ(null,true); R = decomp.getR(null,true)
 
|  || Q = decomp.getQ(null,true); R = decomp.getR(null,true)
Line 188: Line 194:
 
|  || P = decomp.getPivotMatrix(null)
 
|  || P = decomp.getPivotMatrix(null)
 
|-
 
|-
| R = chol(A) || DecompositionFactory.chol(A.numCols,false)  
+
| R = chol(A) || DecompositionFactory_DDRM.chol(A.numCols,false)  
 
|-
 
|-
| [L,U,P] = lu(A) ||DecompositionFactory.lu(A.numCols)
+
| [L,U,P] = lu(A) ||DecompositionFactory_DDRM.lu(A.numCols)
 
|}
 
|}

Latest revision as of 22:03, 17 May 2017

To help Matlab users quickly learn how to use EJML a list of equivalent functions is provided in the sections below. Keep in mind that directly porting Matlab code will often result in inefficient code. In Matlab for loops are very expensive and often extracting sub-matrices is the preferred method. Java like C++ can handle for loops much better and extracting and inserting a matrix can be much less efficient than direct manipulation of the matrix itself.

Equations

If you're a Matlab user you seriously might want to consider using the Equations interface in EJML. It is similar to Matlab and can be mixed with the other interfaces.

eq.process("[A(5:10,:) , ones(5,5)] .* normF(B) \ C")

That equation would be horrendous to implement using SimpleMatrix or the operations interface. Take a look at the Equations tutorial to learn more.

SimpleMatrix

A subset of EJML's functionality is provided in SimpleMatrix. If SimpleMatrix does not provide the functionality you desire then look at the list of #Procedural functions below.

Matlab SimpleMatrix
eye(3) SimpleMatrix.identity(3)
diag([1 2 3]) SimpleMatrix.diag(1,2,3)
C(1,2) = 5 A.set(0,1,5)
C(:) = A C.set(A)
C(:) = 5 C.set(5)
C(2,:) = [1,2,3] C.setRow(1,0,1,2,3)
C(:,2) = [1,2,3] C.setColumn(1,0,1,2,3)
C = A(2:4,3:8) C = A.extractMatrix(1,4,2,8)
A(:,2:end) = B A.insertIntoThis(0,1,B);
C = diag(A) C = A.extractDiag()
C = [A,B] C = A.combine(0,A.numCols(),B)
C = A' C = A.transpose()
C = -A C = A.negative()
C = A*B C = A.mult(B)
C = A + B C = A.plus(B)
C = A - B C = A.minus(B)
C = 2*A C = A.scale(2)
C = A / 2 C = A.divide(2)
C = inv(A) C = A.invert()
C = pinv(A) C = A.pinv()
C = A \ B C = A.solve(B)
C = trace(A) C = A.trace()
det(A) A.det()
C=kron(A,B) C=A.kron(B)
norm(A,"fro") A.normf()
max(abs(A(:))) A.elementMaxAbs()
sum(A(:)) A.elementSum()
rank(A) A.svd(true).rank()
[U,S,V] = svd(A) A.svd(false)
[U,S,V] = svd(A,0) A.svd(true)
[V,L] = eig(A) A.eig()

Procedural

Functions and classes in the procedural interface use DMatrixRMaj as input. Since SimpleMatrix is a wrapper around DMatrixRMaj its internal matrix can be extracted and passed into any of these functions.

Matlab Procedural
eye(3) CommonOps_DDRM.identity(3)
C(1,2) = 5 A.set(0,1,5)
C(:) = A C.setTo(A)
C(2,:) = [1,2,3] CommonOps_DDRM.insert(new DMatrixRMaj(1,3,true,1,2,3),C,1,0)
C(:,2) = [1,2,3] CommonOps_DDRM.insert(new DMatrixRMaj(3,1,true,1,2,3),C,0,1)
C = A(2:4,3:8) CommonOps_DDRM.extract(A,1,4,2,8)
diag([1 2 3]) CommonOps_DDRM.diag(1,2,3)
C = A' CommonOps_DDRM.transpose(A,C)
A = A' CommonOps_DDRM.transpose(A)
A = -A CommonOps_DDRM.changeSign(A)
C = A * B CommonOps_DDRM.mult(A,B,C)
C = A .* B CommonOps_DDRM.elementMult(A,B,C)
A = A .* B CommonOps_DDRM.elementMult(A,B)
C = A ./ B CommonOps_DDRM.elementDiv(A,B,C)
A = A ./ B CommonOps_DDRM.elementDiv(A,B)
C = A + B CommonOps_DDRM.add(A,B,C)
C = A - B CommonOps_DDRM.sub(A,B,C)
C = 2 * A CommonOps_DDRM.scale(2,A,C)
A = 2 * A CommonOps_DDRM.scale(2,A)
C = A / 2 CommonOps_DDRM.divide(2,A,C)
A = A / 2 CommonOps_DDRM.divide(2,A)
C = inv(A) CommonOps_DDRM.invert(A,C)
A = inv(A) CommonOps_DDRM.invert(A)
C = pinv(A) CommonOps_DDRM.pinv(A)
C = trace(A) C = CommonOps_DDRM.trace(A)
C = det(A) C = CommonOps_DDRM.det(A)
C=kron(A,B) CommonOps_DDRM.kron(A,B,C)
B=rref(A) B = CommonOps_DDRM.rref(A,-1,null)
norm(A,"fro") NormOps_DDRM.normf(A)
norm(A,1) NormOps_DDRM.normP1(A)
norm(A,2) NormOps_DDRM.normP2(A)
norm(A,Inf) NormOps_DDRM.normPInf(A)
max(abs(A(:))) CommonOps_DDRM.elementMaxAbs(A)
sum(A(:)) CommonOps_DDRM.elementSum(A)
rank(A,tol) svd.decompose(A); SingularOps_DDRM.rank(svd,tol)
[U,S,V] = svd(A) DecompositionFactory_DDRM.svd(A.numRows,A.numCols,true,true,false)
SingularOps_DDRM.descendingOrder(U,false,S,V,false)
[U,S,V] = svd(A,0) DecompositionFactory_DDRM.svd(A.numRows,A.numCols,true,true,true)
SingularOps_DDRM.descendingOrder(U,false,S,V,false)
S = svd(A) DecompositionFactory_DDRM.svd(A.numRows,A.numCols,false,false,true)
[V,D] = eig(A) eig = DecompositionFactory_DDRM.eig(A.numCols); eig.decompose(A)
V = EigenOps_DDRM.createMatrixV(eig); D = EigenOps_DDRM.createMatrixD(eig)
[Q,R] = qr(A) decomp = DecompositionFactory_DDRM.qr(A.numRows,A.numCols)
Q = decomp.getQ(null,false); R = decomp.getR(null,false)
[Q,R] = qr(A,0) decomp = DecompositionFactory_DDRM.qr(A.numRows,A.numCols)
Q = decomp.getQ(null,true); R = decomp.getR(null,true)
[Q,R,P] = qr(A) decomp = DecompositionFactory_DDRM.qrp(A.numRows,A.numCols)
Q = decomp.getQ(null,false); R = decomp.getR(null,false)
P = decomp.getPivotMatrix(null)
[Q,R,P] = qr(A,0) decomp = DecompositionFactory_DDRM.qrp(A.numRows,A.numCols)
Q = decomp.getQ(null,true); R = decomp.getR(null,true)
P = decomp.getPivotMatrix(null)
R = chol(A) DecompositionFactory_DDRM.chol(A.numCols,false)
[L,U,P] = lu(A) DecompositionFactory_DDRM.lu(A.numCols)