[英]MathNet.Symbolics Algebraic.Expand in descending order?
With MathNet.Symbolics
library, I try to print a polynomial with a descending power order:使用MathNet.Symbolics
库,我尝试以降序打印多项式:
using mse = MathNet.Symbolics.Expression;
using MathNet.Symbolics;
public void Symbolics()
{
var x1 = 2;
var y2 = 3;
var x2 = 4;
// (2a+3)(a-4)
var x = mse.Symbol("x");
var a = mse.Symbol("x");
var y = mse.Symbol("y");
var expression = (x1 * a + y2) * (a - x2);
var expanded = Algebraic.Expand(expression);
var firstResult = Infix.Format(expanded);
Debug.Log("Expression: " + expression);
Debug.Log("Expanded: " + firstResult);
}
The current output is in ascending order: -12 - 5*x + 2*x^2
, but I want it the other way.当前输出按升序排列: -12 - 5*x + 2*x^2
,但我想要其他方式。 I tried .ToStringDescending()
function, in polynomials but was unable to get it working here.我在多项式中尝试了.ToStringDescending()
函数,但无法在这里工作。
Also, output to Latex would be useful if possible but not essential.此外,如果可能,输出到 Latex 会很有用,但不是必需的。
How do I have have the result of Algebraic.Expand
in descending not ascending order?我如何得到Algebraic.Expand
的降序而不是升序的结果?
You can have Latex output with the corresponding format provider:您可以使用相应的格式提供程序获得 Latex 输出:
Console.WriteLine("LaTeX.Format: " + LaTeX.Format(expanded));
The best I can get is to use Summands
to get them and the reverse the list.我能得到的最好的方法是使用Summands
来获取它们并反转列表。 Sadly the +
sign is lost in the process:遗憾的是, +
号在此过程中丢失了:
Console.WriteLine("Summands: " + string.Join("+", Algebraic.Summands(expanded).Reverse().Select(Infix.Format)));
Numerics.Polynomial has ToString() that returns the string in ascending form and Numerics.Polynomial 有 ToString() 以升序形式返回字符串和
ToStringDescending(string format, IFormatProvider formatProvider) Format the polynomial in descending order, eg "x^3 + 2.0x^2 - 4.3" ToStringDescending(string format, IFormatProvider formatProvider) 按降序格式化多项式,例如“x^3 + 2.0x^2 - 4.3”
that returns the string in descending form.以降序形式返回字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.