MyraMath
Quick reference for converting Matlab/Octave programs.

Overview

Matlab is the de-facto standard for algebra-oriented scripting. The following tables provide a guide for converting between Matlab notation and MyraMath. Many MyraMath constructs are heavily inspired by Matlab, but there are some differences.

Below are links to the sections of this page:

Dense operations.

Both platforms provide basic manipulation of dense Matrix's (slicing, concatenation, special matrices, etc).

Matlab Syntax MyraMath Syntax Remarks

A(1,1) A(0,0) MyraMath indices are 0-based.
A(N,N) A(N-1,N-1)

I = size(A,1) int I = A.size().first Matrix.size() returns a std::pair<int,int>.
J = size(A,2) int J = A.size().second
N = length(x) int N = x.size() Vector.size() returns an int.

A(:,j) A.column(j) Column slicing. Shallow view of underlying data.
A(:,7:12) A.columns(7,12)
A(:,1:3) A.left(3)
A(:,end-4:end) A.right(4)

A(i,:) A.row(i) Row slicing. Shallow view of underlying data.
A(4:7,:) A.rows(4,7)
A(1:5) A.top(5)
A(end-2:end,:) A.bottom(2)

A(5:10,7:9) A.window(5,10,7,9) Contiguous row/column slice. Shallow view of underlying data.

x(6:12) x.window(6,12) Vector slicing. Shallow view of underlying data.
x(1:5) x.first(5)
x(end-7:end) x.last(7)

D = diag(A) auto D = diag(A) Deep copy of diagonal entries. MyraMath returns DiagonalMatrix.

A = zeros(5,6) auto A = Matrix<double>::zeros(5,6) Special matrices are static member functions.
A = ones(4,3) auto A = Matrix<double>::ones(4,3)
A = identity(3) auto A = Matrix<double>::identity(3)
A = randu(7,2) auto A = Matrix<double>::random(7,2)

A = [1 2; 3 4] auto A = Matrix<double>::fill_rmajor(2,2,{1,2,3,4}) Elementwise initialization from a std::initializer_list (C++11 only).
x = [1 2 3 4 5] auto x = Vector<double>::fill({1,2,3,4,5})
x = (0:10).^2 auto x = Vector<double>::evaluate(pow(linspace(0.0,10.0,11),2)) Initialization from an Expression uses the evaluate() static member function.

B = [A1 A2] auto B = horzcat(A1,A2) Horizontal concatenation. Deep copy.
B = [A1;A2] auto B = vertcat(A1,A2) Vertical concatenation. Deep copy.
y = [x1;x2] auto y = vectorcat(x1,x2) Vector concatenation. Deep copy.

B = A.' auto B = transpose(A) Returns a transposed/hermitian'd/conjugated copy of A.
B = A' auto B = hermitian(A)
B = conj(A) auto B = conjugate(A)
A = A.' transpose_inplace(A) Overwrites A with its transpose. A must be square.
A = A' hermitian_inplace(A) Overwrites A with its hermitian transpose. A must be square.
A = conj(A) conjugate_inplace(A) Overwrites A with its conjugate.
C = complex(A,B) auto C = make_complex(A,B) Given real A and B, returns C = A+j*B.

BLAS operations.

MyraMath offers many functions and overloaded operators for general matrix arithmetic that allows one to convert Matlab code directly into C++ with few changes. However, sometimes changes to Matlab expressions can be profitable, if it allows better exploitation of the underlying BLAS (Basic Linear Algebra Subprograms, an API implemented by most hardware vendors). The BLAS can be optimized to obtain the highest level of performance possible for your machine, through the use of vector intrinsics, architecture specific instruction sets, algorithmic reblocking for optimum cache usage, the works. The following functions all pass straight through directly to the BLAS. Use them whenever possible. Semantically equivalent Matlab code is given in the first column.

Matlab Syntax MyraMath Syntax Remarks

y = alpha*x + y axpy(alpha,x,y) Adding Vector's (note AXPY is a mnemonic, for Alpha*X Plus Y)

alpha = x'*y alpha = dot(x,y) Vector inner products.
alpha = x.'*y alpha = dotu(x,y)

