101
101
102
102
# Multiplication #
103
103
# ----------------#
104
- for T in (:Bool , :Real )
105
- @eval begin
106
- * (t:: TensorNumber , x:: $ (T)) = TensorNumber (hessnum (t) * x, tens (t) * x)
107
- * (x:: $ (T), t:: TensorNumber ) = TensorNumber (x * hessnum (t), x * tens (t))
108
- end
109
- end
110
-
111
104
function * {N}(t1:: TensorNumber{N} , t2:: TensorNumber{N} )
112
105
mul_h = hessnum (t1)* hessnum (t2)
113
106
tensvec = Array (eltype (mul_h), halftenslen (N))
@@ -134,23 +127,15 @@ function *{N}(t1::TensorNumber{N}, t2::TensorNumber{N})
134
127
return TensorNumber (mul_h, tensvec)
135
128
end
136
129
137
- # Division #
138
- # ----------#
139
- / (t:: TensorNumber , x:: Real ) = TensorNumber (hessnum (t) / x, tens (t) / x)
140
-
141
- function / (x:: Real , t:: TensorNumber )
142
- a = value (t)
143
- div_a = x / a
144
- div_a_sq = div_a / a
145
- div_a_cb = div_a_sq / a
146
-
147
- deriv1 = - div_a_sq
148
- deriv2 = div_a_cb + div_a_cb
149
- deriv3 = - (deriv2 + deriv2 + deriv2)/ a
150
-
151
- return tensnum_from_deriv (t, div_a, deriv1, deriv2, deriv3)
130
+ for T in (:Bool , :Real )
131
+ @eval begin
132
+ * (t:: TensorNumber , x:: $ (T)) = TensorNumber (hessnum (t) * x, tens (t) * x)
133
+ * (x:: $ (T), t:: TensorNumber ) = TensorNumber (x * hessnum (t), x * tens (t))
134
+ end
152
135
end
153
136
137
+ # Division #
138
+ # ----------#
154
139
function / {N}(t1:: TensorNumber{N} , t2:: TensorNumber{N} )
155
140
div_h = hessnum (t1)/ hessnum (t2)
156
141
tensvec = Array (eltype (div_h), halftenslen (N))
@@ -187,39 +172,23 @@ function /{N}(t1::TensorNumber{N}, t2::TensorNumber{N})
187
172
return TensorNumber (div_h, tensvec)
188
173
end
189
174
190
- # Exponentiation #
191
- # ----------------#
192
- ^ (:: Base.Irrational{:e} , t:: TensorNumber ) = exp (t)
193
-
194
- for T in (:Rational , :Integer , :Real )
195
- @eval begin
196
- function ^ (t:: TensorNumber , x:: $ (T))
197
- a = value (t)
198
- x_min_one = x - 1
199
- x_min_two = x - 2
200
- x_x_min_one = x * x_min_one
201
-
202
- exp_a = a^ x
203
- deriv1 = x * a^ x_min_one
204
- deriv2 = x_x_min_one * a^ x_min_two
205
- deriv3 = x_x_min_one * x_min_two * a^ (x - 3 )
206
-
207
- return tensnum_from_deriv (t, exp_a, deriv1, deriv2, deriv3)
208
- end
175
+ / (t:: TensorNumber , x:: Real ) = TensorNumber (hessnum (t) / x, tens (t) / x)
209
176
210
- function ^ (x:: $ (T), t:: TensorNumber )
211
- log_x = log (x)
177
+ function / (x:: Real , t:: TensorNumber )
178
+ a = value (t)
179
+ div_a = x / a
180
+ div_a_sq = div_a / a
181
+ div_a_cb = div_a_sq / a
212
182
213
- exp_x = x^ value (t)
214
- deriv1 = exp_x * log_x
215
- deriv2 = deriv1 * log_x
216
- deriv3 = deriv2 * log_x
183
+ deriv1 = - div_a_sq
184
+ deriv2 = div_a_cb + div_a_cb
185
+ deriv3 = - (deriv2 + deriv2 + deriv2)/ a
217
186
218
- return tensnum_from_deriv (t, exp_x, deriv1, deriv2, deriv3)
219
- end
220
- end
187
+ return tensnum_from_deriv (t, div_a, deriv1, deriv2, deriv3)
221
188
end
222
189
190
+ # Exponentiation #
191
+ # ----------------#
223
192
function ^ {N}(t1:: TensorNumber{N} , t2:: TensorNumber{N} )
224
193
exp_h = hessnum (t1)^ hessnum (t2)
225
194
tensvec = Array (eltype (exp_h), halftenslen (N))
@@ -273,6 +242,37 @@ function ^{N}(t1::TensorNumber{N}, t2::TensorNumber{N})
273
242
return TensorNumber (exp_h, tensvec)
274
243
end
275
244
245
+ ^ (:: Base.Irrational{:e} , t:: TensorNumber ) = exp (t)
246
+
247
+ for T in (:Rational , :Integer , :Real )
248
+ @eval begin
249
+ function ^ (t:: TensorNumber , x:: $ (T))
250
+ a = value (t)
251
+ x_min_one = x - 1
252
+ x_min_two = x - 2
253
+ x_x_min_one = x * x_min_one
254
+
255
+ exp_a = a^ x
256
+ deriv1 = x * a^ x_min_one
257
+ deriv2 = x_x_min_one * a^ x_min_two
258
+ deriv3 = x_x_min_one * x_min_two * a^ (x - 3 )
259
+
260
+ return tensnum_from_deriv (t, exp_a, deriv1, deriv2, deriv3)
261
+ end
262
+
263
+ function ^ (x:: $ (T), t:: TensorNumber )
264
+ log_x = log (x)
265
+
266
+ exp_x = x^ value (t)
267
+ deriv1 = exp_x * log_x
268
+ deriv2 = deriv1 * log_x
269
+ deriv3 = deriv2 * log_x
270
+
271
+ return tensnum_from_deriv (t, exp_x, deriv1, deriv2, deriv3)
272
+ end
273
+ end
274
+ end
275
+
276
276
# Unary functions on TensorNumbers #
277
277
# ----------------------------------#
278
278
# the third derivatives of functions in unsupported_unary_tens_funcs
325
325
@inline calc_atan2 (y:: Real , x:: TensorNumber ) = calc_atan2 (y, hessnum (x))
326
326
@inline calc_atan2 (y:: TensorNumber , x:: Real ) = calc_atan2 (hessnum (y), x)
327
327
328
- for Y in (:Real , :TensorNumber ), X in (:Real , :TensorNumber )
328
+ for Y in (:TensorNumber , :Real ), X in (:TensorNumber , :Real )
329
329
if ! (Y == :Real && X == :Real )
330
330
@eval begin
331
331
function atan2 (y:: $Y , x:: $X )
0 commit comments