1

I've got a math equation that I want to visually record the substitution of.

The equation is y = h * f(t + h, h + f(t, h)), where f(x, y) = x + y - 1 I want to substitute f into y, such that I get:
y = h * f(t + h, h + (t + h - 1))
y = h * (t + h + h + (t + h - 1) - 1)

I've had issues with replace not allowing me to do multi-parameter substitution

I don't have much code, since I'm not sure how to implement it

from sympy import *

f = Function('f')(x, y)
eqn = h * f(t + h, h + f(t, h))

Thanks

2 Answers2

0

sympy.Function is used for declaring undefined functions but in your case the function is known.

The following code seems to work fine over here

from sympy import *
x,y,t,h = symbols('x y t h')

def f(x,y):
    return x + y - 1

y = h * f(t+h,h+f(t,h))
y = expand(y)

display(y)

The role of the expand function was to work out the outer multiplication by h in the definition of y.

You can run it in a Jupyter notebook or as an alternative use the print or display function, I get the following result:

enter image description here

wsdookadr
  • 2,584
  • 1
  • 21
  • 44
0

Extending average's answer -- Their solution works perfectly if the function is known.

To make it work for a function that's input from the user, you need to do this:

function = input()
def f(x, y, evaluate = False):
    eqn = sympify(function)
    if evaluate:
        eqn = eqn.subs([("x", x), ("y", y)])
    return eqn

y = h + f(h, t, True)

This way, if the user inputs "x ** y" for f, y will expand to h + h ** t