#๐Ÿ”’ Writing a function that takes a set of variables, and an expression in these variables.

9 messages ยท Page 1 of 1 (latest)

dawn sigil
#

I want to write a function that does a certain calculation, which requires a set of variables, and a function of these variables. That is, a function that looks like this

  return some_calculation_using_func```
Is it possible to write a function this way? If not, what's a good alternative?
solemn starBOT
#

@dawn sigil

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

dawn sigil
#

To be clear, when the function is called, I want to be able to input func as an expression of the symbols in vars

dusty wolf
#

pretty sure you just need to define the symbols before then pass it normally? e.g. ```py
x = sp.Symbol('x')
func = x**2 + 2*x + 1
result = calculate(func, x)

if you want a more complete example doing non-trivial operations: <https://claude.ai/public/artifacts/53697402-7aeb-4037-9815-8486ec2c4792>
#

for multiple variables I would probably recommend something like ```py
x, y, z = sympy.symbols("x y z")

def calculate(func, **vars):
return func.subs(vars)

calculate(x+y+z, x=x, y=y, z=z)
calculate(x+y+z, x=1, y=2, z=3)

dawn sigil
#

Hmm, I see

#

This helps me somewhat, thanks!

solemn starBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.