Difference between revisions of "Python:Symbolic Computations"
Jump to navigation
Jump to search
(→Defining Symbols) |
|||
Line 27: | Line 27: | ||
== Defining Symbols == | == Defining Symbols == | ||
+ | * There are a variety of different ways to define symbols; your choice will be based on how you are writing and running code (for example, Spyder versus Jupyter versus Trinket). | ||
* 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> | * 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> | ||
** 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> | ** 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> | ||
** 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> | ** 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> | ||
− | * | + | ** If you want the best of both worlds, use <code>a, b, c = sym.var('a b c')</code> and <code>THING = a, b, c</code> |
− | ** The symbolic representation can be entirely different from the variable with <code>a, b, c = sym. | + | ** The symbolic representation can be entirely different from the variable with something like <code>v1, ib, pabsR3 = sym.var('v_1 i_b p_{{abs\,R_{3}}}}')</code> or <code>a, b, c = sym.var('let\'s go Duke')</code>. When Python displays the variables, it will use the characters assigned to the variable in the string. This doesn't look pretty in all version of Python, though, and requires <code>sym.init_printing()</code> to have a chance at working. |
+ | * <code>sym.symbols()</code> is the same as <code>sym.var()</code> except that you must explicitly list the names on the right of an = (that is to say, <code>a, b, c = sym.var('a b c')</code> is the same as <code>a, b, c = sym.symbols('a b c')</code> but <code>sym.symbols('a b c')</code> does not create variables while <code>sym.var('a b c')</code> does. | ||
== Substitutions == | == Substitutions == |
Revision as of 02:09, 30 September 2022
This is a sandbox for information on symbolic computation with Python. It is about as organized as one might expect...
Contents
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 - for ECE 110 in Fall 2022, this might be a good place to start; it has complete examples!
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
- There are a variety of different ways to define symbols; your choice will be based on how you are writing and running code (for example, Spyder versus Jupyter versus Trinket).
- If the symbolic and variable names exactly match, it is most efficient to use
sym.var('a b c')
orsym.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')
- If you want the best of both worlds, use
a, b, c = sym.var('a b c')
andTHING = a, b, c
- The symbolic representation can be entirely different from the variable with something like
v1, ib, pabsR3 = sym.var('v_1 i_b p_{{abs\,R_{3}}}}')
ora, b, c = sym.var('let\'s go Duke')
. When Python displays the variables, it will use the characters assigned to the variable in the string. This doesn't look pretty in all version of Python, though, and requiressym.init_printing()
to have a chance at working.
- 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
sym.symbols()
is the same assym.var()
except that you must explicitly list the names on the right of an = (that is to say,a, b, c = sym.var('a b c')
is the same asa, b, c = sym.symbols('a b c')
butsym.symbols('a b c')
does not create variables whilesym.var('a b c')
does.
Substitutions
- If you have some symbolic object X that contains several other symbols, you can use
X.subs(variable, value)
for a single substitution orX.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.