@@ -42,7 +42,7 @@ Base.copy(p::Parameter) = @match p begin
42
42
Factor (f, _) => Parameter (Val (:Factor ), f)
43
43
end
44
44
45
- function Base. show (io:: IO , p:: Parameter )
45
+ function Base. show (io:: IO , :: MIME"text/plain" , p:: Parameter )
46
46
@match p begin
47
47
PiUnit (pu, _) && if pu isa Number
48
48
end => print (io, " $(pu) ⋅π" )
@@ -70,9 +70,27 @@ Base.:(==)(p1::Parameter, p2::Parameter) = eqeq(p1, p2)
70
70
Base.:(== )(p1:: Parameter , p2:: Number ) = eqeq (p1, p2)
71
71
Base.:(== )(p1:: Number , p2:: Parameter ) = eqeq (p2, p1)
72
72
73
- # following the same convention in Phase.jl implementation
73
+ function Base. contains (p:: Parameter , θ:: Symbol )
74
+ @match p begin
75
+ PiUnit (pu, pt) && if ! (pu isa Number)
76
+ end => Base. contains (repr (pu), " :" * string (θ))
77
+ _ => false
78
+ end
79
+ end
80
+
81
+ function Base. contains (p:: Parameter , θ:: Expr )
82
+ @match p begin
83
+ PiUnit (pu, pt) && if ! (pu isa Number)
84
+ end => Base. contains (repr (pu), " :(" * string (θ) * " )" )
85
+ _ => false
86
+ end
87
+ end
88
+
89
+ # following the same convention in phase.jl implementation
74
90
# comparison have inconsistent, we are comparing phases to numbers
75
91
# if cause trouble, will change
92
+ #
93
+
76
94
Base. isless (p1:: Parameter , p2:: Number ) = @match p1 begin
77
95
PiUnit (_... ) => p1. pu isa Number && p1. pu < p2
78
96
_ => p1. f < p2
@@ -109,8 +127,9 @@ function add_param(p1, p2)
109
127
@match (p1, p2) begin
110
128
(PiUnit (pu1, _), PiUnit (pu2, _)) && if pu1 isa Number && pu2 isa Number
111
129
end => Parameter (Val (:PiUnit ), pu1 + pu2)
112
- (PiUnit (pu1, _), PiUnit (pu2, _)) && if ! (pu1 isa Number) || ! (pu2 isa Number)
113
- end => Parameter (Val (:PiUnit ), Expr (:call , :+ , pu1, pu2))
130
+ (PiUnit (pu1, pu1_t), PiUnit (pu2, pu2_t)) &&
131
+ if ! (pu1 isa Number) || ! (pu2 isa Number)
132
+ end => PiUnit (Expr (:call , :+ , pu1, pu2), Base. promote_op (+ , pu1_t, pu2_t))
114
133
(Factor (f1, _), Factor (f2, _)) => Parameter (Val (:Factor ), f1 + f2)
115
134
(PiUnit (pu1, _), Factor (f2, _)) => Parameter (Val (:Factor ), exp (im * pu1 * π) * f2)
116
135
(Factor (f1, _), PiUnit (pu2, _)) => Parameter (Val (:Factor ), exp (im * pu2 * π) * f1)
@@ -134,8 +153,8 @@ function subt_param(p1, p2)
134
153
end => Parameter (Val (:PiUnit ), pu1 - pu2)
135
154
(PiUnit (pu1, pu_t1), PiUnit (pu2, pu_t2)) &&
136
155
if ! (pu1 isa Number) || ! (pu2 isa Number)
137
- end => Parameter ( Val ( : PiUnit), Expr (:call , :- , pu1, pu2))
138
- (Factor (f1, _), Factor (f2, _)) => Factor ( f1 - f2)
156
+ end => PiUnit ( Expr (:call , :- , pu1, pu2), Base . promote_op ( - , pu_t1, pu_t2 ))
157
+ (Factor (f1, _), Factor (f2, _)) => Parameter ( Val ( :Factor ), f1 - f2)
139
158
(PiUnit (_... ), Factor (_... )) => Parameter (Val (:Factor ), exp (im * p1. pu * π) - p2. f)
140
159
(Factor (_... ), PiUnit (_... )) => Parameter (Val (:Factor ), p1. f - exp (im * p2. pu * π))
141
160
(_, PiUnit (_... )) => Parameter (Val (:PiUnit ), p1 - p2. pu)
@@ -151,6 +170,29 @@ Base.:(-)(p1::Parameter, p2::Parameter) = subt_param(p1, p2)
151
170
Base.:(- )(p1:: Number , p2:: Parameter ) = subt_param (p1, p2)
152
171
Base.:(- )(p1:: Parameter , p2:: Number ) = add_param (p1, - p2)
153
172
173
+ # needed for tensor contraction
174
+ function mul_param (p1, p2)
175
+ @match (p1, p2) begin
176
+ (PiUnit (p1u, _), PiUnit (p2u, _)) && if p1u isa Number && p2u isa Number
177
+ end => exp (im * (p1u + p2u) * π)
178
+ (PiUnit (p1u, _), n2:: Number ) && if p1u isa Number
179
+ end => exp (im * p1u * π) * n2
180
+ (Factor (f1, _), PiUnit (p2u, _)) && if p2u isa Number
181
+ end => f1 * exp (im * p2u * π)
182
+ (PiUnit (p1u, _), Factor (f2, _)) && if p1u isa Number
183
+ end => f2 * exp (im * p1u * π)
184
+ (Factor (f1, _), Factor (f2, _)) => f1 * f2
185
+ (Factor (f1, _), n2:: Number ) => f1 * n2
186
+ _ => error (
187
+ " Invalid input '$(p1) ' of type $(typeof (p1)) and '$(p2) ' of type $(typeof (p2)) for ADT: *" ,
188
+ )
189
+ end
190
+ end
191
+
192
+ Base.:(* )(p1:: Parameter , p2:: Parameter ) = mul_param (p1, p2)
193
+ Base.:(* )(p1:: Parameter , p2:: Number ) = mul_param (p1, p2)
194
+ Base.:(* )(p1:: Number , p2:: Parameter ) = mul_param (p2, p1)
195
+
154
196
function Base. rem (p:: Parameter , d:: Number )
155
197
@match p begin
156
198
PiUnit (pu, pu_t) && if pu isa Number
@@ -163,3 +205,14 @@ function Base.rem(p::Parameter, d::Number)
163
205
)
164
206
end
165
207
end
208
+
209
+ function Base. inv (p:: Parameter )
210
+ @match p begin
211
+ PiUnit (pu, pu_t) && if pu isa Number
212
+ end => Parameter (Val (:PiUnit ), - pu)
213
+ PiUnit (pu, pu_t) && if ! (pu isa Number)
214
+ end => Parameter (Val (:PiUnit ), Expr (:call , :- , pu))
215
+ Factor (f, _) => Parameter (Val (:Factor ), inv (f))
216
+ _ => error (" Invalid input '$(p) ' of type $(typeof (p)) for ADT: inv" )
217
+ end
218
+ end
0 commit comments