forked from AlgebraicJulia/CANMOD-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
intro.jl
274 lines (185 loc) · 3.47 KB
/
intro.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
using Pkg
Pkg.status()
Pkg.add("Plots")
Pkg.update()
Pkg.status()
# ] add OrdinaryDiffEq
# Variables
a = 1
b = 2
c = a + b
# Printing
println(a)
println("a = $a")
println("a+b = $(a+b)")
# Functions
function f(x)
return x + 2
end
g(x) = 2x
threex = x -> 3x
h = f∘g
h(10)
h′ = g∘f
h′(10)
threex(2)
# Booleans and Logical operators
3 == 3
3 != 4
1 <= 2
1 < 2 < 3
1 ≤ 2
"a" < 2
# Conditionals
a = 3
if a == 2
println("a == 2")
elseif a < 2
println("a < 2")
else
println("a > 2")
end
# short circuiting
a, b, c, d = 3,4,2,2
a == b || c == d
a == b || "made it"
c == d || "made it"
a == b && "made it"
c == d && "made it"
# used for error handling
c == d && error("c should not equal d")
# Asserts and Testing
threex(2) == 6
@assert threex(2) == 6
using Test
@test threex(2) == 6
@testset "Test1" begin
@test threex(2) == 6
end
# Julia requires conditionals to be Bool. No "Truthy/Falsey"
nilstring = ""
if nilstring
println("nil")
else
println("notnil")
end
if nilstring == ""
println("nil")
else
println("notnil")
end
# Nothing is the programmers nil
println(nothing)
isnothing(nothing)
isnothing("")
# Missing is the statisticians N/A
Missing()
[1, 2, Missing(), 4]
# Arrays
v = [1,2,3,5]
rowv = [1 2 3 5]
vcat(v,v)
hcat(v,v)
vcat(rowv, rowv)
hcat(rowv, rowv)
rowv'
# collect forces any type to Array
collect(rowv')
# Loops and broadcasting
for i in 1:4
@show i, v[i]
end
for (i, x) in enumerate(v)
@show i, x
end
# arithmetic automatically broadcasts over arrays.
2v + v
# you can broadcast any function with the point syntax.
2 .* v .+ v
[1,2,3,4] .+ [5 6 7]
2 .* [5 6 7] .+ [1,2,3,4]
sin.((1:10)/2π)
# map and fold
map(x->2x, 1:10)
foldl(+, 1:10)
map(1:10) do x
2x
end
# indexing Arrays
v[3] == 3
# mutating Arrays
v[1] = 1
# array syntax is just syntax for functions https://docs.julialang.org/en/v1/manual/functions/#Operators-With-Special-Names
# adding entries with push!
push!(v, 6)
push!(v, 6)
# Randomness
using Random
r = rand()
r10 = rand(10)
rnormal = randn()
rnormal10 = randn(10)
binrand = rand(Bool, 10)
catrand = rand(1:3, 10)
# Tuples
p = (1,2)
p[1]
p[2]
for x in p
println(x)
end
function f(x,y)
return x + y, x*y
end
f(2,3)
f(p...)
# Types
v = [1,2,3]
typeof(v)
eltype(v)
w = v .+ 1.0
typeof(w)
eltype(w)
push!(w, Missing())
vcat(w, [Missing()])
𝟙₂₃ = ones(Float64, 2,3)
typeof(𝟙₂₃)
eltype(𝟙₂₃)
Rational{Int64}
Rational{Int8}(2,3)
Rational{Int8}(2,30)
Rational{Int8}(2,254)
Rational{Int8}(2,256)
struct MyRat{T}
num::T
den::T
end
MyRat(2,4)
myrat(x::Int,y::Int) = begin
@show d = gcd(x,y)
MyRat(div(x,d), div(y,d))
end
myrat(2,4)
# Plotting
using Plots
domₓ = (-10:10)/2π
codₛ = sin.(domₓ)
plot(domₓ,codₛ)
plot(domₓ,codₛ, line=:scatter)
# more tutorials for plots
# https://docs.juliaplots.org/stable/tutorial/
# https://github.com/JuliaPlots/StatsPlots.jl
# https://diffeq.sciml.ai/dev/basics/plot/
# For differences from R or Python see this list
# https://docs.julialang.org/en/v1/manual/noteworthy-differences/
# DiffEQ Solving
using OrdinaryDiffEq
f(u,p,t) = 1.01*u
u0 = 1/2
tspan = (0.0,1.0)
prob = ODEProblem(f,u0,tspan)
sol = solve(prob, Tsit5(), reltol=1e-8, abstol=1e-8)
using Plots
plot(sol,linewidth=5,title="Solution to the linear ODE with a thick line",
xaxis="Time (t)",yaxis="u(t) (in μm)",label="My Thick Line!") # legend=false
plot!(sol.t, t->0.5*exp(1.01t),lw=3,ls=:dash,label="True Solution!")