![为多个偏微分创建一个宏](https://linux22.com/image/353079/%E4%B8%BA%E5%A4%9A%E4%B8%AA%E5%81%8F%E5%BE%AE%E5%88%86%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AA%E5%AE%8F.png)
请看以下MWE:
\documentclass{article}
\usepackage{xparse}
\NewDocumentCommand\pdiff{s o m O{1} g O{1} g O{1}}% Star, function, variable 1, how many differentiations, ...
{\newcount\pdiffn% Count the number of differentiations
\advance\pdiffn #4\relax%
\IfValueT{#5}{\advance\pdiffn #6\relax}%
\IfValueT{#7}{\advance\pdiffn #8\relax}%
\frac%
{\partial \ifnum\pdiffn >1\relax ^{\the\pdiffn} \fi%
\IfValueT{#2}{#2}}%
{\IfValueT{#7}{\mathinner{\partial #7 \ifnum#8 >1\relax ^{#8} \fi}}%
\IfValueT{#5}{\mathinner{\partial #5 \ifnum#6 >1\relax ^{#6} \fi}}%
\mathinner{\partial #3 \ifnum#4 >1\relax ^{#4} \fi}}}
\begin{document}
\Huge
\[ \pdiff[f(x,y,z)]{y} \]
\[ \pdiff[f(x,y,z)]{y}[3]{x}{z} \]
\[ \pdiff[f(x,y,z)]{y}[9999]{x}[3333]{z}[8888] \]
\end{document}
(忽略星号类型参数,我用它来创建平面分形(现在不需要)。)
如果微分限制为 3 个变量,这种方法效果很好。是否可以将此宏扩展到更多变量?这样就行不通了,因为已经有 8 个参数了。
我知道“SplitList”和“ProcessList”,但这里更复杂,因为我必须为每个变量添加区分数。
有任何想法吗?
答案1
使用键值的方法listofitems
。语法如下
\pdiff[y=3,x,z,p=2,d=3,q,r=2,s]{f(x,y,z,p,d,q,r,s)}
这是 MWE
\documentclass{article}
\usepackage{listofitems,tikz}
\newcounter{totalindex}
\newcommand\pdiff[2][1]{%
\setsepchar{,/=}%
\readlist\Partialvars{#1}%
\setcounter{totalindex}{0}%
\foreach \Varindex in {1,...,\listlen\Partialvars[]}%
{%
\ifnum\listlen\Partialvars[\Varindex]>1\relax%
\addtocounter{totalindex}{\Partialvars[\Varindex,2]}%
\else%
\stepcounter{totalindex}%
\fi%
}%
\frac{\partial\ifnum\thetotalindex>1\relax^{\thetotalindex}\fi#2}%
{%
\foreach\Varindex in {1,...,\listlen\Partialvars[]}%
{%
\partial\Partialvars[\Varindex,1]%
\ifnum\listlen\Partialvars[\Varindex]>1\relax^{\Partialvars[\Varindex,2]}\fi%
}%
}%
}
\begin{document}
\[ \pdiff[y]{f(x,y,z)} \]
\[ \pdiff[y=3,x,z]{f(x,y,z)} \]
\[ \pdiff[y=9999, x=3333, z=8888]{f(x,y,z)} \]
\[ \pdiff[y=3,x,z,p=2,d=3,q,r=2,s]{f(x,y,z,p,d,q,r,s)} \]
\end{document}
虽然tikz
\foreach
上面使用的循环是遍历索引的易于理解的构造,但可以tikz
使用包中内置的循环构造完全消除包的使用listofitems
。但在这种情况下,循环遍历列表的项目,并且可以通过 间接访问索引号\<loop-variable>cnt
。
\documentclass{article}
\usepackage{listofitems}
\newcounter{totalindex}
\newcommand\pdiff[2][1]{%
\setsepchar{,/=}%
\readlist\Partialvars{#1}%
\setcounter{totalindex}{0}%
\foreachitem \Var \in \Partialvars[]%
{%
\ifnum\listlen\Partialvars[\Varcnt]>1\relax%
\addtocounter{totalindex}{\Partialvars[\Varcnt,2]}%
\else%
\stepcounter{totalindex}%
\fi%
}%
\frac{\partial\ifnum\thetotalindex>1\relax^{\thetotalindex}\fi#2}%
{%
\foreachitem \Var \in \Partialvars%
{%
\partial\Partialvars[\Varcnt,1]%
\ifnum\listlen\Partialvars[\Varcnt]>1\relax^{\Partialvars[\Varcnt,2]}\fi%
}%
}%
}
\begin{document}
\[ \pdiff[y]{f(x,y,z)} \]
\[ \pdiff[y=3,x,z]{f(x,y,z)} \]
\[ \pdiff[y=9999, x=3333, z=8888]{f(x,y,z)} \]
\[ \pdiff[y=3,x,z,p=2,d=3,q,r=2,s]{f(x,y,z,p,d,q,r,s)} \]
\end{document}
答案2
答案3
您可以使用逗号分隔列表来列出变量。我使用的惯例是
x/2,y/3,z
代表“两次X,三次是并且有一次是x/2,y/3,z/1
”。如果愿意,您也可以使用。
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\pdiff}{smm}
{
\IfBooleanTF{#1}
{% with \pdiff* call the inner function for inline
\egreg_pdiff_inline:nn { #2 } { #3 } % TO DO
}
{% with \pdiff call the inner function for display
\egreg_pdiff_frac:nn { #2 } { #3 }
}
}
% allocate some variables
\int_new:N \l__egreg_pdiff_total_int
\seq_new:N \l__egreg_pdiff_vars_seq
\seq_new:N \l__egreg_pdiff_var_seq
\tl_new:N \l__egreg_pdiff_denom_tl
\cs_new_protected:Nn \egreg_pdiff_frac:nn
{
% clear the variables to be given values later
\int_zero:N \l__egreg_pdiff_total_int
\tl_clear:N \l__egreg_pdiff_denom_tl
% split the second argumen at commas
\seq_set_split:Nnn \l__egreg_pdiff_vars_seq { , } { #2 }
% map this sequence for adding to the denominator and
% computing the number of derivatives
\seq_map_function:NN \l__egreg_pdiff_vars_seq \__egreg_pdiff_var:n
% now print:
% \l__egreg_pdiff_total_int is set to the number of derivatives
% \l__egreg_pdiff_denom_tl contains the denominator
\frac
{
\partial
\int_compare:nT { \l__egreg_pdiff_total_int > 1 }
{ \sp { \int_to_arabic:n { \l__egreg_pdiff_total_int } } }
#1
}
{
\tl_use:N \l__egreg_pdiff_denom_tl
}
}
\cs_new_protected:Nn \__egreg_pdiff_var:n
{
% split the argument at / (it should be in the form 'x' or 'x/2')
\seq_set_split:Nnn \l__egreg_pdiff_var_seq { / } { #1 }
% if the sequence has one term, no exponent is present
\int_compare:nTF { \seq_count:N \l__egreg_pdiff_var_seq < 2 }
{% simple variable: add to the denominator and increment the number
\tl_put_right:Nn \l__egreg_pdiff_denom_tl { \partial #1 }
\int_incr:N \l__egreg_pdiff_total_int
}
{% multiple: add to the denominator and increment the number
% item 1 contains the variable, item 2 the exponent
\tl_put_right:Nx \l__egreg_pdiff_denom_tl
{
\partial
\seq_item:Nn \l__egreg_pdiff_var_seq { 1 }
\sp { \seq_item:Nn \l__egreg_pdiff_var_seq { 2 } }
}
\int_add:Nn \l__egreg_pdiff_total_int { \seq_item:Nn \l__egreg_pdiff_var_seq { 2 } }
}
}
\ExplSyntaxOff
\begin{document}
\[
\pdiff{f(x,y)}{x}
\quad
\pdiff{f(x,y,z)}{x/20,y/32,z}
\]
\end{document}
答案4
更新最后请参阅更好的版本。
尽管有实现这一点的软件包,但我还是提出了另一种方法,因为xparse
这里已经负责了;-)
expl3
拆分导数说明符列表并构建标记列表的方法:
语法:\pdiff{x/3,y/4,z/17}{f(x,y,z)}
将指定关于 x 的第三导数、关于 y 的第四个导数以及关于 z 的第十七个导数。
省略幂或的/
将被解释为导数阶1
。
(应添加对意外负数的检查)
函数参数未经评估或检查。
导数的数量是内部计算的。
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\cs_generate_variant:Nn \int_add:Nn {Nx}
\seq_new:N \l_pdiff_deriv_seq
\NewDocumentCommand{\pdiffs}{mm}{%
\int_zero:N \l_tmpa_int
\seq_set_from_clist:Nn \l_tmpa_seq {#1} % Split the comma list
\seq_map_inline:Nn \l_tmpa_seq {% Traverse the local entries
\seq_set_split:Nnn \l_tmpb_seq {/} {##1}% Split into variable and order
\tl_put_right:Nn \l_tmpa_tl {\partial}% Built the nominator list
\int_compare:nNnTF {\seq_count:N \l_tmpb_seq } = {\c_one} {% Is there only one split element?
\seq_put_right:Nn \l_tmpb_seq {\c_one}% Yes
\tl_put_right:Nx \l_tmpa_tl {\seq_item:Nn \l_tmpb_seq {1}} % Add now 'power'
}{%
\int_compare:nNnTF {\seq_item:Nn \l_tmpb_seq {2}} = {\c_one } {
% Is the derivative order one --> no power?
\tl_put_right:Nx \l_tmpa_tl {\seq_item:Nn \l_tmpb_seq {1}}
}{
\tl_put_right:Nx \l_tmpa_tl {\seq_item:Nn \l_tmpb_seq {1}^{\seq_item:Nn \l_tmpb_seq {2}}}
}
}
\int_add:Nx \l_tmpa_int {\seq_item:Nn \l_tmpb_seq {2}}
}
% Display the list
\mathinner{\frac{\partial^{\int_use:N\l_tmpa_int}#2}{\tl_use:N \l_tmpa_tl}}
}
\ExplSyntaxOff
\begin{document}
\Huge
\[\pdiffs{x/5,y/3}{f(x,y,z)}\]
\[\pdiffs{x/4,y/8,z/17}{f(x,y,z)}\]
\end{document}
改进的版本带有检查和变量自动扩展的功能:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\cs_generate_variant:Nn \int_add:Nn {Nx}
\cs_generate_variant:Nn \int_set:Nn {Nx}
\seq_new:N \l_pdiff_var_seq
\msg_new:nnn {pdiff}{negativedifforder}{Error:~The~order~of~derivative~is~negative~for~variable~#1!}
\NewDocumentCommand{\pdiffs}{mm}{%
\int_zero:N \l_tmpa_int
\tl_clear:N \l_tmpa_tl
\seq_clear:N \l_pdiff_var_seq
\seq_set_from_clist:Nn \l_tmpa_seq {#1}
\seq_map_inline:Nn \l_tmpa_seq {%
\seq_set_split:Nnn \l_tmpb_seq {/} {##1}
\seq_put_right:Nx \l_pdiff_var_seq {\seq_item:Nn \l_tmpb_seq {1}}
\int_compare:nNnTF {\seq_count:N \l_tmpb_seq } = {\c_one} {
\int_set:Nn \l_tmpb_int {\c_one}
\seq_put_right:Nn \l_tmpb_seq {\c_one}
\tl_put_right:Nn \l_tmpa_tl {\partial}
\tl_put_right:Nx \l_tmpa_tl {\seq_item:Nn \l_tmpb_seq {1}}
}{%
\int_set:Nx \l_tmpb_int {\seq_item:Nn \l_tmpb_seq {2}}
\int_compare:nNnTF { \l_tmpb_int } < {\c_zero} {
\msg_fatal:nnx {pdiff}{negativedifforder}{\seq_item:Nn \l_tmpb_seq {1}}
}{%
\int_compare:nNnF {\l_tmpb_int } = {\c_zero} {%
\tl_put_right:Nn \l_tmpa_tl {\partial}
\int_compare:nNnTF{ \l_tmpb_int } = {\c_one } {
\tl_put_right:Nx \l_tmpa_tl {\seq_item:Nn \l_tmpb_seq {1}}
}{%
\tl_put_right:Nx \l_tmpa_tl {\seq_item:Nn \l_tmpb_seq {1}^{\seq_item:Nn \l_tmpb_seq {2}}}
}
}
}
}
\int_add:Nx \l_tmpa_int {\l_tmpb_int}
}
\mathinner{\frac{\partial^{\int_use:N\l_tmpa_int}#2(\seq_use:Nn \l_pdiff_var_seq {,})}{\tl_use:N \l_tmpa_tl}}
}
\ExplSyntaxOff
\begin{document}
\Huge
\[\pdiffs{x/5,y/3,z/0}{f}\]
\[\pdiffs{x/4,y/8,z/17}{f}\]
\end{document}