问题描述
我想知道 Maxima 中是否有 sqrt
函数的泛化?特别是,我希望能够控制 x^(a/b)
是否显示为 $x^{\frac{a}{b}}$
或 $\sqrt[b]{x^{a}}$
。
我在文档索引中搜索了 root
,但没有找到任何我认为相关的内容,并且没有来自 sqrtdispflag
条目的任何链接。
解决方法
这是一个有趣的问题,再次表明 Maxima 可以从更灵活的 TeX 输出系统中受益,但我离题了。这是一个可能的解决方案。
这使用模式匹配(在 defmatch
中)来识别某商的表达式以供进一步处理,如果匹配成功,则提取商的分子和分母。模式匹配器无法识别 b/c^2
之类的东西——这可能应该被认为是模式匹配器中的一个错误。
默认的 TeX 输出函数是一个名为 TEX-MEXPT 的 Lisp 函数。我写了一行 Lisp 代码(不可否认)来调用它。
/* tex_mexpt.mac -- look for quotient in exponent
* copyright 2021 by Robert Dodier
* I release this work under terms of the GNU General Public License
*/
matchdeclare (aa,all);
matchdeclare (bb,"#"(1));
defmatch (matches_quotient,aa/bb);
defmatch (matches_minus_quotient,-aa/bb);
:lisp (defun $tex_mexpt_default (e) (let (lop rop) (apply 'concatenate 'string (tex-mexpt e nil nil))))
my_tex_mexpt_quotient (base,expt_num,expt_denom) :=
if expt_num = 1
then printf(false,"\\sqrt[~a]{~a}",tex1 (expt_denom),tex1 (base))
else printf(false,tex_mexpt_default (base^expt_num));
my_tex_mexpt_minus_quotient (base,"\\sqrt[~a]{{1}\\over{~a}}",tex_mexpt_default (base^-expt_num));
my_tex_mexpt (e) :=
if tex_mexpt_look_for_quotient
then block ([base,expt],[base,expt]: args(e),if matches_quotient(expt) # false
then my_tex_mexpt_quotient (base,aa,bb)
elseif matches_minus_quotient(expt) # false
then my_tex_mexpt_minus_quotient (base,bb)
else tex_mexpt_default (e))
else tex_mexpt_default (e);
/* examples */
stringdisp: true $
verbatim_and_equation (e) ::= printf (S,"\\begin{verbatim}~%~a~%\\end{verbatim}~%$$~a$$~%",string(e),tex1(e));
S: openw ("/tmp/foo.tex");
printf (S,"\\documentclass{article}~%\\begin{document}~%");
/* first without my_tex_mexpt at all */
verbatim_and_equation (a^(b/c));
verbatim_and_equation (a^-(b/c));
verbatim_and_equation (a^(-b/c));
verbatim_and_equation (a^(1/c));
verbatim_and_equation (a^-(1/c));
verbatim_and_equation (a^(-1/c));
verbatim_and_equation ((1 + (1 - x)^((y - z)/(y - w)))/((2*u - v)^(1/(n + 1))));
/* now enable my_tex_mexpt */
texput ("^",my_tex_mexpt);
tex_mexpt_look_for_quotient:true;
verbatim_and_equation (a^(b/c));
verbatim_and_equation (a^-(b/c));
verbatim_and_equation (a^(-b/c));
verbatim_and_equation (a^(1/c));
verbatim_and_equation (a^-(1/c));
verbatim_and_equation (a^(-1/c));
verbatim_and_equation ((1 + (1 - x)^((y - z)/(y - w)))/((2*u - v)^(1/(n + 1))));
/* verify disabled produces same output as originally */
tex_mexpt_look_for_quotient:false;
verbatim_and_equation (a^(b/c));
verbatim_and_equation (a^-(b/c));
verbatim_and_equation (a^(-b/c));
verbatim_and_equation (a^(1/c));
verbatim_and_equation (a^-(1/c));
verbatim_and_equation (a^(-1/c));
verbatim_and_equation ((1 + (1 - x)^((y - z)/(y - w)))/((2*u - v)^(1/(n + 1))));
printf (S,"\\end{document}~%");
close(S);
正如你所看到的,我在里面放了一些例子来验证输出。您可以通过 maxima --batch=foo.mac
或任何已保存文件的名称来执行它。它在 /tmp/foo.tex
中生成输出。我用 latex
处理,然后用 xdvi
查看。
作为记录,这是我得到的 foo.tex
输出。
\documentclass{article}
\begin{document}
\begin{verbatim}
a^(b/c)
\end{verbatim}
$$a^{{{b}\over{c}}}$$
\begin{verbatim}
a^-(b/c)
\end{verbatim}
$${{1}\over{a^{{{b}\over{c}}}}}$$
\begin{verbatim}
a^((-b)/c)
\end{verbatim}
$${{1}\over{a^{{{b}\over{c}}}}}$$
\begin{verbatim}
a^(1/c)
\end{verbatim}
$$a^{{{1}\over{c}}}$$
\begin{verbatim}
a^-(1/c)
\end{verbatim}
$${{1}\over{a^{{{1}\over{c}}}}}$$
\begin{verbatim}
a^((-1)/c)
\end{verbatim}
$${{1}\over{a^{{{1}\over{c}}}}}$$
\begin{verbatim}
(1+(1-x)^((y-z)/(y-w)))/(2*u-v)^(1/(n+1))
\end{verbatim}
$${{\left(1-x\right)^{{{y-z}\over{y-w}}}+1}\over{\left(2\,u-v\right)^{{{1}\over{n+1}}}}}$$
\begin{verbatim}
a^(b/c)
\end{verbatim}
$$\sqrt[c]{a^{b}}$$
\begin{verbatim}
a^-(b/c)
\end{verbatim}
$${{1}\over{\sqrt[c]{a^{b}}}}$$
\begin{verbatim}
a^((-b)/c)
\end{verbatim}
$${{1}\over{\sqrt[c]{a^{b}}}}$$
\begin{verbatim}
a^(1/c)
\end{verbatim}
$$\sqrt[c]{a}$$
\begin{verbatim}
a^-(1/c)
\end{verbatim}
$${{1}\over{\sqrt[c]{a}}}$$
\begin{verbatim}
a^((-1)/c)
\end{verbatim}
$${{1}\over{\sqrt[c]{a}}}$$
\begin{verbatim}
(1+(1-x)^((y-z)/(y-w)))/(2*u-v)^(1/(n+1))
\end{verbatim}
$${{\sqrt[y-w]{\left(1-x\right)^{y-z}}+1}\over{\sqrt[n+1]{2\,u-v}}}$$
\begin{verbatim}
a^(b/c)
\end{verbatim}
$$a^{{{b}\over{c}}}$$
\begin{verbatim}
a^-(b/c)
\end{verbatim}
$${{1}\over{a^{{{b}\over{c}}}}}$$
\begin{verbatim}
a^((-b)/c)
\end{verbatim}
$${{1}\over{a^{{{b}\over{c}}}}}$$
\begin{verbatim}
a^(1/c)
\end{verbatim}
$$a^{{{1}\over{c}}}$$
\begin{verbatim}
a^-(1/c)
\end{verbatim}
$${{1}\over{a^{{{1}\over{c}}}}}$$
\begin{verbatim}
a^((-1)/c)
\end{verbatim}
$${{1}\over{a^{{{1}\over{c}}}}}$$
\begin{verbatim}
(1+(1-x)^((y-z)/(y-w)))/(2*u-v)^(1/(n+1))
\end{verbatim}
$${{\left(1-x\right)^{{{y-z}\over{y-w}}}+1}\over{\left(2\,u-v\right)^{{{1}\over{n+1}}}}}$$
\end{document}