Skip to content

Commit 006c85c

Browse files
authored
Merge pull request #117 from ahumenberger/master
Add missing methods for RationalPoly
2 parents fe0d7ca + 9919445 commit 006c85c

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

src/operators.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,13 @@ LinearAlgebra.adjoint(v::AbstractVariable) = v
115115
LinearAlgebra.adjoint(m::AbstractMonomial) = m
116116
LinearAlgebra.adjoint(t::AbstractTerm) = LinearAlgebra.adjoint(coefficient(t)) * monomial(t)
117117
LinearAlgebra.adjoint(p::AbstractPolynomialLike) = polynomial(map(LinearAlgebra.adjoint, terms(p)))
118+
LinearAlgebra.adjoint(r::RationalPoly) = adjoint(numerator(r)) / adjoint(denominator(r))
118119

119120
LinearAlgebra.transpose(v::AbstractVariable) = v
120121
LinearAlgebra.transpose(m::AbstractMonomial) = m
121122
LinearAlgebra.transpose(t::AbstractTerm) = LinearAlgebra.transpose(coefficient(t)) * monomial(t)
122123
LinearAlgebra.transpose(p::AbstractPolynomialLike) = polynomial(map(LinearAlgebra.transpose, terms(p)))
124+
LinearAlgebra.transpose(r::RationalPoly) = transpose(numerator(r)) / transpose(denominator(r))
123125

124126
LinearAlgebra.dot(p1::AbstractPolynomialLike, p2::AbstractPolynomialLike) = p1' * p2
125127
LinearAlgebra.dot(x, p::AbstractPolynomialLike) = x' * p

src/rational.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ struct RationalPoly{NT <: APL, DT <: APL}
66
den::DT
77
end
88

9+
# This constructor is called from LinearAlgebra in the method Matrix{T}(s::UniformScaling{Bool}, dims)
10+
RationalPoly{NT,DT}(x::Bool) where {NT,DT} = ifelse(x, one(RationalPoly{NT,DT}), zero(RationalPoly{NT,DT}))
11+
912
Base.numerator(r::RationalPoly) = r.num
1013
Base.denominator(r::RationalPoly) = r.den
1114

@@ -25,6 +28,7 @@ end
2528
Base.inv(r::RationalPoly) = r.den / r.num
2629
Base.inv(p::APL{T}) where T = one(T) / p
2730
Base.:/(r::RationalPoly, p) = r.num / (r.den * p)
31+
Base.:/(r::RationalPoly, s::RationalPoly) = (r.num * s.den) / (s.num * r.den)
2832
function Base.:/(num::NT, den::DT) where {NT <: APL, DT <: APL}
2933
RationalPoly{NT, DT}(num, den)
3034
end

test/rational.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,9 @@
2626
@test denominator(x / x^2) == x^2
2727
@test inv(x / x^2) == x
2828
@test x / x^2 == inv(x)
29+
@test isone(((x+1) / (x-1)) / ((x+1) / (x-1)))
30+
@test ((x+1)^2 / (x-1)) / ((x+1) / (x-1)) == x+1
31+
RType = typeof(x+1/x)
32+
@test RType(true) == one(RType)
33+
@test RType(false) == zero(RType)
2934
end

0 commit comments

Comments
 (0)