Python:Symbolic Computations

From PrattWiki
Revision as of 17:28, 19 September 2022 by DukeEgr93 (talk | contribs)
Jump to navigation Jump to search

This is a sandbox for information on symbolic computation with Python. It is about as organized as one might expect...

Introduction

SymPy is a package that allows Python to perform symbolic calculations. The main English-language site is https://www.sympy.org/en/index.html

Getting Started

  • Tutorial (note - some of the internal links on SymPy do not get to the tutorial - this link does.
  • Anaconda comes with SymPy already installed so you can skip the installation part if you are using Anaconda.
  • Note that sometimes the tutorial imports SymPy as sympy and other times it imports all of SymPy. This does make it a little harder to keep track of which commands are SymPy-specific!

Useful Pages

  • [SymPy/Initialization and Documentation]
  • Plotting (TBD)
  • [SymPy/Simultaneous Equations]

Preamble

  • This page will be consistent with Python:Nicknames in terms of module imports. Note that there are several ways to get the SymPy package into Python:
    • import sympy as sym (what this page does)
    • import sympy as sp (this is more consistent with bringing in NumPy, but that's what we will use for SciPy)
    • from sympy import * (if you are sure nothing in SymPy will contradict anything in built-in Python)
    • from sympy import TUPLE OF THINGS (if you just have a few specific things you want to do with SymPy)
  • sym.init_session() will automatically bring in x, y, z, and t as symbols; k, m, n as integers; f, g, h as function names; and sym.init_printing HOWEVER it brings in all of sympy with from sympy import *!

Defining Symbols

  • a, b, c = sym.symbols('a b c') or a, b, c = sym.symbols('a, b, c')
  • The symbolic representation can be entirely different from the variable with a, b, c = sym.symbols('let\'s go Duke')
  • If the symbolic and variable names exactly match more efficient to use sym.var('a b c') or sym.var('a, b, c')
    • May want to assign this to a variable or append a ; since this returns a tuple with the variables in it

Substitutions

  • use .subs(variable, value) or .subs(iterable) where iterable has a collection of variables and values

Output

  • To make output prettier: sym.init_printing()
  • Display depends on if LaTeX is installed or not

Solving

  • sym.solve() and sym.solveset()
  • sym.dsolve()

Interesting Things

  • sym.lambdify((variables), expression, "numpy") will return a function that performs the calculation in the expression
  • sym.simplify(expression) will work to simplify an expression
  • sym.Matrix() can have symbols and will calculate things symbolically

Philosophical Things =

  • How to declare things? Knowns versus unknowns? All symbols and then all functions or batches of each?
  • Name equations or just number them?
  • When to solve after numbers are in versus before?
  • How much time to spend figuring out nice subscripts (probably less than already spent)?

References

Future Work

  • Subscripts are...strange. Numbers coming at the end of a variable print as subscripts but letters end that behavior. One workaround is to define a variable with the xa = sym.Symbol('x_a') command but that will only take a single character superscript.
  • You can create a neat symbol like pdelvCC = sym.Symbol('p_{{del,v_{CC}}}}') and it will work great in a notebook or inline. Spyder has a display() function that will also work but creates a plot with the rendering. Displays really ugly on Trinket.