1- const monadic = [deg2rad, rad2deg, transpose, - , conj, asind, log1p, acsch, acos, asec, acosh, acsc, cscd, log, tand, log10, csch, asinh, abs2, cosh, sin, cos, atan, cospi, cbrt, acosd, acoth, inv, acotd, asecd, exp, acot, sqrt, sind, sinpi, asech, log2, tan, exp10, sech, coth, asin, cotd, cosd, sinh, abs, csc, tanh, secd, atand, sec, acscd, cot, exp2, expm1, atanh]
1+ const monadic = [deg2rad, rad2deg, transpose, - , conj, asind, log1p, acsch, acos, asec, acosh, acsc, cscd, log, tand, log10, csch, asinh, abs2, cosh, sin, cos, atan, cospi, cbrt, acosd, acoth, inv, acotd, asecd, exp, acot, sqrt, sind, sinpi, asech, log2, tan, exp10, sech, coth, asin, cotd, cosd, sinh, abs, csc, tanh, secd, atand, sec, acscd, cot, exp2, expm1, atanh, real ]
22
3- const diadic = [+ , - , max, min, * , / , \ , hypot, atan, mod, rem, ^ ]
3+ const diadic = [+ , - , max, min, * , / , \ , hypot, atan, mod, rem, ^ , copysign ]
44
55const previously_declared_for = Set ([])
6+
7+ # TODO : it's not possible to dispatch on the symtype! (only problem is Parameter{})
8+ function assert_number (a, b)
9+ assert_number (a)
10+ assert_number (b)
11+ end
12+
13+ assert_number (a) = symtype (a) <: Number || error (" Can't apply this to not a number" )
614# TODO : keep domains tighter than this
715function number_methods (T, rhs1, rhs2)
816 exprs = []
17+
18+ rhs2 = :($ assert_number (a, b); $ rhs2)
19+ rhs1 = :($ assert_number (a); $ rhs1)
20+
921 for f in diadic
1022 for S in previously_declared_for
1123 push! (exprs, quote
@@ -49,6 +61,9 @@ promote_symtype(::typeof(rem2pi), T::Type{<:Number}, mode) = T
4961Base. rem2pi (x:: Symbolic , mode:: Base.RoundingMode ) = term (rem2pi, x, mode)
5062
5163for f in monadic
64+ if f in [real]
65+ continue
66+ end
5267 @eval promote_symtype (:: $ (typeof (f)), T:: Type{<:Number} ) = Number
5368 @eval (:: $ (typeof (f)))(a:: Symbolic ) = term ($ f, a)
5469end
@@ -64,30 +79,38 @@ for f in [+, *]
6479 @eval (:: $ (typeof (f)))(x:: Symbolic ) = x
6580
6681 # single arg
67- @eval function (:: $ (typeof (f)))(x:: Symbolic , w... )
82+ @eval function (:: $ (typeof (f)))(x:: Symbolic , w:: Number ... )
6883 term ($ f, x,w... ,
6984 type= rec_promote_symtype ($ f, map (symtype, (x,w... ))... ))
7085 end
71- @eval function (:: $ (typeof (f)))(x, y:: Symbolic , w... )
86+ @eval function (:: $ (typeof (f)))(x:: Number , y:: Symbolic , w:: Number ... )
7287 term ($ f, x, y, w... ,
7388 type= rec_promote_symtype ($ f, map (symtype, (x, y, w... ))... ))
7489 end
75- @eval function (:: $ (typeof (f)))(x:: Symbolic , y:: Symbolic , w... )
90+ @eval function (:: $ (typeof (f)))(x:: Symbolic , y:: Symbolic , w:: Number ... )
7691 term ($ f, x, y, w... ,
7792 type= rec_promote_symtype ($ f, map (symtype, (x, y, w... ))... ))
7893 end
7994end
8095
96+ Base.:* (a:: AbstractArray , b:: Symbolic{<:Number} ) = map (x-> x* b, a)
97+ Base.:* (a:: Symbolic{<:Number} , b:: AbstractArray ) = map (x-> a* x, b)
98+
8199for f in [identity, one, zero, * , + ]
82100 @eval promote_symtype (:: $ (typeof (f)), T:: Type{<:Number} ) = T
83101end
84102
103+ promote_symtype (:: typeof (Base. real), T:: Type{<:Number} ) = Real
104+ Base. real (s:: Symbolic{<:Real} ) = s
105+ Base. real (s:: Symbolic{<:Number} ) = term (real, s)
106+
85107# # Booleans
86108
87109# binary ops that return Bool
88110for (f, Domain) in [(== ) => Number, (!= ) => Number,
89111 (<= ) => Real, (>= ) => Real,
90- (< ) => Real, (> ) => Real,
112+ (isless) => Real,
113+ (< ) => Real, (> ) => Real,
91114 (& ) => Bool, (| ) => Bool,
92115 xor => Bool]
93116 @eval begin
101124Base.:! (s:: Symbolic{Bool} ) = Term {Bool} (! , [s])
102125Base.:~ (s:: Symbolic{Bool} ) = Term {Bool} (! , [s])
103126
127+
104128# An ifelse node, ifelse is a built-in unfortunately
105129#
106130cond (_if:: Bool , _then, _else) = ifelse (_if, _then, _else)
107131function cond (_if:: Symbolic{Bool} , _then, _else)
108132 Term {Union{symtype(_then), symtype(_else)}} (cond, Any[_if, _then, _else])
109133end
134+
0 commit comments