AngouriMath

Navigation

← Back to list of members

TryGetPolyQuadratic​(AngouriMath.​Entity,​AngouriMath.​Entity.​Variable,​AngouriMath.​Entity@,​AngouriMath.​Entity@,​AngouriMath.​Entity@)

 Method (no overloads)

Summary

Extracts the quadratic coefficient, linear coefficient and the bias over a variable
a x ^ 2 + b x + c

Parameter "expr"

From which to extract the quadratic function

Parameter "variable"

Over which to extract

Parameter "a"

The quadratic coefficient

Parameter "b"

The linear coefficient

Parameter "c"

The bias

Returns

Whether the extract was successful

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