C#,IronPython和Sympy:unicode_escape_decode()不带参数 [英] C#, IronPython, and Sympy: unicode_escape_decode() takes no arguments
问题描述
我试图使用C#中的IronPython创建一个静态方法来简化sympy中的数学表达式.我已经能够测试并确认IronPython中简单的python语句的执行.问题是当我尝试运行下面的代码时,出现以下错误:
I am trying to create a static method for simplifying mathematical expressions in sympy using IronPython from C#. I have already been able to test and confirm the execution of simple python statements from IronPython. The problem is when I attempt to run the code below, I get the following error:
unicode_escape_decode() takes no arguments (1 given)
我一直在网上寻找解释,并设法找到另一个有相同问题的用户,但似乎无法解决: 在F#中运行python时,Unicode_escape_decode()不会出现参数错误
I have looked online for an explanation and managed to find another user with the same problem but it doesn't seem to be resolved: Unicode_escape_decode() takes no arguments error when run python in F#
这是我的下面的代码:
public static MathExpression SimplifyExpression(MathExpression Input)
{
//Make sure the mathematics engine is initialized
if (!MathEngine.IsInitialized()) throw new Exception("Unable to simplify expression...the mathematics engine isn't initialized!");
//Convert the MathExpression object into a sympy string
string SympyString = Input.ToSympyString();
//Analyze the input for variables
List<Variable> Variables = Input.GetContainedVariables();
if (Variables == null) return Input;
if (Variables.Count < 1) return Input;
//Strings used
string NL = "\n";
//string NL = Environment.NewLine;
string SympyScript = string.Empty;
//Import the required libraries
SympyScript += "from sympy import *" + NL;
SympyScript += "import clr" + NL;
SympyScript += "from System import String" + NL + NL;
//Create the neccessary variables
foreach (Variable v in Variables)
{
SympyScript += String.Format("{0} = symbols('{0}')", v.ToSympyString()) + NL;
}
SympyScript += NL;
//Set the new variable "expr" as the expression string
SympyScript += "expr = " + SympyString + NL;
SympyScript += "expr_simp = simplify(expr)" + NL;
//Convert the sympy expression back into a string
SympyScript += "result = clr.Convert(expr_simp, System.String)" + NL;
//Execute the script
var TempScope = MathEngine.PythonEngine.CreateScope();
var ExecutionScript = MathEngine.PythonEngine.CreateScriptSourceFromString(SympyScript);
ExecutionScript.Execute(TempScope);
//MathEngine.Execute(SympyScript);
//Obtain the simplified string after the script is executed
string SimplifiedString = TempScope.GetVariable("result");
//Convert the simplified sympy string back into a MathExpression object
var expression = SympyStringToExpression(SimplifiedString);
//Return the result
return expression;
}
请务必注意,"MathEngine.PythonEngine"指向Microsoft.Scripting.Hosting.ScriptEngine对象
It is important to note that "MathEngine.PythonEngine" points to a Microsoft.Scripting.Hosting.ScriptEngine object
以下是我的搜索路径:
C:\Python34\Lib\site-packages
C:\Program Files (x86)\IronPython 2.7\Lib
先谢谢您了:)
像上面提到的其他问题一样,我尝试在Python脚本的顶部添加以下几行:
Like the other question referenced above, I tried my crack at adding the following lines to the top of my python script:
import codecs
def my_unicode_escape_decode(x):
return x
codecs.unicode_escape_decode = my_unicode_escape_decode
但是,与该问题的提问者不同的是,它似乎在运行了 VERY 很长时间后才开始工作,直到它最终停止并引发错误:
Unlike the asker of that question however, it seems to work at first after running for a VERY long time until it finally stops and throws the error:
name 'System' is not defined
我将继续寻找可能的问题解决方案,但似乎sympy与当前版本的IronPython不兼容(反之亦然).
I will continue to look for possible solutions to the problem but it seems as though sympy just isn't compatible with IronPython at its current version (and vice versa).
推荐答案
这是您的错误:
SympyScript += "result = clr.Convert(expr_simp, String)" + NL;
//SympyScript += "result = clr.Convert(expr_simp, System.String)" + NL;
还要在此处
这篇关于C#,IronPython和Sympy:unicode_escape_decode()不带参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!