SageMath:定义一系列函数(Callable Symbolic Expressions)
SageMath: Define a series of functions (Callable Symbolic Expressions)
我将 sage 9.4 与 python 3.9.5 一起使用。我尝试定义一个 returns 一系列可调用符号表达式(函数)的函数。该系列可以定义如下:
var('x')
f(x) = 0
for n in range(5):
tmp = function('f'+str(n))(x)
f += tmp*x^n
print(f)
送货
x |--> x^4*f4(x) + x^3*f3(x) + x^2*f2(x) + x*f1(x) + f0(x)
然而,当我试图将它包装在一个函数中时
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)
无法执行
/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
但是,我找到了一个解决方法:
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)
从我的角度来看,此解决方案不方便,因为需要将“零”作为函数参数传递,而该函数参数仅用于初始化函数内部的变量。我的第一个问题是是否有更好的解决方案来解决这个问题。我对 Sage/Python 比较陌生,我的第二个问题是为什么函数 foo 不起作用,因为我做的和上面一样,只是把它包装在 def 环境中。
我认为,问题的根本原因是str
类型和Symbolic Ring
之间没有关系,
我想你想要达到的是以下内容。
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,)
我将 sage 9.4 与 python 3.9.5 一起使用。我尝试定义一个 returns 一系列可调用符号表达式(函数)的函数。该系列可以定义如下:
var('x')
f(x) = 0
for n in range(5):
tmp = function('f'+str(n))(x)
f += tmp*x^n
print(f)
送货
x |--> x^4*f4(x) + x^3*f3(x) + x^2*f2(x) + x*f1(x) + f0(x)
然而,当我试图将它包装在一个函数中时
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)
无法执行
/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
但是,我找到了一个解决方法:
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)
从我的角度来看,此解决方案不方便,因为需要将“零”作为函数参数传递,而该函数参数仅用于初始化函数内部的变量。我的第一个问题是是否有更好的解决方案来解决这个问题。我对 Sage/Python 比较陌生,我的第二个问题是为什么函数 foo 不起作用,因为我做的和上面一样,只是把它包装在 def 环境中。
我认为,问题的根本原因是str
类型和Symbolic Ring
之间没有关系,
我想你想要达到的是以下内容。
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,)