svg_path_editor.math#
Attributes#
Classes#
Control numerical precision for mixed symbolic/numeric operations. |
Functions#
Normalize a |
|
|
Convert a |
|
Convert a SymPy expression to |
|
Coerce a SymPy Boolean to builtin |
|
Test symbolic equality \(a = b\) using SymPy. |
|
Construct an (optionally relaxed) equality constraint \(a = b\). |
|
Construct an (optionally relaxed) inequality \(a ≤ b\). |
|
Construct an (optionally relaxed) inequality \(a \ge b\). |
|
Construct an (optionally relaxed) strict inequality \(a < b\). |
|
Construct an (optionally relaxed) strict inequality \(a > b\). |
|
Test whether an expression is zero. |
|
Substitute symbols in an expression, optionally with numeric evaluation. |
|
Optionally evaluate an expression numerically. |
|
Try to interpret |
|
Replace numerically tiny floating values by exact zero. |
|
Solve the monic quadratic equation |
|
Solve the monic cubic equation |
|
Solve the monic quartic equation |
|
Compute roots of a univariate polynomial up to degree 4. |
|
Construct the Sylvester matrix of two polynomials \(p(x), q(x)\). |
|
Resultant \(\operatorname{res}_y(f, g)\). |
|
Algebraically expand a SymPy expression. |
Module Contents#
- svg_path_editor.math.Number#
- type svg_path_editor.math.Symbol = 'sp.Symbol'#
- type svg_path_editor.math.Expr = 'sp.Expr'#
- type svg_path_editor.math.Poly = 'sp.Poly'#
- type svg_path_editor.math.Boolean = 'sp.logic.boolalg.Boolean'#
- class svg_path_editor.math.Precision[source]#
Control numerical precision for mixed symbolic/numeric operations.
baselinedefines the primary target precision, whileadditionalcan be used to carry extra guard digits during intermediate computations.- Variables:
baseline – Baseline number of significant digits.
additional – Additional guard digits to be used internally.
- baseline: int#
- additional: int#
- property full: int#
Full number of significant digits to use.
- Returns:
baseline + additional.- Return type:
int
- svg_path_editor.math.canonical_decimal(x)[source]#
Normalize a
Decimalto a canonical form.- Returns:
Decimal(0)for any zero value, otherwisex.normalize().- Parameters:
x (decimal.Decimal)
- Return type:
decimal.Decimal
- svg_path_editor.math.dec_to_rat(x)[source]#
Convert a
Decimalto a SymPysympy.Rational.The conversion is exact with respect to the decimal representation: the
Decimalis first converted to a string and then passed tosympy.Rational.- Parameters:
x (decimal.Decimal)
- Return type:
- svg_path_editor.math.rat_to_dec(x)[source]#
Convert a SymPy expression to
Decimalwith current precision.The expression is evaluated numerically using
sympy.Expr.evalf()withn = getcontext().precand then converted toDecimal. The result is normalized viacanonical_decimal().- Parameters:
x (Expr)
- Return type:
decimal.Decimal
- svg_path_editor.math.as_bool(r)[source]#
Coerce a SymPy Boolean to builtin
bool.- Raises:
ValueError – If
rcannot be simplified to a definite Boolean.- Parameters:
r (Boolean)
- Return type:
bool
- svg_path_editor.math.eq(a, b, *, n=None)[source]#
Construct an (optionally relaxed) equality constraint \(a = b\).
If
nisNone, an exact symbolic equalitysympy.Eqis returned. Otherwise a relaxed inequality \(|a - b| < 10^{-\texttt{baseline}}\) is constructed.
- svg_path_editor.math.le(a, b, *, n=None)[source]#
Construct an (optionally relaxed) inequality \(a ≤ b\).
If
nisNone, returnssympy.LessThan(a, b). Otherwise comparesawith \(b + 10^{-\texttt{baseline}}\).
- svg_path_editor.math.ge(a, b, *, n=None)[source]#
Construct an (optionally relaxed) inequality \(a \ge b\).
See
le()for details.
- svg_path_editor.math.lt(a, b, *, n=None)[source]#
Construct an (optionally relaxed) strict inequality \(a < b\).
If
nisNone, returnssympy.StrictLessThan(a, b). Otherwise comparesawith \(b + 10^{-\texttt{baseline}}\).
- svg_path_editor.math.gt(a, b, *, n=None)[source]#
Construct an (optionally relaxed) strict inequality \(a > b\).
See
lt()for details.
- svg_path_editor.math.is_zero(expr, *, n=None)[source]#
Test whether an expression is zero.
If
nisNone, use exact symbolic comparisonexpr == 0. Otherwise, evaluate numerically ton.fullsignificant digits and test \(|\mathtt{expr}| ≤ 10^{-\texttt{baseline}}\).
- svg_path_editor.math.subs(expr, subs, *, n=None)[source]#
Substitute symbols in an expression, optionally with numeric evaluation.
If the expression contains floating-point numbers and
nis notNone,sympy.Expr.evalf()is used with the given precision and substitutions. Otherwise, standard symbolic substitution viasympy.Expr.subs()is performed.
- svg_path_editor.math.evalf(expr, *, n)[source]#
Optionally evaluate an expression numerically.
If
nisNone, returnexprunchanged. Otherwise, returnexpr.evalf(n=n.full); if the imaginary part is at most \(10^{-\texttt{baseline}}\), the real part is returned.
- svg_path_editor.math._as_real_poly(poly, x)[source]#
Try to interpret
polyas a univariate real polynomial inx.
- svg_path_editor.math.cutoff_tiny(v, n=None)[source]#
Replace numerically tiny floating values by exact zero.
If
vis asympy.Floatandis_zero(v, n=n)holds,sympy.S.Zerois returned, otherwisevis returned unchanged.
- svg_path_editor.math.quadratic_roots(a1, a0, *, real_only=True, n=None)[source]#
Solve the monic quadratic equation
\[z^2 + a_1 z + a_0 = 0\]and return only the real roots if
real_only=True(default).The function implements the standard quadratic formula.
- Parameters:
a1 (Expr) – Coefficient \(a_1\) of \(z\).
a0 (Expr) – Constant term \(a_0\).
real_only (bool) – If
True, discard non-real roots.n (Precision | None) – Optional precision used when classifying the discriminant as positive/non-negative via
cutoff_tiny()andge().
- Returns:
A list of roots of \(z^2 + a_1 z + a_0 = 0\) sorted in nondecreasing order (if they are real).
- svg_path_editor.math.cubic_roots(a2, a1, a0, *, real_only=True, n=None)[source]#
Solve the monic cubic equation
\[z^3 + a_2 z^2 + a_1 z + a_0 = 0\]using the algorithm from https://quarticequations.com/Selected_Algorithms.pdf. Only the real roots are returned if
real_only=True.The algorithm follows the two main cases in the reference:
Case 1 (\(r^2 + q^3 > 0\)): one real root.
Case 2 (\(r^2 + q^3 ≤ 0\)): three real roots (Viète’s trigonometric form).
- Parameters:
a2 (Expr) – Coefficient \(a_2\) of \(z^2\).
a1 (Expr) – Coefficient \(a_1\) of \(z\).
a0 (Expr) – Constant term \(a_0\).
real_only (bool) – If
True, return only the real roots; otherwise all three (possibly complex) roots are returned.n (Precision | None) – Optional precision used in
is_zero(),gt(),eq(), and trigonometric evaluations.
- Returns:
A list of roots of \(z^3 + a_2 z^2 + a_1 z + a_0 = 0\) sorted in nondecreasing order (if they are real).
- Return type:
list[Expr]
- svg_path_editor.math.quartic_roots(a3, a2, a1, a0, *, real_only=True, n=None)[source]#
Solve the monic quartic equation
\[z^4 + a_3 z^3 + a_2 z^2 + a_1 z + a_0 = 0\]using the modified Euler algorithm of Wolters, as described in https://quarticequations.com/Selected_Algorithms.pdf.
Only real roots are returned if
real_only=True.The method uses the resolvent cubic
\[r^3 + \frac{b_2}{2}\,r^2 + \frac{b_2^2-4 b_0}{16}\,r - \frac{b_1^2}{64}=0,\]whose three solutions \(r_1,r_2,r_3\) are obtained via
cubic_roots(). The greatest real solution \(r_1\) with \(r_1 ≥ 0\) is then used to compute the four quartic roots.- Parameters:
a3 (Expr) – Coefficient \(a_3\) of \(z^3\).
a2 (Expr) – Coefficient \(a_2\) of \(z^2\).
a1 (Expr) – Coefficient \(a_1\) of \(z\).
a0 (Expr) – Constant term \(a_0\).
real_only (bool) – If
True, discard complex roots derived from negative radicands.n (Precision | None) – Optional precision used in the classification of real radicands via
ge()and in cubic root computation.
- Returns:
A list of real roots of \(z^4 + a_3 z^3 + a_2 z^2 + a_1 z + a_0 = 0\).
- Return type:
list[Expr]
- svg_path_editor.math.polynomial_roots(poly, x, *, real_only=True, n=None)[source]#
Compute roots of a univariate polynomial up to degree 4.
The input expression is converted to
sympy.Polyinxand dispatched to the appropriate specialized solver:degree 4:
quartic_roots()degree 3:
cubic_roots()degree 2:
quadratic_roots()degree 1: explicit linear solution
degree 0: either no solution or infinitely many solutions
The returned dictionary maps each root to its multiplicity using
collections.Counter.- Parameters:
- Returns:
A mapping
{root: multiplicity}.- Raises:
ValueError – If the polynomial degree is greater than 4 or for the identically zero polynomial (infinitely many solutions).
- Return type:
dict[Expr, int]
- svg_path_editor.math._sylvester_matrix(p, q)[source]#
Construct the Sylvester matrix of two polynomials \(p(x), q(x)\).
The input polynomials must be in the same variable and given as
sympy.Polyinstances. The resulting square matrix has size \((\deg(p) + \deg(q)) × (\deg(p) + \deg(q))\) and is constructed from shifted coefficient rows.
- svg_path_editor.math.resultant(f, g, x, y, n=None)[source]#
Resultant \(\operatorname{res}_y(f, g)\).
Eliminates variable \(y\) from the system \(f(x, y) = 0\), \(g(x, y) = 0\).
If both expressions are real polynomials in \(y\), the resultant is computed as the determinant of the Sylvester matrix, then numerically evaluated with
evalf()using precisionn. Coefficients that are numerically zero (according tois_zero()with precisionn) are normalized to exact zero.Otherwise,
sympy.resultant()is used.