methods(+)
\alpha
followed immediately by a TABx = 10
println(x)
β = 0.1
println(β)
_
(underscore) can be used a separator, it is simply ignoredbig(number)
1_000_000_000 + 1
1_00_00_00_00_00 + 1
4000000000000000 * 4e300
big(4000000000000000) * 4e300
1 + 2 + 3 == +(1,2,3)
/
which is floating point divisiondiv
which is intenger division3 + 2 + 1
3 * 2 * 1
4 ^ 5
4 % 5
@code_native 1 + 2 + 3
@code_native +(1,2,3)
3/2
3 ÷2
div(3,2)
x = 20
4 * x + 3
y = 10
(10)(4)(x * y) + 3
sqrt
or $\sqrt*$sin
, cos
, etc.lcm
and gcd
abs
and sign
sqrt(200)
√200
sin(pi/2)
cos(pi/2)
lcm(100,5,20,40)
gcd(100,5,20,40)
abs(10)
abs(-10)
sign(-10)
pi
or $\pi$e
golden
or $\varphi$ (\varphi
NOT \phi
)sin(π/2)
log(e)
φ
function
end
f(x) = x * x
function my_first_function(a,b,c)
a + b * c
end
my_first_function(1,2,4)
my_first_function(1,2,3.5)
my_first_function(1,4//5,4)
function defaults(a,b=10,c=20)
a + b + c
end
methods(defaults)
defaults(10)
@code_native defaults(10)
z(x) = 4x + 3
z(10)
arguments -> function_body
(a,b,c) -> function_body
x = y->10y
x(10)
map(x -> x*x , [1 2 3 4])
[]
end
keywordmean
, median
, etc exist for arraysmy_array = [1 2 3 4 5 6]
my_array[0]
my_array[1]
my_array[end]
my_array[end-1]
my_array = [1,2,3,4]
my_array + 4
my_array * 4
√my_array
mean(my_array)
[1 2 3 4; 5 6 7 8]
[1 2 3 4;
5 6 7 8]
[1 2 3 4; 5 6 7 8; 9 10 11 12]
[ 1 2 3 4]
[1,2,3,4]
.
after the function name but before the parenthesesmap
or a for loop most of the timec = 123456
test = [1 8 1 9 0 4 8 1 6 3]
f(x) = x + c
@time map(f,test)
@time test + c
@time f.(test) #Only works in Julia 0.5 + :(
two_d = [1 8 1; 9 0 4; 8 1 6]
@time map(f,two_d)
@time f.(two_d)
@time two_d + c
name::TYPE
subtypes
and super
or supertype
in new versions of Juliasupertype(Number)
supertype(Float64)
subtypes(AbstractString)
subtypes(Number)
subtypes(Any)
function typed(x)
x ^ 3
end
function typed(x::Integer)
x ^ 3
end
@time typed(10.0)
@time typed(10)
## From https://en.wikibooks.org/wiki/Introducing_Julia/Types
function t1(n)
s = 0
for i in 1:n
s += s/i
end
end
## From https://en.wikibooks.org/wiki/Introducing_Julia/Types
function t2(n)
s = 0.0
for i in 1:n
s += s/i
end
end
@time t1(10000000)
@time t2(10000000)
struct name #(type in older versions)
member1::type1
member2::type2
end
type TIME #struct TIME in Julia > 0.5
hour::Integer
minute::Integer
end
x = TIME(10,30)
x.hour
+
is properly known as Base.:+
importall Base.Operators
function Base.:+(a::TIME, b::TIME) #New style is just Base.:+
TIME(a.hour + b.hour, a.minute + b.minute)
end
x + TIME(11,30)
*
NOT +
string = "Hello"
uni = "Helαβ"
println(typeof(string) , " ", typeof(uni))
string * uni
string ^ 3
another="Hello is $string and $uni"
end
in
keywordfor x in [1 2 3 4 5]
println(x)
end
for x in 1:1:5
println(x)
end
for x in 1:5
println(x)
end
@parallel
macro turns any loop into a parallel loop@parallel
macro can take one argument, which will be the reduce functionnworkers()
addprocs(4)
nworkers()
a = SharedArray{Float64}(10)
@parallel for i = 1:10
print(i)
a[i] = i
end
print(a)
a = SharedArray{Float64}(10)
@sync @parallel for i = 1:10
print(i)
a[i] = i
end
print(a)
a = SharedArray{Float64}(10)
fut = @parallel for i = 1:10
print(i)
a[i] = i
end
for x in fut
fetch(x)
end
print(a)
@time nheads = @parallel (+) for i = 1:200000000
Int(rand(Bool))
end
nheads_old = 0
@time for i = 1:200000000
nheads_old += Int(rand(Bool))
end
if
, elseif
, else
, and end
end
keyword goes at the end of the entire blockx = 20 + 4
if x > 50
println("GOOD")
elseif x < 20
println("BAD")
else
println("OK")
end
(2+3)::Float64
Pkg.add(PACKAGENAME)
using
- Places functions in global namespaceimport
- Need to access using module nameinclude()
, and require()