infusion / Polynomial.js

A JavaScript library to work with polynomials

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Polynomial division - how to do a step by step calculation?

q2apro opened this issue · comments

Hi Robert @infusion I used your library to build a tiny math tool for mathematics: http://www.matheretter.de/formeln/algebra/polynomrechner/

Solving the fraction issue #2 would help a lot.

One of my users has asked me if a step-by-step division could be shown. E.g. something like:

(x^4  + 3x^3  + 2x^2  + 6x) : (x + 3)  =  x^3 + 2x  
 x^4  + 3x^3              
 —————————————————————————
                2x^2  + 6x
                2x^2  + 6x
                ———————————
                          0

So I would need to print the single steps.

Is there any way to achieve that? (Splitting the polynom or ...?)

Really great site! The best idea to solve this problem I had so far is adding a global variable to enable tracing of long lasting operations. I think it makes only sense for divisions, like in your example, but could be extended to operation in theory.

I just added a tracing option, which has to be set to something !== null and after the division, you'll have an array with Polynomial objects with all steps in between.

    Polynomial.trace = true;
    new Polynomial("x^4+3x^3+2x^2+6x")
            .div("x+3");
    console.log(Polynomial.trace)

BTW: I haven't published a min file, as I added this feature for you to test it.

Hi Robert, I have tried the new polynomial.js file and your code above. This is what I get in the console: Array [ Object, Object ]

I print the objects:

var steps = Polynomial.trace;
steps.forEach(function(entry) {
    console.log(entry.toString());
});

and get:

2x^2+6x
0

Isn't that missing the first step x^4 + 3x^3?

Could you please test again? Also with something having a residual like

 new Polynomial("x^4+3x^3+2x^2+6x")
            .div("x+4");

Nice, the initial example gives me now:

x^4+3x^3
2x^2+6x
0

Correct :)

A residual example, just using /(x+2) instead of /(x+3) gives:

x^4+2x^3
x^3+2x^2
6x+12
-12

And correct, -12 / (x + 2) is the rest.

Just need to implement the steps in a good visual way in the program: http://www.matheretter.de/formeln/algebra/polynomrechner/#poldiv