12

Original

The remainder provided by polynom.sty is NOT complete (as shown in the following figure).

alt text


My Objective

When I was in senior high school, my teacher made the diagram like below. :-)

Thus the final step must show the TOTAL remainder and mark it with L-shaped curve.

NOTE

I am sorry. The previous post I forgot to put back -1/4 in the original position. It have made you confused. Now there is no mistake anymore. :-)

alt text


Minimal Code

\documentclass{article}
\usepackage{polynom,array}
\usepackage[table]{xcolor}


\makeatletter
\def\pld@ArrangeResult#1{%
    \ifx\pld@remainder\@empty
        \@tempcnta\pld@maxcol\relax
        \pld@InsertItems@do\pld@lastline
            {\pld@firsttrue\pld@PLD{\pld@R{0}{1}}}%
    \fi
    \ifnum\pld@currstage>\z@
        \pld@Extend\pld@allines{\pld@lastline\cr}%
    \else
        \pld@InsertFake\pld@lastline
    \fi
    \pld@iftopresult
        \def\pld@lastline{\pld@PrintPoly\pld@divisor%
        %====================================================================================
        \quad\smash{{\color{red}\rule[-6pt]{\arrayrulewidth}{17pt}}}\kern-\arrayrulewidth&}%
        %====================================================================================
    \else
        \let\pld@lastline\@empty
        \ifx B\pld@style\else
            \def\pld@lastline{\pld@leftdelim\strut\pld@rightxdelim&}%
        \fi
    \fi
    \expandafter\pld@AR@col\expandafter\pld@PLD
                           \expandafter\pld@lastline#1+\relax+%
    \pld@SplitQuotient
    \pld@iftopresult
        \let\pld@currentline\@empty
        \expandafter\pld@AR@col\expandafter\pld@PLD
                               \expandafter\pld@currentline
                                           \pld@quotient+\relax+%
        \expandafter\pld@AR@col\expandafter\pld@XPLD
                               \expandafter\pld@currentline
                                           \pld@shadow+\relax+%
        \edef\pld@subline{%
            \noexpand\cline{\tw@-\pld@maxcol}%
            \noalign{\vskip\jot}}%
        \pld@Extend\pld@currentline{\expandafter\cr\pld@subline}%
    \else
        \@tempcnta-\@tempcnta
        \advance\@tempcnta\pld@maxcol\relax \advance\@tempcnta\@ne
        \edef\pld@span{\the\@tempcnta}%
        \ifx B\pld@style
          \pld@AddTo\pld@lastline{%
            &\multispan\pld@span${}=%
            \pld@PrintPolyWithDelims\pld@divisor
            \expandafter\pld@IfSum\expandafter{\pld@divisor}{}{\cdot}%
            \expandafter\pld@IfSum\expandafter{\pld@quotient}\pld@true
                                                             \pld@false
            \pld@if \pld@leftdelim
                    \pld@PrintPolyShadow
                    \pld@rightdelim
              \else \pld@PrintPolyShadow \fi
            \pld@firstfalse
            \expandafter\pld@PrintRemain\expandafter{\pld@remainder}$}%
        \else
          \pld@AddTo\pld@lastline{%
            &\multispan\pld@span$\pld@leftxdelim\strut\pld@rightdelim
            \pld@div
            \pld@PrintPolyWithDelims\pld@divisor=
            \pld@PrintPolyShadow
            \ifx\pld@remainder\@empty\else
                +{}%
                \setbox\z@=\hbox{$\displaystyle
                  \frac{\let\strut\@empty\pld@firsttrue \expandafter
                        \pld@PrintRemain\expandafter{\pld@remainder}}%
                       {\let\strut\@empty\pld@PrintPoly\pld@divisor}$}%
                \dp\z@=\z@\box\z@
            \fi
            $}%
        \fi
    \fi
\expandafter\pld@AR@\pld@allines\relax}
\makeatother


\arrayrulecolor{red}
\arrayrulewidth=0.8pt
\def\strut{\rule[-6pt]{0pt}{12pt}}

\begin{document}
\polylongdiv[style=A]{\frac{3}{7}x^9+x^2-\frac{1}{4}}{\frac{9}{5}x^4-1}

\vspace{1cm}
\polylongdiv[style=A]{x^4-1}{x^2-1}
\end{document}

