# Creating and evaluating multivariate polynomials

**URL:** <https://discourse.julialang.org/t/creating-and-evaluating-multivariate-polynomials/91870>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [December 19, 2022, 6:39pm UTC](https://discourse.julialang.org/t/creating-and-evaluating-multivariate-polynomials/91870 "2022-12-19T18:39:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZV314](https://avatars.discourse-cdn.com/v4/letter/z/58f4c7/32.png) [@ZV314](https://discourse.julialang.org/u/ZV314)\
**Post date:** [December 19, 2022, 6:39pm UTC](https://discourse.julialang.org/t/creating-and-evaluating-multivariate-polynomials/91870/1 "2022-12-19T18:39:29Z")

</div>

Newbie here,

I seek to create a multivariable polynomial and evaluate it.

In greater detail, I’d like to start by defining an array of monomials, say for example [1, x1, x1^2, x2, x2^2, x1\*x2] .

With that established, I would want a function that upon input of an array of coefficients, say [3, 5, 4, 3, 0, 1] , creates the associated polynomial p = 3 + 5x1 + 4x1^2 + 3x2 + x1_x2 . And once that is in place, to be able to evaluate for any given array of values, say x1 = 2, x2 = 3, the evaluation p(2,3) = 3 + 5_2 + 4_2^2 + 3_3 + 2\*3.

Are there packages with existing functions that can enable this to be accomplished easily?

---

<div class="post-metadata">

**Author:** ![liamfdoherty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liamfdoherty/32/24516_2.png) [@liamfdoherty](https://discourse.julialang.org/u/liamfdoherty)\
**Post date:** [December 19, 2022, 6:50pm UTC](https://discourse.julialang.org/t/creating-and-evaluating-multivariate-polynomials/91870/2 "2022-12-19T18:50:30Z")

</div>

Maybe [MultivariatePolynomials.jl](https://juliaalgebra.github.io/MultivariatePolynomials.jl/stable/substitution/) is what you’re looking for?

---

<div class="post-metadata">

**Author:** ![ZV314](https://avatars.discourse-cdn.com/v4/letter/z/58f4c7/32.png) [@ZV314](https://discourse.julialang.org/u/ZV314)\
**Post date:** [December 19, 2022, 6:52pm UTC](https://discourse.julialang.org/t/creating-and-evaluating-multivariate-polynomials/91870/3 "2022-12-19T18:52:18Z")

</div>

Quite possibly, but which specific functions there accomplish what I seek?

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [December 19, 2022, 6:52pm UTC](https://discourse.julialang.org/t/creating-and-evaluating-multivariate-polynomials/91870/4 "2022-12-19T18:52:50Z")

</div>

```julia

julia> using TypedPolynomials

julia> @polyvar x1 x2
(x1, x2)

julia> p = mapreduce(*, +, [3, 5, 4, 3, 0, 1], [1, x1, x1^2, x2, x2^2, x1*x2])
4x1² + x1x2 + 5x1 + 3x2 + 3

julia> p(x1 => 2, x2 => 3)
44

julia> evaluate(p, v) = p(variables(p) => v)
evaluate (generic function with 1 method)

julia> evaluate(p, (2, 3))
44

```

---

<div class="post-metadata">

**Author:** ![ZV314](https://avatars.discourse-cdn.com/v4/letter/z/58f4c7/32.png) [@ZV314](https://discourse.julialang.org/u/ZV314)\
**Post date:** [December 19, 2022, 8:48pm UTC](https://discourse.julialang.org/t/creating-and-evaluating-multivariate-polynomials/91870/5 "2022-12-19T20:48:08Z")

</div>

Yup, that works. Spot on
