43

I've been trying this now for hours. I think I don't understand a basic concept, that's why I couldn't answer this question to myself so far.

What I'm trying is to implement a simple mathematical function, like this:

f(x) = x**2 + 1

After that I want to derive that function.

I've defined the symbol and function with:

x = sympy.Symbol('x')
f = sympy.Function('f')(x)

Now I'm struggling with defining the equation to this function f(x). Something like f.exp("x**2 + 1") is not working.

I also wonder how I could get a print out to the console of this function after it's finally defined.

3
  • 2
    i think you want lambdas? i am not familiar with sympy though.
    – Amit Gold
    May 8, 2016 at 12:59
  • 1
    I would love to use lambdas, but the task says, that I have to solve this with sympy.
    – Robin
    May 8, 2016 at 13:00
  • Perhaps you are looking for f = x**2 + 1?
    – unutbu
    May 8, 2016 at 13:05

6 Answers 6

60

sympy.Function is for undefined functions. Like if f = Function('f') then f(x) remains unevaluated in expressions.

If you want an actual function (like if you do f(1) it evaluates x**2 + 1 at x=1, you can use a Python function

def f(x):
    return x**2 + 1

Then f(Symbol('x')) will give a symbolic x**2 + 1 and f(1) will give 2.

Or you can assign the expression to a variable

f = x**2 + 1

and use that. If you want to substitute x for a value, use subs, like

f.subs(x, 1)
2
  • 1
    Thanks for this. Do you still need to do f = Function('f') before defining the function or is that implicit from the python function definition?
    – Bill
    Sep 10, 2019 at 15:04
  • 2
    @Bill def f(...) sets the variable f to be the Python function defined by the def. If you do f = Function('f'), that will override the variable f to be the SymPy object Function('f'). The two options I show here do not use Function at all. That is only if you want something that is completely unevaluated. See also the section in the SymPy tutorial on symbols.
    – asmeurer
    Sep 10, 2019 at 19:45
20

Here's your solution:

>>> import sympy
>>> x = sympy.symbols('x')
>>> f = x**2 + 1
>>> sympy.diff(f, x)
2*x
3
  • For what is sympy.Function uses, when not for defining functions?
    – Robin
    May 8, 2016 at 14:19
  • I suppose it's a base class for a whole range of standard functions.
    – enedil
    May 8, 2016 at 14:23
  • @Robin sympy.Function is for undefined functions. Like if f = Function('f') then f(x) remains unevaluated in expressions.
    – asmeurer
    May 9, 2016 at 17:25
6

Another possibility (isympy command prompt):

>>> type(x)
<class 'sympy.core.symbol.Symbol'>
>>> f = Lambda(x, x**2)
>>> f
     2
x ↦ x 
>>> f(3)
9

Calculating the derivative works like that:

>>> g = Lambda(x, diff(f(x), x))
>>> g
x ↦ 2x
>>> g(3)
6
1

Have a look to: Sympy how to define variables for functions, integrals and polynomials

You can define it according to ways:

  • a python function with def as describe above
  • a python expression g=x**2 + 1
1
  • From Review: Hi, while links are great way of sharing knowledge, they won't really answer the question if they get broken in the future. Add to your answer the essential content of the link which answers the question. In case the content is too complex or too big to fit here, describe the general idea of the proposed solution. Remember to always keep a link reference to the original solution's website. See: How do I write a good answer? Nov 24, 2019 at 14:08
0

Creating a Undefined Function:

from sympy import *
f = symbols("f", cls=Function)

-2

I recommended :

  1. first, define a symbolic variable
x = sympy.symbols('x')
  1. second, define a symbolic function
f = sympy.Function('f')(x)
  1. define a formula
 f = x**x+1

if you have so many variable can use this function

 def symbols_builder(arg):
     globals()[arg]=sp.symbols(str(arg))

if you have so many functions can use this function

def func_build(name, *args):
    globals()[name]=sp.Function(str(name))(args)
1
  • 1
    At step 3 you reassign variable f, meaning step 2 effect is lost. And actually step 2 is not needed, step 3 is enough.
    – mins
    Sep 9, 2022 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.