Creating and evaluating multivariate polynomials

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 + x1x2 . 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 + 52 + 42^2 + 33 + 2*3.

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

Maybe MultivariatePolynomials.jl is what you’re looking for?

1 Like

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


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
2 Likes

Yup, that works. Spot on