MATLAB2Python-Julia Cheatsheet
MATLAB2Python-Julia Cheatsheet
(https://github.com/QuantEcon/QuantEcon.cheatsheet)
MATLAB–Python–Julia cheatsheet¶
In the Python code we assume that you have already run import numpy as np
In the Julia, we assume you are using v1.0.2 or later with Compat v1.3.0 or later and have run
using LinearAlgebra, Statistics, Compat
Creating Vectors¶
A = [1 2 3]
A = np.array([1, 2, A = [1 2 3]
3]).reshape(1, 3)
A = [1; 2; 3]
A = np.array([1, 2, A = [1 2 3]'
3]).reshape(3, 1)
or
A = [1, 2, 3]
A = j:k:n
A = np.arange(j, n+1, k)
A = j:k:n
A = linspace(1, 5, k) A = np.linspace(1, 5, k)
A = range(1, 5,
length = k)
Creating Matrices¶
Create a matrix
A = [1 2; 3 4]
A = np.array([[1, 2], [3, A = [1 2; 3 4]
4]])
A = zeros(2, 2)
A = np.zeros((2, 2))
A = zeros(2, 2)
2 x 2 matrix of ones
A = ones(2, 2)
A = np.ones((2, 2))
A = ones(2, 2)
2 x 2 identity matrix
A = eye(2, 2)
A = np.eye(2)
A = I # will adopt
# neighboring matrices
Diagonal matrix
A = diag([1 2 3])
A = np.diag([1, 2, 3])
A = Diagonal([1, 2,
3])
A = rand(2, 2)
A = np.random.rand(2, 2)
A = rand(2, 2)
A = randn(2, 2)
A = np.random.randn(2, 2)
A = randn(2, 2)
Sparse Matrices
A = sparse(2, 2)
from scipy.sparse import using SparseArrays
A(1, 2) = 4
coo_matrix
A = spzeros(2, 2)
A(2, 2) = 1
A[1, 2] = 4
A = coo_matrix(([4, 1],
A[2, 2] = 1
([0, 1],
[1, 1])),
shape=(2,
2))
Tridiagonal Matrices
A = [1 2 3 NaN;
import sp.sparse as sp
x = [1, 2, 3]
4 5 6 7;
diagonals = [[4, 5, 6, 7], y = [4, 5, 6, 7]
NaN 8 9 0]
[1, 2, 3], [8, 9, 10]]
z = [8, 9, 10]
spdiags(A',[-1 0 1], 4, 4)
sp.diags(diagonals, [0, -1, Tridiagonal(x, y, z)
2]).toarray()
Transpose
A.'
A.T
transpose(A)
A'
A.conj()
A'
Concatenate horizontally
A = [[1 2] [1 2]]
B = np.array([1, 2])
A = [[1 2] [1 2]]
A = np.hstack((B, B))
or or
Concatenate vertically
A = np.vstack((B, B))
or or
A = reshape(1:10, 5, 2)
A = A.reshape(5, 2)
A = reshape(1:10, 5, 2)
A(:)
A = A.flatten()
A[:]
Flip left/right
fliplr(A)
np.fliplr(A)
reverse(A, dims = 2)
Flip up/down
flipud(A)
np.flipud(A)
reverse(A, dims = 1)
repmat(A, 3, 4)
np.tile(A, (4, 3))
repeat(A, 3, 4)
Preallocating/Similar
x = rand(10)
x = np.random.rand(3, 3)
x = rand(3, 3)
size(x, 2))
# new dims
# new dims
y = similar(x, 2, 2)
f = @(x) x.^2
def f(x):
f(x) = x^2
g = @(x, y) x + 2 + y.^2
return x**2
g(x, y) = x + 2 + y^2
x = 1:10
def g(x, y):
x = 1:10
y = 2:11
return x + 2 + y**2
y = 2:11
f(x)
x = np.arange(1, 10, 1)
f.(x)
g(x, y)
y = np.arange(2, 11, 1)
g.(x, y)
f(x)
A(2, 2)
A[1, 1]
A[2, 2]
A(1:4, :)
A[0:4, :]
A[1:4, :]
A(:, 1:4)
A[:, 0:4]
A[:, 1:4]
Remove a row
A([1 2 4], :)
A[[0, 1, 3], :]
A[[1, 2, 4], :]
Diagonals of matrix
diag(A) np.diag(A)
diag(A)
Mathematical Operations¶
Dot product
dot(A, B)
np.dot(A, B) or A @ B
dot(A, B)
A ⋅ B # \cdot<TAB>
A * B
A @ B
A * B
2]).reshape(2, 1)
A = [1 2; 3 4]
4]))
mul!(y, A, x)
y = np.empty_like(x)
np.matmul(A, x, y)
Element-wise multiplication
A .* B
A * B
A .* B
Matrix to a power
A^2
np.linalg.matrix_power(A, A^2
2)
A.^2
A**2
A.^2
Inverse
inv(A)
np.linalg.inv(A)
inv(A)
or or
A^(-1)
A^(-1)
Determinant
det(A)
np.linalg.det(A)
det(A)
Euclidean norm
norm(A) np.linalg.norm(A)
norm(A)
A\b
np.linalg.solve(A, b)
A\b
A\b
np.linalg.lstsq(A, b)
A\b
sum(A, 1)
sum(A, 0)
sum(A, dims = 1)
max(A, [], 1)
np.amax(A, 0)
maximum(A, dims = 1)
min(A, [], 1)
np.amin(A, 0)
minimum(A, dims = 1)
sum(A, 2)
sum(A, 1)
sum(A, dims = 2)
max(A, [], 2)
np.amax(A, 1)
maximum(A, dims = 2)
min(A, [], 2)
np.amin(A, 1)
minimum(A, dims = 2)
sum(A(:))
np.sum(A)
sum(A)
max(A(:))
np.amax(A)
maximum(A)
min(A(:))
np.amin(A)
minimum(A)
cumsum(A, 1)
np.cumsum(A, 0)
cumsum(A, dims = 1)
cummax(A, 1)
np.maximum.accumulate(A, 0)
accumulate(max, A, dims =
cummin(A, 1)
np.minimum.accumulate(A, 0)
1)
accumulate(min, A, dims =
1)
cumsum(A, 2)
np.cumsum(A, 1)
cumsum(A, dims = 2)
cummax(A, 2)
np.maximum.accumulate(A, 1)
accumulate(max, A, dims =
cummin(A, 2)
np.minimum.accumulate(A, 1)
2)
accumulate(min, A, dims =
2)
Programming¶
MATLAB PYTHON JULIA
% This is a comment
# This is a comment
# This is a comment
Comment block
%{
# Block
#=
Comment block
# comment
Comment block
%}
# following PEP8
=#
For loop
for i = 1:N
for i in range(n):
for i in 1:N
% do something
# do something
# do something
end
end
While loop
while i <= N
while i <= N:
while i <= N
% do something
# do something
# do something
end
end
If
if i <= N
if i <= N:
if i <= N
% do something
# do something
# do something
end
end
If / else
if i <= N
if i <= N:
if i <= N
% do something
# do something
# do something
else
else:
else
% do something else
# so something else
# do something else
end
end
x = 10
x = 10
x = 10
fprintf('x = %d \n', x)
print(f'x = {x}')
println("x = $x")
Function: anonymous
f = @(x) x^2
f = lambda x: x**2
f = x -> x^2
# can be rebound
Function
out = x^2
return x**2
return x^2
end
end
Tuples
t = {1 2.0 "test"}
t = (1, 2.0, "test")
t = (1, 2.0, "test")
t{1}
t[0]
t[1]
Named Tuples/
Anonymous Structures
m.x = 1
from collections import # vanilla
m.y = 2
namedtuple
m = (x = 1, y = 2)
m.x
m.x
mdef = namedtuple('m', 'x
y')
# constructor
m = mdef(1, 2)
using Parameters
mdef = @with_kw (x=1, y=2)
m.x
m = mdef() # same as above
m = mdef(x = 3)
Closures
a = 2.0 a = 2.0
a = 2.0
f = @(x) a + x
def f(x):
f(x) = a + x
f(1.0)
return a + x
f(1.0)
f(1.0)
Inplace Modification
(https://blogs.mathworks.com/loren/2007/03/22/in- x **=2
return
place-operations-on-data/)
x = np.random.rand(10)
f(x)
function f!(out, x)
out .= x.^2
end
x = rand(10)
y = similar(x)
f!(y, x)
Credits
This cheat sheet was created by Victoria Gregory (https://github.com/vgregory757), Andrij Stachurski (http://drdrij.com/),
Natasha Watkins (https://github.com/natashawatkins) and other collaborators on behalf of QuantEcon
(http://quantecon.org/).