mathjs/test/unit-tests/function/algebra/polynomialRoot.test.js
Glen Whitney f6d3e9ea8d
feat: add polynomialRoot function (#2839)
* feat: add polynomialRoot function

  This is intended as a benchmark for general arithmetic/basic algebra
  functionality of mathjs, but was chosen to be something of potential
  independent utility as well, worth adding to mathjs in its own right.

  Currently ol=nly computes the numerical roots for real or complex
  polynomials of degree three or less.

  As usual, adds documentation, embedded documentation, tests, TypeScript
  declaration, and TypeScript tests.

  Also updates doc.test.js to make it easier to specify an array of complex
  numbers as the expected output, and comapres with the appropriate fuzz
  in such cases.

  Finally, adds a benchmark that computes the roots of all cubics with
  nonnegative integer coefficients no larger than five.

* doc: fix typo in polynomialRoot embedded docs

Thanks, SamuelTLG

* style: avoid slightly cryptic Boolean flag stand-in
2022-11-18 15:53:45 +01:00

54 lines
2.2 KiB
JavaScript

import approx from '../../../../tools/approx.js'
import math from '../../../../src/defaultInstance.js'
const complex = math.complex
const pRoot = math.polynomialRoot
describe('polynomialRoot', function () {
it('should solve a linear equation with real or complex coefficients',
function () {
approx.deepEqual(pRoot(6, 3), [-2])
approx.deepEqual(pRoot(complex(-3, 2), 2), [complex(1.5, -1)])
approx.deepEqual(pRoot(complex(3, 1), complex(-1, -1)), [complex(2, -1)])
})
it('should solve a quadratic equation with a double root', function () {
approx.deepEqual(pRoot(4, 4, 1), [-2])
approx.deepEqual(pRoot(complex(0, 2), complex(2, 2), 1), [complex(-1, -1)])
})
it('should solve a quadratic with two distinct roots', function () {
approx.deepEqual(pRoot(-3, 2, 1), [1, -3])
approx.deepEqual(pRoot(-2, 0, 1), [math.sqrt(2), -math.sqrt(2)])
approx.deepEqual(
pRoot(4, 2, 1), [complex(-1, math.sqrt(3)), complex(-1, -math.sqrt(3))])
approx.deepEqual(
pRoot(complex(3, 1), -3, 1), [complex(2, -1), complex(1, 1)])
})
it('should solve a cubic with a triple root', function () {
approx.deepEqual(pRoot(8, 12, 6, 1), [-2])
approx.deepEqual(
pRoot(complex(-2, 11), complex(9, -12), complex(-6, 3), 1),
[complex(2, -1)])
})
it('should solve a cubic with one simple and one double root', function () {
approx.deepEqual(pRoot(4, 0, -3, 1), [-1, 2])
approx.deepEqual(
pRoot(complex(9, 9), complex(15, 6), complex(7, 1), 1),
[complex(-1, -1), -3])
approx.deepEqual(
pRoot(complex(0, 6), complex(6, 8), complex(5, 2), 1),
[-3, complex(-1, -1)])
approx.deepEqual(
pRoot(complex(2, 6), complex(8, 6), complex(5, 1), 1),
[complex(-3, 1), complex(-1, -1)])
})
it('should solve a cubic with three distinct roots', function () {
approx.deepEqual(pRoot(6, 11, 6, 1), [-3, -1, -2])
approx.deepEqual(
pRoot(-1, -2, 0, 1), [-1, (1 + math.sqrt(5)) / 2, (1 - math.sqrt(5)) / 2])
approx.deepEqual(pRoot(1, 1, 1, 1), [-1, complex(0, -1), complex(0, 1)])
approx.deepEqual(
pRoot(complex(0, -10), complex(8, 12), complex(-6, -3), 1),
[complex(1, 1), complex(3, 1), complex(2, 1)])
})
})