Changing polynomial bases

Hello, I noticed there is a julia package MultivariateBases.jl for finding the bases of multivariate polynomials. Is there a simple way to get the coefficients corresponding to the basis generated basis elements?

Thank you!

I think you can use [b[i] for i in 1:length(b)] (or the internal b.polynomials) to get the basis polynomials, and for each polynomial p you can use coefficients(p) to get the coefficients. Is this what you want?

This would be simpler if the basis supported the iterate method. Maybe file a GitHub issue about that if that’s indeed what you’re trying to do.

I guess I was not clear. How about this?

using DynamicPolynomials
using MultivariateBases
using LinearAlgebra

coeffs(poly, b::MonomialBasis) = coefficients(poly, b.monomials)
coeffs(poly, b::ChebyshevBasisFirstKind) = coefficients(poly, b.polynomials)

@polyvar x y
poly = 0.5*x^2 + 3*x^2*y

b = basis_covering_monomials(MonomialBasis, monomials(poly))
@assert poly ≈ dot(coeffs(poly, b), monomials(poly))

b = basis_covering_monomials(ChebyshevBasis, monomials(poly))
@assert poly ≈ dot(coeffs(poly, b), monomials(poly))

I wish for the second function to work.

I think that’s this issue, and it’s already implemented on master. Maybe @blegat can say if there’s anything holding the release of a new MultivariateBases.jl version?

1 Like

Yes, this was merged on master, thanks for pinging me, I’ve made a new release :slight_smile:
At the moment, we’re working with @kalmarek in a big rewrite with Proof of concept: non monomial basis by kalmarek · Pull Request #21 · JuliaAlgebra/StarAlgebras.jl · GitHub that should allow to also do arbitrary arithmetics on polynomial on Chebyshev basis, etc… (e.g., multiplications etc…)

2 Likes

Just to clarify, the coeffs function has been generalized for monomial bases, but if I want chebyshev, I’ll have to wait for the big rewrite. Is that correct?

With the latest releases I get the following:

julia> using MultivariateBases, DynamicPolynomials

julia> @polyvar x y
(x, y)

julia> poly = 0.5*x^2 + 3*x^2*y
0.5x² + 3.0x²y

julia> b = basis_covering_monomials(ChebyshevBasis, monomials(poly))
ChebyshevBasisFirstKind([1.0, y, -1.0 + 2.0x², -y + 2.0x²y])

julia> coefficients(poly, b)
4-element Vector{Float64}:
 0.25
 1.5
 0.25
 1.5000000000000002
1 Like

Oh, ok I restarted julia and it works now :man_facepalming:

thank you very much

1 Like