Cannot build Rational{Poly}

Hi,

I tried creating a polynomial ratio:

using Polynomials

julia> a=Rational{Poly}(Poly([1,2]),Poly([3]))

, but got the following error:

ERROR: TypeError: Rational: in T, expected T<:Integer, got Type{Polynomials.Poly{T<:Number}}

It seems that the JuliaDSP group has crated a workaround, called the PolynomialRatio:

DSP.Filters.PolynomialRatio{T<:Number}

Would it not be preferable to instead find a way to support Poly directly from Rational, given that polynomial ratios are not an uncommon occurance?

Since Rational{T} <: Number, I’m not sure Rational{Poly} makes sense…it’s probably better to make your own type that replicates the behaviour of Rational

The built in Rational{T} type is designed for integer T. Designing a type to represent rational functions (ratios of polynomials) would seem to have very little code in common with this except for a few trivial methods, so I’m not sure how much you could gain by making the built-in type more abstract—you would end up having to redefine most of the methods anyway.

(Making the built-in type more abstract is technically difficult because Julia does not have multiple inheritance. You might want to define an AbstractRational{T} type that allows T to be anything and defines a few simple methods, and then define Rational{T<:Integer} <: AbstractRational{T}, Number, but Julia doesn’t allow that. So you can only make Rational more abstract at the price of it not being a subtype of Number, and that is too high a price to pay for sharing a trivial amount of code.)

1 Like

There is https://github.com/aytekinar/RationalFunctions.jl

That may be of help for the task at hand.

2 Likes

Well, Rational{Poly} is sort of a number… it just has an unknown in there.

In all seriousness:
I see what you and Steven are saying. I was not thinking that Rational was a <: Number.

@j_verzani: I see that you also created your own “rational” object: RationalFunction to get your module working.

Maybe we could just define the //(Poly, Poly) operator to help construct the DSP module’s PolynomialRatio in order to keep things easier to read… or maybe even have it added directly to the Polynomials module.

I noticed @j_verzani uses /(Poly, Poly) to construct his polynomials, but I feel like the //(Poly, Poly) operator is more consistent with Julia’s current implementation.

The RationalFunctions.jl package is the contribution of @aytekinar. At first, I thought // was a better choice, but the focus of that package seems more for polys over Float64 or Complex{Float64}, so it seems to me now that / is the better choice.