Reputation: 179
I am using sage 9.4 with python 3.9.5. and I try to define a function that returns a series of Callable Symbolic Expressions (functions). The series can be defined as follows:
var('x')
f(x) = 0
for n in range(5):
tmp = function('f'+str(n))(x)
f += tmp*x^n
print(f)
which delivers
x |--> x^4*f4(x) + x^3*f3(x) + x^2*f2(x) + x*f1(x) + f0(x)
However, when I tried to wrap this in a function
def foo (var, N):
f(var) = 0
for n in range(N):
tmp = function('f'+str(n))(x)
f += tmp*x^n
return f
var('x')
f = foo(x,5)
print(f)
it fails to execute with
/home/users/.../foo.sage.py:8: DeprecationWarning: Substitution using function-call syntax and unnamed arguments is deprecated and will be removed from a future release of Sage; you can use named arguments instead, like EXPR(x=..., y=...)
See http://trac.sagemath.org/5930 for details.
__tmp__=var("var"); f = symbolic_expression(_sage_const_0 ).function(var)
Traceback (most recent call last):
File "sage/symbolic/expression.pyx", line 3543, in sage.symbolic.expression.Expression.coerce_in (build/cythonized/sage/symbolic/expression.cpp:24454)
TypeError: Cannot convert str to sage.symbolic.expression.Expression
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/users/.../foo.sage.py", line 14, in <module>
f = foo(x,_sage_const_5 )
File "/home/users/.../foo.sage.py", line 8, in foo
__tmp__=var("var"); f = symbolic_expression(_sage_const_0 ).function(var)
File "sage/symbolic/expression.pyx", line 5775, in sage.symbolic.expression.Expression.__call__ (build/cythonized/sage/symbolic/expression.cpp:33252)
File "sage/symbolic/ring.pyx", line 1146, in sage.symbolic.ring.SymbolicRing._call_element_ (build/cythonized/sage/symbolic/ring.cpp:12914)
File "sage/symbolic/expression.pyx", line 5631, in sage.symbolic.expression.Expression.substitute (build/cythonized/sage/symbolic/expression.cpp:32326)
File "sage/symbolic/expression.pyx", line 3545, in sage.symbolic.expression.Expression.coerce_in (build/cythonized/sage/symbolic/expression.cpp:24506)
File "sage/structure/parent_old.pyx", line 179, in sage.structure.parent_old.Parent._coerce_ (build/cythonized/sage/structure/parent_old.c:3805)
File "sage/structure/parent.pyx", line 1207, in sage.structure.parent.Parent.coerce (build/cythonized/sage/structure/parent.c:10940)
TypeError: no canonical coercion from <class 'str'> to Symbolic Ring
However, I found a workaround with:
def bar(var,N,zero_callable):
f = zero_callable
for n in range(N):
tmp = function('f'+str(n))(var)
f += tmp*var^n
return f
f(x) = 0
expr = bar(x,5,f)
print(expr)
This solution is inconvenient from my point of view because of the need to pass a "zero" as a function argument that is only used to initialize a variable inside the function. My first question is if there is any better solution to this problem. I am relatively new to Sage/Python and my second question is why the function foo does not work because I am doing the same as above but just wrapped it in def enviroment.
Upvotes: 1
Views: 589
Reputation: 136
I think, the root cause of the problem is no relationship between str
type and Symbolic Ring
,
I suppose what you are trying to reach is the following.
def foo(x, N):
f(x) = sum([function('f'+str(n))(x) for n in range(N)])
return f
x = var('x')
y = var('y')
f = foo(y, 20)
g = foo(x, 10)
f
y |--> y^19*f19(y) + y^18*f18(y) + y^17*f17(y) + y^16*f16(y) + y^15*f15(y) + y^14*f14(y) + y^13*f13(y) + y^12*f12(y) + y^11*f11(y) + y^10*f10(y) + y^9*f9(y) + y^8*f8(y) + y^7*f7(y) + y^6*f6(y) + y^5*f5(y) + y^4*f4(y) + y^3*f3(y) + y^2*f2(y) + y*f1(y) + f0(y)
g
x |--> x^9*f9(x) + x^8*f8(x) + x^7*f7(x) + x^6*f6(x) + x^5*f5(x) + x^4*f4(x) + x^3*f3(x) + x^2*f2(x) + x*f1(x) + f0(x)
g.variables()
(x,)
f.variables()
(y,)
Upvotes: 1