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
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.
> ------=_Part_23_12817751.1344186761653
> 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.
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?
> 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,
Timo Kluck <tkl...@gmail.com> wrote:
> 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
> 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.
> @symbolic_function(x)
> def steps(x):
> if x < 0:
> return -x
> elif x < 3:
> return x
> else:
> return 3
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.