C#、IronPython 和 Sympy : unicode_escape_decode() takes no arguments

标签 c# python ironpython sympy

我正在尝试使用 C# 中的 IronPython 创建一个静态方法来简化 sympy 中的数学表达式。我已经能够测试并确认来自 IronPython 的简单 python 语句的执行。问题是当我尝试运行下面的代码时,出现以下错误:

unicode_escape_decode() takes no arguments (1 given)

我在网上寻找解释并设法找到另一个有同样问题的用户,但似乎没有得到解决: 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 对象

以下是我的搜索路径:

C:\Python34\Lib\site-packages
C:\Program Files (x86)\IronPython 2.7\Lib

提前致谢:)

编辑: 与上面提到的其他问题一样,我尝试将以下几行添加到我的 python 脚本的顶部:

import codecs
def my_unicode_escape_decode(x):
    return x
codecs.unicode_escape_decode = my_unicode_escape_decode

然而,与那个问题的提问者不同,它似乎在运行了很长时间非常之后才开始工作,直到它最终停止并抛出错误:

name 'System' is not defined

我将继续寻找问题的可能解决方案,但似乎 sympy 与当前版本的 IronPython 不兼容(反之亦然)。

最佳答案

这是你的错误:

SympyScript += "result = clr.Convert(expr_simp, String)" + NL;
//SympyScript += "result = clr.Convert(expr_simp, System.String)" + NL;

也可以找到我的完整演示 here

关于C#、IronPython 和 Sympy : unicode_escape_decode() takes no arguments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29683868/

相关文章:

ironpython - Spotfire IronPython 脚本滚动过滤器并每一步更新可视化(通过日期范围播放按钮)

c# - 如何从 DataGridView 构建 DataTable?

c# - 如何在 C# 中获取以前的 url?

c# - 通过引用传递表单对象

python - 如何在 IronPython 下将模块安装为 egg?

python - 是否可以将 P4Python API 与 IronPython 一起使用?

javascript - 用于设置地址字段的复选框切换

python - 使用更新游标填充要素类名称和 OID 的 2 个字段

C#相当于Python的日志库

python - Tkinter 标签未显示在弹出窗口中