bertini.sympy_bridge¶
Exact two-way conversion between sympy expressions and bertini function trees.
sympy is an optional dependency of bertini; this module raises a helpful ImportError if it is missing.
The conversions are EXACT for symbols and exact number types: sympy Rationals
become bertini Rationals (never a float64 round-trip), Integers stay integers
of any size, and Floats travel as strings carrying their precision. This is
why sympy.lambdify is deliberately not used – it lowers Rational(1,3)
to a float literal.
Forward (define in sympy, solve with bertini):
import sympy
from bertini.sympy_bridge import system_from_sympy
from bertini.nag_algorithm import ZeroDimCauchyAdaptivePrecisionTotalDegree
x, y = sympy.symbols('x y')
sys = system_from_sympy([x**2 + y**2 - 1, x + y], [x, y])
solver = ZeroDimCauchyAdaptivePrecisionTotalDegree(sys)
solver.solve()
solver.all_solutions() # in your variables, comparable with sympy.solve output
Reverse (inspect bertini trees symbolically):
from bertini.sympy_bridge import to_sympy
expr = to_sympy(sys.function(0).root())
- bertini.sympy_bridge.from_sympy(expr, variables=None)[source]¶
Convert a sympy expression to a bertini function-tree node, exactly.
variablesmay be an iterable of bertini Variables or a name->Variable dict; sympy Symbols are matched to them by name. Symbols not supplied are created fresh, and repeated occurrences share one Variable.Raises NotImplementedError for sympy heads bertini has no node for (e.g. the hyperbolics – rewrite first:
expr.rewrite(sympy.exp)).
- bertini.sympy_bridge.to_sympy(node)[source]¶
Convert a bertini function-tree node to a sympy expression, exactly.
Walks the tree via the introspection API. Variables map to sympy Symbols by name; Integer/Rational leaves convert exactly; Float leaves carry their mpfr precision into sympy’s.
Raises NotImplementedError for Differential leaves (trees built by the no-argument Jacobian form of differentiate; differentiate with an explicit variable instead) and for node types with no sympy equivalent.
- bertini.sympy_bridge.system_from_sympy(exprs, variables)[source]¶
Build a bertini System from sympy expressions, with one affine variable group.
variablesfixes the variable order (iterable of sympy Symbols, bertini Variables, or names). Returns the System; itsvariable_ordering()matches the order given.