alpha = norm(A,'fro') alpha = frobenius(A) Frobenius norm of a Matrix.
alpha = norm(x) alpha = euclidean(x) Euclidean norm of a Vector.

C = C + A*B gemm_inplace(C,A,B) Multiplies C+=A*B, General Matrix-Matrix multiplication.
[no equivalent] symm_inplace('L',C,A,B) Multiplies C+=A*B, by symmetric A from left.
[no equivalent] symm_inplace('L',C,B,A) Multiplies C+=B*A, by symmetric A from right.
[no equivalent] hemm_inplace('L',C,A,B) Multiplies C+=A*B, by hermitian A from left.
[no equivalent] hemm_inplace('L',C,B,A) Multiplies C+=B*A, by hermitian A from right.

tril(C) = tril(C) + A*A' herk_inplace(C,'L',A) Hermitian rank-K update. Only touches one triangle of C.
triu(C) = triu(C) + A*A' herk_inplace(C,'U',A)

tril(C) = tril(C) + A*A.' syrk_inplace(C,'L',A) Symmetric rank-K update. Only touches one triangle of C.
triu(C) = triu(C) + A*A.' syrk_inplace(C,'U',A)

B = tril(A)\B trsm_inplace('L','L',A,B) Backsolution by triangular A. Overwrites B.
B = triu(A)\B trsm_inplace('L','U',A,B)
B = B/tril(A) trsm_inplace('R','L',A,B)
B = B/triu(A) trsm_inplace('R','U',A,B)

B = tril(A)*B trmm_inplace('L','L',A,B) Multiplication by triangular A. Overwrites B.
B = triu(A)*B trmm_inplace('L','U',A,B)
B = B*tril(A) trmm_inplace('R','L',A,B)
B = B*triu(A) trmm_inplace('R','U',A,B)

LAPACK operations.

LAPACK is a library that contains robust implementations of many classical matrix decompositions. Like the BLAS, highly optimized implementations of LAPACK are available from machine vendors. The functions below all delegate directly to LAPACK. Semantically equivalent Matlab code is given in the first column.

Matlab Syntax MyraMath Syntax Remarks

[L,U,P] = lu(A) auto P = getrf_inplace(A) LU decomposition. A overwritten with L,U factors. Returns P.
L = chol(A,'lower') potrf_inplace(A) Cholesky decomposition. Overwrites A. Most efficient upon LowerMatrix.
[L,D,P] = ldl(A) auto PQRI = sytrf_inplace(A) LDL' decomposition. Overwrites A. Most efficient upon LowerMatrix.
[Q,R] = qr(A) auto tau = geqrf_inplace(A) QR decomposition. Overwrites A with R and Q (implicit householder form).
[V,D] = eig(A) auto D = syev_inplace(A) Symmetric eigendecomposition. Returns eigenvalues (D), overwrites A with eigenvectors X.
[V,D,W] = eig(A) auto DRL = geev(A) Nonsymmetric eigendecomposition. Returns eigenvalues (D) and Left/Right eigenvectors.
[U,S,V] = svd(A) auto USV = gesvd(A) Singular value decomposition.
N = null(A) auto N = nullspace(A) Orthonormal basis for nullspace of A. Based on gesvd().
R = orth(A) auto R = rangespace(A) Orthonormal basis for range of A. Based on gesvd().
B = pinv(A) auto B = pinv(A) Moore-Penrose psueoinverse of A. Based on gesvd().

Sparse operations.

The following table lists equivalences between Matlab and MyraMath, for sparse matrix manipulation.

Matlab Syntax MyraMath Syntax Remarks

A = speye(n) auto A = SparseMatrix<double>::identity(n) Fills A with identity.
A = spones(S) auto A = SparseMatrix<double>::ones(S.pattern()) Fills A with 1's, according to pattern of S.
A = sparse(i,j,v) auto A = SparseMatrix<double>::fill(i,j,v) Fills A from coordinate format (i,j,v).
A = sprand(S) auto A = SparseMatrix<double>::random(S.pattern()) Fills A with random values, according to pattern of S.
A = sprand(m,n,density) auto A = SparseMatrix<double>::random(m,n,density*m*n) Fills A with random values, random pattern, but specific fill-density (number of nonzeros).

