-
Notifications
You must be signed in to change notification settings - Fork 156
/
num.jl
187 lines (155 loc) · 5.95 KB
/
num.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
@symbolic_wrap struct Num <: Real
val
end
unwrap(x::Num) = x.val
"""
Num(val)
Wrap anything in a type that is a subtype of Real
"""
Num
const show_numwrap = Ref(false)
Num(x::Num) = x # ideally this should never be called
(n::Num)(args...) = Num(value(n)(map(value,args)...))
value(x) = unwrap(x)
SciMLBase.issymbollike(::Num) = true
SciMLBase.issymbollike(::SymbolicUtils.Symbolic) = true
SymbolicUtils.@number_methods(
Num,
Num(f(value(a))),
Num(f(value(a), value(b))),
[conj, real, transpose]
)
Base.conj(x::Num) = x
Base.transpose(x::Num) = x
Base.eps(::Type{Num}) = Num(0)
Base.typemin(::Type{Num}) = Num(-Inf)
Base.typemax(::Type{Num}) = Num(Inf)
Base.float(x::Num) = x
IfElse.ifelse(x::Num,y,z) = Num(IfElse.ifelse(value(x), value(y), value(z)))
Base.promote_rule(::Type{Bool}, ::Type{<:Num}) = Num
for C in [Complex, Complex{Bool}]
@eval begin
Base.:*(x::Num, z::$C) = Complex(x * real(z), x * imag(z))
Base.:*(z::$C, x::Num) = Complex(real(z) * x, imag(z) * x)
Base.:/(x::Num, z::$C) = let (a, b) = reim(z), den = a^2 + b^2
Complex(x * a / den, -x * b / den)
end
Base.:/(z::$C, x::Num) = Complex(real(z) / x, imag(z) / x)
Base.:+(x::Num, z::$C) = Complex(x + real(z), imag(z))
Base.:+(z::$C, x::Num) = Complex(real(z) + x, imag(z))
Base.:-(x::Num, z::$C) = Complex(x - real(z), -imag(z))
Base.:-(z::$C, x::Num) = Complex(real(z) - x, imag(z))
end
end
function Base.inv(z::Complex{Num})
a, b = reim(z)
den = a^2 + b^2
Complex(a/den, -b/den)
end
function Base.:/(x::Complex{Num}, y::Complex{Num})
a, b = reim(x)
c, d = reim(y)
den = c^2 + d^2
Complex((a*c + b*d)/den, (b*c - a*d)/den)
end
Base.:^(z::Complex{Num}, n::Integer) = Base.power_by_squaring(z, n)
Base.:^(::Irrational{:ℯ}, x::Num) = exp(x)
function Base.show(io::IO, z::Complex{<:Num})
r, i = reim(z)
compact = get(io, :compact, false)
show(io, r)
print(io, (compact ? "+" : " + ") * "(")
show(io, i)
print(io, ")*im")
end
# TODO: move this to SymbolicUtils
substitute(expr, s::Pair; kw...) = substituter([s[1] => s[2]])(expr; kw...)
substitute(expr, s::Vector; kw...) = substituter(s)(expr; kw...)
substituter(pair::Pair) = substituter((pair,))
function substituter(pairs)
dict = Dict(value(k) => value(v) for (k, v) in pairs)
(expr; kw...) -> SymbolicUtils.substitute(expr, dict; kw...)
end
SymbolicUtils.symtype(n::Num) = symtype(value(n))
Base.nameof(n::Num) = nameof(value(n))
Base.iszero(x::Num) = SymbolicUtils.fraction_iszero(unwrap(x))
Base.isone(x::Num) = SymbolicUtils.fraction_isone(unwrap(x))
import SymbolicUtils: <ₑ, Symbolic, Term, operation, arguments
Base.show(io::IO, n::Num) = show_numwrap[] ? print(io, :(Num($(value(n))))) : Base.show(io, value(n))
Base.promote_rule(::Type{<:Number}, ::Type{<:Num}) = Num
Base.promote_rule(::Type{<:Symbolic{<:Number}}, ::Type{<:Num}) = Num
function Base.getproperty(t::Union{Add, Mul, Pow, Term}, f::Symbol)
if f === :op
Base.depwarn("`x.op` is deprecated, use `operation(x)` instead", :getproperty, force=true)
operation(t)
elseif f === :args
Base.depwarn("`x.args` is deprecated, use `arguments(x)` instead", :getproperty, force=true)
arguments(t)
else
getfield(t, f)
end
end
<ₑ(s::Num, x) = value(s) <ₑ value(x)
<ₑ(s, x::Num) = value(s) <ₑ value(x)
<ₑ(s::Num, x::Num) = value(s) <ₑ value(x)
Num(q::Irrational) = Num(Term(identity, [q]))
for T in (Integer, Rational)
@eval Base.:(^)(n::Num, i::$T) = Num(value(n)^i)
end
macro num_method(f, expr, Ts=nothing)
if Ts === nothing
Ts = [Any]
else
@assert Ts.head == :tuple
# e.g. a tuple or vector
Ts = Ts.args
end
ms = [quote
$f(a::$T, b::$Num) = $expr
$f(a::$Num, b::$T) = $expr
end for T in Ts]
quote
$f(a::$Num, b::$Num) = $expr
$(ms...)
end |> esc
end
# Boolean operations
for (f, Domain) in [:(==) => :((AbstractFloat, Number)), :(!=) => :((AbstractFloat, Number)),
:(<=) => :((Real,)), :(>=) => :((Real,)),
:(isless) => :((Real, AbstractFloat)),
:(<) => :((Real,)), :(> ) => :((Real,)),
:(& )=> :((Bool,)), :(| ) => :((Bool,)),
:xor => :((Bool,))]
@eval @num_method Base.$f (val = $f(value(a), value(b)); val isa Bool ? val : Num(val)) $Domain
end
for f in [:!, :~]
@eval Base.$f(x::Num) = (val = $f(value(x)); val isa Bool ? val : Num(val))
end
@num_method Base.isequal isequal(value(a), value(b)) (AbstractFloat, Number, Symbolic)
Base.hash(x::Num, h::UInt) = hash(value(x), h)
Base.convert(::Type{Num}, x::Symbolic{<:Number}) = Num(x)
Base.convert(::Type{Num}, x::Number) = Num(x)
Base.convert(::Type{Num}, x::Num) = x
Base.convert(::Type{<:Array{Num}}, x::AbstractArray) = map(Num, x)
Base.convert(::Type{<:Array{Num}}, x::AbstractArray{Num}) = x
Base.convert(::Type{Sym}, x::Num) = value(x) isa Sym ? value(x) : error("cannot convert $x to Sym")
LinearAlgebra.lu(x::Union{Adjoint{<:Num},Transpose{<:Num},Array{<:Num}}; check=true, kw...) = sym_lu(x; check=check)
_iszero(x::Number) = iszero(x)
_isone(x::Number) = isone(x)
_iszero(::Symbolic) = false
_isone(::Symbolic) = false
_iszero(x::Num) = _iszero(value(x))
_isone(x::Num) = _isone(value(x))
Code.cse(x::Num) = Code.cse(unwrap(x))
## Documentation
# This method makes the docstring show all entries in the metadata dict associated with an instance of Num
function Base.Docs.getdoc(x::Num)
x = unwrap(x)
strings =
["A variable of type Symbolics.Num (Num wraps anything in a type that is a subtype of Real)";
"# Metadata"]
for (key, val) in collect(pairs(x.metadata))
push!(strings, string(string(key), ": ", string(val)))
end
Markdown.parse(join(strings, "\n\n "))
end