|
1 | | - |
2 | 1 | """ |
3 | 2 | `Contractor` represents a `Contractor` from ``\\mathbb{R}^N`` to ``\\mathbb{R}^N``. |
4 | 3 | Nout is the output dimension of the forward part. |
5 | 4 | """ |
6 | 5 | abstract type AbstractContractor end |
7 | 6 |
|
8 | | -struct Contractor{N, Nout, F1<:Function, F2<:Function, ex<:Union{Operation,Expr}} <:AbstractContractor |
| 7 | +struct Contractor{N, Nout, F1<:Function, F2<:Function, ex} <:AbstractContractor |
9 | 8 | variables::Vector{Symbol} # input variables |
10 | 9 | forward::GeneratedFunction{F1} |
11 | 10 | backward::GeneratedFunction{F2} |
@@ -86,70 +85,73 @@ function (C::BasicContractor)(A::IntervalBox{Nout,T}, X::IntervalBox{N,T})where |
86 | 85 | end |
87 | 86 |
|
88 | 87 | # allow 1D contractors to take Interval instead of IntervalBox for simplicty: |
89 | | -(C::BasicContractor)(A::Interval{T}, X::IntervalBox{N,T}) where {N,Nout,T} = C(IntervalBox(A), X) |
90 | | - |
91 | | -""" Contractor can also be construct without the use of macros |
92 | | - vars = @variables x y z |
93 | | - C = Contractor(x + y , vars) |
94 | | - C(-Inf..1, IntervalBox(0.5..1.5,3)) |
95 | | - """ |
| 88 | +(C::BasicContractor)(A::Interval{T}, X::IntervalBox{N,T}) where {N,T} = C(IntervalBox(A), X) |
96 | 89 |
|
97 | | -function Contractor(variables, expr::Operation) |
98 | 90 |
|
99 | | - var = [i.op.name for i in variables] |
100 | | - top, linear_AST = flatten(expr, var) |
| 91 | +function Base.show(io::IO, C::BasicContractor{F1,F2}) where {F1,F2} |
| 92 | + println(io, " Basic version of Contractor") |
| 93 | +end |
101 | 94 |
|
| 95 | +function _load_MT_contractor() |
| 96 | + return quote |
| 97 | + """ Contractor can also be construct without the use of macros |
| 98 | + vars = @variables x y z |
| 99 | + C = Contractor(x + y , vars) |
| 100 | + C(-Inf..1, IntervalBox(0.5..1.5,3)) |
| 101 | + """ |
| 102 | + function Contractor(variables, expr::Operation) |
102 | 103 |
|
103 | | - forward_code, backward_code = forward_backward(linear_AST) |
| 104 | + var = [i.op.name for i in variables] |
| 105 | + top, linear_AST = flatten(expr, var) |
104 | 106 |
|
105 | 107 |
|
106 | | - # @show top |
| 108 | + forward_code, backward_code = forward_backward(linear_AST) |
107 | 109 |
|
108 | | - if isa(top, Symbol) |
109 | | - top = [top] |
110 | | - end |
111 | 110 |
|
112 | | - forward = eval(forward_code) |
113 | | - backward = eval(backward_code) |
| 111 | + # @show top |
114 | 112 |
|
115 | | - Contractor(linear_AST.variables, |
116 | | - top, |
117 | | - GeneratedFunction(forward, forward_code), |
118 | | - GeneratedFunction(backward, backward_code), |
119 | | - expr) |
| 113 | + if isa(top, Symbol) |
| 114 | + top = [top] |
| 115 | + end |
120 | 116 |
|
121 | | -end |
| 117 | + forward = eval(forward_code) |
| 118 | + backward = eval(backward_code) |
122 | 119 |
|
| 120 | + Contractor(linear_AST.variables, |
| 121 | + top, |
| 122 | + GeneratedFunction(forward, forward_code), |
| 123 | + GeneratedFunction(backward, backward_code), |
| 124 | + expr) |
123 | 125 |
|
124 | | -function BasicContractor(variables, expr::Operation) |
| 126 | + end |
125 | 127 |
|
126 | | - var = [i.op.name for i in variables] |
127 | | - top, linear_AST = flatten(expr, var) |
128 | 128 |
|
129 | | - forward_code, backward_code = forward_backward(linear_AST) |
| 129 | + function BasicContractor(variables, expr::Operation) |
130 | 130 |
|
131 | | - forward = eval(forward_code) |
132 | | - backward = eval(backward_code) |
| 131 | + var = [i.op.name for i in variables] |
| 132 | + top, linear_AST = flatten(expr, var) |
133 | 133 |
|
134 | | - BasicContractor{typeof(forward), typeof(backward)}(forward, backward) |
135 | | -end |
| 134 | + forward_code, backward_code = forward_backward(linear_AST) |
136 | 135 |
|
137 | | -function Base.show(io::IO, C::BasicContractor{F1,F2}) where {F1,F2} |
138 | | - println(io, " Basic version of Contractor") |
139 | | -end |
| 136 | + forward = eval(forward_code) |
| 137 | + backward = eval(backward_code) |
140 | 138 |
|
141 | | -BasicContractor(expr::Operation) = BasicContractor([], expr::Operation) |
| 139 | + BasicContractor{typeof(forward), typeof(backward)}(forward, backward) |
| 140 | + end |
142 | 141 |
|
143 | | -BasicContractor(vars::Union{Vector{Operation}, Tuple{Vararg{Operation,N}}}, g::Function) where N = BasicContractor(vars, g(vars...)) #Contractor can be constructed by function name only |
| 142 | + BasicContractor(expr::Operation) = BasicContractor([], expr::Operation) |
144 | 143 |
|
145 | | -BasicContractor(vars, f::Function) = BasicContractor([Variable(Symbol(i))() for i in vars], f([Variable(Symbol(i))() for i in vars]...))#if vars is not vector of Operation |
| 144 | + BasicContractor(vars::Union{Vector{Operation}, Tuple{Vararg{Operation,N}}}, g::Function) where N = BasicContractor(vars, g(vars...)) #Contractor can be constructed by function name only |
146 | 145 |
|
| 146 | + BasicContractor(vars, f::Function) = BasicContractor([Variable(Symbol(i))() for i in vars], f([Variable(Symbol(i))() for i in vars]...))#if vars is not vector of Operation |
147 | 147 |
|
148 | | -Contractor(expr::Operation) = Contractor([], expr::Operation) |
| 148 | + Contractor(expr::Operation) = Contractor([], expr::Operation) |
149 | 149 |
|
150 | | -Contractor(vars::Union{Vector{Operation}, Tuple{Vararg{Operation,N}}}, g::Function) where N = Contractor(vars, g(vars...)) #Contractor can be constructed by function name only |
| 150 | + Contractor(vars::Union{Vector{Operation}, Tuple{Vararg{Operation,N}}}, g::Function) where N = Contractor(vars, g(vars...)) #Contractor can be constructed by function name only |
151 | 151 |
|
152 | | -Contractor(vars, f::Function) = Contractor([Variable(Symbol(i))() for i in vars], f([Variable(Symbol(i))() for i in vars]...))#if vars is not vector of Operation |
| 152 | + Contractor(vars, f::Function) = Contractor([Variable(Symbol(i))() for i in vars], f([Variable(Symbol(i))() for i in vars]...))#if vars is not vector of Operation |
| 153 | + end |
| 154 | +end |
153 | 155 |
|
154 | 156 | function make_contractor(expr::Expr, var = []) |
155 | 157 | # println("Entering Contractor(ex) with ex=$ex") |
|
0 commit comments