我正在使用面积模型(通常称为“盒子方法”)编写多项式除法的工作表:
对于数十个问题,这个过程非常缓慢,尤其是当问题较大时,例如用五次多项式除以三次多项式。将来有没有办法自动创建这些框划分?
我希望能够输入类似的内容\polyboxdiv{6x^3+25x^2-24x+11}{x+5}
(类似于\polylongdiv{6x^3+25x^2-24x+11}{x+5}
来自polynom
包的内容)来自动生成框划分表示。
这里有一个关联致 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}
谢谢你!
答案1
我可以帮你解答部分问题:如何自动化解决问题。但是,格式化相当繁琐。顶行是商,最右边一列的框是余数。计算机代数系统智者基于 Python 的 可以轻松告诉我们 的商和余数q,r = numerator.quo_rem(denominator)
。 函数FormatTerm(a,deg)
将帮助格式化各个项,函数PolyBoxDivL(f,g)
将格式化表格。不过,效果不如你想要的那么好。
\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 不是 LaTeX 的一部分。它需要下载到您的机器上,或者更简单的是,通过免费的可钙账户。Cocalc 中运行的代码如下所示:
需要注意的非常重要的一行是output += r"\begin{tabular}{c|"+"c|"*(length)+"}"
。这允许表格改变列数,因为 SAGE 可以计算计算列数所需的非零项数。具有上除数(?)的面积模型以类似的方式处理。问题通过 计算,\begin{sagesilent} Q4 = PolyBoxDivL(x^9-7*x^4,x^3-x+4) \end{sagesilent}
然后将结果字符串放入带有 的文档中\sagestr{Q4}
。
编辑:为了回应下面的评论,我添加了 0 系数项,以便指数在对角线上相同。我不知道如何让表格格式处理某些行的垂直线。我选择给单元格上色来解决这个问题
\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}
输出为
答案2
我所描述的不是一个答案,而是一个为你指明正确方向的东西
由于它不适合放在评论栏,所以我把它发布在这里
希望版主不会介意!
你要问的是
将多项式的值解析到表格环境中
桌子设计有四种不同的设计
从你在分子和分母部分输入多项式开始,这个过程就是自动化的
请参阅此网站上的答案
\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}
表格形式的输出如下
第一部分代码
\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*}
类似于对多项式进行广告宣传,然后将其解析为表格形式
网页上有类似的查询,您可能想探索一下
我相信本网站的一位专家很快就会回复您