0

I have a function to return value a based on a PolyFit, depending on values a0, c, c0:

import sympy

a = Symbol("a")
a0 = Symbol("a0")
c = Symbol("c")
c0 = Symbol("c0")
t = Symbol("t")

a_func = Poly(0, a0, c, c0) 
coeff = np.array([-0.71424954,  1.7335939 , -1.76528173,  1.2361201 ,  0.00255529])  
num_a_coeff = len(coeff)
for ii in xrange(num_a_coeff):

    a_func += coeff[ii] * ((c-c0)/a0)**(num_a_coeff-1-ii)

a_func = a0 + (a_func * c)

it is not yet nicely formated like a typical polynomial expression, num_a_coeff is in my case 5. I want to implement this a_func in another expression:

multiFit_coeff = np.array([944210317e-03,-280710762e-03,378400594e-04])
# gives power of 1, a, c, t
multiFit_power = np.array([[5,0,0,0],[4,3,2,0],[,4,1,0]])
main_func = []
num_coeff = len(multiFit_coeff)
for i in xrange(num_coeff):

    temp = multiFit_coeff[i] * 
        (1**multiFit_power_less[i][0] * 
        a_func**inc.multiFit_power[i][1] * 
        c**multiFit_power[i][2] *
        t**multiFit_power[i][3])

    main_func.append(temp)

So I have an expression which depends on a0, c0, c and t. The maximum power of each variable (a_func, c and t) in the last expression is 5. So I have eg an expression like:

a_func**4 * c

Since at the end I would like to have a polynomial of c (a0, c0, t are constants), I tried to use

main_func.append(sympy.polys.poly_from_expr(temp, c))

in the last for loop to save the nicely formated polynomial into the list. But the calculation does not come to an end when I run into the case that a_func is powered by 3 or bigger values... Any ideas how to solve this issue? I thought this calculation shouldnt be that hard for sympy. Many thanks to any suggestions. This is my first question btw :-)

1 Answers1

0

I have a hard time understanding what is going on in your code dump, but if I understand what you want correctly, I think you just want collect(a_func, c), which will pull out all the coefficients of a_func with respect to c.

asmeurer
  • 86,894
  • 26
  • 169
  • 240
  • Thank you for your answer, but this function doesnt do anything in my case it just returns the same expression. poly_from_expr works but it takes too long for very big equations – user2489877 Jun 17 '13 at 01:43
  • 1
    If this is not what you want then I suggest you try to make your question clearer. Put a sample of the expression you have and what you want to have (instead of expecting people to try to figure out what is going on from your code). – asmeurer Jun 17 '13 at 01:46