# Changing polynomial bases

**URL:** https://discourse.julialang.org/t/changing-polynomial-bases/111238
**Category:** Optimization (Mathematical)
**Tags:** polynomials, basis
**Created:** [March 6, 2024, 12:32pm UTC](https://discourse.julialang.org/t/changing-polynomial-bases/111238 "2024-03-06T12:32:08Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![votroto](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/votroto/32/16416_2.png) [@votroto](https://discourse.julialang.org/u/votroto)
#### Post date: [March 6, 2024, 12:32pm UTC](https://discourse.julialang.org/t/changing-polynomial-bases/111238/1 "2024-03-06T12:32:08Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [March 6, 2024, 2:13pm UTC](https://discourse.julialang.org/t/changing-polynomial-bases/111238/2 "2024-03-06T14:13:33Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![votroto](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/votroto/32/16416_2.png) [@votroto](https://discourse.julialang.org/u/votroto)
#### Post date: [March 7, 2024, 10:23am UTC](https://discourse.julialang.org/t/changing-polynomial-bases/111238/3 "2024-03-07T10:23:46Z")

</div>

I guess I was not clear. How about this?

```jl
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.

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [March 7, 2024, 12:36pm UTC](https://discourse.julialang.org/t/changing-polynomial-bases/111238/4 "2024-03-07T12:36:22Z")

</div>

I think that’s [this issue](https://github.com/JuliaAlgebra/MultivariateBases.jl/issues/4), and it’s already [implemented](https://github.com/JuliaAlgebra/MultivariateBases.jl/pull/5) on master. Maybe @blegat can say if there’s anything holding the release of a new MultivariateBases.jl version?

---

<div class="post-metadata">

### Author: ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)
#### Post date: [March 7, 2024, 1:09pm UTC](https://discourse.julialang.org/t/changing-polynomial-bases/111238/5 "2024-03-07T13:09:25Z")

</div>

Yes, this was merged on master, thanks for pinging me, I’ve made a new release 🙂  
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](https://github.com/JuliaAlgebra/StarAlgebras.jl/pull/21) that should allow to also do arbitrary arithmetics on polynomial on Chebyshev basis, etc… (e.g., multiplications etc…)

---

<div class="post-metadata">

### Author: ![votroto](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/votroto/32/16416_2.png) [@votroto](https://discourse.julialang.org/u/votroto)
#### Post date: [March 8, 2024, 1:03pm UTC](https://discourse.julialang.org/t/changing-polynomial-bases/111238/6 "2024-03-08T13:03:43Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [March 8, 2024, 1:34pm UTC](https://discourse.julialang.org/t/changing-polynomial-bases/111238/7 "2024-03-08T13:34:29Z")

</div>

With the latest releases I get the following:

```julia
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

```

---

<div class="post-metadata">

### Author: ![votroto](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/votroto/32/16416_2.png) [@votroto](https://discourse.julialang.org/u/votroto)
#### Post date: [March 8, 2024, 1:43pm UTC](https://discourse.julialang.org/t/changing-polynomial-bases/111238/8 "2024-03-08T13:43:34Z")

</div>

Oh, ok I restarted julia and it works now 🤦‍♂️

thank you very much
