使用rSymPy计算泰勒级数

Gra*_*m G 5 cas r

我一直在试用CAS接口rSymPy到CAS SymPy,它运行得很好.但是,我找不到使用某些更复杂功能的正确语法,例如查找Taylor系列.例如,我尝试了以下内容:

library(rSymPy)
sympy("var('p')")
#
##### Cannot make this work ???
#
sympy("from sympy.mpmath import *")
xt <- sympy("p=taylor(exp, 0, 10)")
Run Code Online (Sandbox Code Playgroud)

但它抛出了错误:

Error in .jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", cl,  : 
  SyntaxError: ("no viable alternative at input '='", ('<string>', 1, 8, '__Rsympy= from sympy.mpmath import *\n'))
Run Code Online (Sandbox Code Playgroud)

任何帮助赞赏.

Gra*_*m G 4

似乎没有明确的泰勒级数可用,但级数函数可用。以下代码有效:

library(rSymPy)
sympy("var('p')")
sympy("var('x')") # or sympy("x = Symbol('x', real=True)")
#
xt <- sympy("p=series(exp(x), x, 0, 10)") # expand about 0 to 10th order
Run Code Online (Sandbox Code Playgroud)

给出了答案:

[1] "1 + x + x**2/2 + x**3/6 + x**4/24 + x**5/120 + x**6/720 + x**7/5040 + x**8/40320 + x**9/362880 + O(x**10)"
Run Code Online (Sandbox Code Playgroud)

我们可以通过将代码修改为来检查这个答案:

library(rSymPy)
sympy("var('p')")
sympy("var('x')") # or sympy("x = Symbol('x', real=True)")
#
xt <- sympy("p=series(exp(x), x, 0, 10)") # expand about 0 to 10th order
# Remove order information
xt0 <- sympy("p.removeO()")
# Test results
x <- 1/3
T1 <- eval(parse(text=xt0)) # Evaluate the result, xt0
T2 <- exp(x)                # The correct value
print(T1-T2)                # Print the error
Run Code Online (Sandbox Code Playgroud)

最后,级数展开的误差为:

[1] -4.811929e-12
Run Code Online (Sandbox Code Playgroud)

我希望这对任何希望使用 R 包 rSymPy 的人有所帮助