-
Notifications
You must be signed in to change notification settings - Fork 0
/
4_piecewise_functions.jl
81 lines (75 loc) · 3.08 KB
/
4_piecewise_functions.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
import SchumakerSpline.evaluate
function evaluate(f::Piecewise_Function, point::Real)
which_function = searchsortedlast(f.starts_, point)
return evaluate(f.functions_[which_function], point)
end
function (s::Piecewise_Function)(x::Union{Real,Date,DateTime,DatePeriod})
return evaluate(s, x)
end
function derivative(f::Piecewise_Function)
derivatives = derivative.( f.functions_)
return Piecewise_Function(f.starts_, derivatives)
end
function indefinite_integral(f::Piecewise_Function)
indef_integrals = indefinite_integral.( f.functions_)
return Piecewise_Function(f.starts_, indef_integrals)
end
function +(f::Piecewise_Function,number::Real)
functions_with_addition = f.functions_ .+ number
return Piecewise_Function(f.starts_, functions_with_addition)
end
function -(f::Piecewise_Function,number::Real)
functions_with_subtraction = f.functions_ .- number
return Piecewise_Function(f.starts_, functions_with_subtraction)
end
function *(f::Piecewise_Function,number::Real)
functions_with_multiplication = f.functions_ .* number
return Piecewise_Function(f.starts_, functions_with_multiplication)
end
function /(f::Piecewise_Function,number::Real)
functions_with_division = f.functions_ ./ number
return Piecewise_Function(f.starts_, functions_with_division)
end
function create_common_pieces(f1::Piecewise_Function,f2::Piecewise_Function)
starts_ = unique(sort!(vcat(f1.starts_,f2.starts_)))
functions1_ = Array{UnivariateFunction}(undef, length(starts_))
functions2_ = Array{UnivariateFunction}(undef, length(starts_))
for i in 1:length(starts_)
point = starts_[i]
which_1 = searchsortedlast(f1.starts_, point)
functions1_[i] = f1.functions_[which_1]
which_2 = searchsortedlast(f2.starts_, point)
functions2_[i] = f2.functions_[which_2]
end
return Piecewise_Function(starts_, functions1_) , Piecewise_Function(starts_, functions2_)
end
function +(f1::Piecewise_Function,f2::Piecewise_Function)
c_f1, c_f2 = create_common_pieces(f1,f2)
starts_ = c_f1.starts_
number_of_pieces = length(starts_)
functions_ = Array{UnivariateFunction}(undef, number_of_pieces)
for i in 1:number_of_pieces
functions_[i] = c_f1.functions_[i] + c_f2.functions_[i]
end
return Piecewise_Function(starts_, functions_)
end
function *(f1::Piecewise_Function,f2::Piecewise_Function)
c_f1, c_f2 = create_common_pieces(f1,f2)
starts_ = c_f1.starts_
number_of_pieces = length(starts_)
functions_ = Array{UnivariateFunction}(undef, number_of_pieces)
for i in 1:number_of_pieces
functions_[i] = c_f1.functions_[i] * c_f2.functions_[i]
end
return Piecewise_Function(starts_, functions_)
end
function -(f1::Piecewise_Function,f2::Piecewise_Function)
c_f1, c_f2 = create_common_pieces(f1,f2)
starts_ = c_f1.starts_
number_of_pieces = length(starts_)
functions_ = Array{UnivariateFunction}(undef, number_of_pieces)
for i in 1:number_of_pieces
functions_[i] = c_f1.functions_[i] - c_f2.functions_[i]
end
return Piecewise_Function(starts_, functions_)
end