-
Notifications
You must be signed in to change notification settings - Fork 20
/
julia_tutorial.jl
109 lines (84 loc) · 1.89 KB
/
julia_tutorial.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# JULIATUTORIAL Crash course in Julia
## Column major indexing:
srand(1)
C = randn(5,5)
C[:]
reshape(C[:], 5, 5)
## Arrays are passed by reference, no clearing variables
B = zeros(5)
A = B
B[3] = 42
A
# Chinese can be used
朱莉婭 = 5
朱莉婭 * 4
whos()
workspace()
whos()
## Function naming convention, timing
function addones!(x::Vector, z::Vector)
z = x + ones(length(x))
x = x - ones(length(x))
return x, z
end
function addones(x::Vector)
z = similar(y)
z = x + ones(length(x))
end
x = 3 * ones(5)
y = x
z = zeros(5)
@time x, z = addones!(x, z)
@time z2 = addones(y)
#Try it again
@time x, z = addones!(x,z)
@time z2 = addones(y)
#What happens?
## Types:
f(x::Float64) = x^3
f(4.5)
# now try f(4)
#what happens?
g(x) = x^3
g(4.5)
g(4)
g("Ho ")
## Linear Algebra fun(ctions)!
srand(2015)
A = rand(5, 5)
B = A * A'
@assert issym(B)
@assert isposdef(B)
# what does assert do? how can we quickly look it up?
chol(B)
L = chol(B, :L)
U = chol(B, :U)
W,S,V = svd(B)
(),S2 = svd(A)
## PyPlotting
using PyPlot
PyPlot.plot(S2)
PyPlot.figure()
PyPlot.loglog(S2,linewidth=3,"go--")
PyPlot.close("all")
PyPlot.figure()
PyPlot.plot(S2, label = "sv's of A",color="green", linestyle="dashed", marker="o", markersize=12)
PyPlot.plot(S, label = "sv's of B",color="magenta", linestyle="dashed", marker="o", markersize=12)
PyPlot.legend()
PyPlot.figure()
x = linspace(0, 25, 100)
PyPlot.plot(x, sin(x))
PyPlot.plot(y, cos(x))
PyPlot.legend(["sin","cos"],loc="lower left")
a = 42
PyPlot.title("Title, a = $(a)"), xlabel("the x-axis label"), ylabel ("the y-axislabel")
PyPlot.title("A title with latex, \$\\alpha + \\beta\$, a = $(a)")
mypath = pwd()
PyPlot.savefig(mypath * "/deleteme.pdf")
PyPlot.savefig("deletemetoo.eps")
## saving data
using JLD
mypath = pwd()
JLD.save(mypath * "/deletedata.jld","randA",A,"randB",B)
A = JLD.load(mypath * "deletedata.jld","randA")
newnameB = JLD.load("deletedata.jld","randB")