Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
feature suggestion: decorator to convert python function to symbolic function
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Timo Kluck  
View profile  
 More options Aug 5 2012, 1:12 pm
From: Timo Kluck <tkl...@gmail.com>
Date: Sun, 5 Aug 2012 10:12:41 -0700 (PDT)
Local: Sun, Aug 5 2012 1:12 pm
Subject: feature suggestion: decorator to convert python function to symbolic function

Hi,

I think that the distinction between

def f(x):
    return x^2

and

f(x) = x^2

is a common source of confusion, and everyone will run into it when trying
to define piecewise functions. It would be possible to introduce a
decorator, say @symbolic_function, that makes sure that

@symbolic_function(x)
def f(x):
    return x^2

makes f a symbolic function. I've done just that in this published
worksheet:
http://www.sagenb.org/home/pub/4919

As you can see, it works with piecewise functions, too. Of course, it
should probably do something sane with multiple variables, class methods,
etc, but it seems possible.

I can probably produce a patch for this, but I would like some feedback
first.

Best regards,
Timo Kluck


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andrea Lazzarotto  
View profile  
 More options Aug 5 2012, 1:14 pm
From: Andrea Lazzarotto <andrea.lazzaro...@gmail.com>
Date: Sun, 5 Aug 2012 19:14:02 +0200
Local: Sun, Aug 5 2012 1:14 pm
Subject: Re: [sage-devel] feature suggestion: decorator to convert python function to symbolic function

2012/8/5 Timo Kluck <tkl...@gmail.com>

> I've done just that in this published worksheet:
> http://www.sagenb.org/home/pub/4919

Unfortunately... "Public worksheets are currently disabled."

:(

--
*Andrea Lazzarotto* - http://andrealazzarotto.com*
*


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Timo Kluck  
View profile  
 More options Aug 5 2012, 1:28 pm
From: Timo Kluck <tkl...@gmail.com>
Date: Sun, 5 Aug 2012 10:28:43 -0700 (PDT)
Local: Sun, Aug 5 2012 1:28 pm
Subject: Re: [sage-devel] feature suggestion: decorator to convert python function to symbolic function

Sorry, I didn't realize that. Here's the code.

---
def symbolic_function(variables, *args, **kwds):
    def result(f):
        def evalf_func(self,x,parent=None):
            if parent == None:
                return f(x)
            else:
                return parent(f(x))
        return function(f.__name__, variables, evalf_func=evalf_func,
*args, **kwds).operator()

    return result

@symbolic_function(x)
def steps(x):
    if x < 0:
        return -x
    elif x < 3:
        return x
    else:
        return 3

steps(5)

n(steps(5))

plot(steps(x),(x,-3,6))


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nils Bruin  
View profile  
 More options Aug 5 2012, 2:08 pm
From: Nils Bruin <nbr...@sfu.ca>
Date: Sun, 5 Aug 2012 11:08:36 -0700 (PDT)
Local: Sun, Aug 5 2012 2:08 pm
Subject: Re: feature suggestion: decorator to convert python function to symbolic function
On Aug 5, 10:28 am, Timo Kluck <tkl...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Simon King  
View profile  
 More options Aug 5 2012, 2:27 pm
From: Simon King <simon.k...@uni-jena.de>
Date: Sun, 5 Aug 2012 18:27:34 +0000 (UTC)
Local: Sun, Aug 5 2012 2:27 pm
Subject: Re: feature suggestion: decorator to convert python function to symbolic function
Hi!

On 2012-08-05, Timo Kluck <tkl...@gmail.com> wrote:

Please excuse a stupid question of someone who doesn't use symbolic
functions or symbolic variables (I rather work with polynomials).

Why should one want to turn a Python function (or method) into a
symbolic function? I guess one couldn't do calculus (differentiate,
integrate) the resulting symbolic function, or can one? So, beyond
calculus, what can one do with a symbolic function that can't be done
with a Python function?

Best regards,
Simon


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Timo Kluck  
View profile  
 More options Aug 5 2012, 2:45 pm
From: Timo Kluck <tkl...@gmail.com>
Date: Sun, 5 Aug 2012 11:45:36 -0700 (PDT)
Local: Sun, Aug 5 2012 2:45 pm
Subject: Re: feature suggestion: decorator to convert python function to symbolic function

> Why should one want to turn a Python function (or method) into a
> symbolic function? I guess one couldn't do calculus (differentiate,
> integrate) the resulting symbolic function, or can one? So, beyond
> calculus, what can one do with a symbolic function that can't be done
> with a Python function?

You can differentiate and integrate, but the result will be formal:

diff(steps(x),x)
D[0](steps)(x)

That's already more than you can do with Python functions. Additionally,
you can specify a derivative_func to the decorator that returns the exact
derivative.

Also, this allows you to use the function in an expression and have it
simplify. For example,

2*steps(x) - steps(x) - steps(x)

will be zero.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Burcin Erocal  
View profile  
 More options Aug 13 2012, 9:25 am
From: Burcin Erocal <bur...@erocal.org>
Date: Mon, 13 Aug 2012 15:25:25 +0200
Local: Mon, Aug 13 2012 9:25 am
Subject: Re: [sage-devel] feature suggestion: decorator to convert python function to symbolic function
Hi Timo,

On Sun, 5 Aug 2012 10:12:41 -0700 (PDT)

This looks great. Can you upload your code to #7636 on trac.

http://trac.sagemath.org/sage_trac/ticket/7636

> As you can see, it works with piecewise functions, too. Of course, it
> should probably do something sane with multiple variables, class
> methods, etc, but it seems possible.

> I can probably produce a patch for this, but I would like some
> feedback first.

On Sun, 5 Aug 2012 10:28:43 -0700 (PDT)

It might be better to use the function_factory method from
sage.symbolic.function_factory instead of the top level function()
method. This would remove the need to specify the names of the
variables, then get the symbolic function using .operator().

You can get the number of arguments with f.__code__.co_argcount.

Cheers,
Burcin


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »