4

In order to calculate derivatives and other expressions I used the sympy package and said that T = sy.Symbol('T') now that I have calculated the right expression:

E= -T**2*F_deriv_T(T,rho)

where

def F_deriv_rho(T,rho):
    ret = 0
    for n in range(5):
        for m in range(4):
            inner= c[n,m]*g_rho_deriv_rho_np*g_T_np
        ret += inner
    return ret

that looks like this:

F_deriv_rho: [0.0 7.76971e-5*T 0.0001553942*T**2*rho T*(-5.14488e-5*log(rho) - 5.14488e-5)*log(T) + T*(1.22574e-5*log(rho)+1.22574e-5)*log(T) + T*(1.89488e-5*log(rho) + 1.89488e-5)*log(T) + T(2.29441e-5*log(rho) + 2.29441e-5)*log(T) + T*(7.49956e-5*log(rho) + 7.49956e-5)*log(T) T**2*(-0.0001028976*rho*log(rho) - 5.14488e-5*rho)*log(T) + T**2*(2.45148e-5*rho*log(rho) + 1.22574e-5*rho)*log(T) + T**2*(3.78976e-5*rho*log(rho) + 1.89488e-5*rho)*log(T) + T**2*(4.58882e-5*rho*log(rho) + 2.29441e-5*rho)*log(T) + T**2*(0.0001499912*rho*log(rho) + 7.49956e 5*rho)*log(T)]

with python I would like to change T (and rho) as a symbol to a value. How could I do that?

So, I would like to create 10 numbers like T_def = np.arange(2000, 10000, 800)and exchange all my sy.symbol(T) by iterating through the 10 values I created in the array.

Thanks for your help

Bjoern Dahlgren
  • 931
  • 8
  • 18
Shaun
  • 461
  • 3
  • 5
  • 22
  • Your example code is not self-contained, e.g. you are referencing things such as ``g_T_np`` making it harder to help. But from what I gather you might be interested in "lambdify" which creates a callback for numerical evaluation based on a symbolic expression. – Bjoern Dahlgren Nov 16 '16 at 09:39
  • @BjoernDahlgren: I know but the rest of the code isn't important for my question and might be more confusing. All I want is to change the symbolic T and replace it with a numerical value. But i'm checking lamdify right now and it might be very helpful! thank you very much – Shaun Nov 16 '16 at 15:23

1 Answers1

10

I have found the solution according to this post:

How to substitute multiple symbols in an expression in sympy?

by usings "subs":

>>> from sympy import Symbol
>>> x, y = Symbol('x y')
>>> f = x + y
>>> f.subs({x:10, y: 20})
>>> f
    30

There's more for this kinda thing here: http://docs.sympy.org/latest/tutorial/basic_operations.html

EDIT: A faster way would be by using "lamdify" as suggested by @Bjoern Dahlgren

Community
  • 1
  • 1
Shaun
  • 461
  • 3
  • 5
  • 22