A = sparse(5,5)
A(2,2) = 5.0
A(4,3) = 3.0
SparseMatrixBuilder<double> B(5,5);
B(2,2) = 5.0;
B(4,3) = 3.0;
auto A = B.make_SparseMatrix()
Use a SparseMatrixBuilder for random pattern mutation via A(i,j)=v.

A = full(S) auto A = S.make_Matrix() Converts from SparseMatrix to dense Matrix.
S = sparse(A) auto S = dense2sparse_threshold(A,tolerance) Converts from dense Matrix to SparseMatrix, by thresholding.

n = nnz(A) int n = A.n_nonzeros() Nonzero count.
spy(A) std::cout << A.pattern() Pretty-print the pattern of A.
parents = etree(A) auto parents = EliminationTree(A).parents(); Returns elimination tree of A.
p = symrcm(A) auto p = rcm(A) Reorders to reduce bandwidth.
p = symamd(A) auto p = mmd(A) Reorders to reduce fillin.
[no equivalent] auto p = reorder(A) Reorders to reduce fillin and expose parallelism.

x = pcg(A,b,tol,maxit,M) pcg(M,A,b,x,tol,maxit) Preconditioned conjugate gradients, an iterative solver. In MyraMath, A and M are Action's.
x = bicgstab(A,b,tol,maxit,M) bicgstab(M,A,b,x,tol,maxit) Biconjugate Gradients Stabilized, an iterative solver. In MyraMath, A and M are Action's.
x = gmres(A,b,restart,tol,maxit,M) gmres(M,A,b,x,tol,restart,maxit) Generalized minimum residual, an iterative solver. In MyraMath, A and M are Action's.
L = ichol(A) auto LLt = ICholeskySolver(A) Incomplete cholesky preconditioner. Can be adapted into a make_SolveAction().

x = sparse(A)\b See Tutorial for /multifrontal solvers (part 1). Sparse direct solver framework.

Exporting raw data from Matlab into MyraMath.

Within the /matlab folder, there are several .m-files that can be used to export Matlab datatypes (full matrices, vectors, sparse matrices) into corresponding MyraMath containers (Matrix's, Vector's, SparseMatrix's).

Matlab Commands (exporting) MyraMath Commands (importing) Remarks

A = rand(5,8);
save_Matrix('A.bin',A);
FileInputStream in('A.bin');
Matrix<double> A(in);
in.close();
Exports a dense Matrix A from Matlab, imports it into MyraMath.

x = linspace(0,5,21);
save_Vector('x.bin',x);
FileInputStream in('x.bin');
Vector<double> x(in);
in.close();
Exports a dense Vector x from Matlab, imports it into MyraMath.

S = speye(12);
save_SparseMatrix('S.bin',S);
FileInputStream in('S.bin');
SparseMatrix<double> S(in);
in.close();
Exports a SparseMatrix S from Matlab, imports it into MyraMath.

Exporting raw data from MyraMath into Matlab.

Within /matlab, there are also .m-files that can be used to import data into Matlab after it was saved from a MyraMath container. Both of these operations prove useful when debugging.

MyraMath Commands (exporting) Matlab Commands (importing) Remarks

auto A = Matrix<double>::random(5,8);
FileOutputStream out('A.bin');
A.write(out);
out.close();
A = load_Matrix('A.bin') Exports a dense Matrix A from MyraMath, imports it into Matlab.

auto x = Vector<double>::evaluate(linspace(0.0,5.0,21));
FileOutputStream out('x.bin');
x.write(out);
out.close();
x = load_Vector('x.bin') Exports a dense Vector x from MyraMath, imports it into Matlab.

auto S = SparseMatrix<double>::identity(12);
FileOutputStream out('S.bin');
S.write(out);
out.close();
S = load_SparseMatrix('S.bin') Exports a SparseMatrix S from MyraMath, imports it into Matlab.

Go back to API Tutorials