Skip to content

Instantly share code, notes, and snippets.

@khuzema786
Forked from AlexanderFabisch/JuliaOctaveNumpy.md
Created April 30, 2020 02:16
Show Gist options
  • Save khuzema786/c03704c6a99dcbc07e6ac99888aca89a to your computer and use it in GitHub Desktop.
Save khuzema786/c03704c6a99dcbc07e6ac99888aca89a to your computer and use it in GitHub Desktop.
Comparison of Julia, NumPy and Octave
srand(0)
1:2:9 # implicit representation
2^2
a = zeros(10)
a = ones(10)
a = linspace(0, 1, 10)
A = eye(10)
A = rand(10, 10)
A = randn(10, 10)
B = [1 2 3 4 5 6 7 8 9 10; 11 12 13 14 15 16 17 18 19 20]
ndims(B)
size(B)
size(B, 1)
B[1:2, 2:5] # copies submatrix on assignment, sub(A, 1:2, 2:5) creates a view, slice(A, 1, 2:5) creates a view without keeping the first dimension of the array (of size 1)
B * A
B .* A[1:2, :] # .^, ./
A'
a'
inv(A)
pinv(A)
norm(A)
det(A)
A[1, 1]
A[end, end]
broadcast(+, A, a')
A[:]
reshape(A, 100)
exp(A)
indmax(A) # indmin, findmax, findmin
max(A) # min
w, v = eig(A)
chol(A*A')
U, s, V = svd(A) # U * diagm(s) * V' == A

Comparison of Julia, Python and Octave

Overview

Julia is a new languange for technical computing. It is a mix of R, Matlab, Python and other similar languages. Its main advantage is its speed: it is just in time (JIT) compiled and almost as fast as C. Another advantage is its type inference, i.e. you do not have to specify types (although you can), but all variables are statically typed. It is a high level language that is fast as well. Here I compare the behavior to similar languages like Octave (Matlab) and Python (with NumPy).

Julia Octave Python (NumPy)
Storage Order
Column-major Column-major Row-major
Indexing
1 based 1 based 0 based

Storage Order

Julia's storage order is column major, numpy's storage order row major, i.e. A[:, 1] is faster in Julia and A[0, :] is faster in numpy because these vectors are contiguous chunks in memory.

Indexing

Julia's arrays start with index 1, loops over the range 1:N go over [1, N]. Pythons arrays start with index 0 and loops over range(N) go over [0, N-1].

Links

randn('seed', 0)
1:2:9 # explicit representation
2^2
a = zeros(1, 10)
a = ones(1, 10)
a = linspace(0, 1, 10)
A = eye(10)
A = rand(10, 10)
A = randn(10, 10)
B = [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]]
ndims(B)
size(B)
size(B, 1)
B(1:2, 2:5) # slices copy parts of arrays
B * A
B .* A(1:2, :) # .^, ./
A'
a'
inv(A)
pinv(A)
norm(A)
det(A)
A(1, 1)
A(end, end)
A + a # broadcasting only works sometimes (e.g. with randn but not with eye)
A(:)
reshape(A, 100)
exp(A)
[value, idx] = max(A(:)) # min
max(A(:)) # min
[w, v] = eig(A)
chol(A*A')
[U, s, V] = svd(A) # U * s * V' == A
import numpy
numpy.random.seed(0)
range(1, 10, 2) # explicit representation, (implicit with xrange(1, 10, 2))
2**2
a = numpy.zeros(10)
a = numpy.ones(10)
a = numpy.linspace(0, 1, 10)
A = numpy.eye(10)
A = numpy.random.rand(10, 10)
A = numpy.random.randn(10, 10)
B = numpy.array([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]])
B.ndim
B.shape
B.shape[0]
B[:2, 1:5] # slices create views on original arrays
B.dot(A)
B * A[:2, :]
A.T
a[:, numpy.newaxis]
numpy.linalg.inv(A)
numpy.linalg.pinv(A)
numpy.linalg.norm(A)
numpy.linalg.det(A)
A[0, 0]
A[-1, -1]
A + a
A.T.ravel()
A.T.reshape(100)
numpy.exp(A)
A.argmax() # argmin
A.max()
w, v = numpy.linalg.eig(A)
numpy.linalg.cholesky(A.dot(A.T))
U, s, V = numpy.linalg.svd(A) # U.dot(numpy.diag(s)).dot(V) == A, V is not transposed!
@khuzema786
Copy link
Author

Helpful, Thanks !!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment