IsQuadratic
IsQuadratic
IsQuadratic[ expr, x ] checks if an expression is quadratic in a variable, and returns its coefficients.
Usages
IsQuadratic[ expr, x ]
IsQuadratic[ expr, x ] checks whether expr can be written as a x^2 + b x + c with a ≠ 0, where a, b and c do not depend on x. Brackets are expanded first. If so, the result is IsQuadraticResult[a=>a, b=>b, c=>c]; otherwise the result is False. In a script, wrap the command in Simplify.
| Example | in | out |
|---|---|---|
| A quadratic expression. | Simplify[ IsQuadratic[2x^2 + 3x + 1, x] ] |
IsQuadraticResult[a=>2, b=>3, c=>1] |
| Brackets are expanded. | Simplify[ IsQuadratic[(x + 1)^2, x] ] |
IsQuadraticResult[a=>1, b=>2, c=>1] |
| A product. | Simplify[ IsQuadratic[x(x - 3), x] ] |
IsQuadraticResult[a=>1, b=>-3, c=>0] |
| Other variables are allowed in the coefficients. | Simplify[ IsQuadratic[x^2 + y, x] ] |
IsQuadraticResult[a=>1, b=>0, c=>y] |
| A linear expression is not quadratic. | Simplify[ IsQuadratic[3x + 1, x] ] |
False |
| A cubic expression is not quadratic. | Simplify[ IsQuadratic[x^3 + x, x] ] |
False |
Using the result
Use Type to check the result, and GetOption to get the coefficients.
| Example | in | out |
|---|---|---|
| Get the coefficients. | r := Simplify[IsQuadratic[2x^2 + 3x + 1, x]] |
a = 2b = 3c = 1 |
| Check whether the expression is quadratic. | r := Simplify[IsQuadratic[x + 1, x]] |
t = 0 |
Details
- Without
Simplify, the command is not calculated.
Warning
Do not check the result with r == False: for a quadratic expression this comparison cannot be decided, so in an If it is treated as True. Use Type[r] == IsQuadraticResult, as in the example above.
See also: IsLinear, PolynomialDegree, Solve, Type