Difference between revisions of "Python:Symbolic Computations"

From PrattWiki
Jump to navigation Jump to search
(Useful Pages)
Line 12: Line 12:
 
* [[SymPy/Initialization and Documentation]]
 
* [[SymPy/Initialization and Documentation]]
 
* Plotting (TBD)
 
* Plotting (TBD)
* [[SymPy/Simultaneous Equations]]
+
* [[SymPy/Simultaneous Equations]] - for ECE 110 in Fall 2022, this might be a good place to start; it has complete examples!
  
 
== Preamble ==
 
== 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:
 
* 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)
+
** <code>import sympy as sym</code> (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)
+
** <code>import sympy as sp</code> (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)
+
** <code>from sympy import *</code> (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)
+
** <code>from sympy import TUPLE OF THINGS</code> (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 *!
+
* <code>sym.init_session()</code> 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 *!
 +
 
 +
== Output ==
 +
* To make output prettier: <code>sym.init_printing()</code>
 +
* The display depends on if LaTeX is installed or not; the command above will try to make the output as pretty as possible given the circumstances.
  
 
== Defining Symbols ==
 
== Defining Symbols ==
* a, b, c = sym.symbols('a b c') or a, b, c = sym.symbols('a, b, c')
+
* If the symbolic and variable names exactly match, it is most efficient to use <code>sym.var('a b c')</code> or <code>sym.var('a, b, c')</code>
* The symbolic representation can be entirely different from the variable with a, b, c = sym.symbols('let\'s go Duke')
+
** Spyder's editor will not recognize that the variables are defined, meaning you will get error flags in the editor.  If you want to explicitly demonstrate that the variables exist, you can use <code>a, b, c = sym.var('a b c')</code>
* If the symbolic and variable names exactly match more efficient to use sym.var('a b c') or sym.var('a, b, c')
+
** Alternatively, if you want to create a list with all the variables in it, you can use <code>THING = sym.var('a b c')</code>
** May want to assign this to a variable or append a ; since this returns a tuple with the variables in it
+
* You can also yse the <code>sym.symbols</code> command, which gives a little more flexibility <code>a, b, c = sym.symbols('a b c')</code> or <code>a, b, c = sym.symbols('a, b, c')</code>
 +
** The symbolic representation can be entirely different from the variable with <code>a, b, c = sym.symbols('let\'s go Duke')</code>. This doesn't look pretty in all version of Python, though.
  
 
== Substitutions ==
 
== Substitutions ==
* use .subs(variable, value) or .subs(iterable) where iterable has a collection of variables and values
+
* If you have some symbolic object X that contains several other symbols, you can use <code>X.subs(variable, value)</code> for a single substitution or <code>X.subs(iterable)</code> where iterable has a collection of variables and values; for example, <code>X.subs([(a, 10), (b, 20)])</code>
 
+
* If you have several variables and several values, it may be useful to have a list of each and "zip" them together:
== Output ==
+
<syntaxhighlight lang=python>
* To make output prettier: sym.init_printing()
+
> vars = ['a', 'b']
* Display depends on if LaTeX is installed or not
+
> vals = [10, 20]
 +
> print(list(zip(vars, vals)))
 +
[('a', 10), ('b', 20)]
 +
> x = a + b
 +
> print(x)
 +
a + b
 +
> x.subs(zip(vars, vals))
 +
30
 +
</syntaxhighlight>
  
 
== Solving ==
 
== Solving ==
* sym.solve() and sym.solveset()
+
* sym.solve(EQNS, VARS) for a system of equations
* sym.dsolve()
+
* sym.dsolve(EQNS, VARS) for a system of differential equations
  
 
== Interesting Things ==
 
== Interesting Things ==
Line 45: Line 58:
  
 
== Philosophical Things ===
 
== 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 ==
 
== References ==
Line 55: Line 65:
  
 
== Future Work ==
 
== Future Work ==
 +
* Determine good ways to collect equations and variables.
 +
* Clarify when to solve after numbers are in versus before.
 
* 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.
 
* 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.
+
* 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.  Best to avoid for now.

Revision as of 20:21, 21 September 2022

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

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 *!

Output

  • To make output prettier: sym.init_printing()
  • The display depends on if LaTeX is installed or not; the command above will try to make the output as pretty as possible given the circumstances.

Defining Symbols

  • If the symbolic and variable names exactly match, it is most efficient to use sym.var('a b c') or sym.var('a, b, c')
    • Spyder's editor will not recognize that the variables are defined, meaning you will get error flags in the editor. If you want to explicitly demonstrate that the variables exist, you can use a, b, c = sym.var('a b c')
    • Alternatively, if you want to create a list with all the variables in it, you can use THING = sym.var('a b c')
  • You can also yse the sym.symbols command, which gives a little more flexibility 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'). This doesn't look pretty in all version of Python, though.

Substitutions

  • If you have some symbolic object X that contains several other symbols, you can use X.subs(variable, value) for a single substitution or X.subs(iterable) where iterable has a collection of variables and values; for example, X.subs([(a, 10), (b, 20)])
  • If you have several variables and several values, it may be useful to have a list of each and "zip" them together:
> vars = ['a', 'b']
> vals = [10, 20]
> print(list(zip(vars, vals)))
[('a', 10), ('b', 20)]
> x = a + b
> print(x)
a + b
> x.subs(zip(vars, vals))
30

Solving

  • sym.solve(EQNS, VARS) for a system of equations
  • sym.dsolve(EQNS, VARS) for a system of differential equations

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 =

References

Future Work

  • Determine good ways to collect equations and variables.
  • Clarify when to solve after numbers are in versus before.
  • 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. Best to avoid for now.