Real Functions

78 views
Skip to first unread message

brombo

unread,
Sep 19, 2015, 12:58:53 PM9/19/15
to sympy
How do I define a real function of a real variable?

X = symbols('x y z',real=True)

f = Function('f',real=True)(*X)

"real=True" in Function doesn't do anything.

Pranit Bauva

unread,
Sep 19, 2015, 1:20:29 PM9/19/15
to sympy
Brombo,
         The parameter 'real' in Function is an optional parameter. So if you don't include that parameter, it is assumed that the function is a real variable function. If you want to specify otherwise then you can pass in the value 'real=False'. But it is a good practice specifying explicitly that the function is a real valued function. For more info you can visit the docs. link : https://docs.python.org/3/tutorial/controlflow.html#default-argument-values

Pranit Bauva

brombo

unread,
Sep 19, 2015, 2:47:08 PM9/19/15
to sympy

Let me clarify.  If I use the above to define f then

re(f) returns re(f) not f and
im(f) returns im(f) not 0.

f is being treated as a fuction with an arbitrary real and imaginary part.
How do I avoid this so that
f = re(f)
0= im(f)

 

Björn Dahlgren

unread,
Sep 19, 2015, 3:21:56 PM9/19/15
to sympy


On Saturday, 19 September 2015 20:47:08 UTC+2, brombo wrote:

Let me clarify.  If I use the above to define f then

re(f) returns re(f) not f and
im(f) returns im(f) not 0.


Here is a work around I've used:
def RealFunction(*args, **kwargs):
    instance
= Function(*args, **kwargs)
    instance
.is_real = True
   
return instance

 

brombo

unread,
Sep 19, 2015, 3:47:56 PM9/19/15
to sympy
Your method works for a function, but not the derivative of a function -

x = Symbol('x',real=True)

f = RealFunction('f')(x)

print re(f)
f
print im(f)
0

df = f.diff(x)
print re(df)
re(df)
print im(df)
im(df)

What should happen is that re(df) = df and im(df) = 0, but is doesn't happen.  Any suggestions?





On Saturday, September 19, 2015 at 12:58:53 PM UTC-4, brombo wrote:

Aaron Meurer

unread,
Sep 19, 2015, 6:00:11 PM9/19/15
to sy...@googlegroups.com
The only way to do it presently is to subclass Function manually:

class f(Function):
is_real = True

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/6a732f8b-c09a-440e-b524-145343e1a499%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

brombo

unread,
Sep 19, 2015, 6:22:30 PM9/19/15
to sympy
Could you elucidate further or give me a link.  I  don't have a clue to implement what you suggested.

Christophe Bal

unread,
Sep 19, 2015, 6:34:32 PM9/19/15
to sympy-list
Hello.

Have you tried this ?


from sympy import *

class f(Function):
    is_real = True

x = Symbol('x',real=True)

df = f.diff(x)

print(re(df))
print(im(df))




Christophe BAL
Enseignant de mathématiques en Lycée et développeur Python amateur
---
French math teacher in a "Lycée" and Python amateur developer

Alan Bromborsky

unread,
Sep 19, 2015, 7:37:26 PM9/19/15
to sy...@googlegroups.com
Then how do I instanciate a real function of several variables as in

X = symbols('x y z',real=True)

F = Function('F')(*X)

Alan Bromborsky

unread,
Sep 19, 2015, 7:44:46 PM9/19/15
to sy...@googlegroups.com
Tried the following -

class f(Function):
    is_real = True

x = symbols('x',real=True)

F = f('F',x)

print f
print re(f)
print im(f)

df = F.diff(x)

print re(df)
print im(df)

and got -

f
f
0
re(D{x}f)
im(D{x}f)

Again it works for the function, but not the derivatives of the function.

Aaron Meurer

unread,
Sep 22, 2015, 3:34:26 PM9/22/15
to sy...@googlegroups.com
That should probably be considered a bug (Derivative doesn't know that
is_real should be True if the function's is_real is True). It would be
best to just fix the bug (and it probably isn't hard to fix), but to
work around it, you'd have to define _eval_derivative or fdiff to
return another custom Function subclass for the derivative that has
is_real set to True.

Aaron Meurer
> https://groups.google.com/d/msgid/sympy/CALOxT-naJ7unWN3qqO62Ntak%2Br1nv9yo5%3DOJx8N2CGtMu2m6Wg%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages