10.5 Evaluation of multivariate functions

You can evaluate polynomials in Sage as usual by substituting in points:

sage: x = PolynomialRing(RationalField(), 3, 'x').gens()
sage: f = x[0] + x[1] - 2*x[1]*x[2]
sage: f
-2*x1*x2 + x0 + x1
sage: f(1,2,0)
3
sage: f(1,2,5)
-17

This also will work with rational functions:

sage: h = f /(x[1] + x[2])
sage: h
(-2*x1*x2 + x0 + x1)/(x1 + x2)
sage: h(1,2,3)
-9/5

Sage also performs symbolic manipulation:

sage: var('x,y,z')
(x, y, z)
sage: f = (x + 3*y + x^2*y)^3
sage: print f
                                 2             3
                               (x  y + 3 y + x)
sage: f(x=1,y=2,z=3)
729
sage: f.expand()
x^6*y^3 + 9*x^4*y^3 + 27*x^2*y^3 + 27*y^3 + 3*x^5*y^2 + 18*x^3*y^2 +
27*x*y^2 + 3*x^4*y + 9*x^2*y + x^3
sage: f(x = 5/z)
(5/z + 25*y/z^2 + 3*y)^3
sage: g = f.subs(x = 5/z); g
(5/z + 25*y/z^2 + 3*y)^3
sage: h = g.rational_simplify(); h
(27*y^3*z^6 + 135*y^2*z^5 + (675*y^3 + 225*y)*z^4 + (2250*y^2 + 125)*z^3 +
(5625*y^3 + 1875*y)*z^2 + 9375*y^2*z + 15625*y^3)/z^6

See About this document... for information on suggesting changes.