Just moozing

Before you can check your notes, you must make them…

Sympy – symbolic math with python

leave a comment »

Ever since the first time I lost a constant or some signs while doing equations manipulations, I have always wanted to do algebra on the PC. The measurements on the temperature of a lamp filament seemed to be a good test of Sympy.

Redoing the calculation from my previous post, I end up with the following short python program that does all the symbolic calculations based on my base equations. It is easy and it works šŸ™‚

(click to see the code).


'''
Created on 13 Feb 2013

@author: moz
'''
from sympy.utilities.lambdify import lambdify
from sympy.core.symbol import var
from sympy.core.relational import Eq

def LampSetupEquations( substitutionslist = {}, PrintEquations = True ):
    # all vars
    var('I VL Vin Pf alpha1 a1 alpha2 a2 T Rf' )
    var( 'alpha R_0 T_0 c RL' )

    # def all equations
    orig_reps = {
        Vin : alpha2 * a2,
        VL : alpha1 * a1,
        I : VL/RL,
        Rf : (Vin - VL)/I,
        T : 1/c*(Rf/R_0 -1) + T_0,
        Pf : (Vin-VL)*I,
    }

    if PrintEquations:
        print "The original equations"
        for eq in orig_reps.keys():
            print "- %s"%Eq(eq, orig_reps[eq])

    # substituting into each other
    eqlist = {}
    for eq in orig_reps.keys():
        eqlist[eq] = orig_reps[eq].subs( orig_reps )

    #insert any other math operations her šŸ™‚

    # substituting constants
    for eq in eqlist.keys():
        eqlist[eq] = eqlist[eq].subs( substitutionslist ).simplify()

    # converting the equations to
    fctlist = {}
    for eq in eqlist.keys():
        fctlist[str(eq)] = lambdify( (a1, a2), eqlist[eq] )

    return fctlist

if __name__ == "__main__":
    consts = { 'alpha1': 1.93e-2, 'alpha2': 1.93e-2, 'RL':12.0,
              'R_0': 5.3, 'T_0': 14+173.15, 'c': 4.5e-3 }

    eqs = LampSetupEquations( consts )

    # this is to be changed to the actual measurements...
    a1_measured = range(0, 100)
    a2_measured = range(0, 100)*2

    for i in range( 0, len( a1_measured ) ):
        print eqs['Vin']( float(a1_measured[i]), float(a2_measured[i]) )

Some comments

  • Lines 15-22 is where we define our basic equations.
  • Lines 30-33 is where we substitute them into each other. It would this step that should be specified in greater detail if the equations had not been so nice.
  • Lines 37-39, we substitute whatever constants we have specified as parameter.
  • Lines 41-46, we convert the resulting equations to python functions, and returns the list.

The end result is that we have specified the equations in math-speak, done the algebra and returns python functions that may be used everywhere as any other python function. That is cool.

The feature list also include stuff related to physics like units and something called “quantum”. That will be something for another post.

Next step is to get the measurements data, use the functions, and pipe it into matplotlib. It seems to be a very promising way of combining math, measurements and visualization.

Advertisements

Written by moozing

February 16, 2013 at 09:00

Posted in Tech

Tagged with ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: