-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add antidifferentiation for monomials * Add antidifferentiation for polynomials * Add support for rational coefficients in antidifferentiation * Update src/anti_diff.jl Co-authored-by: Benoît Legat <[email protected]> * Remove superfluous print * Use coefficient_type in polynomial antidifferentiaton tests * Make monomial antidifferentiation coefficient types rationals --------- Co-authored-by: Benoît Legat <[email protected]>
- Loading branch information
Showing
4 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
function _div_by_power(x::T, y::Int) where {T} | ||
x / y | ||
end | ||
|
||
function _div_by_power(x::T, y::Int)::Rational{T} where {T<:Integer} | ||
x // y | ||
end | ||
|
||
function MP.antidifferentiate(m::Monomial{V,M}, x::Variable{V,M}) where {V,M} | ||
z = copy(m.z) | ||
i = findfirst(isequal(x), MP.variables(m)) | ||
if (i === nothing || i == 0) || m.z[i] == 0 | ||
Monomial(MP.variables(m), z) * x | ||
else | ||
z[i] += 1 | ||
(1 // (m.z[i] + 1)) * Monomial(MP.variables(m), z) | ||
end | ||
end | ||
|
||
function MP.antidifferentiate(p::Polynomial{V,M,T}, x::Variable{V,M}) where {V,M,T} | ||
i = something(findfirst(isequal(x), MP.variables(p)), 0) | ||
S = typeof(_div_by_power(zero(T), Int(1))) | ||
if iszero(i) | ||
x * p | ||
else | ||
Z = copy.(p.x.Z) | ||
a = Vector{S}(undef, length(p.a)) | ||
for j in 1:length(Z) | ||
a[j] = _div_by_power(p.a[j], (Z[j][i] + 1)) | ||
Z[j][i] += 1 | ||
end | ||
Polynomial(a, MonomialVector(MP.variables(p), Z)) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters