问题
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)
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#
Here is my code below:
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;
}
It is important to note that "MathEngine.PythonEngine" points to a Microsoft.Scripting.Hosting.ScriptEngine object
The following are my search paths:
C:\Python34\Lib\site-packages
C:\Program Files (x86)\IronPython 2.7\Lib
Thanks in advance :)
EDIT: 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
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
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).
回答1:
Here is your error:
SympyScript += "result = clr.Convert(expr_simp, String)" + NL;
//SympyScript += "result = clr.Convert(expr_simp, System.String)" + NL;
Find also my complete demo here
来源:https://stackoverflow.com/questions/29683868/c-ironpython-and-sympy-unicode-escape-decode-takes-no-arguments