Unicode polynomial equation

Revision as of 02:41, 17 November 2011 by rosettacode>NevilleDNZ (Normalise a polynomial: e.g. x⁵ - 2x⁴ + 42x³ + 40x + 1)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

The objective of this task is to parse in a difficult polynomial, and generate a "pretty" representation of polynomial in Unicode.

Unicode polynomial equation is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

In the target language define a "polynomial" object (or structure or record). Using this object also define the routines for parsing a polynomial as input, and generating a normalised Unicode representation of the polynomial as output.

Task details:

Given a string containing any untidy Unicode polynomial, e.g.

-0.00x⁺¹⁰ + 1.0·x ** 5 + -2e0x^4 + +0,042.00 × x ⁺³ + +.0x² + 20.000 000 000x¹ - -1x⁺⁰ + .0x⁻¹ + 20.x¹

Coerce (or convert) the string into the "polynomial" object, at the same time normalise the polynomial to a canonical form. The ideal normalised output (in this example) would be:

x⁵ - 2x⁴ + 42x³ + 40x + 1
Examples of original polynomial text to be parsed.
Description Input Example
"Zero" coefficients are removed x⁵ - 2x⁴ + 42x³ + 0x² + 40x + 1
"One" coefficients are normalised 1x⁵ - 2x⁴ + 42x³ + 40x + 1x⁰
Signs are normalised +x⁺⁵ + -2x⁻⁻⁴ + 42x⁺⁺³ + 40x - -1
ASCII representations are parsed x^5 - 2x**4 + 42x^3 + 40x + 1
Non-ASCII representations are parsed x⁵ - 2.00·x⁴ + 42.00·x³ + 40·00·x + 1
Terms with negative exponents are parsed and stored x⁻⁵ - 2⁄x⁻⁴ + 42x⁻³ + 40/x + 1 (n.b. Unicode Fraction)
Spaces in numbers and between operators are ignored x⁵ - 2x⁴ + 42.000 000x³ + 40x + 1
Single commas are ignored in numbers x⁵ - 2x⁴ + 0,042x³ + 40.000,000x + 1
A coefficient may be duplicated, zero, of missing 0x⁸ + 10x + 10x + x⁵ - 2x⁴ + 42x³ + 20x + 1
Support Scientific notation
and optionally support Unicode Decimal Exponent Symbol U+23E8/⏨
1E0x⁵ - 2e0x⁴ + 4.2⏨1x³ + .40e-2x + 1
Support the set minimum of Unicode characters as follows: ⁰ ¹ ² ³ ⁴ ⁵ ⁶ ⁷ ⁸ ⁹ ⁻ ⁺ · × ⁄ .
(Where · & × are multiplication, and ⁄ is Unicode Fraction)
Optionally support Unicode Vulgar fractions ¼ ½ ¾ ⅐ ⅑ ⅒ ⅓ ⅔ ⅕ ⅖ ⅗ ⅘ ⅙ ⅚ ⅛ ⅜ ⅝ ⅞ ↉
On output round decimal the to appropriate fraction.

There are (at least) three possible ways of achieving this task.

  • Using an external parsing library.
  • Using a built-in parsing/formatting library.
  • Coding a custom polynomial parsing routing.

Either one, or all of these approaches are acceptable.