JuliaMath / Polynomials.jl

Polynomial manipulations in Julia
http://juliamath.github.io/Polynomials.jl/
Other
303 stars 75 forks source link

(Low priority) Axis offset warning when using OffsetArrays #564

Closed NAThompson closed 5 months ago

NAThompson commented 5 months ago

It feels natural to have polynomial coefficients zero-indexed, rather than 1-indexed. However, when I do this, I receive the following warning:

┌ Warning: ignoring the axis offset of the coefficient vector
└ @ Polynomials ~/.julia/packages/Polynomials/5ZhzG/src/polynomial-container-types/mutable-dense-polynomial.jl:9

Code to reproduce:

julia
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.10.2 (2024-03-01)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> using OffsetArrays

julia> using Polynomials
julia> c = OffsetArray{Float64}(undef, 0:2)
julia> c[0] = 1
julia> c[1] = 2
julia> c[2] = 3
julia> p = Polynomial(c)
┌ Warning: ignoring the axis offset of the coefficient vector
└ @ Polynomials ~/.julia/packages/Polynomials/5ZhzG/src/polynomial-container-types/mutable-dense-polynomial.jl:9
Polynomial(1.0 + 2.0*x + 3.0*x^2)
julia> p(0)
1.0
julia> p(1)
6.0

Low priority as it would appear that numerical evaluation is nonetheless correct.

jverzani commented 5 months ago

Thanks for the comment. At one time Polynomial(::OffsetVector) created a LaurentPolynomial but that was removed. Now you would use that constructor directly:

julia> LaurentPolynomial(c)
┌ Warning: Using the axis offset of the coefficient vector ...
LaurentPolynomial(1.0 + 2.0*x + 3.0*x²)

This allows for other value dependent offsets:

julia> c = OffsetArray([1,2,3], -1:1);
julia> LaurentPolynomial(c)
┌ Warning: Using the axis offset of the coefficient vector...
LaurentPolynomial(x⁻¹ + 2 + 3*x)

For this c, Polynomial(c) would be incorrect.

The LaurentPolynomial usage should be identical to that of a Polynomial, though a shade less performant, should that matter.

nsajko commented 5 months ago

Wouldn't documentation be strongly preferable to warnings?

jverzani commented 5 months ago

I was surprised we had a warning for the Laurent cases. For the Polynomial case I'd suggest the warning is warranted, as the default is an unexpected ignoring of the offset. Let me adjust the Laurent case.