FooBar
FooBar

Reputation: 16528

Sympy: How to substitute function with function?

Say I have a function like this:

In [44]: eta(p)
Out[44]: 
                                ⎛                  Λ⋅(-F(p) + 1)    ⎞  -Λ
                1               ⎝-Λ⋅(-F(p) + 1) + ℯ              - 1⎠⋅ℯ  
1 - ───────────────────────── + ─────────────────────────────────────────
      ⎛      Λ    ⎞  -2⋅Λ                 ⎛     -Λ⎞                      
    - ⎝-Λ + ℯ  - 1⎠⋅ℯ     + 1             ⎝1 - ℯ  ⎠⋅(-F(p) + 1)  

I would like to plot the function under some simplifying assumptions. I know that I can use subs() to replace the variables like Lambda with real numbers. That leaves me with F(p).

How can I replace the sympbolic F(p) with an actual numerical function?

Upvotes: 3

Views: 605

Answers (2)

MaxNoe
MaxNoe

Reputation: 15017

You can use lambdify() and make it use numpy.

Here is a simplified example:

import sympy as sy
import numpy as np
import matplotlib.pyplot as plt


t, omega = sy.symbols('t omega')

func = sy.sin(omega * t)

func_sub = func.subs({'omega': 2 * sy.pi})

numpy_func = sy.lambdify(t, func_sub, modules='numpy')

px = np.linspace(0, 1, 1000)

plt.plot(px, numpy_func(px))
plt.show()

Upvotes: 1

eph
eph

Reputation: 2028

You can define the numerical function with sympy.utilities.lambdify.implemented_function, and use subs to replace.

Upvotes: 0

Related Questions