解析MATHML纯数学表达式

编程入门 行业动态 更新时间:2024-10-19 00:25:23
本文介绍了解析MATHML纯数学表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用 MathDox 公式编辑器产生MathML了。现在我想通过MathDox生产以表达我以后可以用它来评估来找到答案MATHML转换。

I am using MathDox formula editor to produce MathML. Now I want to convert the MathML produced by MathDox to expression which I can later use to evaluate to find the answer.

For eg: MathML: <math xmlns='www.w3/1998/Math/MathML'> <mrow> <mn>3</mn> <mo>+</mo> <mn>5</mn> </mrow> </math> Want to convert to expression as: 3+5

现在我可以使用3 + 5得到的答案8。

Now I can use 3+5 to get answer 8.

我在搜索的JavaScript 或 C#此转换的解决方案。试图谷歌,但并没有获得太大的帮助。有点接近的解决方案,我发现 rel=\"nofollow\">,但它是一个桌面应用程序和商业了。不过,我想我的问题开源Web应用程序的解决方案。 。任何帮助将不胜感激。

I am in a search of javascript or c# solution for this conversion. Tried to google it, but didn't get much help. Somewhat closer solution I found here, but it is a desktop application and commercial too. However, I want open source web app solution for my problem. Any help will be appreciated.

注意:为了简单起见,我只提了简单的加法上面的例子中,但MATHML也可以包含复杂的epression 。像推导和日志

Note: For simplicity I've mentioned only simple addition in example above but the mathml can also contain complex epression like derivations and log.

推荐答案

这可以使用JavaScript中的下列步骤来实现:

This can be achieved using the following steps in JavaScript:

  • 从MATHML转换为XML DOM
  • 从XML DOM转换为纯文本
  • 使用EVAL函数来获得表达的十进制值
  • 下面的代码确实恰恰是:

    The following code does precisely that:

    function getDOM(xmlstring) { parser=new DOMParser(); return parser.parseFromString(xmlstring, "text/xml"); } function remove_tags(node) { var result = ""; var nodes = node.childNodes; var tagName = node.tagName; if (!nodes.length) { if (node.nodeValue == "π") result = "pi"; else if (node.nodeValue == " ") result = ""; else result = node.nodeValue; } else if (tagName == "mfrac") { result = "("+remove_tags(nodes[0])+")/("+remove_tags(nodes[1])+")"; } else if (tagName == "msup") { result = "Math.pow(("+remove_tags(nodes[0])+"),("+remove_tags(nodes[1])+"))"; } else for (var i = 0; i < nodes.length; ++i) { result += remove_tags(nodes[i]); } if (tagName == "mfenced") result = "("+result+")"; if (tagName == "msqrt") result = "Math.sqrt("+result+")"; return result; } function stringifyMathML(mml) { xmlDoc = getDOM(mml); return remove_tags(xmlDoc.documentElement); } // Some testing s = stringifyMathML("<math><mn>3</mn><mo>+</mo><mn>5</mn></math>"); alert(s); alert(eval(s)); s = stringifyMathML("<math><mfrac><mn>1</mn><mn>2</mn></mfrac><mo>+</mo><mn>1</mn></math>"); alert(s); alert(eval(s)); s = stringifyMathML("<math><msup><mn>2</mn><mn>4</mn></msup></math>"); alert(s); alert(eval(s)); s = stringifyMathML("<math><msqrt><mn>4</mn></msqrt></math>"); alert(s); alert(eval(s));

    按照前面的代码,就可以延长接受MathML了。例如,它很容易增加三角或任何其他自定义功能

    Following the previous code, it is possible to extend the accepted MathML. For example, it would be easy to add trigonometry or any other custom function.

    有关这篇文章的目的,我使用的 MATHML编辑打造MATHML(在代码的测试部分使用)。

    For the purpose of this post, I used the tool from mathml editor to build the MathML (used in the test part of the code).

    更多推荐

    解析MATHML纯数学表达式

    本文发布于:2023-11-10 16:13:35,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1575853.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:表达式   数学   MATHML

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!