Skip to main content

A Python module for the exact solutions of a cubic equation

Project description

Python cubic equation solver for exact values

A Python module for the exact solutions of a cubic equation: a x^3 + b x^2 + c x + d = 0

Usage

Calculate cubic solution

The solutions are three symexpress3 objects: x1Optimized, x2Optimized, x3Optimized

>>> import cubicequation
>>> objCubic = cubicequation.CubicEquation()
>>> objCubic.a = "1"
>>> objCubic.b = "2"
>>> objCubic.c = "3"
>>> objCubic.d = "4"
>>> objCubic.calcSolutions()
>>> print( f"x1: {objCubic.x1Optimized}\nx2: {objCubic.x2Optimized}\nx3: {objCubic.x3Optimized}\n" )
x1: (-2/3) + (15 * 6^^(1/2) + 35)^^(1/3) * (-1/3) + (1/3) * (15 * 6^^(1/2) + (-35))^^(1/3)
x2: (-2/3) + (1/6) * (15 * 6^^(1/2) + 35)^^(1/3) + (-1/6) * (15 * 6^^(1/2) + 35)^^(1/3) * i * 3^^(1/2) + (-1/6) * i * 3^^(1/2) * (15 * 6^^(1/2) + (-35))^^(1/3) + (-1/6) * (15 * 6^^(1/2) + (-35))^^(1/3)
x3: (-2/3) + (15 * 6^^(1/2) + 35)^^(1/3) * (1/6) + (15 * 6^^(1/2) + 35)^^(1/3) * i * 3^^(1/2) * (1/6) + (1/6) * i * 3^^(1/2) * (15 * 6^^(1/2) + (-35))^^(1/3) + (-1/6) * (15 * 6^^(1/2) + (-35))^^(1/3)

Numeric input values

The parameters may be real numbers or symexpress3 strings

>>> import cubicequation
>>> objCubic = cubicequation.CubicEquation()
>>> objCubic.a = 1.10
>>> objCubic.b = 2.25
>>> objCubic.c = "2 + 1 + 3"
>>> objCubic.d = "4/2"
>>> objCubic.calcSolutions()
>>> print( f"x1: {objCubic.x1Optimized}\nx2: {objCubic.x2Optimized}\nx3: {objCubic.x3Optimized}\n" )
x1: (-15/22) + (-5/11) * ((11/25) * (3373/2)^^(1/2) + (-1349/200))^^(1/3) + (5/11) * ((11/25) * (3373/2)^^(1/2) + (1349/200))^^(1/3)
x2: (-15/22) + (5/22) * ((11/25) * (3373/2)^^(1/2) + (-1349/200))^^(1/3) + (-5/22) * i * 3^^(1/2) * ((11/25) * (3373/2)^^(1/2) + (-1349/200))^^(1/3) + (-5/22) * i * 3^^(1/2) * ((11/25) * (3373/2)^^(1/2) + (1349/200))^^(1/3) + (-5/22) * ((11/25) * (3373/2)^^(1/2) + (1349/200))^^(1/3)
x3: (-15/22) + (5/22) * ((11/25) * (3373/2)^^(1/2) + (-1349/200))^^(1/3) + (5/22) * i * 3^^(1/2) * ((11/25) * (3373/2)^^(1/2) + (-1349/200))^^(1/3) + (5/22) * i * 3^^(1/2) * ((11/25) * (3373/2)^^(1/2) + (1349/200))^^(1/3) + (-5/22) * ((11/25) * (3373/2)^^(1/2) + (1349/200))^^(1/3)

Calculate real values

>>> import cubicequation
>>> objCubic = cubicequation.CubicEquation()
>>> objCubic.a = 1.10
>>> objCubic.b = 2.25
>>> objCubic.c = "2 + 1 + 3"
>>> objCubic.d = "4/2"
>>> objCubic.calcSolutions()
>>> print( f"x1: {objCubic.x1Value}\nx2: {objCubic.x2Value}\nx3: {objCubic.x3Value}\n" )
x1: -0.3767589142748171
x2: (-0.8343478155898642-2.0321695851742962j)
x3: (-0.8343478155898642+2.0321695851742962j)

Example optimized exact values

>>> import cubicequation
>>> objCubic = cubicequation.CubicEquation()
>>> objCubic.a = 1
>>> objCubic.b = 6
>>> objCubic.c = 11
>>> objCubic.d = 6
>>> objCubic.calcSolutions()
>>> print( f"x1: {objCubic.x1Optimized}\nx2: {objCubic.x2Optimized}\nx3: {objCubic.x3Optimized}\n" )
x1: (-3)
x2: (-1)
x3: (-2)

Command line

python -m cubicequation

  • Help: python -m cubicequation -h
  • Cubic solution: python -m cubicequation 1 2 3 4

Graphical user interface

https://github.com/SWVandenEnden/websym3

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

cubicequation-0.0.3.tar.gz (27.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cubicequation-0.0.3-py3-none-any.whl (20.2 kB view details)

Uploaded Python 3

File details

Details for the file cubicequation-0.0.3.tar.gz.

File metadata

  • Download URL: cubicequation-0.0.3.tar.gz
  • Upload date:
  • Size: 27.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for cubicequation-0.0.3.tar.gz
Algorithm Hash digest
SHA256 507c6cb182b6d0271fbda9ce3825851867d936cc0131ea977350f242714e2896
MD5 cb94cea8df0b90ef00dfdbc3c94c415b
BLAKE2b-256 b35596134fbab9e6cc3c6d05613412e1f06f6c6c4f7db86d4b4efd64b1f667e6

See more details on using hashes here.

File details

Details for the file cubicequation-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: cubicequation-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 20.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for cubicequation-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c0d9554c8d3720eaf94bc9525259bd713aa74b7364fcf93d324f0b58e3d41894
MD5 538ba621bea08bc5809e00d9665e4051
BLAKE2b-256 19d2a551ff0ba52c87cac1ea1fa47b22d0ee1fcfd335fdf77c0af4f049d2e577

See more details on using hashes here.

Supported by

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