Skip to main content

Python Math library for Matrix and Polynomial manipulation extending to the complex plane

Project description

Pythematics

Pythematics is a zero-dependency math library for Python that aims to extend some Mathematical fields that are seemingly abandoned by other libraries while offering a fully Pythonic experience.

The main field that this library aims to enhance to Python is Polynomials in a way that allows for super-complicated and high degree polynomial equations to be solved giving all Real and Complex Solutions as well as combining it with fields such as Linear Algebra and allowing for Matrix-Polynomial Manipulation methods like finding Eigenvalues and Eigenvectors.

Pythematics can also be used as an ordinary math module since it contains various sub-packages for computations in Trigonometry, Number Theory and other fields such as Random Number Generation or Calculus

The thing that makes this library unique is the ability to Interact will Polynomials at the following format:

import pythematics.polynomials as pl

x = pl.x
pol: pl.Polynomial = x**4 + 3*x**2 + 2*x
print("Solve x^4 + 3x^2+ 2x = 0 : ", pol == 0)
print("Inequality: ", pol > 0)

which outputs

Solve x^4 + 3x^2+ 2x = 0 :  [(-0.5960716379833215+1.2508571630922372e-33j), (0.29803581899166076+1.8073394944520218j), (0.2980358189916609-1.8073394944520218j), 0j]
Inequality:  {(-inf, -0.5960716379833215): True, (-0.5960716379833215, 0.0): False, (0.0, inf): True}

And the results can be verified by subsituting

roots = pol.roots(5000) # Same as pol == 0
for root in roots:
    print(pol(root)) # Substituting

# The output: Very small numbers close to 0
    # (1.1102230246251565e-16+1.0171730768082222e-32j)
    # 0j
    # (-1.7763568394002505e-15-8.881784197001252e-16j)
    # (3.552713678800501e-15+8.881784197001252e-16j)

This can be generalised not to just polynomials

a, b = symbol('a', 'b')
for i in range(5):
    print(f"{i}: ", (a + b)**i)
  • Output
0:  1 # Unicode can be disabled by setting `Polynomial.use_unicode=False` and/or `Multinomial.use_unicode=False`
1:  Multivariable Polynomial : b + a
2:  Multivariable Polynomial : b² + a² + 2ab 
3:  Multivariable Polynomial : b³ + a³ + 3a²b + 3ab²
4:  Multivariable Polynomial : b + a + 4a³b + 6a²b² + 4ab³

Linear Algebra is another huge part of this module. Here is an example of solving a 3x3 system of Linear of equations.

import pythematics.linear as lin
A = lin.Matrix([
    [1,2,3],  # R1:  x +  2y +  3z
    [4,7,8],  # R2: 4x +  7y +  8z
    [5,10,11] # R3: 5x + 10y + 11z
])

unknowns = ('x','y','z') # Our Varaibles
Output = Vector([10, 15, 25]) # Our Target Output (R1=10, R2=15, R3=25)

print(A.solve(Output, unknowns)) # Using Cramer's rule of the determinants
print(A.solve(Output, unknowns, useRef=True)) # Using Row Reduction

And as expected the results are identical dispite a small floating point error

{'x': -8.75, 'y': 0.0, 'z': 6.25}
{'x': -8.749999999999993, 'y': -6.217248937900877e-15, 'z': 6.250000000000002}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pythematics-4.0.0.tar.gz (35.2 kB view hashes)

Uploaded Source

Built Distribution

pythematics-4.0.0-py3-none-any.whl (37.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page