Julia memo
Frequently confused julia, python (numpy) and fortran
python,numpy | julia | fortran | |
---|---|---|---|
Array shape | np.shape | size | |
Total number of array elements | np.size | length | |
Array dimensions | np.ndim | ndims | |
Checking array typers | .dtype | typeof | |
Specification of axes in sum or mean | axis= | dims= | dim= |
Array maximum and minimum | np.max, np.min | maximum, minimum | maxval, minval |
Maximum and minimum locations | np.argmax, np.argmin | argmax, argmin | maxloc, minloc |
Zero element array | np.zeros | zeros | |
Array with (first, last, interval) | np.arange | range(step=) | |
Array with (first, last, number of elements) | np.linspace | range(length=) | |
Integer division | A//B | A÷B | A/B |
character combining | A+B | A*B | A//B |
Array cyclic shift | np.roll | circshift | cshift |
Numeric→Character | str | string | write |
Forced loop termination | break | break | exit |
Return to the beginning of the loop | continue | continue | cycle |
surplus | % | % | mod |
Logical OR, Logical product | or, and | ||, && | or, and |
\((-\pi,\pi]\) Returning inverse tangent | np.arctan2(y,x) | atan(y,x) | atan2(y,x) |
Quick and fast (written based on experience)
Make every program a function
Avoid computing anything outside of a function, as programs outside of functions are unlikely to benefit from the speedup.Do not use global variables
If you define a variable outside of a function, you can use that variable in all function.Const for constatns that do not change value
As an exception, constants given by const are not affected by the slowdown, even outside the function.Passing an array to another function as an argument
As global variables are not available, to pass an array from one function to another, pass it as a function argument.All arrays (vectors and matrices) to be used should be defined first, and then overwritten
One of the features of Julia and Python is that vectors and matrices can be easily created in the middle of a calculation. If you are aiming for high speed, define all necessary vectors and matrices first, and then use them by overwriting them.Actively use in place operations when they are provided
In-place is often faster than out-of-place for in-place and out-of-place operations.Use for instead of array expressions when in place operations are not provided
In Python, it is recommended to use for as little as possible and to write array expressions as arrays, but Julia often finds it faster to do the opposite and calculate each component using for.The point is that it is faster to write with some degree of fortran awareness.
I / O
Reading a space-delimited text file (common in Fortran)
using CSV using DataFrames data = CSV.read("file.txt", delim = " ", ignorerepeated = true)
Formatted export of ASCII data
Example with 5 real numbersusing Printf fmt1 = Printf.Format("%16s" ^ 5 * "\n") fmt2 = Printf.Format("%16.6e" ^ 5) open("value.txt", "w") do file Printf.format(file, fmt1, "π", "ℯ", "√2", "(1 + √5) / 2", "ζ(2)") Printf.format(file, fmt2, π, ℯ, √2, 0.5 * (1 + √5), π ^ 2 / 6) endThe contents of value.txt are
π ℯ √2 (1 + √5) / 2 ζ(2) 3.141593e+00 2.718282e+00 1.414214e+00 1.618034e+00 3.321928e+00
read/write binary data
write
using Random # Not necessary since we are only calling for random numbers
using HDF5
v1 = rand(5)
v2 = rand(6, 3)
h5open("value.bin", "w") do file
write(file, "Vec", v1)
write(file, "Mat", v2)
end
read
using HDF5
v1 = zeros(5)
v2 = zeros(ComplexF64, 6, 3) # Real number when written out, but can be read as real part of complex number when read
h5open("value.bin", "r") do file
v1 .= read(file, "Vec")
v2 .= read(file, "Mat")
end
Write immediately
flush(file) writes immediately. file is a file identifier. For standard output, stdout.