Hi,
I am a new user and I want to show you my Python library.

pypol
pypol is a Python library that allows you to manipulate monomials, polynomials and algebraic fractions.
An example:

>>> import pypol
>>> a = pypol.polynomial('3xy - 3a^2 + 2b')
>>> a
- 3a² + 3xy + 2b
>>> a.append('-2')
>>> a.right_hand_side
-2
>>> del a[-1]
>>> a.right_hand_side
False
>>> a.append('-2')
>>> a.letters
('a', 'b', 'x', 'y')
>>> a.powers()
{'a': [2, 0], 'b': [1, 0], 'x': [1, 0], 'y': [1, 0]}
>>> a.iscomplete('x')
True
>>> a.iscomplete('a')
False
>>> a._make_complete('a')
True
>>> a.iscomplete('a')
True
>>> a.monomials
((-3, {'a': 2}), (0, {'a': 1}), (3, {'x': 1, 'y': 1}), (2, {'b': 1}), (-2, {}))
>>> a
- 3a²  + 3xy + 2b - 2
>>> a.max_power('a')
2
>>> a.min_power('a')
0
>>> a.isordered('a')
True
>>> b = pypol.polynomial('3a - 1')
>>> b.zeros
()
>>> b.print_format = False
>>> b
+ 3a - 1
>>> b.append('2x2')
>>> b
+ 2x^2 + 3a - 1
>>> b.print_format = True
>>> b
+ 2x² + 3a - 1
>>> p = pypol.Polynomial(((3, {'a': 2}), (-2, {'a': 1}), (2, {})))
>>> p
+ 3a² - 2a + 2
>>> a
- 3a²  + 3xy + 2b - 2
>>> p
+ 3a² - 2a + 2
>>> a + p
+ 3xy + 2b - 2a
>>> a - p
- 6a² + 3xy + 2b + 2a - 4
>>> a * p
- 9a⁴ + 6a³ + 9a²xy + 6a²b - 12a² - 6axy + 6xy - 4ab + 4b + 4a - 4
>>> p(2)
10
>>> pp = pypol.polynomial('3x4 - 2x3 + x - 4')
>>> ppp = pypol.polynomial('x - 2')
>>> divmod(pp, ppp)
(+ 3x³ + 4x² + 8x + 17, + 30)
>>> c, d = divmod(pp, ppp) # quotient and remainder
>>> c * ppp + d
+ 3x⁴ - 2x³  + x - 4
>>> c * ppp + d == pp
True

There are many others features...
The project is at http://github.com/rubik/pypol
and

and here is the documentation:

What do you think?

Thanks,
rubik-pypol

Dani AI

Generated

A short expert note on pypol (from the thread history and the recent feature posts by )

pypol is a well-scoped pure‑Python toolkit for building and manipulating monomials, polynomials and algebraic fractions, and the thread shows steady feature work (series generators, a roots module and multiple iterative solvers). For learning, small symbolic tasks, or tightly integrated Python code it fills a useful niche. For larger numeric workloads it helps to be explicit about the solver choices and expected numeric behavior.

Root-finding: tradeoffs and practical choices
As observed, different root algorithms have different strengths. Local iterative methods (Newton, Halley, Householder) are fast near a good initial guess but can fail on poor starts or multiple roots. Durand–Kerner (Weierstrass) computes all complex roots simultaneously and is a reasonable pure‑Python choice, but it has convergence caveats for clustered roots. The Jenkins–Traub family is a robust, globally convergent algorithm used in many production solvers. A commonly used numeric approach (NumPy/SciPy) is to form the companion matrix and compute its eigenvalues (LAPACK) — fast and widely available, but it can be ill‑conditioned for repeated roots. (en.wikipedia.org)

Complementary libraries and when to use them
For exact algebraic work and multiplicity information, SymPy’s polynomial tools and RootOf machinery are very helpful; SymPy also provides a slower but more reliable numeric nroots. For high‑precision numeric roots, mpmath’s polyroots/polyval support arbitrary precision and error estimates. For speed on floating numeric problems, NumPy/SciPy’s LAPACK-backed routines are the usual choice. Offering solver options that interoperate with these libraries gives pypol both flexibility and reliability. (docs.sympy.org)

Practical checklist and a small sanity check

  • Add a test suite that includes clustered roots, multiple roots, Wilkinson‑style polynomials and random high‑degree cases.
  • Make the roots solver modular so callers can choose: companion‑matrix (fast), Durand–Kerner (pure Python, simultaneous), or mpmath (high precision).
  • Verify roots by direct evaluation and (when needed) symbolic factoring to detect multiplicity.

Example quick numeric sanity check (conceptual):

# f is a callable numeric evaluator for the polynomial,
# roots is a list of computed roots, tol chosen for the problem
for r in roots:
    assert abs(f(r)) < 1e-12  # adjust tol for precision/conditioning

This keeps results grounded: document each solver’s pros/cons in the README, add regression tests against NumPy/SymPy/mpmath, and include a recommended default for typical users.

Recommended Answers

All 11 Replies

What do you think?

Up

Up

You don't have questions, nowone cared about this, so just let it sink, till someone finds it attractive.


Cheers and Happy coding

Released version 0.2.

Released version 0.3

I note that your module computes the roots of polynomials by bisection. This is a primitive way to find the roots and there are many other methods (see for example this article http://en.wikipedia.org/wiki/Root-finding_algorithm). I hope you intend to strengthen pypol on this point. I once reviewed a few methods available in python to get the roots of polynomials: see http://www.daniweb.com/code/snippet271301.html.

I think it's a good idea to write a module for polynomials and algebraic fractions, but you should include in the doc a small review of other existing modules for such tasks. Especially, a pure python implementation may have performance issues that other implementations don't have. See for example the C based ratfun.

Thank you! I use the quadratic equation for the polynomial of degree 2 and bisection is temporary, I'm implementing the newton method and durand-kerner method.
Algebraic fractions are still very weak, and I will strengthen them when I will finish the root-finding algorithm.

If you are willing to collaborate, you are welcome.

P.S. I'm adding series-generator too, like Fibonacci polynomials, hermite polynomials, chebyshev polynomial... ecc.

Implemented quadratic, cubic and quartic Formeln! Now we can solve any polynomial up to degree 4! And I have added the newton's method, halley's method and householder's method too!!
:cool: :cool:

I need to update a link (can someone edit my first post?):

Released version 0.4. Added 2 new modules: pypol.series and pypol.roots.
For the complete CHANGELOG:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.