Last active
March 19, 2017 20:53
-
-
Save aijony/8675b9b348a7c510d634f768d5ad7e8e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sage.symbolic.expression import is_Expression, is_SymbolicEquation | |
def name(func_name, expr): | |
r""" | |
Name a an unnamed symbolic function or expression. | |
INPUT: | |
- ``func_name`` -- string (default: 1) The to be given name | |
if the function | |
- ``expr`` -- expression (default: 2) the | |
given expression to be named | |
OUTPUT: | |
The expression in the form of a named function ``func_name``. | |
.. SEEALSO:: | |
:mod:'~sage.symbolic.expression.Expression' | |
EXAMPLES: | |
Name functions:: | |
sage: g(x,y) = x + sin(y) | |
sage: g | |
(x, y) |--> x + sin(y) | |
sage: g = name('g', g) | |
sage: g | |
g(x, y) == x + sin(y) | |
Works with unnamed arguments:: | |
sage: var('x, y') | |
(x, y) | |
sage: h = x/y | |
sage: h | |
x/y | |
sage: h = name('h', h) | |
sage: h | |
h(x, y) == x/y | |
No previous function needed:: | |
sage: g = name('g', x^2) | |
sage: g | |
g(x) == x^2 | |
Most things are independent:: | |
sage: f(x) = 2*x | |
sage: h = name('w', f) | |
sage: h | |
w(x) == 2*x | |
TESTS:: | |
Check combination of named and unnamed:: | |
sage: f(x) = x + y | |
sage: f | |
x |--> x + y | |
sage: f = name('f', f) | |
sage: f | |
f(x) == x + y | |
Change up the syntax:: | |
sage: f(x) = x | |
sage: name('f', f(x)) | |
f(x) == x | |
#Thanks to Nils Bruin for inspiration/code of how to fix this issue. | |
Detect overwrite:: | |
sage:f(x) = x; name('f', f); f | |
f(x) == x | |
f |--> x | |
It is an error to give a named function:: | |
sage: name('f', 2) | |
Traceback (most recent call last): | |
... | |
TypeError: 2 is not an expression or unnamed function, so it cannot be named | |
It is an error to give a something that isn't an expression:: | |
sage: f(x) = x | |
sage: f = name('f', f) | |
sage: f = name('f', f) | |
Traceback (most recent call last): | |
... | |
TypeError: f(x) == x is a named function, name will not be overwritten; possibly try name('f', f(x) == x.right()) | |
It is an error to give a non-string for name:: | |
sage: name(12.2, x^2) | |
Traceback (most recent call last): | |
... | |
TypeError: 12.2000000000000 is not a string | |
""" | |
if not is_Expression(expr): | |
msg = "{} is not an expression or unnamed function, so it cannot be named" | |
raise TypeError(msg.format(expr)) | |
elif is_SymbolicEquation(expr): | |
msg = "{} is a named function, name will not be overwritten; possibly try name('{}', {}.right())" | |
raise TypeError(msg.format(expr, func_name, expr)) | |
elif not isinstance(func_name, (str, unicode)): | |
msg = "{} is not a string" | |
raise TypeError(msg.format(func_name)) | |
else: | |
newFunc = sage.symbolic.function_factory.function(func_name)(*(expr.arguments())) | |
#Checks if expression has unnamed arguments | |
#Names the arguments of a function-call with unnamed arguments | |
if(expr.parent() == SR): | |
#Puts variables in definition to prevent substitution using function-call syntax and unnamed arguments. | |
#See http://trac.sagemath.org/5930 for details. | |
variableDefinition = {} | |
for n in (expr.args()): | |
variableDefinition.setdefault(n, n) | |
return newFunc == expr(variableDefinition) | |
else: | |
# *expr.args() - converts tuple into parameters | |
return newFunc == expr(*expr.args()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment