6

I want to create a polynomial ring which has float Coefficients like this. I can create with integers but, Floats does not work.

using Oscar

S, (a,b,c,d) = PolynomialRing(QQ,["a","b","c","d"])
RR = AbstractAlgebra.RealField
s1 = S( 8*a - RR(0.51234)*a*(1+RR(1/2)*a+RR(1/3)*b+RR(1/4)*c) - 8)
s2 = S( 8*b - RR(0.51234)*b*(1+RR(2/3)*a+RR(2/4)*b+RR(2/5)*c) - 8)
s3 = S( 8*c - RR(0.51234)*c*(1+RR(3/4)*a+RR(3/5)*b+RR(3/6)*c) - 8)
s4 = S( 8*d - RR(0.51234)*d*(1+RR(4/5)*a+RR(4/6)*b+RR(4/7)*c) - 8)

It gives me this error. How can I create polynomials like this.

ERROR: LoadError: MethodError: no method matching (::FmpqMPolyRing)(::BigFloat)
Closest candidates are:
  (::FmpqMPolyRing)() at ~/.julia/packages/Nemo/5CDLD/src/flint/fmpq_mpoly.jl:1063
  (::AbstractAlgebra.Ring)(::Singular.n_RingElem{Singular.RingElemWrapper{S, T}}) where {S, T} at ~/.julia/packages/Singular/uG7uo/src/number/n_unknown.jl:358
  (::AbstractAlgebra.Ring)(::Union{Singular.n_FieldElem{T}, Singular.n_RingElem{T}} where T) at ~/.julia/packages/Oscar/iRpOQ/src/Rings/mpoly.jl:736
  ...
Stacktrace:
 [1] *(x::BigFloat, y::fmpq_mpoly)
   @ AbstractAlgebra ~/.julia/packages/AbstractAlgebra/mQIYL/src/Rings.jl:84
 [2] top-level scope
   @ /mnt/c/Users/yusuf/Desktop/7.Semester/bitirme/Repo_Resultant_System/resultant-system/chandra4.jl:7
in expression starting at /mnt/c/Users/yusuf/Desktop/7.Semester/bitirme/Repo_Resultant_System/resultant-system/chandra4.jl:7

1 Answers1

4

While I do not have previous experience with this particular (from appearances, rather sophisticated) package Oscar.jl, parsing this error message tells me that the function you are trying to call is being given a BigFloat as input, but simply does not have a method for that type.

At first this was a bit surprising given that there are no BigFloats in your input, but after a bit of investigation, it appears that the culprit is the following

julia> RR = AbstractAlgebra.RealField
Floats

julia> RR(1/3)
0.333333333333333314829616256247390992939472198486328125

julia> typeof(ans)
BigFloat

However, changing these inputs from BigFloat to a more standard Float64 does not fix the problem; S has no method for those either. It does, however, have methods for Rationals such as 1//3. Consequently, a simple apparent fix would be to write

using Oscar

S, (a,b,c,d) = PolynomialRing(QQ,["a","b","c","d"])
RR = AbstractAlgebra.Rational # Note the change here!
s1 = S( 8*a - RR(0.51234)*a*(1+RR(1/2)*a+RR(1/3)*b+RR(1/4)*c) - 8)
s2 = S( 8*b - RR(0.51234)*b*(1+RR(2/3)*a+RR(2/4)*b+RR(2/5)*c) - 8)
s3 = S( 8*c - RR(0.51234)*c*(1+RR(3/4)*a+RR(3/5)*b+RR(3/6)*c) - 8)
s4 = S( 8*d - RR(0.51234)*d*(1+RR(4/5)*a+RR(4/6)*b+RR(4/7)*c) - 8)

which runs without error.

Or perhaps a bit more cleanly, by directly inputting your coefficients as rationals from the start:

S, (a,b,c,d) = PolynomialRing(QQ,["a","b","c","d"])
RR = AbstractAlgebra.Rational
s1 = S( 8*a - RR(51234//100000)*a*(1+RR(1//2)*a+RR(1//3)*b+RR(1//4)*c) - 8)
s2 = S( 8*b - RR(51234//100000)*b*(1+RR(2//3)*a+RR(2//4)*b+RR(2//5)*c) - 8)
s3 = S( 8*c - RR(51234//100000)*c*(1+RR(3//4)*a+RR(3//5)*b+RR(3//6)*c) - 8)
s4 = S( 8*d - RR(51234//100000)*d*(1+RR(4//5)*a+RR(4//6)*b+RR(4//7)*c) - 8)

which yields

julia> s1 = S( 8*a - RR(51234//100000)*a*(1+RR(1//2)*a+RR(1//3)*b+RR(1//4)*c) - 8)
-25617//100000*a^2 - 8539//50000*a*b - 25617//200000*a*c + 374383//50000*a - 8

julia> s2 = S( 8*b - RR(51234//100000)*b*(1+RR(2//3)*a+RR(2//4)*b+RR(2//5)*c) - 8)
-8539//25000*a*b - 25617//100000*b^2 - 25617//125000*b*c + 374383//50000*b - 8

julia> s3 = S( 8*c - RR(51234//100000)*c*(1+RR(3//4)*a+RR(3//5)*b+RR(3//6)*c) - 8)
-76851//200000*a*c - 76851//250000*b*c - 25617//100000*c^2 + 374383//50000*c - 8

julia> s4 = S( 8*d - RR(51234//100000)*d*(1+RR(4//5)*a+RR(4//6)*b+RR(4//7)*c) - 8)
-25617//62500*a*d - 8539//25000*b*d - 25617//87500*c*d + 374383//50000*d - 8

In this latter case, the RR wrapper does not appear to be necessary, as it does not change the type of the inputs, but I suppose it doesn't hurt.

cbk
  • 4,225
  • 6
  • 27
  • 1
    Fun fact: Every number encoded in an IEEE-754 floating-point format with the exception of infinity is a rational number (i.e. a fraction with a power-of-two divisor). Maybe what is needed here is an enhancement to the language that converts `Float64` to `Rationals` under the hood? – njuffa Jan 19 '22 at 01:34
  • It's a good point! Minus the Infs and NaNs.. The uncountably infinite majority of real numbers* are terrifying monstrosities. Absolute libraries of Babel, the lot of them. (*minus the countably infinite dense subset of the rationals) – cbk Jan 19 '22 at 01:59
  • 1
    You can also encode Inf and NaN as rationals with zero denominators. Julia uses that encoding for infinite rationals but errors for NaN since one generally wants an error as soon as a NaN is produced and IEEE floats only defer signaling errors for performance reasons. – StefanKarpinski Jan 19 '22 at 02:40
  • Oh yeah, as a matter of practice w/r/t Julia rationals one could map IEEE +/-Inf to +/-1//0, all the (many!) IEEE NaNs to 0//0, or some such. But technically I think the mathematicians exclude these from the Rationals! wiki: "In mathematics, a rational number is a number that can be expressed as the quotient or fraction p/q of two integers, a numerator p and a non-zero denominator q" – cbk Jan 19 '22 at 02:44
  • One could probably come up with an internally consistent definition of Rationals that does include those, would be interesting. But I guess numbers are usually different on a computer anyways, what with most implementations of the integers being Integers modulo some large n, etc.. – cbk Jan 19 '22 at 02:51