-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathhelpers.jl
73 lines (69 loc) · 1.95 KB
/
helpers.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
using Printf
using LinearAlgebra
macro max(range, ex)
:(maximum($(Expr(:typed_comprehension, :Float64, ex, range))))
end
macro sum(range, ex)
:(sum($(Expr(:typed_comprehension, :Float64, ex, range))))
end
macro min(range, ex)
:(minimum($(Expr(:typed_comprehension, :Float64, ex, range))))
end
macro prod(range, ex)
:(prod($(Expr(:typed_comprehension, :Float64, ex, range))))
end
macro argmax(range, ex)
@assert(range.head == :in)
@assert(length(range.args) == 2)
:($(range.args[2])[indmax($(Expr(:typed_comprehension, :Float64, ex, range)))])
end
macro argmin(range, ex)
@assert(range.head == :in)
@assert(length(range.args) == 2)
:($(range.args[2])[indmin($(Expr(:typed_comprehension, :Float64, ex, range)))])
end
macro array(range, ex)
:($(Expr(:typed_comprehension, :Float64, ex, range)))
end
function polyfit(x, y, n)
A = [float(xi)^p for xi in x, p = 0:n]
(q, r) = LinearAlgebra.qr(A)
r \ (q[:,1:n+1]' * y)
end
function prettyPolynomial(λ)
o = IOBuffer()
Printf.@printf(o, "\$")
for i = 1:length(λ)
if i == 1
Printf.@printf(o, "%0.2f", λ[i])
elseif i == 2
if λ[i] < 0
Printf.@printf(o, "%0.2f x", λ[i])
else
Printf.@printf(o, "+%0.2f x", λ[i])
end
else
if λ[i] < 0
Printf.@printf(o, "%0.2fx^{%d}", λ[i], i-1)
else
Printf.@printf(o, "+%0.2fx^{%d}", λ[i], i-1)
end
end
end
Printf.@printf(o, "\$")
String(take!(o))
end
using TikzPictures
function plot_chain(len; fill::Dict{Int,String}=Dict{Int64,String}())
str = "\\draw "
for i in 1:len
fl = get(fill, i, "white")
str = string(str, "($(i)cm, 0cm) node[draw=black,circle,fill=$fl]{$i}")
if i == len
str = string(str, ";")
else
str = string(str, " -- ")
end
end
return TikzPicture(str)
end