1

I am doing computations with Julia polynomials of rationals. After an operation I might have a polynomial of high degree, I want to truncate to a polynomial of a smaller degree by throwing out the higher order terms. I figured out a solution below. But the code is going to run for hours and I need to make things as optimized as possible. I basically re-created a new Poly. Is there a better way?

using Polynomials
R = Polynomial{Rational{BigInt}, :x}([1,2,2,2])
L = coeffs(R)
R = Polynomial{Rational{BigInt}, :x}(L[1:2])
Josh
  • 11
  • 2
  • Does `resize!(coeffs(R), N)` work for you? – Dan Getz Aug 10 '23 at 04:26
  • 1
    Your question is a bit incomplete. Julia does not have built-in polynomials, so you are presumably using some package. Please include information about which package, and also include a minimal working example that shows how you created `R`. – DNF Aug 10 '23 at 10:28

1 Answers1

0

Assuming you are using Polynomials.jl package, we have:

julia> using Polynomials

julia> R = Polynomial(Rational{BigInt}[1,2,3,4,5], :x)
Polynomial(1//1 + 2//1*x + 3//1*x^2 + 4//1*x^3 + 5//1*x^4)

julia> degree(R)
4

julia> resize!(coeffs(R), 3);

julia> R
Polynomial(1//1 + 2//1*x + 3//1*x^2)

julia> degree(R)
2

This might be slightly using internals, and a function which reduced degree specifically in the package might be better. I didn't search enough to see if there is one, but it is reasonable that if there is one, it would be doing this simple resize! trick.

Dan Getz
  • 17,002
  • 2
  • 23
  • 41
  • Interesting, I would think that resize! would modify the array coeffs(R) and not actually modify R. I guess I don't fully understand how Julia works. – Josh Aug 10 '23 at 16:34
  • 1
    I presume coeffs(R) is the actual internal array used to represent the Polynomial. Where can I look this kind of thing up. Also, does anyone know if fast Fourier transforms are used to implement polynomial multiplication? – Josh Aug 10 '23 at 16:38
  • 1
    I don't see in what way this is using internals. This seems like it _should_ work. – DNF Aug 10 '23 at 16:59
  • For `coeffs(R)` to return the internal coefficient array is the most performant option, and in Julia performance is of the essence. This also allows you to achieve high performance in your own algorithm which would make it feel like it is native to Polynomials package, another Julia idiom: make extensions as good as native. – Dan Getz Aug 10 '23 at 17:17