Computing complex math equations in python

Posted by dassouki on Stack Overflow See other posts from Stack Overflow or by dassouki
Published on 2010-05-26T11:33:35Z Indexed on 2010/05/26 12:11 UTC
Read the original article Hit count: 359

Filed under:
|
|
|

Are there any libraries or techniques that simplify computing equations ?

Take the following two examples:

  • F = B * { [ a * b * sumOf (A / B ''' for all i ''' ) ] / [ sumOf(c * d * j) ] }

    where:

    F = cost from i to j

    B, a, b, c, d, j are all vectors in the format [ [zone_i, zone_j, cost_of_i_to_j], [..]]

    This should produce a vector F [ [1,2, F_1_2], ..., [i,j, F_i_j] ]

  • T_ij = [ P_i * A_i * F_i_j] / [ SumOf [ Aj * F_i_j ] // j = 1 to j = n ]

    where:

    n is the number of zones

    T = vector [ [1, 2, A_1_2, P_1_2], ..., [i, j, A_i_j, P_i_j] ]

    F = vector [1, 2, F_1_2], ..., [i, j, F_i_j]

    so P_i would be the sum of all P_i_j for all j and Aj would be sum of all P_j for all i

I'm not sure what I'm looking for, but perhaps a parser for these equations or methods to deal with multiple multiplications and products between vectors?

To calculate some of the factors, for example A_j, this is what i use

from collections import defaultdict

A_j_dict = defaultdict(float)
for A_item in TG: A_j_dict[A_item[1]] += A_item[3]

Although this works fine, I really feel that it is a brute force / hacking method and unmaintainable in the case we want to add more variables or parameters. Are there any math equation parsers you'd recommend?

Side Note: These equations are used to model travel. Currently I use excel to solve a lot of these equations; and I find that process to be daunting. I'd rather move to python where it pulls the data directly from our database (postgres) and outputs the results into the database. All that is figured out. I'm just struggling with evaluating the equations themselves.

Thanks :)

© Stack Overflow or respective owner

Related posts about python

Related posts about math