Dependencies and Setup
In Python the cheatsheet assumes import numpy as np. In Julia it assumes using LinearAlgebra, Statistics, Compat. In MAD-NG, it assumes import from standard MAD environment using local range, nrange, vector, matrix, linspace in MAD.
Creating Vectors
| Operation | MATLAB | Python | Julia | MAD-NG |
|---|---|---|---|---|
| Row vector: size (1, n) | A = [1 2 3] |
A = np.array([1, 2, 3]).reshape(1, 3) |
A = [1 2 3] |
A = vector{1, 2, 3}:t() |
| Column vector: size (n, 1) | A = [1; 2; 3] |
A = np.array([1, 2, 3]).reshape(3, 1) |
A = [1 2 3]' |
A = vector{1, 2, 3} |
| 1d array: size (n,) | Not possible |
A = np.array([1, 2, 3]) |
A = [1; 2; 3]
# or
A = [1, 2, 3] |
A = {1, 2, 3} |
| Integers from j to n with step size k | A = j:k:n |
A = np.arange(j, n+1, k) |
A = j:k:n |
A = range(j, n, k) |
| Linearly spaced range of k points | A = linspace(1, 5, k) |
A = np.linspace(1, 5, k) |
A = range(1, 5, length = k) |
A = nrange(1, 5, k) |
| Linearly spaced vector of k points | A = linspace(1, 5, k) |
A = np.linspace(1, 5, k) |
A = collect(range(1, 5, length = k)) |
A = linspace(1, 5, k) |
Creating Matrices
| Operation | MATLAB | Python | Julia | MAD-NG |
|---|---|---|---|---|
| Create a matrix | A = [1 2; 3 4] |
A = np.array([[1, 2], [3, 4]]) |
A = [1 2; 3 4] |
A = matrix{{1,2},{3,4}} |
| 2 × 2 matrix of zeros | A = zeros(2, 2) |
A = np.zeros((2, 2)) |
A = zeros(2, 2) |
A = matrix(2):zeros() |
| 2 × 2 matrix of ones | A = ones(2, 2) |
A = np.ones((2, 2)) |
A = ones(2, 2) |
A = matrix(2):ones() |
| 2 × 2 identity matrix | A = eye(2, 2) |
A = np.eye(2) |
A = I |
A = matrix(2):eye() |
| Diagonal matrix | A = diag([1 2 3]) |
A = np.diag([1, 2, 3]) |
A = Diagonal([1, 2, 3]) |
A = vector{1,2,3}:diag() |
| Uniform random numbers | A = rand(2, 2) |
A = np.random.rand(2, 2) |
A = rand(2, 2) |
A = matrix(2):rand() |
| Normal random numbers | A = randn(2, 2) |
A = np.random.randn(2, 2) |
A = randn(2, 2) |
A = matrix(2):randn() |
| Sparse matrices | A = sparse(2, 2) A(1, 2) = 4 A(2, 2) = 1 |
from scipy.sparse import coo_matrix A = coo_matrix(([4, 1], ([0, 1], [1, 1])), shape=(2, 2)) |
using SparseArrays A = spzeros(2, 2) A[1, 2] = 4 A[2, 2] = 1 |
A = matrix(2):setcol(2,{4,1}) |
Manipulating Vectors and Matrices
| Operation | MATLAB | Python | Julia | MAD-NG |
|---|---|---|---|---|
| Transpose | A.' |
A.T |
transpose(A) |
A:t() -- or A:t(false) |
| Complex conjugate transpose | A' |
A.conj().T |
A' |
A:t() |
| Concatenate horizontally | A = [[1 2] [1 2]] |
A = np.hstack((B, B)) |
A = hcat([1 2], [1 2]) |
A = B:concat(B,'row') |
| Concatenate vertically | A = [[1 2]; [1 2]] |
A = np.vstack((B, B)) |
A = vcat([1 2], [1 2]) |
A = B:concat(B,'col') |
| Reshape to 5 rows, 2 columns | A = reshape(1:10, 5, 2) |
A = A.reshape(5, 2) |
A = reshape(1:10, 5, 2) |
A:reshape(5, 2) |
| Convert matrix to vector | A(:) |
A = A.flatten() |
A[:] |
A:vec() |
| Flip left/right | fliplr(A) |
np.fliplr(A) |
reverse(A, dims = 2) |
A:rev'row' |
| Flip up/down | flipud(A) |
np.flipud(A) |
reverse(A, dims = 1) |
A:rev'col' |
| Preallocating / similar | y = zeros(size(x, 1), size(x, 2)) |
y = np.empty_like(x) |
y = similar(x) |
y = x:same() |
| Broadcast a function | f = @(x) x.^2 f(x) |
def f(x): return x**2 f(x) |
f.(x) |
A:map \x x^2 |
Accessing Vector/Matrix Elements
| Operation | MATLAB | Python | Julia | MAD-NG |
|---|---|---|---|---|
| Access one element | A(2, 2) |
A[1, 1] |
A[2, 2] |
A:get(2, 2) |
| Access specific rows | A(1:4, :) |
A[0:4, :] |
A[1:4, :] |
A:getrow(range(1,4)) |
| Access specific columns | A(:, 1:4) |
A[:, 0:4] |
A[:, 1:4] |
A:getcol(range(1,4)) |
| Remove a row | A([1 2 4], :) |
A[[0, 1, 3], :] |
A[[1, 2, 4], :] |
A:remrow(3) |
| Diagonals of matrix | diag(A) |
np.diag(A) |
diag(A) |
A:diag() |
| Get dimensions of matrix | [nrow ncol] = size(A) |
nrow, ncol = np.shape(A) |
nrow, ncol = size(A) |
nrow, ncol = A:sizes() |
Mathematical Operations
MAD-NG generic math dispatches functions such as sqrt, exp, log, sin, cos, tan, erf, real, imag, conj, and related functions to numbers or objects supporting the corresponding methods.
| Operation | MATLAB | Python | Julia | MAD-NG |
|---|---|---|---|---|
| Dot product | dot(A, B) | np.dot(A, B) | dot(A, B) | A:dot(B) |
| Matrix multiplication | A * B | A @ B | A * B | A * B |
| In-place matrix multiplication | Not possible | np.matmul(A, x, y) | mul!(y, A, x) | A:mul(x,y) |
| Element-wise multiplication | A .* B | A * B | A .* B | A:emul(B) |
| Matrix to a power | A^2 | np.linalg.matrix_power(A, 2) | A^2 | A^2 |
| Element-wise power | A.^2 | A**2 | A.^2 | A:epow(2) |
| Inverse | inv(A) | np.linalg.inv(A) | inv(A) | A:inv() |
| Determinant | det(A) | np.linalg.det(A) | det(A) | A:det() |
| Eigenvalues and eigenvectors | [vec, val] = eig(A) | val, vec = np.linalg.eig(A) | val, vec = eigen(A) | w,info,vr,vl = A:eigen() |
| Euclidean norm | norm(A) | np.linalg.norm(A) | norm(A) | A:norm() |
| Solve square linear system Ax=b | A\b | np.linalg.solve(A, b) | A\b | A:solve(b) |
| Solve least squares Ax=b | A\b | np.linalg.lstsq(A, b) | A\b | A:ssolve(b) |
Sum / max / min
| Operation | MATLAB | Python | Julia | MAD-NG |
|---|---|---|---|---|
| Sum / max / min of each column | sum(A, 1) max(A, [], 1) min(A, [], 1) |
np.sum(A, 0) np.max(A, 0) np.min(A, 0) |
sum(A, dims = 1) maximum(A, dims = 1) minimum(A, dims = 1) |
A:sum'col' A:max'col' A:min'col' |
| Sum / max / min of each row | sum(A, 2) max(A, [], 2) min(A, [], 2) | np.sum(A, 1) np.max(A, 1) np.min(A, 1) | sum(A, dims = 2) maximum(A, dims = 2) minimum(A, dims = 2) | A:sum'row' A:max'row' A:min'row' |
| Sum / max / min of entire matrix | sum(A(:)) max(A(:)) min(A(:)) | np.sum(A) np.amax(A) np.amin(A) | sum(A) maximum(A) minimum(A) | A:sum() A:max() A:min() |
| Cumulative sum / max / min by row | cumsum(A, 1) |
np.cumsum(A, 0) |
cumsum(A, dims = 1) |
A:accsum'row' A:accmax'row' A:accmin'row' |
| Cumulative sum / max / min by column | cumsum(A, 2) |
np.cumsum(A, 1) |
cumsum(A, dims = 2) |
A:accsum'col' A:accmax'col' A:accmin'col' |
Programming
MAD-NG examples assume local printf in MAD.utility.
| Operation | MATLAB | Python | Julia | MAD-NG |
|---|---|---|---|---|
| Comment one line | % This is a comment | # This is a comment | # This is a comment | -- This is a comment |
| Comment block | %{
Comment block
%} | # Block # comment | #= Comment block =# | --[[ Comment block --]] |
| For loop | for i = 1:N % do something end | for i in range(N):
# do something | for i in 1:N # do something end | for i=1,N do -- do something end |
| While loop | while i <= N % do something end | while i <= N:
# do something | while i <= N # do something end | while i <= N do -- do something end |
| If | if i <= N % do something end | if i <= N: # do something | if i <= N # do something end | if i <= N then -- do something end |
| If / else | if i <= N % do something else % do something else end | if i <= N:
# do something
else:
# do something else | if i <= N # do something else # do something else end | if i <= N then -- do something else -- do something else end |
| Print text and variable | x = 10
fprintf('x = %d \n', x) | x = 10
print(f'x = {x}') | x = 10
println("x = $x") | x = 10
printf("x = %d\n", x) |
| Function: anonymous | f = @(x) x^2 | f = lambda x: x**2 | f = x -> x^2 | f = \x -> x^2 |
| Function | function out = f(x) out = x^2 end | def f(x):
return x**2 | function f(x) return x^2 end | function f(x) return x^2 end |
| Tuples / heterogeneous containers | t = {1 2.0 "test"}
t{1} | t = (1, 2.0, "test") t[0] | t = (1, 2.0, "test") t[1] | t = {1, 2.0, "test"}
t[1] |
| Named tuples / anonymous structures | m.x = 1 m.y = 2 m.x | from collections import namedtuple
mdef = namedtuple('m', 'x y')
m = mdef(1, 2)
m.x | m = (x = 1, y = 2) m.x | m = {x = 1, y = 2}
m.x |
| Closures | a = 2.0 f = @(x) a + x f(1.0) | a = 2.0
def f(x):
return a + x
f(1.0) | a = 2.0 f = x -> a + x f(1.0) | a = 2.0 f = \x -> a + x f(1.0) |