Hendrik Vogt
  • 37,935
Display Name
  • 46,933
  • @Herbert + @Ulrike: Please see my updated figure (second figure). When I was in senior high school, my teacher made the diagram like that. :-) He stopped the operation until showing the total remainder. – Display Name Jan 12 '11 at 10:12
  • I guess you don't want to move the 1/4 down, but you want to copy it. After all, you want to divide \frac{3}{7}x^9+x^2-\frac{1}{4} (including the 1/4) by \frac{9}{5}x^4-1. – Hendrik Vogt Jan 12 '11 at 12:18
  • I've told you before that it's not necessary to write the whole question in the title of the question :-) – Hendrik Vogt Jan 12 '11 at 13:41
  • @Hendrik: You are correct. I have to say "copy" instead of "drop". And I did NOT realize that delete -1/4 from the original position. That was my big mistake and my teacher did NOT teach like that. I am thinking to shrink my title. Thanks. – Display Name Jan 12 '11 at 13:48

2 Answers2

10

The following code should work; I've included a few test cases. To keep the code shorter, I didn't include your changes to the style of the output (red color, and bar between dividend and divisor instead of parenthesis). My changes and additions to polynom are marked with !!!.

\documentclass{article}
\usepackage{polynom,array}

\makeatletter
\def\pld@DivPoly@l{%
    \ifx\pld@remainder\@empty\else
        \pld@IfNeedsDivision\pld@remainder\pld@divisor
        {\pld@ExtendPoly\pld@quotient\pld@factor
         \pld@NMultiplyPoly\pld@sub\pld@divisor\pld@factor
         \pld@SubtractPoly\pld@remainder\pld@sub
         \expandafter\pld@DivPoly@l}%
        {\expandafter\pld@insert@remainder                       % !!!
         \pld@last@remainder+\relax\relax}                       % !!!
    \fi}
\def\pld@insert@remainder#1+#2\relax{%                           % !!!
    \ifx\relax#1\relax\else\pld@InsertItems\@empty\@empty{#1}\fi % !!!
    \ifx\relax#2\relax\else\pld@insert@remainder#2\relax\fi}     % !!!
