5

I am writing worksheets with polynomial division using area models (often called "box method"):

enter image description here

The process is very slow for making dozens of problems, especially when the problems are bigger, like dividing a 5th degree polynomial by a 3rd degree polynomial. Is there a way to automate the creation of these box divisions in the future?

I would like to be able to type something like \polyboxdiv{6x^3+25x^2-24x+11}{x+5} (similar to \polylongdiv{6x^3+25x^2-24x+11}{x+5}from the polynom package) to automatically generate the box division representation.

Here is a link to the MWE:

\documentclass[addpoints]{exam}
\usepackage[utf8]{inputenc}
\usepackage[margin=.75 in]{geometry}
\usepackage{amsmath,amsfonts,amssymb,amsthm,color,srcltx,enumitem,bm,cancel,thmtools,physics}
\usepackage{multicol} %see http://stackoverflow.com/questions/1398127/breaking-a-list-into-multiple-columns-in-latex
\usepackage{multirow,array} %Used for the "hand-made" payoff matrix
\usepackage{hhline}
\usepackage{float}
\usepackage{polynom} %for polynomial long division and synthetic division, see http://texdoc.net/texmf-dist/doc/latex/polynom/polynom.pdf
\usepackage[table]{xcolor}

%For solution tables, see https://tex.stackexchange.com/questions/133397/printanswer-in-table-in-exam-class
\makeatletter
\newcommand{\st}[1]{\ifprintanswers\begingroup\Solution@Emphasis#1\if@shadedsolutions%
{\cellcolor{white}}
\else
\fi\endgroup\else\phantom{#1}\fi}
\makeatother
\SolutionEmphasis{\color{black}} %color font solutions
%\unframedsolutions  %Not necessary for table environment
%\shadedsolutions    %gives background color to solutions
\printanswers

\begin{document}
\section{Polynomials Division Test}
\begin{questions}

\question[40]
For the division problem
\begin{equation*}
(6x^3+25x^2-24x+11) \divisionsymbol (x+5)
\end{equation*}
\begin{parts}

\begin{multicols}{2}
\part Divide using an area model with a left divisor.
{\renewcommand{\arraystretch}{2}
\begin{table}[H]
\centering
\begin{tabular}{*{1}{>{\centering\arraybackslash}m{1.25cm}}|*{4}{>{\centering\arraybackslash}m{1.25cm}|}}
\multicolumn{1}{c}{} & \multicolumn{1}{c}{$\st{6x^2}$}  & \multicolumn{1}{c}{$\st{-5x}$} & \multicolumn{1}{c}{$\st{+1}$} & \multicolumn{1}{c}{} \\ \hhline{~----}
\multirow{2}*{} $\st{x}$   & $\st{6x^3}$    & $\st{-5x^2}$   & $\st{x}$ & $\st{6}$ \\ \hhline{~----}
                $\st{+5}$  & $\st{30x^2}$   & $\st{-25x}$    & $\st{5}$ & \cellcolor{black!10} \\ \hhline{~----}
\end{tabular}
\end{table}}
\columnbreak
\part Divide using an area model with an upper divisor.
{\renewcommand{\arraystretch}{2}
\begin{table}[H]
\centering
\begin{tabular}{*{1}{>{\centering\arraybackslash}m{1.25cm}}|*{2}{>{\centering\arraybackslash}m{1.25cm}|}}
\multicolumn{1}{c}{} & \multicolumn{1}{c}{$\st{x}$}  & \multicolumn{1}{c}{$\st{+5}$} \\ \hhline{~--}
\multirow{2}*{} $\st{6x^2}$ & $\st{6x^3}$   & $\st{30x^2}$          \\ \hhline{~--}
                $\st{-5x}$  & $\st{-5x^2}$  & $\st{-25x}$           \\ \hhline{~--}
                $\st{+1}$   & $\st{x}$      & $\st{5}$              \\ \hhline{~--}
                            & $\st{6}$      & \cellcolor{black!10}  \\ \hhline{~--}
\end{tabular}
\end{table}}
\end{multicols}
\vspace*{\stretch{1}}

\begin{multicols}{2}
\part Divide using the classic long-division algorithm.
\begin{solution}
\begin{center}
\polylongdiv{6x^3+25x^2-24x+11}{x+5}
\end{center}
\end{solution}
\columnbreak
\part Divide using Horner's method of synthetic division.
\begin{solution}
\begin{center}
\polyhornerscheme[x=-5]{6x^3+25x^2-24x+11}
\end{center}
\end{solution}
\end{multicols}
\vspace*{\stretch{1}}

\end{parts}
\end{questions}
\end{document}

Thank you!

7
  • Please add a minimal working example (MWE) directly into your question instead of just providing a link to overleaf. See also: tex.meta.stackexchange.com/q/8402/134144
    – leandriis
    Commented Oct 25, 2019 at 20:33
  • I think I accidentally deleted the MWE during an edit, but it's back now. Thank you! Commented Oct 25, 2019 at 22:14
  • \usepackage[table]{xcolor}
    – cfr
    Commented Oct 26, 2019 at 2:45
  • They won't match because the widths of the preceding stuff differ and \vln is just being used in the content of cells rather than between them.
    – cfr
    Commented Oct 26, 2019 at 2:48
  • The sagetex package lets you incorporate a computer algebra system into your LaTeX. This helps with automation and with many other math problems you are working with. See, for example, my answer to the question here.
    – DJP
    Commented Oct 26, 2019 at 15:42

2 Answers 2

1

I can help with a partial answer: how to automate the problem. The formatting, however, is quite tedious. Your top row is the quotient and the box in the rightmost column is your remainder. The computer algebra system SAGE, which is Python based, can easily tell us the quotient and remainder with q,r = numerator.quo_rem(denominator). The function FormatTerm(a,deg) will help format the individual terms and the function PolyBoxDivL(f,g) will format the table. Not as nicely as you wanted, though.

\documentclass[addpoints]{exam}
\usepackage[utf8]{inputenc}
\usepackage[margin=.75 in]{geometry}
\usepackage{amsmath,amsfonts,amssymb,amsthm,color,srcltx,enumitem,bm,cancel,thmtools,physics}
\usepackage{multicol}
\usepackage{multirow,array}
\usepackage[table]{xcolor}
\usepackage{sagetex}          %gives us access to SAGE
%%%%%%%%%%%%%%%
\begin{document}
\section{Polynomials Division Test}
\begin{sagesilent}
R.<x> = PolynomialRing(ZZ)    #### Ring of polynomials with integer coefficients
def FormatTerm(a,deg):
    if deg == 0:
        return "$%s$"%(a)

    if deg == 1:
        if a == 1:
            return "$x$"
        elif a == -1:
            return "$-x$"
        else:
            return "$%s x$"%(a)

    if deg >1:
        if a == 1:
            return "$x^{%s}$"%(deg)
        else:
            return "$%s x^{%s}$"%(a,deg)

def PolyBoxDivL(f,g):
    numerator = f
    denominator = g
    q,r = numerator.quo_rem(denominator)
    degreeq = q.degree()
    length = q.number_of_terms()+2   ##### +1 to include the remainder and vertical poly
    width = g.number_of_terms()
########### Table header    
    output = r""
    output += r"\begin{tabular}{c|"+"c|"*(length)+"}"
    output += r"\cline{2-%s}"%(length)
    output += r" & "
    for i in range(degreeq,-1,-1):
        if q[i] != 0:
            output += r"%s & "%(FormatTerm(q[i],i))
    output += r"\rule{0pt}{13pt} \\[6pt]\cline{2-%s}"%(length)
#### rows which aren't the header 

    for i in range(g.degree(),-1,-1):
        if denominator[i] != 0:
            output += r"\rule{0pt}{18pt} %s &"%(FormatTerm(denominator[i],i))
            for j in range(q.degree(),-1,-1):
                if q[j] != 0:
                    output += r"%s & "%(FormatTerm(denominator[i]*q[j],i+j))
            if i == g.degree():
                output += r"$%s$ \\[8pt]\cline{2-%s}"%(latex(r),length)
            else:
                output += r"\cellcolor{black!10} \\[8pt]\cline{2-%s}"%(length)
    output += r"\end{tabular}"

    return output
\end{sagesilent}

Use the area model with a left divisor to find $(6x^3+25x^2-24x+11)   \divisionsymbol (x+5)$:\\\\
\begin{sagesilent}
Q1 = PolyBoxDivL(6*x^3+25*x^2-24*x+11,x+5)
\end{sagesilent}
\sagestr{Q1}

\vspace{.5cm}
Use the area model with a left divisor to find $(5x^5-3x^2+x-1) \divisionsymbol (x^2+x+1)$:\\\\
\begin{sagesilent}
Q2 = PolyBoxDivL(5*x^5-3*x^2+x-1,x^2+x+1)
\end{sagesilent}
\sagestr{Q2}

\vspace{.5cm}
Use the area model with a left divisor to find $(x^4-1) \divisionsymbol (x^2-1)$:\\\\
\begin{sagesilent}
Q3 = PolyBoxDivL(x^4-1,x^2-1)
\end{sagesilent}
\sagestr{Q3}

\vspace{.5cm}
Use the area model with a left divisor to find $(x^9-7x^4) \divisionsymbol (x^3-x+4)$:\\\\
\begin{sagesilent}
Q4 = PolyBoxDivL(x^9-7*x^4,x^3-x+4)
\end{sagesilent}
\sagestr{Q4}
\end{document}

SAGE is not part of LaTeX. It needs to be downloaded to your machine or, even easier, accessed through a free Cocalc account. The code running in Cocalc looks like this:

enter image description here

A very important line to notice is output += r"\begin{tabular}{c|"+"c|"*(length)+"}". This allows the table to vary the number of columns because SAGE can calculate the number of nonzero terms needed to calculate the number of columns. The area model with an upper(?) divisor is handled in similar fashion. The problem is calculated by \begin{sagesilent} Q4 = PolyBoxDivL(x^9-7*x^4,x^3-x+4) \end{sagesilent} and then placing the resulting string into the document with \sagestr{Q4}.

EDIT: In response to comments below, I've added 0 coefficient terms so that the exponents are the same on diagonals. I don't know how to get the table formatting to handle vertical lines for some rows. I've chosen to color the cells to resolve the problem

\documentclass[addpoints]{exam}
\usepackage[utf8]{inputenc}
\usepackage[margin=.75 in]{geometry}
     \usepackage{amsmath,amsfonts,amssymb,amsthm,color,srcltx,enumitem,bm,cancel,thmtools,physics}
\usepackage{multicol}
\usepackage{multirow,array}
\usepackage[table]{xcolor}
\usepackage{sagetex}          %gives us access to SAGE
%%%%%%%%%%%%%%%
\begin{document}
\section{Polynomials Division Test}
\begin{sagesilent}
R.<x> = PolynomialRing(ZZ)    #### Ring of polynomials with integer   coefficients
def FormatTerm(a,deg):
    if deg == 0:
        return "$%s$"%(a)

    if deg == 1:
        if a == 1:
            return "$x$"
        elif a == -1:
            return "$-x$"
        else:
            return "$%s x$"%(a)

    if deg >1:
        if a == 1:
            return "$x^{%s}$"%(deg)
        else:
            return "$%s x^{%s}$"%(a,deg)

def PolyBoxDivL(f,g):
    numerator = f
    denominator = g
    q,r = numerator.quo_rem(denominator)
    length = q.degree()+3
    width = g.degree()+1
########### Table header    
    output = r""
    output += r"\begin{tabular}{"+"c"*(length)+"}"
    output += r" & "
    for i in range(q.degree(),-1,-1):
        output += r" \cellcolor{black!10} %s & "%(FormatTerm(q[i],i))
    output += r"\rule{0pt}{13pt} \\[6pt]"
#### rows which aren't the header

    for i in range(g.degree(),-1,-1):
        output += r"\rule{0pt}{18pt} \cellcolor{black!10} %s &"%(FormatTerm(denominator[i],i))
        for j in range(q.degree(),-1,-1):
            output += r"\cellcolor{orange!10} %s & "%(FormatTerm(denominator[i]*q[j],i+j))
        if i == g.degree():
            output += r"\cellcolor{blue!10} $%s$ \\[8pt]"%(latex(r))
        else:
            output += r"\cellcolor{blue!10} \\[8pt]"
    output += r"\end{tabular}"

    return output
\end{sagesilent}

Use the area model with a left divisor to find $(6x^3+25x^2-24x+11)     \divisionsymbol (x+5)$:\\\\
\begin{sagesilent}
Q1 = PolyBoxDivL(6*x^3+25*x^2-24*x+11,x+5)
\end{sagesilent}
\sagestr{Q1}

\vspace{.5cm}
Use the area model with a left divisor to find $(5x^5-3x^2+x-1) \divisionsymbol (x^2+x+1)$:\\\\
\begin{sagesilent}
Q2 = PolyBoxDivL(5*x^5-3*x^2+x-1,x^2+x+1)
\end{sagesilent}
\sagestr{Q2}

\vspace{.5cm}
Use the area model with a left divisor to find $(x^4-1) \divisionsymbol (x^2-1)$:\\\\
\begin{sagesilent}
Q3 = PolyBoxDivL(x^4-1,x^2-1)
\end{sagesilent}
\sagestr{Q3}

\vspace{.5cm}
Use the area model with a left divisor to find $(x^9-7x^4) \divisionsymbol (x^3-x+4)$:\\\\
\begin{sagesilent}
Q4 = PolyBoxDivL(x^9-7*x^4,x^3-x+4)
\end{sagesilent}
\sagestr{Q4}
\end{document}

The output is

enter image description here

8
  • Thank you! I will look into learning Sage. Is there some way to remove the table lines / rules around the quotient? This way, both the divisor and quotient are "outside" the box? Commented Nov 22, 2019 at 17:41
  • There might be; I searched and found this but I found myself wasting too much time getting the formatting like your example. I just don't know much about table formatting.
    – DJP
    Commented Nov 22, 2019 at 19:08
  • Hi DJP, I like that you were able to remove the gridlines/table rules for the box lengths/dimensions along the left, so the numbers appear "outside" the table. Is it possible to use the same approach for the numbers along the top row, so they also appear "outside" the table? Commented Apr 16, 2020 at 3:53
  • Also, a nice feature of area models is the exponents along the diagonals are always equal. In the first three of your examples this is the case, but not in the fourth example. I think this is because there aren't "placeholder" rows & columns, e.g. a 0x^2 row and 0x^5 column. Is this modification possible? Thanks again! Commented Apr 16, 2020 at 3:54
  • @Mathemanic it's almost certainly possible for the numbers to be outside the table. I only use tables in a most basic form (vertical lines to separate, avoid "cells") so I don't know how that's typically done. As I mentioned in my 2nd comment, I was wasting too much time on formatting issues when main issue had been solved. As long as there is a rule to do it programmatically it has to be possible. I'm not seeing the diagonal in second case being equal (looks like 3, 3, 2) so I'm not sure I understand exactly what you want.
    – DJP
    Commented Apr 16, 2020 at 22:31
0

What I am describing is not an answer but something to point you in the right direction

Since it will not fit in the comments column I am posting it here

Hope the mods will not mind!

What you are asking is to

  • Parse the value of the polynomial into a tabular environment

  • The table design varies to four different designs

  • The process is automated from the time you feed in the polynomial in the numerator and denominator portion

Please see the answers on this website at

Automatically creating a table from datatool using references in the text

Formatting complex table from CSV using datatool

\begin{filecontents*}{\jobname.dat}
  Hammer001,   Hammer,    1 ,  0 , 1 , 10 , 1 , light (add some words here to wrap around)
  Hammer002,   Hammer,    2 ,  0 , 1 , 10 , 1 , heavy
  Hammer003,   Hammer,    3 ,  0 , 1 , 10 , 1 , really heavy
  Longsword001,Longsword, 1 , -1 , 2 , 75 , 2 , one-handed
  Longsword002,Longsword, 2 , -1 , 2 , 75 , 2 , two-handed
  Longsword003,Longsword, 3 , -1 , 2 , 75 , 2 , three-handed
\end{filecontents*}

\documentclass{article}
\usepackage{array}
\usepackage{datatool}
\usepackage{longtable}
\usepackage{etoolbox}

\newcommand{\colhead}[1]{\multicolumn{1}{>{\bfseries}l}{#1}}
\newcounter{tabenum}\setcounter{tabenum}{0}
\newcommand{\nextnuml}[1]{%
  \refstepcounter{tabenum}\thetabenum.\label{#1}%
}

\newcommand{\PrintDTLTable}[2][]{%
 % #1 = list of rowIDs
 % #2 = database to search
  \begin{longtable}{l l l l l l l m{2in}}
  & \colhead{Label} & \colhead{Cost} & \colhead{Weight} &
    \colhead{PropA} & \colhead{PropB} & \colhead{PropC} & \colhead{Description}\\
  \hline
  \DTLforeach
    [%
     \(\equal{#1}{}\AND\DTLisSubString{\ReferencedIDs}{\RowID}\)
     \OR
     \(\DTLisSubString{#1}{\RowID}\AND\DTLisSubString{\ReferencedIDs}{\RowID}\)%
    ]
    {#2}{%
      \RowID=RowID,%
      \Label=Label,%
      \Cost=Cost,%
      \Weight=Weight,%
      \PropA=PropA,%
      \PropB=PropB,%
      \PropC=PropC,%
      \Description=Description%
     }{%
       \nextnuml{\RowID} & \Label &\Cost & \Weight & \PropA & \PropB & \PropC & \Description \\
    }%
    \end{longtable}
}

\makeatletter
\let\oldref\ref
\def\ref#1{%
  \immediate\write\@auxout{%
    \string\gappto\string\ReferencedIDs{#1,}%
  }%
  \oldref{#1}%
}
\def\ReferencedIDs{}
\makeatother


\begin{document}

% \DTLsetseparator{&}% Define separator of the data
\DTLloaddb[noheader,keys={RowID,Label,Cost,Weight,PropA,PropB,PropC,Description}]{myDB}{\jobname.dat}

% \DTLdisplaydb{myDB}% Useful for debugging.

\PrintDTLTable[Hammer001,Hammer003,Longsword003]{myDB}

\PrintDTLTable[Hammer002,Longsword002]{myDB}

This is a reference to ~\ref{Hammer003}.

This is a reference to ~\ref{Longsword002}.

\end{document}

Output in tabular form is as below

enter image description here

The first part of the code

\begin{filecontents*}{\jobname.dat} Hammer001, Hammer, 1 , 0 , 1 , 10 , 1 , light (add some words here to wrap around) Hammer002, Hammer, 2 , 0 , 1 , 10 , 1 , heavy Hammer003, Hammer, 3 , 0 , 1 , 10 , 1 , really heavy Longsword001,Longsword, 1 , -1 , 2 , 75 , 2 , one-handed Longsword002,Longsword, 2 , -1 , 2 , 75 , 2 , two-handed Longsword003,Longsword, 3 , -1 , 2 , 75 , 2 , three-handed \end{filecontents*}

is similar to advertising the polynomial which is then parsed into a table form

There are similar queries on the webpage which you may like to explore

enter image description here

I am sure one of the experts on this site will shortly get back to you

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .