Polynomial Division Program for the HP28.


The HP28 can't calculate the quotient and the remainder
of polynomial division by itself.

However, if the polynomials are entered as vectors,
the PDiv program returns the quotient on lvl.2 and
the remainder on lvl.1



Ex.1   ( X^3 + 2*X^2 + 2*X + 1 ) / ( X + 1 )


                                   PDiv
2:                  [ 1 2 2 1 ]     =>        2:               [ 1 1 1 ]
1:                      [ 1 1 ]     =>        1:                   [ 0 ]


quotient :   X^2 + X + 1
remainder:             0


PDiv divides complex polynomials too.

Ex.2   ( Z^3 + Z^2 + 2*i*Z^2 + Z + 1 + 2*i ) / ( Z^2 + 1 )


                                   PDiv
2:  [ (1,0) (1,2) (1,0) (1,2) ]     =>        2:         [ (1,0) (1,2) ]
1:                    [ 1 0 1 ]     =>        1:               [ (0,0) ]


quotient :   Z + 1 + 2*i
remainder:             0


Remarks: * PDiv will not work if a vector has a 0 or (0,0)
           as first element. [ 0 1 2 ] must be entered as
           [ 1 2 ].

         * The vector on lvl.1 must have at least two
           elements. If your divisor is a polynomial
           of degree 0, you don't need PDiv.



And now the programs:

***

PDiv                              ; divides two polynomial vectors
<<
[ 0 ]                               ; put the quotient on lvl.3
3 ROLLD V->L SWAP                   ; convert divisor.vector to list
OVER DUP2
  WHILE SIZE SWAP                   ; loop until...
VSize DUP ROT >=                    ; degree.remainder<degree.divisor
  REPEAT 1 DUP ->LIST                 ; result, temporary represented...
3 ROLLD                               ; as { 1 }, is put on lvl.3 
    WHILE DUP2 SWAP                   ; loop until...
SIZE >                                ; size.divisor.list=degree.remainder
    REPEAT ROT 0 +                      ; increase degree of divisor and...  
ROT 0 + ROT                             ; result by 1.loop end
    END DROP SWAP 5                   ; put result on lvl.5. get 1st elem-
ROLLD OVER 1 GET                      ; ents of rem.vec &res.list / 
OVER 1 GET / DUP 7                    ; get result , * k, get quotient,VAdd
ROLL L->V * 6 ROLL                    ; put new quotient on lvl.5
VAdd 5 ROLLD SWAP                     ; swap down divisor.list convert to..
L->V * - Cut OVER                     ; vector, * k, -, Cut, -> new remainder
DUP2                                  ; swap down divisor dup2 
  END DROP2 3 ROLLD                   ; loop end 
DROP SWAP
>>

***

VSize
<< SIZE 1 GET                     ; get size of vector
>>

***

Cut                             ; cut zeroes [ 0 0 1 2 ] -> [ 1 2 ]
<< 1 0 ROT ARRY-> 1
GET DUP
  IF 1 -                          ; check (size of vector - 1)
  THEN 1 +                        ; if size of vector > 1
    DO DUP PICK ABS                 ; loop until picked abs > ' 0 '
.00000000001 > SWAP
OVER NOT - SWAP                       ; decrease lvl to pick from by truthvalue
    UNTIL
    END                             ; loop end
    IF 1 - DUP                      ; checks no. of elements <> 0
    THEN 1 ->LIST                   ; if no.>0 put no. ->list
    ELSE 1 + 1 ->LIST               ; if all elements are 0 then 1 ->list
    END ->ARRY                      ; make vector
    DO SWAP DROP                    ; get rid of zeroes above lvl.1
    UNTIL OVER ABS                  
.00000000001 >
    END SWAP DROP                   ; swap the 1 down drop it
  ELSE 1 ->LIST ->ARRY            ; can't cut vector of size 1 
3 ROLLD DROP2                       ; make vector , drop 0 1
  END
>>

***

L->V                            ; { 1 2 3 4 5 } -> [ 1 2 3 4 5 ]
<< LIST-> 1 ->LIST
->ARRY
>>

***

V->L                            ; [ 5 4 3 2 1 ] -> { 5 4 3 2 1 }
<< ARRY-> LIST-> DROP
->LIST
>>

***

VAdd                            ; adds polynomial vectors of different sizes 
<< DUP2 VSize SWAP
VSize - DUP                       ; get difference in sizes
  IF 0 ==                         ; if sizes is same 
  THEN DROP                         ; drop difference
  ELSE DUP                        
    IF 0 >                        ; if the 'bigger' vector was on lvl.1
    THEN ROT SWAP                   ; swap lvl2. and lvl.3
    ELSE ABS
    END FillN                     ; fill vector on lvl.2 so vectors have...
  END +                           ; equal sizes. add them
>>

***

FillN                             ; takes integer n, from lvl.1 .put n zeroes 
<< -> n                           ; on the left side of vector at lvl.2  
  << 1 n
     START 0 SWAP
     NEXT ARRY-> LIST->
SWAP n
  >> + SWAP ->LIST
->ARRY
>>

***


I'm not used too write comments to my programs,
so they might be a little bit strange.

When I made my first verion of PDiv I had to
do a lot of thinking to make it work.
Then I have optimized the speed of calculation
as far as I can (by now).
I have programmed the HP28 for about 4 month,
so I'm not that experienced.
Comments on errors and further optimization
are welcome indeed.


As a challenge I've tried to write a program
that converts an algebraic-polynomial to
a polynomial vector. It is quite easily done
if the polynomial is real. But a complex-
polynomial isn't easy if you don't write a
very big(and slow) program.

I hope that I havn't made any typing errors...



                        Mikael Sundstrom Lulea University

------------------------------------------------------------------------------

Hello net
 Here is a little program I wrote that divide two polynoms.The program receive
two vectors of coefficent and return a vector with the division result and
another one with the reminder .I also include a program that transform a
coefficent vector to a polynom.


div [D3C4]  ; This is the main program but all the work is done by DIVV
  << DUP SIZE ! GET -> a b c
      << a b WHILE OVER SIZE
         1 GET c REPEAT DIVV EDD
         DROP -> e
           << a SIZE 1 GET c 1 - -
              ->LIST ->ARRY  e
            >>
      >>
   >>

DIVV [3075] ; This program is used by the previous one
<< DUP 1 GET -> a b c
    << a 1 GET c / DUP b * a SIZE RDM a
       SWAP - ARRY-> 1 GETI 1 - PUT
       ->ARRY SWAP DROP b
    >>
>>

poly [B9C2] ; This program expect a vector and a algebraic object
            ; and return a polynom in this object.
<<  -> a x
      << a SIZE 1 GET -> c
         << 0 0 c 1 - FOR j
             x j ^ a c j - GET * +
             NEXT
         >>
      >>
>>

B.T.W can anyone tell me how  to send programs to the HP28 server
without having FTP access ?
Thats all for today.
Bye

Eliel Louzoun
Jerusalem
Israel .
