Conversion of symbolic expressions to other types¶
This module provides routines for converting new symbolic expressions
to other types. Primarily, it provides a class Converter
which will walk the expression tree and make calls to methods
overridden by subclasses.
-
class
sage.symbolic.expression_conversions.
AlgebraicConverter
(field)¶ Bases:
sage.symbolic.expression_conversions.Converter
EXAMPLES:
sage: from sage.symbolic.expression_conversions import AlgebraicConverter sage: a = AlgebraicConverter(QQbar) sage: a.field Algebraic Field sage: a.reciprocal_trig_functions['cot'] tan
-
arithmetic
(ex, operator)¶ Convert a symbolic expression to an algebraic number.
EXAMPLES:
sage: from sage.symbolic.expression_conversions import AlgebraicConverter sage: f = 2^(1/2) sage: a = AlgebraicConverter(QQbar) sage: a.arithmetic(f, f.operator()) 1.414213562373095?
-
composition
(ex, operator)¶ Coerce to an algebraic number.
EXAMPLES:
sage: from sage.symbolic.expression_conversions import AlgebraicConverter sage: a = AlgebraicConverter(QQbar) sage: a.composition(exp(I*pi/3, hold=True), exp) 0.500000000000000? + 0.866025403784439?*I sage: a.composition(sin(pi/7), sin) 0.4338837391175581? + 0.?e-18*I
-
pyobject
(ex, obj)¶ EXAMPLES:
sage: from sage.symbolic.expression_conversions import AlgebraicConverter sage: a = AlgebraicConverter(QQbar) sage: f = SR(2) sage: a.pyobject(f, f.pyobject()) 2 sage: _.parent() Algebraic Field
-
-
class
sage.symbolic.expression_conversions.
Converter
(use_fake_div=False)¶ Bases:
object
If use_fake_div is set to True, then the converter will try to replace expressions whose operator is operator.mul with the corresponding expression whose operator is operator.truediv.
EXAMPLES:
sage: from sage.symbolic.expression_conversions import Converter sage: c = Converter(use_fake_div=True) sage: c.use_fake_div True
-
arithmetic
(ex, operator)¶ The input to this method is a symbolic expression and the infix operator corresponding to that expression. Typically, one will convert all of the arguments and then perform the operation afterward.
-
composition
(ex, operator)¶ The input to this method is a symbolic expression and its operator. This method will get called when you have a symbolic function application.
-
derivative
(ex, operator)¶ The input to this method is a symbolic expression which corresponds to a relation.
-
get_fake_div
(ex)¶ EXAMPLES:
sage: from sage.symbolic.expression_conversions import Converter sage: c = Converter(use_fake_div=True) sage: c.get_fake_div(sin(x)/x) FakeExpression([sin(x), x], <built-in function truediv>) sage: c.get_fake_div(-1*sin(x)) FakeExpression([sin(x)], <built-in function neg>) sage: c.get_fake_div(-x) FakeExpression([x], <built-in function neg>) sage: c.get_fake_div((2*x^3+2*x-1)/((x-2)*(x+1))) FakeExpression([2*x^3 + 2*x - 1, FakeExpression([x + 1, x - 2], <built-in function mul>)], <built-in function truediv>)
Check if trac ticket #8056 is fixed, i.e., if numerator is 1.:
sage: c.get_fake_div(1/pi/x) FakeExpression([1, FakeExpression([pi, x], <built-in function mul>)], <built-in function truediv>)
-
pyobject
(ex, obj)¶ The input to this method is the result of calling
pyobject()
on a symbolic expression.Note
Note that if a constant such as
pi
is encountered in the expression tree, its corresponding pyobject which is an instance ofsage.symbolic.constants.Pi
will be passed into this method. One cannot do arithmetic using such an object.
-
relation
(ex, operator)¶ The input to this method is a symbolic expression which corresponds to a relation.
-
symbol
(ex)¶ The input to this method is a symbolic expression which corresponds to a single variable. For example, this method could be used to return a generator for a polynomial ring.
-
-
class
sage.symbolic.expression_conversions.
ExpressionTreeWalker
(ex)¶ Bases:
sage.symbolic.expression_conversions.Converter
A class that walks the tree. Mainly for subclassing.
EXAMPLES:
sage: from sage.symbolic.expression_conversions import ExpressionTreeWalker sage: from sage.symbolic.random_tests import random_expr sage: ex = sin(atan(0,hold=True)+hypergeometric((1,),(1,),x)) sage: s = ExpressionTreeWalker(ex) sage: bool(s() == ex) True sage: foo = random_expr(20, nvars=2) sage: s = ExpressionTreeWalker(foo) sage: bool(s() == foo) True
-
arithmetic
(ex, operator)¶ EXAMPLES:
sage: from sage.symbolic.expression_conversions import ExpressionTreeWalker sage: foo = function('foo') sage: f = x*foo(x) + pi/foo(x) sage: s = ExpressionTreeWalker(f) sage: bool(s.arithmetic(f, f.operator()) == f) True
-
composition
(ex, operator)¶ EXAMPLES:
sage: from sage.symbolic.expression_conversions import ExpressionTreeWalker sage: foo = function('foo') sage: f = foo(atan2(0, 0, hold=True)) sage: s = ExpressionTreeWalker(f) sage: bool(s.composition(f, f.operator()) == f) True
-
derivative
(ex, operator)¶ EXAMPLES:
sage: from sage.symbolic.expression_conversions import ExpressionTreeWalker sage: foo = function('foo') sage: f = foo(x).diff(x) sage: s = ExpressionTreeWalker(f) sage: bool(s.derivative(f, f.operator()) == f) True
-
pyobject
(ex, obj)¶ EXAMPLES:
sage: from sage.symbolic.expression_conversions import ExpressionTreeWalker sage: f = SR(2) sage: s = ExpressionTreeWalker(f) sage: bool(s.pyobject(f, f.pyobject()) == f.pyobject()) True
-
relation
(ex, operator)¶ EXAMPLES:
sage: from sage.symbolic.expression_conversions import ExpressionTreeWalker sage: foo = function('foo') sage: eq = foo(x) == x sage: s = ExpressionTreeWalker(eq) sage: s.relation(eq, eq.operator()) == eq True
-
symbol
(ex)¶ EXAMPLES:
sage: from sage.symbolic.expression_conversions import ExpressionTreeWalker sage: s = ExpressionTreeWalker(x) sage: bool(s.symbol(x) == x) True
-
tuple
(ex)¶ EXAMPLES:
sage: from sage.symbolic.expression_conversions import ExpressionTreeWalker sage: foo = function('foo') sage: f = hypergeometric((1,2,3,),(x,),x) sage: s = ExpressionTreeWalker(f) sage: bool(s() == f) True
-
-
class
sage.symbolic.expression_conversions.
FakeExpression
(operands, operator)¶ Bases:
object
Pynac represents \(x/y\) as \(xy^{-1}\). Often, tree-walkers would prefer to see divisions instead of multiplications and negative exponents. To allow for this (since Pynac internally doesn’t have division at all), there is a possibility to pass use_fake_div=True; this will rewrite an Expression into a mixture of Expression and FakeExpression nodes, where the FakeExpression nodes are used to represent divisions. These nodes are intended to act sufficiently like Expression nodes that tree-walkers won’t care about the difference.
-
operands
()¶ EXAMPLES:
sage: from sage.symbolic.expression_conversions import FakeExpression sage: import operator; x,y = var('x,y') sage: f = FakeExpression([x, y], operator.truediv) sage: f.operands() [x, y]
-
operator
()¶ EXAMPLES:
sage: from sage.symbolic.expression_conversions import FakeExpression sage: import operator; x,y = var('x,y') sage: f = FakeExpression([x, y], operator.truediv) sage: f.operator() <built-in function truediv>
-
pyobject
()¶ EXAMPLES:
sage: from sage.symbolic.expression_conversions import FakeExpression sage: import operator; x,y = var('x,y') sage: f = FakeExpression([x, y], operator.truediv) sage: f.pyobject() Traceback (most recent call last): ... TypeError: self must be a numeric expression
-
-
class
sage.symbolic.expression_conversions.
FastCallableConverter
(ex, etb)¶ Bases:
sage.symbolic.expression_conversions.Converter
EXAMPLES:
sage: from sage.symbolic.expression_conversions import FastCallableConverter sage: from sage.ext.fast_callable import ExpressionTreeBuilder sage: etb = ExpressionTreeBuilder(vars=['x']) sage: f = FastCallableConverter(x+2, etb) sage: f.ex x + 2 sage: f.etb <sage.ext.fast_callable.ExpressionTreeBuilder object at 0x...> sage: f.use_fake_div True
-
arithmetic
(ex, operator)¶ EXAMPLES:
sage: from sage.ext.fast_callable import ExpressionTreeBuilder sage: etb = ExpressionTreeBuilder(vars=['x','y']) sage: var('x,y') (x, y) sage: (x+y)._fast_callable_(etb) add(v_0, v_1) sage: (-x)._fast_callable_(etb) neg(v_0) sage: (x+y+x^2)._fast_callable_(etb) add(add(ipow(v_0, 2), v_0), v_1)
-
composition
(ex, function)¶ Given an ExpressionTreeBuilder, return an Expression representing this value.
EXAMPLES:
sage: from sage.ext.fast_callable import ExpressionTreeBuilder sage: etb = ExpressionTreeBuilder(vars=['x','y']) sage: x,y = var('x,y') sage: sin(sqrt(x+y))._fast_callable_(etb) sin(sqrt(add(v_0, v_1))) sage: arctan2(x,y)._fast_callable_(etb) {arctan2}(v_0, v_1)
-
pyobject
(ex, obj)¶ EXAMPLES:
sage: from sage.ext.fast_callable import ExpressionTreeBuilder sage: etb = ExpressionTreeBuilder(vars=['x']) sage: pi._fast_callable_(etb) pi sage: etb = ExpressionTreeBuilder(vars=['x'], domain=RDF) sage: pi._fast_callable_(etb) 3.141592653589793
-
relation
(ex, operator)¶ EXAMPLES:
sage: ff = fast_callable(x == 2, vars=['x']) sage: ff(2) 0 sage: ff(4) 2 sage: ff = fast_callable(x < 2, vars=['x']) Traceback (most recent call last): ... NotImplementedError
-
symbol
(ex)¶ Given an ExpressionTreeBuilder, return an Expression representing this value.
EXAMPLES:
sage: from sage.ext.fast_callable import ExpressionTreeBuilder sage: etb = ExpressionTreeBuilder(vars=['x','y']) sage: x, y, z = var('x,y,z') sage: x._fast_callable_(etb) v_0 sage: y._fast_callable_(etb) v_1 sage: z._fast_callable_(etb) Traceback (most recent call last): ... ValueError: Variable 'z' not found
-
tuple
(ex)¶ Given a symbolic tuple, return its elements as a Python list.
EXAMPLES:
sage: from sage.ext.fast_callable import ExpressionTreeBuilder sage: etb = ExpressionTreeBuilder(vars=['x']) sage: SR._force_pyobject((2, 3, x^2))._fast_callable_(etb) [2, 3, x^2]
-
-
class
sage.symbolic.expression_conversions.
FastFloatConverter
(ex, *vars)¶ Bases:
sage.symbolic.expression_conversions.Converter
Returns an object which provides fast floating point evaluation of the symbolic expression ex. This is an class used internally and is not meant to be used directly.
See
sage.ext.fast_eval
for more information.EXAMPLES:
sage: x,y,z = var('x,y,z') sage: f = 1 + sin(x)/x + sqrt(z^2+y^2)/cosh(x) sage: ff = f._fast_float_('x', 'y', 'z') sage: f(x=1.0,y=2.0,z=3.0).n() 4.1780638977... sage: ff(1.0,2.0,3.0) 4.1780638977...
Using
_fast_float_
without specifying the variable names is no longer possible:sage: f = x._fast_float_() Traceback (most recent call last): ... ValueError: please specify the variable names
Using
_fast_float_
on a function which is the identity is now supported (see trac ticket #10246):sage: f = symbolic_expression(x).function(x) sage: f._fast_float_(x) <sage.ext.fast_eval.FastDoubleFunc object at ...> sage: f(22) 22
-
arithmetic
(ex, operator)¶ EXAMPLES:
sage: x,y = var('x,y') sage: f = x*x-y sage: ff = f._fast_float_('x','y') sage: ff(2,3) 1.0 sage: a = x + 2*y sage: f = a._fast_float_('x', 'y') sage: f(1,0) 1.0 sage: f(0,1) 2.0 sage: f = sqrt(x)._fast_float_('x'); f.op_list() ['load 0', 'call sqrt(1)'] sage: f = (1/2*x)._fast_float_('x'); f.op_list() ['load 0', 'push 0.5', 'mul']
-
composition
(ex, operator)¶ EXAMPLES:
sage: f = sqrt(x)._fast_float_('x') sage: f(2) 1.41421356237309... sage: y = var('y') sage: f = sqrt(x+y)._fast_float_('x', 'y') sage: f(1,1) 1.41421356237309...
sage: f = sqrt(x+2*y)._fast_float_('x', 'y') sage: f(2,0) 1.41421356237309... sage: f(0,1) 1.41421356237309...
-
pyobject
(ex, obj)¶ EXAMPLES:
sage: f = SR(2)._fast_float_() sage: f(3) 2.0
-
relation
(ex, operator)¶ EXAMPLES:
sage: ff = fast_float(x == 2, 'x') sage: ff(2) 0.0 sage: ff(4) 2.0 sage: ff = fast_float(x < 2, 'x') Traceback (most recent call last): ... NotImplementedError
-
symbol
(ex)¶ EXAMPLES:
sage: f = x._fast_float_('x', 'y') sage: f(1,2) 1.0 sage: f = x._fast_float_('y', 'x') sage: f(1,2) 2.0
-
-
class
sage.symbolic.expression_conversions.
FriCASConverter
¶ Bases:
sage.symbolic.expression_conversions.InterfaceInit
Converts any expression to FriCAS.
EXAMPLES:
sage: var('x,y') (x, y) sage: f = exp(x^2) - arcsin(pi+x)/y sage: f._fricas_() # optional - fricas 2 x y %e - asin(x + %pi) ---------------------- y
-
derivative
(ex, operator)¶ Convert the derivative of
self
in FriCAS.INPUT:
ex
– a symbolic expressionoperator
– operator
EXAMPLES:
sage: var('x,y,z') (x, y, z) sage: f = function("F") sage: f(x)._fricas_() # optional - fricas F(x) sage: diff(f(x,y,z), x, z, x)._fricas_() # optional - fricas F (x,y,z) ,1,1,3
Check that trac ticket #25838 is fixed:
sage: var('x') x sage: F = function('F') sage: integrate(F(x), x, algorithm="fricas") # optional - fricas integral(F(x), x) sage: integrate(diff(F(x), x)*sin(F(x)), x, algorithm="fricas") # optional - fricas -cos(F(x))
-
-
class
sage.symbolic.expression_conversions.
HoldRemover
(ex, exclude=None)¶ Bases:
sage.symbolic.expression_conversions.ExpressionTreeWalker
A class that walks the tree and evaluates every operator that is not in a given list of exceptions.
EXAMPLES:
sage: from sage.symbolic.expression_conversions import HoldRemover sage: ex = sin(pi*cos(0, hold=True), hold=True); ex sin(pi*cos(0)) sage: h = HoldRemover(ex) sage: h() 0 sage: h = HoldRemover(ex, [sin]) sage: h() sin(pi) sage: h = HoldRemover(ex, [cos]) sage: h() sin(pi*cos(0)) sage: ex = atan2(0, 0, hold=True) + hypergeometric([1,2], [3,4], 0, hold=True) sage: h = HoldRemover(ex, [atan2]) sage: h() arctan2(0, 0) + 1 sage: h = HoldRemover(ex, [hypergeometric]) sage: h() NaN + hypergeometric((1, 2), (3, 4), 0)
-
composition
(ex, operator)¶ EXAMPLES:
sage: from sage.symbolic.expression_conversions import HoldRemover sage: ex = sin(pi*cos(0, hold=True), hold=True); ex sin(pi*cos(0)) sage: h = HoldRemover(ex) sage: h() 0
-
-
class
sage.symbolic.expression_conversions.
InterfaceInit
(interface)¶ Bases:
sage.symbolic.expression_conversions.Converter
EXAMPLES:
sage: from sage.symbolic.expression_conversions import InterfaceInit sage: m = InterfaceInit(maxima) sage: a = pi + 2 sage: m(a) '(%pi)+(2)' sage: m(sin(a)) 'sin((%pi)+(2))' sage: m(exp(x^2) + pi + 2) '(%pi)+(exp((_SAGE_VAR_x)^(2)))+(2)'
-
arithmetic
(ex, operator)¶ EXAMPLES:
sage: import operator sage: from sage.symbolic.expression_conversions import InterfaceInit sage: m = InterfaceInit(maxima) sage: m.arithmetic(x+2, sage.symbolic.operators.add_vararg) '(_SAGE_VAR_x)+(2)'
-
composition
(ex, operator)¶ EXAMPLES:
sage: from sage.symbolic.expression_conversions import InterfaceInit sage: m = InterfaceInit(maxima) sage: m.composition(sin(x), sin) 'sin(_SAGE_VAR_x)' sage: m.composition(ceil(x), ceil) 'ceiling(_SAGE_VAR_x)' sage: m = InterfaceInit(mathematica) sage: m.composition(sin(x), sin) 'Sin[x]'
-
derivative
(ex, operator)¶ EXAMPLES:
sage: from sage.symbolic.expression_conversions import InterfaceInit sage: m = InterfaceInit(maxima) sage: f = function('f') sage: a = f(x).diff(x); a diff(f(x), x) sage: print(m.derivative(a, a.operator())) diff('f(_SAGE_VAR_x), _SAGE_VAR_x, 1) sage: b = f(x).diff(x, x) sage: print(m.derivative(b, b.operator())) diff('f(_SAGE_VAR_x), _SAGE_VAR_x, 2)
We can also convert expressions where the argument is not just a variable, but the result is an “at” expression using temporary variables:
sage: y = var('y') sage: t = (f(x*y).diff(x))/y sage: t D[0](f)(x*y) sage: m.derivative(t, t.operator()) "at(diff('f(_SAGE_VAR_t0), _SAGE_VAR_t0, 1), [_SAGE_VAR_t0 = (_SAGE_VAR_x)*(_SAGE_VAR_y)])"
-
pyobject
(ex, obj)¶ EXAMPLES:
sage: from sage.symbolic.expression_conversions import InterfaceInit sage: ii = InterfaceInit(gp) sage: f = 2+I sage: ii.pyobject(f, f.pyobject()) 'I + 2' sage: ii.pyobject(SR(2), 2) '2' sage: ii.pyobject(pi, pi.pyobject()) 'Pi'
-
relation
(ex, operator)¶ EXAMPLES:
sage: import operator sage: from sage.symbolic.expression_conversions import InterfaceInit sage: m = InterfaceInit(maxima) sage: m.relation(x==3, operator.eq) '_SAGE_VAR_x = 3' sage: m.relation(x==3, operator.lt) '_SAGE_VAR_x < 3'
-
symbol
(ex)¶ EXAMPLES:
sage: from sage.symbolic.expression_conversions import InterfaceInit sage: m = InterfaceInit(maxima) sage: m.symbol(x) '_SAGE_VAR_x' sage: f(x) = x sage: m.symbol(f) '_SAGE_VAR_x' sage: ii = InterfaceInit(gp) sage: ii.symbol(x) 'x'
-
tuple
(ex)¶ EXAMPLES:
sage: from sage.symbolic.expression_conversions import InterfaceInit sage: m = InterfaceInit(maxima) sage: t = SR._force_pyobject((3, 4, e^x)) sage: m.tuple(t) '[3,4,exp(_SAGE_VAR_x)]'
-
-
class
sage.symbolic.expression_conversions.
LaurentPolynomialConverter
(ex, base_ring=None, ring=None)¶ Bases:
sage.symbolic.expression_conversions.PolynomialConverter
A converter from symbolic expressions to Laurent polynomials.
See
laurent_polynomial()
for details.
-
class
sage.symbolic.expression_conversions.
PolynomialConverter
(ex, base_ring=None, ring=None)¶ Bases:
sage.symbolic.expression_conversions.Converter
A converter from symbolic expressions to polynomials.
See
polynomial()
for details.EXAMPLES:
sage: from sage.symbolic.expression_conversions import PolynomialConverter sage: x, y = var('x,y') sage: p = PolynomialConverter(x+y, base_ring=QQ) sage: p.base_ring Rational Field sage: p.ring Multivariate Polynomial Ring in x, y over Rational Field sage: p = PolynomialConverter(x, base_ring=QQ) sage: p.base_ring Rational Field sage: p.ring Univariate Polynomial Ring in x over Rational Field sage: p = PolynomialConverter(x, ring=QQ['x,y']) sage: p.base_ring Rational Field sage: p.ring Multivariate Polynomial Ring in x, y over Rational Field sage: p = PolynomialConverter(x+y, ring=QQ['x']) Traceback (most recent call last): ... TypeError: y is not a variable of Univariate Polynomial Ring in x over Rational Field
-
arithmetic
(ex, operator)¶ EXAMPLES:
sage: import operator sage: from sage.symbolic.expression_conversions import PolynomialConverter sage: x, y = var('x, y') sage: p = PolynomialConverter(x, base_ring=RR) sage: p.arithmetic(pi+e, operator.add) 5.85987448204884 sage: p.arithmetic(x^2, operator.pow) x^2 sage: p = PolynomialConverter(x+y, base_ring=RR) sage: p.arithmetic(x*y+y^2, operator.add) x*y + y^2 sage: p = PolynomialConverter(y^(3/2), ring=SR['x']) sage: p.arithmetic(y^(3/2), operator.pow) y^(3/2) sage: _.parent() Symbolic Ring
-
composition
(ex, operator)¶ EXAMPLES:
sage: from sage.symbolic.expression_conversions import PolynomialConverter sage: a = sin(2) sage: p = PolynomialConverter(a*x, base_ring=RR) sage: p.composition(a, a.operator()) 0.909297426825682
-
pyobject
(ex, obj)¶ EXAMPLES:
sage: from sage.symbolic.expression_conversions import PolynomialConverter sage: p = PolynomialConverter(x, base_ring=QQ) sage: f = SR(2) sage: p.pyobject(f, f.pyobject()) 2 sage: _.parent() Rational Field
-
relation
(ex, op)¶ EXAMPLES:
sage: import operator sage: from sage.symbolic.expression_conversions import PolynomialConverter sage: x, y = var('x, y') sage: p = PolynomialConverter(x, base_ring=RR) sage: p.relation(x==3, operator.eq) x - 3.00000000000000 sage: p.relation(x==3, operator.lt) Traceback (most recent call last): ... ValueError: Unable to represent as a polynomial sage: p = PolynomialConverter(x - y, base_ring=QQ) sage: p.relation(x^2 - y^3 + 1 == x^3, operator.eq) -x^3 - y^3 + x^2 + 1
-
symbol
(ex)¶ Returns a variable in the polynomial ring.
EXAMPLES:
sage: from sage.symbolic.expression_conversions import PolynomialConverter sage: p = PolynomialConverter(x, base_ring=QQ) sage: p.symbol(x) x sage: _.parent() Univariate Polynomial Ring in x over Rational Field sage: y = var('y') sage: p = PolynomialConverter(x*y, ring=SR['x']) sage: p.symbol(y) y
-
-
class
sage.symbolic.expression_conversions.
RingConverter
(R, subs_dict=None)¶ Bases:
sage.symbolic.expression_conversions.Converter
A class to convert expressions to other rings.
EXAMPLES:
sage: from sage.symbolic.expression_conversions import RingConverter sage: R = RingConverter(RIF, subs_dict={x:2}) sage: R.ring Real Interval Field with 53 bits of precision sage: R.subs_dict {x: 2} sage: R(pi+e) 5.85987448204884? sage: loads(dumps(R)) <sage.symbolic.expression_conversions.RingConverter object at 0x...>
-
arithmetic
(ex, operator)¶ EXAMPLES:
sage: from sage.symbolic.expression_conversions import RingConverter sage: P.<z> = ZZ[] sage: R = RingConverter(P, subs_dict={x:z}) sage: a = 2*x^2 + x + 3 sage: R(a) 2*z^2 + z + 3
-
composition
(ex, operator)¶ EXAMPLES:
sage: from sage.symbolic.expression_conversions import RingConverter sage: R = RingConverter(RIF) sage: R(cos(2)) -0.4161468365471424?
-
pyobject
(ex, obj)¶ EXAMPLES:
sage: from sage.symbolic.expression_conversions import RingConverter sage: R = RingConverter(RIF) sage: R(SR(5/2)) 2.5000000000000000?
-
symbol
(ex)¶ All symbols appearing in the expression must either appear in subs_dict or be convertable by the ring’s element constructor in order for the conversion to be successful.
EXAMPLES:
sage: from sage.symbolic.expression_conversions import RingConverter sage: R = RingConverter(RIF, subs_dict={x:2}) sage: R(x+pi) 5.141592653589794? sage: R = RingConverter(RIF) sage: R(x+pi) Traceback (most recent call last): ... TypeError: unable to simplify to a real interval approximation sage: R = RingConverter(QQ['x']) sage: R(x^2+x) x^2 + x sage: R(x^2+x).parent() Univariate Polynomial Ring in x over Rational Field
-
-
class
sage.symbolic.expression_conversions.
SubstituteFunction
(ex, original, new)¶ Bases:
sage.symbolic.expression_conversions.ExpressionTreeWalker
A class that walks the tree and replaces occurrences of a function with another.
EXAMPLES:
sage: from sage.symbolic.expression_conversions import SubstituteFunction sage: foo = function('foo'); bar = function('bar') sage: s = SubstituteFunction(foo(x), foo, bar) sage: s(1/foo(foo(x)) + foo(2)) 1/bar(bar(x)) + bar(2)
-
composition
(ex, operator)¶ EXAMPLES:
sage: from sage.symbolic.expression_conversions import SubstituteFunction sage: foo = function('foo'); bar = function('bar') sage: s = SubstituteFunction(foo(x), foo, bar) sage: f = foo(x) sage: s.composition(f, f.operator()) bar(x) sage: f = foo(foo(x)) sage: s.composition(f, f.operator()) bar(bar(x)) sage: f = sin(foo(x)) sage: s.composition(f, f.operator()) sin(bar(x)) sage: f = foo(sin(x)) sage: s.composition(f, f.operator()) bar(sin(x))
-
derivative
(ex, operator)¶ EXAMPLES:
sage: from sage.symbolic.expression_conversions import SubstituteFunction sage: foo = function('foo'); bar = function('bar') sage: s = SubstituteFunction(foo(x), foo, bar) sage: f = foo(x).diff(x) sage: s.derivative(f, f.operator()) diff(bar(x), x)
-
-
class
sage.symbolic.expression_conversions.
SympyConverter
¶ Bases:
sage.symbolic.expression_conversions.Converter
Converts any expression to SymPy.
EXAMPLES:
sage: import sympy sage: var('x,y') (x, y) sage: f = exp(x^2) - arcsin(pi+x)/y sage: f._sympy_() exp(x**2) - asin(x + pi)/y sage: _._sage_() -arcsin(pi + x)/y + e^(x^2) sage: sympy.sympify(x) # indirect doctest x
-
arithmetic
(ex, operator)¶ EXAMPLES:
sage: from sage.symbolic.expression_conversions import SympyConverter sage: s = SympyConverter() sage: f = x + 2 sage: s.arithmetic(f, f.operator()) x + 2
-
composition
(ex, operator)¶ EXAMPLES:
sage: from sage.symbolic.expression_conversions import SympyConverter sage: s = SympyConverter() sage: f = sin(2) sage: s.composition(f, f.operator()) sin(2) sage: type(_) sin sage: f = arcsin(2) sage: s.composition(f, f.operator()) asin(2)
-
derivative
(ex, operator)¶ Convert the derivative of
self
in sympy.INPUT:
ex
– a symbolic expressionoperator
– operator
-
pyobject
(ex, obj)¶ EXAMPLES:
sage: from sage.symbolic.expression_conversions import SympyConverter sage: s = SympyConverter() sage: f = SR(2) sage: s.pyobject(f, f.pyobject()) 2 sage: type(_) <class 'sympy.core.numbers.Integer'>
-
relation
(ex, op)¶ EXAMPLES:
sage: import operator sage: from sage.symbolic.expression_conversions import SympyConverter sage: s = SympyConverter() sage: s.relation(x == 3, operator.eq) Eq(x, 3) sage: s.relation(pi < 3, operator.lt) pi < 3 sage: s.relation(x != pi, operator.ne) Ne(x, pi) sage: s.relation(x > 0, operator.gt) x > 0
-
symbol
(ex)¶ EXAMPLES:
sage: from sage.symbolic.expression_conversions import SympyConverter sage: s = SympyConverter() sage: s.symbol(x) x sage: type(_) <class 'sympy.core.symbol.Symbol'>
-
tuple
(ex)¶ Conversion of tuples.
EXAMPLES:
sage: t = SR._force_pyobject((3, 4, e^x)) sage: t._sympy_() (3, 4, e^x) sage: t = SR._force_pyobject((cos(x),)) sage: t._sympy_() (cos(x),)
-
-
sage.symbolic.expression_conversions.
algebraic
(ex, field)¶ Returns the symbolic expression ex as a element of the algebraic field field.
EXAMPLES:
sage: a = SR(5/6) sage: AA(a) 5/6 sage: type(AA(a)) <class 'sage.rings.qqbar.AlgebraicReal'> sage: QQbar(a) 5/6 sage: type(QQbar(a)) <class 'sage.rings.qqbar.AlgebraicNumber'> sage: QQbar(i) I sage: AA(golden_ratio) 1.618033988749895? sage: QQbar(golden_ratio) 1.618033988749895? sage: QQbar(sin(pi/3)) 0.866025403784439? sage: QQbar(sqrt(2) + sqrt(8)) 4.242640687119285? sage: AA(sqrt(2) ^ 4) == 4 True sage: AA(-golden_ratio) -1.618033988749895? sage: QQbar((2*I)^(1/2)) 1 + 1*I sage: QQbar(e^(pi*I/3)) 0.50000000000000000? + 0.866025403784439?*I sage: AA(x*sin(0)) 0 sage: QQbar(x*sin(0)) 0
-
sage.symbolic.expression_conversions.
fast_callable
(ex, etb)¶ Given an ExpressionTreeBuilder etb, return an Expression representing the symbolic expression ex.
EXAMPLES:
sage: from sage.ext.fast_callable import ExpressionTreeBuilder sage: etb = ExpressionTreeBuilder(vars=['x','y']) sage: x,y = var('x,y') sage: f = y+2*x^2 sage: f._fast_callable_(etb) add(mul(ipow(v_0, 2), 2), v_1) sage: f = (2*x^3+2*x-1)/((x-2)*(x+1)) sage: f._fast_callable_(etb) div(add(add(mul(ipow(v_0, 3), 2), mul(v_0, 2)), -1), mul(add(v_0, 1), add(v_0, -2)))
-
sage.symbolic.expression_conversions.
fast_float
(ex, *vars)¶ Returns an object which provides fast floating point evaluation of the symbolic expression ex.
See
sage.ext.fast_eval
for more information.EXAMPLES:
sage: from sage.symbolic.expression_conversions import fast_float sage: f = sqrt(x+1) sage: ff = fast_float(f, 'x') sage: ff(1.0) 1.4142135623730951
-
sage.symbolic.expression_conversions.
laurent_polynomial
(ex, base_ring=None, ring=None)¶ Return a Laurent polynomial from the symbolic expression
ex
.INPUT:
ex
– a symbolic expressionbase_ring
,ring
– Either abase_ring
or a Laurent polynomialring
can be specified for the parent of result. If just abase_ring
is given, then the variables of thebase_ring
will be the variables of the expressionex
.
OUTPUT:
A Laurent polynomial.
EXAMPLES:
sage: from sage.symbolic.expression_conversions import laurent_polynomial sage: f = x^2 + 2/x sage: laurent_polynomial(f, base_ring=QQ) 2*x^-1 + x^2 sage: _.parent() Univariate Laurent Polynomial Ring in x over Rational Field sage: laurent_polynomial(f, ring=LaurentPolynomialRing(QQ, 'x, y')) x^2 + 2*x^-1 sage: _.parent() Multivariate Laurent Polynomial Ring in x, y over Rational Field sage: x, y = var('x, y') sage: laurent_polynomial(x + 1/y^2, ring=LaurentPolynomialRing(QQ, 'x, y')) x + y^-2 sage: _.parent() Multivariate Laurent Polynomial Ring in x, y over Rational Field
-
sage.symbolic.expression_conversions.
polynomial
(ex, base_ring=None, ring=None)¶ Return a polynomial from the symbolic expression
ex
.INPUT:
ex
– a symbolic expressionbase_ring
,ring
– Either abase_ring
or a polynomialring
can be specified for the parent of result. If just abase_ring
is given, then the variables of thebase_ring
will be the variables of the expressionex
.
OUTPUT:
A polynomial.
EXAMPLES:
sage: from sage.symbolic.expression_conversions import polynomial sage: f = x^2 + 2 sage: polynomial(f, base_ring=QQ) x^2 + 2 sage: _.parent() Univariate Polynomial Ring in x over Rational Field sage: polynomial(f, ring=QQ['x,y']) x^2 + 2 sage: _.parent() Multivariate Polynomial Ring in x, y over Rational Field sage: x, y = var('x, y') sage: polynomial(x + y^2, ring=QQ['x,y']) y^2 + x sage: _.parent() Multivariate Polynomial Ring in x, y over Rational Field sage: s,t=var('s,t') sage: expr=t^2-2*s*t+1 sage: expr.polynomial(None,ring=SR['t']) t^2 - 2*s*t + 1 sage: _.parent() Univariate Polynomial Ring in t over Symbolic Ring sage: polynomial(x*y, ring=SR['x']) y*x sage: polynomial(y - sqrt(x), ring=SR['y']) y - sqrt(x) sage: _.list() [-sqrt(x), 1]
The polynomials can have arbitrary (constant) coefficients so long as they coerce into the base ring:
sage: polynomial(2^sin(2)*x^2 + exp(3), base_ring=RR) 1.87813065119873*x^2 + 20.0855369231877