constructing scientiï¬c programs with sympy
TRANSCRIPT
![Page 1: Constructing Scientiï¬c Programs with SymPy](https://reader030.vdocuments.us/reader030/viewer/2022021009/6203aa85da24ad121e4c0d3f/html5/thumbnails/1.jpg)
Constructing Scientific Programs with SymPy
Mark Dewing
July 14, 2011
![Page 2: Constructing Scientiï¬c Programs with SymPy](https://reader030.vdocuments.us/reader030/viewer/2022021009/6203aa85da24ad121e4c0d3f/html5/thumbnails/2.jpg)
Outline
Motivation and Overview of Writing Scientific Programs
Implementation of a Framework
Example: Partition Function Integral
![Page 3: Constructing Scientiï¬c Programs with SymPy](https://reader030.vdocuments.us/reader030/viewer/2022021009/6203aa85da24ad121e4c0d3f/html5/thumbnails/3.jpg)
Writing Scientific Programs by Hand
Derive equations Convert to code
Problems:
I Transcription errors
I Identifying error from testing final program
![Page 4: Constructing Scientiï¬c Programs with SymPy](https://reader030.vdocuments.us/reader030/viewer/2022021009/6203aa85da24ad121e4c0d3f/html5/thumbnails/4.jpg)
Writing Scientific Programs by Hand
Derive equations Convert to code
Problems:
I Transcription errors
I Identifying error from testing final program
![Page 5: Constructing Scientiï¬c Programs with SymPy](https://reader030.vdocuments.us/reader030/viewer/2022021009/6203aa85da24ad121e4c0d3f/html5/thumbnails/5.jpg)
How Should We Write Scientific Programs?
Any problem in computer science can be solved withanother layer of indirection.
David Wheeler
I’d rather write programs to write programs thanwrite programs
Richard Sites
Computational Thinking - The thought processesinvolved in formulating problems so their solutionscan be represented as computational steps andalgorithms. Alfred Aho
![Page 6: Constructing Scientiï¬c Programs with SymPy](https://reader030.vdocuments.us/reader030/viewer/2022021009/6203aa85da24ad121e4c0d3f/html5/thumbnails/6.jpg)
Components of a Program to Write Scientific
Programs
I Description of problemI Domain Specific LanguageI Symbolic mathematics
I Transformation to target
I Representation of target language/system
![Page 7: Constructing Scientiï¬c Programs with SymPy](https://reader030.vdocuments.us/reader030/viewer/2022021009/6203aa85da24ad121e4c0d3f/html5/thumbnails/7.jpg)
Other Projects
I FEniCS - Finite element solutions to differential equations
I SAGA (Scientific computing with Algebraic andGenerative Abstractions) - PDE’s
I Spiral - signal processing transforms
I TCE (Tensor Contraction Engine) - quantum chemistry
I FLAME (Formal Linear Algebra Method Environment) -Linear algebra
See Andy Terrel’s article in CiSE March/April 2011
![Page 8: Constructing Scientiï¬c Programs with SymPy](https://reader030.vdocuments.us/reader030/viewer/2022021009/6203aa85da24ad121e4c0d3f/html5/thumbnails/8.jpg)
Advantages and Disadvantages
I AdvantagesI Improved notation for expressing problems and algorithmsI Testability - transforms are ’ordinary software’I Optimization of generated code
I Domain specific optimizationsI Explore larger parameter spaceI Restructuring for various target systems
I DisadvantagesI If problem domain isn’t covered by existing project, ?
![Page 9: Constructing Scientiï¬c Programs with SymPy](https://reader030.vdocuments.us/reader030/viewer/2022021009/6203aa85da24ad121e4c0d3f/html5/thumbnails/9.jpg)
Outline
Motivation and Overview of Writing Scientific Programs
Implementation of a Framework
Example: Partition Function Integral
![Page 10: Constructing Scientiï¬c Programs with SymPy](https://reader030.vdocuments.us/reader030/viewer/2022021009/6203aa85da24ad121e4c0d3f/html5/thumbnails/10.jpg)
Implementing Components of a Program to Write
Scientific Programs
I Description of problemI Symbolic mathematics - SymPy expressionsI Structure above expressions - derivation modeling
I Transformation to target - pattern matching
I Representation of target language/system - classes forC++ and Python
![Page 11: Constructing Scientiï¬c Programs with SymPy](https://reader030.vdocuments.us/reader030/viewer/2022021009/6203aa85da24ad121e4c0d3f/html5/thumbnails/11.jpg)
Derivation Modeling - What is it?
Think of math homework
I Series of steps
I Show your work
Solve for x:
2x+ y = 44
2x = 44 − y
x = 22 − y/2Types of steps
I Exact transformations
I Approximations
I Specialization - no. of spatial dimensions, no. of particles
![Page 12: Constructing Scientiï¬c Programs with SymPy](https://reader030.vdocuments.us/reader030/viewer/2022021009/6203aa85da24ad121e4c0d3f/html5/thumbnails/12.jpg)
Derivation Modeling
derivation class
I constructor takes initial equation
I add_step
I final or new_derivation
Examples of steps:
I replace
I add_term
I specialize_integral
Also outputs steps to web page in MathML or MathJax fornicely rendered math.
![Page 13: Constructing Scientiï¬c Programs with SymPy](https://reader030.vdocuments.us/reader030/viewer/2022021009/6203aa85da24ad121e4c0d3f/html5/thumbnails/13.jpg)
Derivation Modeling - Example
from sympy import Symbol,Sfrom prototype . derivation import \
derivation , add_term, mul_factor
x ,y = Symbol( ’x ’ ) ,Symbol( ’y ’ )d = derivation (2*x+y,44)d. add_step(add_term(−y) , ’ Subtract y ’ )d. add_step( mul_factor (S. Half ) , ’Divide by 2 ’ )print d. f ina l ( )
Output:x == -y/2 + 22
![Page 14: Constructing Scientiï¬c Programs with SymPy](https://reader030.vdocuments.us/reader030/viewer/2022021009/6203aa85da24ad121e4c0d3f/html5/thumbnails/14.jpg)
Transform to Target System - Pattern Matching
from sympy import Symbol, print_treex ,y = Symbol( ’x ’ ) , Symbol( ’y ’ )e = x+yprint_tree (e)
Add: x + y+−Symbol: y| comparable : False+−Symbol: x
comparable : False
![Page 15: Constructing Scientiï¬c Programs with SymPy](https://reader030.vdocuments.us/reader030/viewer/2022021009/6203aa85da24ad121e4c0d3f/html5/thumbnails/15.jpg)
Transform to Target System - Pattern Matching
Add: x + y+−Symbol: y| comparable : False+−Symbol: x
comparable : False
Match SymPy expression in Python
v = AutoVar ( )m = Match(e)i f m(Add, v .e1, v .e2) :
# operate on v .e1 and v .e2
![Page 16: Constructing Scientiï¬c Programs with SymPy](https://reader030.vdocuments.us/reader030/viewer/2022021009/6203aa85da24ad121e4c0d3f/html5/thumbnails/16.jpg)
Transform to Target System - Pattern Matching 2
object.__getattr__(self,name)If attribute not found, this method is called
class AutoVar( object ) :def __ in i t __ ( se l f ) :
se l f . vars = [ ]def __getattr__ ( self ,name) :
se l f . vars .append(name)return AutoVarInstance ( self ,name)
![Page 17: Constructing Scientiï¬c Programs with SymPy](https://reader030.vdocuments.us/reader030/viewer/2022021009/6203aa85da24ad121e4c0d3f/html5/thumbnails/17.jpg)
Transform to Target System - Pattern Matching 3
def expr_to_py (e ) :v = AutoVar ( )m = Match(e)# subtractioni f m(Add, (Mul , S.NegativeOne , v .e1) , v .e2) :return py_expr(py_expr .PY_OP_MINUS, expr_to_py (v .e2) ,
expr_to_py (v .e1) )# additioni f m(Add, v .e1, v .e2) :return py_expr(py_expr .PY_OP_PLUS, expr_to_py (v .e1) ,
expr_to_py (v .e2) )# divisioni f m(Mul , v .e1, (Pow, v .e2, S.NegativeOne ) ) :return py_expr(py_expr .PY_OP_DIVIDE , expr_to_py (v .e1) ,
expr_to_py (v .e2) )
![Page 18: Constructing Scientiï¬c Programs with SymPy](https://reader030.vdocuments.us/reader030/viewer/2022021009/6203aa85da24ad121e4c0d3f/html5/thumbnails/18.jpg)
Approaches to Code Generation
I Print target as string
print "print ’Hello’ "
I General (text-based) templating
I Structured model of target language and system
py_print_stmt(py_string("Hello"))
![Page 19: Constructing Scientiï¬c Programs with SymPy](https://reader030.vdocuments.us/reader030/viewer/2022021009/6203aa85da24ad121e4c0d3f/html5/thumbnails/19.jpg)
Overview of workflow
Input fileHTML + MathJax
Code generator
Python
C++
![Page 20: Constructing Scientiï¬c Programs with SymPy](https://reader030.vdocuments.us/reader030/viewer/2022021009/6203aa85da24ad121e4c0d3f/html5/thumbnails/20.jpg)
Outline
Motivation and Overview of Writing Scientific Programs
Implementation of a Framework
Example: Partition Function Integral
![Page 21: Constructing Scientiï¬c Programs with SymPy](https://reader030.vdocuments.us/reader030/viewer/2022021009/6203aa85da24ad121e4c0d3f/html5/thumbnails/21.jpg)
Example from Statistical Mechanics
Partition Function Integral
Quadrature Method - Trapezoidal Rule
Code Generation
Python or C++
Existing Quadrature Library
Derivation
Code Generation
Python or C++
Derivation
![Page 22: Constructing Scientiï¬c Programs with SymPy](https://reader030.vdocuments.us/reader030/viewer/2022021009/6203aa85da24ad121e4c0d3f/html5/thumbnails/22.jpg)
Example from Statistical Mechanics
Partition function describes thermodynamics of a system
Z = Symbol( ’Z ’ )partit ion_function =
derivation (Z, Integral (exp(−V/ ( k*T) ) ,R) )
Z =
∫e−
VTk dR
![Page 23: Constructing Scientiï¬c Programs with SymPy](https://reader030.vdocuments.us/reader030/viewer/2022021009/6203aa85da24ad121e4c0d3f/html5/thumbnails/23.jpg)
Example from Statistical Mechanics 2
n2. add_step( special ize_integral (R, ( r1 , r2 ) ) ," specialize to N=2" )
n2. add_step( replace (V,V2( r1 , r2 ) ) ," replace potential with N=2" )
Z =
∫ ∫e−β V(r1,r2) dr1dr2
![Page 24: Constructing Scientiï¬c Programs with SymPy](https://reader030.vdocuments.us/reader030/viewer/2022021009/6203aa85da24ad121e4c0d3f/html5/thumbnails/24.jpg)
Example from Statistical Mechanics 3
r_cm = Vector ( ’r_cm ’ ,dim=2)r_12 = Vector ( ’ r_12 ’ ,dim=2)r_12_def = def init ion ( r_12 , r2−r1 )r_cm_def = def init ion (r_cm, ( r1+r2 ) /2)V12 = Function ( ’V ’ )n2. add_step( special ize_integral ( r1 , ( r_12 , r_cm)) ,
’Switch variables ’ )n2. add_step( replace (V2( r1 , r2 ) ,V12( r_12 ) ) ,
’ Specialize to a potential that depends only on interpart ic le distance ’ )n2. add_step( replace (V12( r_12 ) ,V12(Abs( r_12 ) ) ) ,
’Depend only on the magnitude of the distance ’ )
Z =
∫ ∫e−β V(r12) dr12drcm
![Page 25: Constructing Scientiï¬c Programs with SymPy](https://reader030.vdocuments.us/reader030/viewer/2022021009/6203aa85da24ad121e4c0d3f/html5/thumbnails/25.jpg)
Example from Statistical Mechanics 4
Integrate out rcm, decompose into vector components andadd integration limits
Z = L2∫ 1
2L
− 12L
∫ 12L
− 12Le−β V(r12x,r12y) dr12xdr12y
![Page 26: Constructing Scientiï¬c Programs with SymPy](https://reader030.vdocuments.us/reader030/viewer/2022021009/6203aa85da24ad121e4c0d3f/html5/thumbnails/26.jpg)
Example from Statistical Mechanics 5
Specialize to Lennard-Jones potential.
V (r) = − 4
r6+
4
r12(1)
Insert values for box size, and temperature
Z = 16.0
∫ 2.0
−2.0
∫ 2.0
−2.0e
4.0 1
(r212x+r212y)3 −4.0 1
(r212x+r212y)6
dr12xdr12y
![Page 27: Constructing Scientiï¬c Programs with SymPy](https://reader030.vdocuments.us/reader030/viewer/2022021009/6203aa85da24ad121e4c0d3f/html5/thumbnails/27.jpg)
Results
Method Value Time (seconds)
scipy.integrate.dblquad 285.97597 0.4
Trapezoidal rule (N=1000) 285.97594Python 2.9Shedskin (Python -> C++) 0.5C++ 0.5
![Page 28: Constructing Scientiï¬c Programs with SymPy](https://reader030.vdocuments.us/reader030/viewer/2022021009/6203aa85da24ad121e4c0d3f/html5/thumbnails/28.jpg)
Summary
More information at
http://quantum_mc.blogspot.com
Code available on GitHub
https://github.com/markdewing/sympy/tree/derivation_modeling/sympy/prototype
![Page 29: Constructing Scientiï¬c Programs with SymPy](https://reader030.vdocuments.us/reader030/viewer/2022021009/6203aa85da24ad121e4c0d3f/html5/thumbnails/29.jpg)
Backup
![Page 30: Constructing Scientiï¬c Programs with SymPy](https://reader030.vdocuments.us/reader030/viewer/2022021009/6203aa85da24ad121e4c0d3f/html5/thumbnails/30.jpg)
Input file
![Page 31: Constructing Scientiï¬c Programs with SymPy](https://reader030.vdocuments.us/reader030/viewer/2022021009/6203aa85da24ad121e4c0d3f/html5/thumbnails/31.jpg)
Output - HTML + MathJax
![Page 32: Constructing Scientiï¬c Programs with SymPy](https://reader030.vdocuments.us/reader030/viewer/2022021009/6203aa85da24ad121e4c0d3f/html5/thumbnails/32.jpg)
Code generation output - Python
![Page 33: Constructing Scientiï¬c Programs with SymPy](https://reader030.vdocuments.us/reader030/viewer/2022021009/6203aa85da24ad121e4c0d3f/html5/thumbnails/33.jpg)
Code generation output - C++