Skip to main content

A Python module for symbolic calculations

Project description

Python Symbolic Expression 3

A Python module for symbolic calculations

Usage

Optimize expression

>>> import symexpress3
>>> objExpress = symexpress3.SymFormulaParser( '(x+1) * (x+2)' )
>>> objExpress.optimizeExtended()
>>> print( objExpress )
x^2 + x * 3 + 2

Supported operators

+   -   *   /   (   )   ^   ^^
^ gives root values by fractions ^^ gives principal root values by fractions

>>> import symexpress3
>>> objExpress = symexpress3.SymFormulaParser( '(4)^(1/2)' )
>>> objExpress.optimizeExtended()
>>> print( objExpress )
[ (-2) | 2 ]
>>> objExpress = symexpress3.SymFormulaParser( '(4)^^(1/2)' )
>>> objExpress.optimizeExtended()
>>> print( objExpress )
2

Numeric values

Only integers are supported. For fractions use /

>>> import symexpress3
>>> objExpress = symexpress3.SymFormulaParser( '7/10 + 3/11' )
>>> objExpress.optimizeExtended()
>>> print( objExpress )
(107/110)

Get the real value

If there is more the one value, it gives an array back

>>> import symexpress3
>>> objExpress = symexpress3.SymFormulaParser( '7/10 + 3/11' )
>>> objExpress.optimizeExtended()
>>> print( objExpress.getValue() )
0.9727272727272728
>>> objExpress = symexpress3.SymFormulaParser( '(4)^(1/2)' )
>>> objExpress.optimizeExtended()
>>> print( objExpress.getValue() )
[-2, 2]

Get all defined functions

Get a dictionary of all the defined functions. Key is the functions syntax, value is the description

>>> import symexpress3
>>> print( symexpress3.GetAllFunctions() )

Get all defined optimize actions

Get a dictionary of all the optimize actions. Key is the optimize action, value is the description

>>> import symexpress3
>>> print( symexpress3.GetAllOptimizeActions() )

Get all fixed variables

Get a dictionary of all the fixed variables. Key is the variable name, value is the description

>>> import symexpress3
>>> print( symexpress3.GetFixedVariables() )

Predefined optimization methods

The optimizeNormal method use the multiply, onlyOneRoot, i, power and add optimize actions The power action do not optimize roots. onlyOneRoot get radicals in it lowest form.

>>> import symexpress3
>>> objExpress = symexpress3.SymFormulaParser( '5 * 2^2 + i * i + (4)^(1/2)' )
>>> objExpress.optimizeNormal()
>>> print( objExpress )
19 + 4^(1/2)

The optimizeExtended method use the optimizeNormal method and the following optimize actions: rootToPrincipalRoot, powerArrays,arrayPower, functionToValues, negRootToI, functionToValues, unnestingRadicals, functionToValues, radicalDenominatorToCounter, rootIToSinCos, functionToValues, rootOfImagNumToCosISin, functionToValues, nestedRadicals, imaginairDenominator, sinTwoCosTwo, cosXplusYtoSinCos, sinXplusYtoSinCos, splitDenominator and expandArrays

>>> import symexpress3
>>> objExpress = symexpress3.SymFormulaParser( '5 * 2^2 + i * i + (4)^(1/2)' )
>>> objExpress.optimizeExtended()
>>> print( objExpress )
19 + [ (-2) | 2 ]
>>> print( objExpress.getValue() )
[17, 21]

Optimize expression

Use the optimize method for optimization

Remove unnecessary () and optimize the internal structure

>>> import symexpress3
>>> objExpress = symexpress3.SymFormulaParser( '((2) + (3))' )
>>> objExpress.optimize( None )
>>> print( objExpress )
2 + 3

Use of the optimize actions. Use after SymFormulaParser and each optimize() an optimize( None ) Most of the time use optimizeNormal() instead of optimize( None )

>>> import symexpress3
>>> objExpress = symexpress3.SymFormulaParser( 'cos( pi/4 )' )
>>> objExpress.optimize( None )
>>> objExpress.optimize( 'functionToValues' )
>>> objExpress.optimize( None )
>>> print( objExpress )
2^^(1/2) * (1/2)

Output formats

MathMl output

>>> import symexpress3
>>> objExpress = symexpress3.SymFormulaParser( 'cos( pi/4 )' )
>>> objExpress.optimize( None )
>>> print( objExpress.mathMl() )

Html output with expression in MathMl and string format. It create the symexpress3_demo.html file in the current directory.

>>> import symexpress3
>>> objExpress = symexpress3.SymFormulaParser( 'cos( pi/4 )' )
>>> objExpress.optimize( None )
>>> objOutput = symexpress3.SymToHtml( "symexpress3_demo.html", "SymExpress 3 demo" )
>>> objOutput.writeSymExpress( objExpress )
>>> objOutput.writeLine( str( objExpress ))
>>> objOutput.closeFile()

Calculate value with variables

Define a dictionary for the variables. The key is the variable name and de value is a number.

>>> import symexpress3
>>> objExpress = symexpress3.SymFormulaParser( '(x+1) (y+2)' )
>>> dictVar = {}
>>> dictVar[ 'x' ] = 0.33333
>>> dictVar[ 'y' ] = 3
>>> valueExpression = objExpress.getValue( dictVar )
>>> print( valueExpression )
6.66665

Command line

python -m symexpress3

  • Help: python -m symexpress3 -h
  • Direct optimize: python -m symexpress3 "cos( pi / 4 )^^(1/3)"

Notes

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

symexpress3-0.0.16.tar.gz (129.5 kB view details)

Uploaded Source

Built Distribution

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

symexpress3-0.0.16-py3-none-any.whl (215.9 kB view details)

Uploaded Python 3

File details

Details for the file symexpress3-0.0.16.tar.gz.

File metadata

  • Download URL: symexpress3-0.0.16.tar.gz
  • Upload date:
  • Size: 129.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for symexpress3-0.0.16.tar.gz
Algorithm Hash digest
SHA256 6fc3a6e6b81e92190c9430d51ad513c0db90efe91938c2bcff072e86d3e292e3
MD5 559bb2b7d1ad679e71bf69e6c171680c
BLAKE2b-256 134e2aebb54caf95d4b8359b7fd86dadcc74c890925efc2f45e6a486a4e0c49c

See more details on using hashes here.

File details

Details for the file symexpress3-0.0.16-py3-none-any.whl.

File metadata

  • Download URL: symexpress3-0.0.16-py3-none-any.whl
  • Upload date:
  • Size: 215.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for symexpress3-0.0.16-py3-none-any.whl
Algorithm Hash digest
SHA256 b94d23862297e1c7cf8d9df5d39d4e4e632ef71ad70b55f90dfc38b087f04e7d
MD5 7d2c6eb67682d39fc352c3ad6ef88523
BLAKE2b-256 fa30e56334d0e87c9d5c05ce286b44d7a786821a2f5615a596c7e4323ceb0e6b

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