-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy patharithmetic.jl
144 lines (130 loc) · 4.05 KB
/
arithmetic.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
const _APL = MP.AbstractPolynomialLike
# We don't define it for all `AlgebraElement` as this would be type piracy
const _AE = SA.AlgebraElement{<:Algebra}
for op in [:+, :-, :*]
@eval begin
function MA.promote_operation(
::typeof($op),
::Type{P},
::Type{Q},
) where {P<:_APL,Q<:_AE}
return MA.promote_operation($op, P, MP.polynomial_type(Q))
end
Base.$op(p::_APL, q::_AE) = $op(p, MP.polynomial(q))
function MA.promote_operation(
::typeof($op),
::Type{P},
::Type{Q},
) where {P<:_AE,Q<:_APL}
return MA.promote_operation($op, MP.polynomial_type(P), Q)
end
Base.$op(p::_AE, q::_APL) = $op(MP.polynomial(p), q)
# Break ambiguity between the two defined below and the generic one in SA
function MA.promote_operation(
::typeof($op),
::Type{P},
::Type{Q},
) where {P<:_AE,Q<:_AE}
return SA.algebra_promote_operation($op, P, Q)
end
function Base.$op(p::_AE, q::_AE)
return MA.operate_to!(SA._preallocate_output($op, p, q), $op, p, q)
end
end
end
for op in [:+, :-]
@eval begin
function MA.promote_operation(
::typeof($op),
::Type{P},
::Type{Q},
) where {P,Q<:_AE}
I = MA.promote_operation(implicit, Q)
return MA.promote_operation(
$op,
constant_algebra_element_type(
MA.promote_operation(SA.basis, I),
P,
),
I,
)
end
function Base.$op(p, q::_AE)
i = implicit(q)
return $op(constant_algebra_element(typeof(SA.basis(i)), p), i)
end
function MA.promote_operation(
::typeof($op),
::Type{P},
::Type{Q},
) where {P<:_AE,Q}
I = MA.promote_operation(implicit, P)
return MA.promote_operation(
$op,
I,
constant_algebra_element_type(
MA.promote_operation(SA.basis, I),
Q,
),
)
end
function Base.$op(p::_AE, q)
i = implicit(p)
return $op(i, constant_algebra_element(typeof(SA.basis(i)), q))
end
end
end
function term_element(α, p::Polynomial{B,M}) where {B,M}
return algebra_element(
sparse_coefficients(MP.term(α, p.monomial)),
FullBasis{B,M}(),
)
end
# Needed by `SymbolicWedderburn` which multiplies elements of the basis by `Int`
# We'll see if `::Number` is too restrictive
# Should be able to remove once https://github.com/kalmarek/SymbolicWedderburn.jl/issues/88 is closed
Base.:*(α::Number, p::Polynomial) = term_element(α, p)
function MA.operate!(op::Union{typeof(+),typeof(-),typeof(*)}, p::_APL, q::_AE)
return MA.operate!(op, p, MP.polynomial(q))
end
function MA.operate_to!(
res::MP.AbstractPolynomial,
op::typeof(*),
p::_AE,
q::_APL,
)
return MA.operate_to!(res, op, MP.polynomial(p), q)
end
function MA.operate_to!(
res::MP.AbstractPolynomial,
op::typeof(*),
p::_APL,
q::_AE,
)
return MA.operate_to!(res, op, p, MP.polynomial(q))
end
# These are not implemented yet for arbitrary bases so we
# fall back to polynomials
function MP.substitute(
s::MP.AbstractSubstitutionType,
p::_AE,
args::MP.Substitutions,
)
return MP.substitute(s, MP.polynomial(p), args)
end
function MP.subs(p::_AE, args::MP.AbstractSubstitution...)
return MP.substitute(MP.Subs(), p, args)
end
function (p::_AE)(args::MP.AbstractSubstitution...)
return MP.substitute(MP.Eval(), p, args)
end
function (p::_AE)(x::NTuple{N,<:Number}) where {N}
return (MP.polynomial(p))(x)
end
function (p::_AE)(x::AbstractVector{<:Number})
return (MP.polynomial(p))(x)
end
(p::_AE)(x::Number...) = (MP.polynomial(p))(x...)
function MP.differentiate(p::_AE, x)
return MP.differentiate(MP.polynomial(p), x)
end