\def\pld@SubtractPoly@l#1+#2\@empty#3+#4\@empty{%
    \ifx\relax#1\relax
        \let\pld@last@remainder\@empty                           % !!!
        \ifx\relax#3\relax \let\pld@next\@empty \else
          \pld@AddToPoly\pld@tempoly{#3}%
          \pld@if \pld@InsertItems{#3}{#3}{}\fi
          \def\pld@next{\pld@SubtractPoly@l\relax+\@empty#4\@empty}%
        \fi
    \else
    \ifx\relax#3\relax
        \pld@SubtractPoly@r#1+#2\@empty
        \let\pld@next\@empty
    \else
        \pld@IfMonomE{#1}{#3}%
        {\def\pld@temp{#1+#3}%
         \pld@CondenseMonomials\pld@true\pld@temp
         \ifx\pld@temp\@empty\else
             \pld@ExtendPoly\pld@tempoly\pld@temp
         \fi
         \pld@if \expandafter\pld@InsertItems\expandafter
                 {\pld@temp}{#3}{#1}\fi
         \def\pld@next{\pld@SubtractPoly@l#2\@empty#4\@empty}}%
        {\pld@IfMonomL{#1}{#3}%
         {\pld@AddToPoly\pld@tempoly{#3}%
          \pld@if \pld@InsertItems{#3}{#3}{}\fi
          \def\pld@next{\pld@SubtractPoly@l#1+#2\@empty#4\@empty}}%
         {\pld@AddToPoly\pld@tempoly{#1}%
          \pld@if \pld@InsertItems{#1}{}{#1}\fi
          \def\pld@next{\pld@SubtractPoly@l#2\@empty#3+#4\@empty}}%
        }%
    \fi \fi
    \pld@next}
\def\pld@SubtractPoly@r#1+\relax+\@empty{%
    \pld@AddToPoly\pld@tempoly{#1}%
    \def\pld@last@remainder{#1}}                                 % !!!
\makeatother
\def\strut{\rule[-6pt]{0pt}{12pt}}

\begin{document}
\polylongdiv{x^5-1}{x-1}

\polylongdiv{x^5-x^2}{x^2-1}

\polylongdiv{\frac{3}{7}x^9+x^2-\frac{1}{4}}{\frac{9}{5}x^4-1}

\polylongdiv{x^9+x^2-1}{x^4-x}

\polylongdiv{x^{15}+1}{x^5+x^3+x+1}

\polylongdiv{x^4-1}{x^2-1}
\end{document}
Hendrik Vogt
  • 37,935
  • Well done!! –  Jan 12 '11 at 21:36
  • Thanks for this answer. I am really satisfied with it. The bounty will be given several hours later because the time barrier must be elapsed for 24 hours since the bounty is initiated. – Display Name Jan 13 '11 at 01:54
3

Well the 1/4 is not the remainder, it is the last part of the first argument. The line you are trying to change is the line which shows both polynomials. You can see this if you use a bit simpler arguments:

\documentclass{article}
\usepackage{polynom,array}
\usepackage[table]{xcolor}

\arrayrulecolor{red}
\arrayrulewidth=0.8pt
\def\strut{\rule[-6pt]{0pt}{12pt}}

\begin{document}
\polylongdiv[style=A]{x^3-1}{x^2+1}

\bigskip

\polylongdiv[style=B]{x^3-1}{x^2+1}

\bigskip

\polylongdiv[style=C]{x^3-1}{x^2+1}

\end{document}

Edit: The good news are that the remainder is accessible:

\documentclass{article}
\usepackage{polynom}
\usepackage[table]{xcolor}

\arrayrulecolor{red}
\arrayrulewidth=0.8pt
\def\strut{\rule[-6pt]{0pt}{12pt}}

\makeatletter
\renewcommand*\polylongdiv[1][]{%
    \begingroup
    \let\pld@stage\maxdimen \polyset{#1}%
    \pld@GetPoly{\pld@polya\pld@polyb}%
                {\pld@LongDividePoly\pld@polya\pld@polyb
                 \pld@PrintLongDiv\\                   
   $\expandafter\pld@PrintRemain\expandafter{\pld@remainder}$%new
    \endgroup \ignorespaces}}
\begin{document}
\polylongdiv[style=A,stage=5]{x^3-1}{x^2+1}

\bigskip

\polylongdiv[style=A]{\frac{3}{7}x^9+x^2-\frac{1}{4}}{\frac{9}{5}x^4-1}
\end{document}

But I don't have the time to find out how to insert it correctly in the array.

Ulrike Fischer
  • 327,261
  • @Ulrike: Using your example, the remainder is -x-1. But style=A only produces the remainder = -x. – Display Name Jan 12 '11 at 09:41
  • @Ulrike: -1/4 in my example is the last term of the remainder. :-) – Display Name Jan 12 '11 at 09:46
  • @Herbert: In mathematics, the remainder of x^3-1 divided by x^2+1 is -x-1. The polynom should drop -1 to make a final remainder. – Display Name Jan 12 '11 at 10:04
  • @Herbert: My final remainder is not -1/4 only. But -1/4 is the last term of the remainder. If you see the updated figure (second figure), it is the final diagram when doing this calculation. – Display Name Jan 12 '11 at 10:31
  • @Herbert + @Ulrike: The polynom.sty does NOT do wrong calculation but I need a modification such that at the final step the diagram show the total remainder as what many students used. The polynom.sty diagram looks unfamiliar for some students because it stops the operation without showing the final remainder. – Display Name Jan 12 '11 at 10:37
  • @Herbert: OK. How to visualize the final diagram as shown in the second figure is the problem we are talking. :-) – Display Name Jan 12 '11 at 10:45
  • Sorry your diagram is illogical. The x^2 and the 25/189x in the last line have been copied from the line above but the -1/4 is coming out of the blue. While I do understand that you want a final "sum" of the remainder parts it is wrong to delete the -1/4 from the starting polynomial. – Ulrike Fischer Jan 12 '11 at 10:49
  • @Ulrike: I forgot to put back the -1/4 and I did realize it. :-) Sorry. Now see the updated figure. It is what my teacher explained. – Display Name Jan 12 '11 at 13:34
  • @Herbert: Please see my updated figure. I forgot to put back the -1/4 when doing image manipulation. Sorry. It might be the source of confusion. – Display Name Jan 12 '11 at 13:35