Difference between revisions of "Matlab to EJML"
(2 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
= Equations = | = Equations = | ||
− | Equations is | + | 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 interface. Take a look at the [[Equations|Equations tutorial]] to learn more. | ||
= SimpleMatrix = | = SimpleMatrix = | ||
Line 78: | Line 84: | ||
= Procedural = | = Procedural = | ||
− | Functions and classes in the procedural interface use | + | 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) || | + | | 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] || | + | | C(2,:) = [1,2,3] || CommonOps_DDRM.insert(new DMatrixRMaj(1,3,true,1,2,3),C,1,0) |
|- | |- | ||
− | | C(:,2) = [1,2,3] || | + | | 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) || | + | | C = A(2:4,3:8) || CommonOps_DDRM.extract(A,1,4,2,8) |
|- | |- | ||
− | | diag([1 2 3]) || | + | | diag([1 2 3]) || CommonOps_DDRM.diag(1,2,3) |
|- | |- | ||
− | | C = A' || | + | | C = A' || CommonOps_DDRM.transpose(A,C) |
|- | |- | ||
− | | A = A' || | + | | A = A' || CommonOps_DDRM.transpose(A) |
|- | |- | ||
− | | A = -A || | + | | A = -A || CommonOps_DDRM.changeSign(A) |
|- | |- | ||
− | | C = A * B || | + | | C = A * B || CommonOps_DDRM.mult(A,B,C) |
|- | |- | ||
− | | C = A .* B || | + | | C = A .* B || CommonOps_DDRM.elementMult(A,B,C) |
|- | |- | ||
− | | A = A .* B || | + | | A = A .* B || CommonOps_DDRM.elementMult(A,B) |
|- | |- | ||
− | | C = A ./ B || | + | | C = A ./ B || CommonOps_DDRM.elementDiv(A,B,C) |
|- | |- | ||
− | | A = A ./ B || | + | | A = A ./ B || CommonOps_DDRM.elementDiv(A,B) |
|- | |- | ||
− | | C = A + B || | + | | C = A + B || CommonOps_DDRM.add(A,B,C) |
|- | |- | ||
− | | C = A - B || | + | | C = A - B || CommonOps_DDRM.sub(A,B,C) |
|- | |- | ||
− | | C = 2 * A || | + | | C = 2 * A || CommonOps_DDRM.scale(2,A,C) |
|- | |- | ||
− | | A = 2 * A || | + | | A = 2 * A || CommonOps_DDRM.scale(2,A) |
|- | |- | ||
− | | C = A / 2 || | + | | C = A / 2 || CommonOps_DDRM.divide(2,A,C) |
|- | |- | ||
− | | A = A / 2 || | + | | A = A / 2 || CommonOps_DDRM.divide(2,A) |
|- | |- | ||
− | | C = inv(A) || | + | | C = inv(A) || CommonOps_DDRM.invert(A,C) |
|- | |- | ||
− | | A = inv(A) || | + | | A = inv(A) || CommonOps_DDRM.invert(A) |
|- | |- | ||
− | | C = pinv(A) || | + | | C = pinv(A) || CommonOps_DDRM.pinv(A) |
|- | |- | ||
− | | C = trace(A) || C = | + | | C = trace(A) || C = CommonOps_DDRM.trace(A) |
|- | |- | ||
− | | C = det(A) || C = | + | | C = det(A) || C = CommonOps_DDRM.det(A) |
|- | |- | ||
− | | C=kron(A,B) || | + | | C=kron(A,B) || CommonOps_DDRM.kron(A,B,C) |
|- | |- | ||
− | | B=rref(A) || B = | + | | B=rref(A) || B = CommonOps_DDRM.rref(A,-1,null) |
|- | |- | ||
− | | norm(A,"fro") || | + | | norm(A,"fro") || NormOps_DDRM.normf(A) |
|- | |- | ||
− | | norm(A,1) || | + | | norm(A,1) || NormOps_DDRM.normP1(A) |
|- | |- | ||
− | | norm(A,2) || | + | | norm(A,2) || NormOps_DDRM.normP2(A) |
|- | |- | ||
− | | norm(A,Inf) || | + | | norm(A,Inf) || NormOps_DDRM.normPInf(A) |
|- | |- | ||
− | | max(abs(A(:))) || | + | | max(abs(A(:))) || CommonOps_DDRM.elementMaxAbs(A) |
|- | |- | ||
− | | sum(A(:)) || | + | | sum(A(:)) || CommonOps_DDRM.elementSum(A) |
|- | |- | ||
− | | rank(A,tol) || svd.decompose(A); | + | | rank(A,tol) || svd.decompose(A); SingularOps_DDRM.rank(svd,tol) |
|- | |- | ||
− | | [U,S,V] = svd(A) || | + | | [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) || | + | | [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) || | + | | S = svd(A) || DecompositionFactory_DDRM.svd(A.numRows,A.numCols,false,false,true) |
|- | |- | ||
− | | [V,D] = eig(A) || eig = | + | | [V,D] = eig(A) || eig = DecompositionFactory_DDRM.eig(A.numCols); eig.decompose(A) |
|- | |- | ||
− | | || V = | + | | || V = EigenOps_DDRM.createMatrixV(eig); D = EigenOps_DDRM.createMatrixD(eig) |
|- | |- | ||
− | | [Q,R] = qr(A) || decomp = | + | | [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 = | + | | [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 = | + | | [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 = | + | | [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) || | + | | R = chol(A) || DecompositionFactory_DDRM.chol(A.numCols,false) |
|- | |- | ||
− | | [L,U,P] = lu(A) || | + | | [L,U,P] = lu(A) ||DecompositionFactory_DDRM.lu(A.numCols) |
|} | |} |
Latest revision as of 21: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) |