@@ -644,172 +644,9 @@ See `requirements/DONE.md` for implementation details.
644644
645645## Polynomial Operations
646646
647- ### 33. Polynomial Factoring
647+ ### ~~ 33. Polynomial Factoring~~ ✅ COMPLETED
648648
649- ** Problem:** The system lacks polynomial factoring capability to convert expanded
650- polynomials into factored forms. This prevents simplification of expressions like
651- ` √(x²+2x+1) ` which should factor to ` √((x+1)²) ` and simplify to ` |x+1| ` .
652-
653- ** Related issue:** [ #180 ] ( https://github.com/cortex-js/compute-engine/issues/180 )
654- - "Factoring before trying to simplify"
655-
656- ** Current behavior:**
657-
658- ``` typescript
659- // Factored form works
660- ce .parse (' \\ sqrt{(x+1)^2}' ).simplify ().latex ;
661- // → "\vert x+1\vert" ✓
662-
663- // Expanded form doesn't factor first
664- ce .parse (' \\ sqrt{x^2+2x+1}' ).simplify ().latex ;
665- // → "\sqrt{x^2+2x+1}" ✗ (should recognize perfect square)
666-
667- // Rational simplification works (partial fix in #180)
668- ce .parse (' \\ frac{x}{x^2-x}' ).simplify ().latex ;
669- // → "\frac{1}{x-1}" ✓ (factors denominator for cancellation)
670- ```
671-
672- ** Expected behavior:**
673-
674- ``` typescript
675- // Perfect square trinomials
676- ce .parse (' \\ sqrt{x^2+2x+1}' ).simplify ().latex ;
677- // → "\vert x+1\vert"
678-
679- ce .parse (' \\ sqrt{a^2+2ab+b^2}' ).simplify ().latex ;
680- // → "\vert a+b\vert"
681-
682- ce .parse (' \\ sqrt{a^2-2ab+b^2}' ).simplify ().latex ;
683- // → "\vert a-b\vert"
684-
685- // General quadratic factoring
686- ce .parse (' x^2+5x+6' ).factor ().latex ;
687- // → "(x+2)(x+3)"
688-
689- ce .parse (' 2x^2-8' ).factor ().latex ;
690- // → "2(x-2)(x+2)"
691- ```
692-
693- ** Implementation approach:**
694-
695- 1 . ** Add ` factor() ` method to BoxedExpression:**
696- - Extend existing ` factor() ` in ` src/compute-engine/boxed-expression/factor.ts `
697- - Current implementation only factors out common coefficients/terms
698- - Need to add polynomial factorization algorithms
699-
700- 2 . ** Factoring algorithms to implement:**
701- - ** Perfect square detection:** ` a²+2ab+b² ` → ` (a+b)² ` , ` a²-2ab+b² ` → ` (a-b)² `
702- - ** Difference of squares:** ` a²-b² ` → ` (a-b)(a+b) `
703- - ** Quadratic formula factoring:** For ` ax²+bx+c ` , use roots to construct factors
704- - ** Common factor extraction:** ` 6x²+9x ` → ` 3x(2x+3) ` (already implemented)
705- - ** Rational root theorem:** For higher-degree polynomials with integer coefficients
706- - ** Kronecker's method:** General factorization over integers (optional, for completeness)
707-
708- 3 . ** Perfect square trinomial detection (priority for issue #180 ):**
709- ``` typescript
710- function isPerfectSquare(expr : BoxedExpression ): BoxedExpression | null {
711- // For Add with 3 terms
712- if (expr .operator !== ' Add' || expr .ops .length !== 3 ) return null ;
713-
714- // Check pattern: a² + 2ab + b² or a² - 2ab + b²
715- // Extract terms, identify squares and cross term
716- // Return (a+b)² or (a-b)² if match found
717- }
718- ```
719-
720- 4 . ** Integration with simplification:**
721- - Modify ` simplifyPower() ` in ` src/compute-engine/symbolic/simplify-power.ts `
722- - Before checking ` sqrt(x^2) ` patterns, try factoring the argument
723- - Add check around line 126 (Sqrt operator handling):
724-
725- ``` typescript
726- if (op === ' Sqrt' ) {
727- const arg = x .op1 ;
728- if (! arg ) return undefined ;
729-
730- // Try factoring first for perfect squares
731- if (arg .operator === ' Add' ) {
732- const factored = factorPerfectSquare (arg );
733- if (factored ?.operator === ' Power' && factored .op2 ?.is (2 )) {
734- // Found perfect square, apply sqrt(x^2) -> |x| rule
735- return {
736- value: ce ._fn (' Abs' , [factored .op1 ]),
737- because: ' sqrt(perfect square trinomial) -> |factor|'
738- };
739- }
740- }
741-
742- // ... existing sqrt simplification rules
743- }
744- ```
745-
746- 5 . ** IMPORTANT - Recursion prevention:**
747- - Factoring functions should NOT call ` .simplify() ` on results
748- - Follow pattern from ` polynomialDivide() ` and other polynomial operations
749- - Return canonical expressions, let caller decide if simplification needed
750- - See ` CLAUDE.md ` "Simplification and Recursion Prevention" section
751-
752- ** Use cases:**
753-
754- 1 . ** Square root simplification** (issue #180 ):
755- - ` √(x²+2x+1) ` → ` |x+1| `
756- - ` √(4x²-12x+9) ` → ` |2x-3| `
757-
758- 2 . ** Rational expression simplification:**
759- - ` (x²-1)/(x+1) ` → ` x-1 ` (currently requires already factored form)
760- - ` (x²+5x+6)/(x+2) ` → ` x+3 `
761-
762- 3 . ** Equation solving:**
763- - ` x²+5x+6 = 0 ` → factor → ` (x+2)(x+3) = 0 ` → ` x = -2 ` or ` x = -3 `
764-
765- 4 . ** Partial fraction decomposition:**
766- - Requires factored denominators
767-
768- ** Files to modify:**
769-
770- - ` src/compute-engine/boxed-expression/factor.ts ` - Add polynomial factoring
771- - ` src/compute-engine/symbolic/simplify-power.ts ` - Use factoring in sqrt simplification
772- - ` src/compute-engine/boxed-expression/polynomials.ts ` - Add helper functions if needed
773- - ` src/compute-engine/boxed-expression/abstract-boxed-expression.ts ` - Ensure ` .factor() ` method exists
774-
775- ** Tests to add:**
776-
777- ``` typescript
778- // test/compute-engine/factor.test.ts
779- describe (' Polynomial factoring' , () => {
780- test (' perfect square trinomials' , () => {
781- expect (parse (' x^2+2x+1' ).factor ()).toMatchInlineSnapshot (` ["Square", ["Add", "x", 1]] ` );
782- expect (parse (' a^2+2ab+b^2' ).factor ()).toMatchInlineSnapshot (` ["Square", ["Add", "a", "b"]] ` );
783- });
784-
785- test (' difference of squares' , () => {
786- expect (parse (' x^2-4' ).factor ()).toMatchInlineSnapshot (` ["Multiply", ["Add", "x", -2], ["Add", "x", 2]] ` );
787- });
788-
789- test (' quadratic with roots' , () => {
790- expect (parse (' x^2+5x+6' ).factor ()).toMatchInlineSnapshot (` ["Multiply", ["Add", "x", 2], ["Add", "x", 3]] ` );
791- });
792- });
793-
794- // test/compute-engine/simplify.test.ts - add to existing tests
795- test (' sqrt of perfect square trinomial' , () => {
796- expect (parse (' \\ sqrt{x^2+2x+1}' ).simplify ()).toMatchInlineSnapshot (` ["Abs", ["Add", "x", 1]] ` );
797- expect (parse (' \\ sqrt{a^2-2ab+b^2}' ).simplify ()).toMatchInlineSnapshot (` ["Abs", ["Add", "a", ["Negate", "b"]]] ` );
798- });
799- ```
800-
801- ** Implementation priority:**
802-
803- 1 . ** High priority:** Perfect square trinomial detection for sqrt simplification (fixes issue #180 cases)
804- 2 . ** Medium priority:** General quadratic factoring with rational roots
805- 3 . ** Low priority:** Higher-degree polynomial factoring (Kronecker's method, etc.)
806-
807- ** References:**
808-
809- - Issue #180 : https://github.com/cortex-js/compute-engine/issues/180
810- - Current factor implementation: ` src/compute-engine/boxed-expression/factor.ts `
811- - Sqrt simplification: ` src/compute-engine/symbolic/simplify-power.ts:124-254 `
812- - Polynomial utilities: ` src/compute-engine/boxed-expression/polynomials.ts `
649+ See ` requirements/DONE.md ` for implementation details.
813650
814651---
815652
0 commit comments