AngouriMath

Navigation

← Back to list of members

TryGetPolynomial​(AngouriMath.​Entity,​AngouriMath.​Entity.​Variable,​System.​Collections.​Generic.​Dictionary{PeterO.​Numbers.​EInteger,​AngouriMath.​Entity}@)

 Method (no overloads)

Summary

Extracts a polynomial with integer powers

Parameter "expr"

From which to extract the polynomial

Parameter "variable"

Over which variable to extract the polynomial

Parameter "dst"

Where to put the dictionary, whose keys
are powers, and values - coefficients

Returns

Whether the input expression is a valid polynomial

Example

using System;
using AngouriMath;
using static AngouriMath.MathS.Utils;

Entity expr = "(x^2 + 2)(a + b + 2x) + x + sin(h)";
if (TryGetPolynomial(expr, "x", out var dict))
    foreach (var (pow, coef) in dict)
        Console.WriteLine($"Pow: {pow}. Coef: {coef}");
Console.WriteLine("------------------------");
Entity expr1 = "sin(x) + a";
if (TryGetPolynomial(expr1, "x", out var dict1))
    foreach (var (pow, coef) in dict1)
        Console.WriteLine($"Pow: {pow}. Coef: {coef}");
else
    Console.WriteLine("Failed to interpret as polynomial");
Console.WriteLine("------------------------");
Entity expr2 = "(x + a)(b + x) + a + 2 + x";
if (TryGetPolyQuadratic(expr2, "x", out var a, out var b, out var c))
    Console.WriteLine($"The expr is ({a}) * x^2 + ({b}) * x + ({c})");
Console.WriteLine("------------------------");
Entity expr3 = "(b + x) + a + 2 + x";
if (TryGetPolyLinear(expr3, "x", out var a1, out var b1))
    Console.WriteLine($"The expr is ({a1}) * x + ({b1})");

Prints
Pow: 2. Coef: 1 * 1 ^ 2 * a + 1 * 1 ^ 2 * b
Pow: 3. Coef: 1 * 1 ^ 2 * 2
Pow: 0. Coef: 2 * a + 2 * b + sin(h)
Pow: 1. Coef: 1 * 2 * 2 + 1
------------------------
Failed to interpret as polynomial
------------------------
The expr is (1 * 1 ^ 2) * x^2 + (1 * b + 1 * a + 1) * x + (a * b + a + 2)
------------------------
The expr is (1 + 1) * x + (b + a + 2)

























Angouri © 2019-2023 · Project's repo · Site's repo · Octicons · Transparency · 1534 pages online