![如何为以下矩阵编写代码?](https://linux22.com/image/413497/%E5%A6%82%E4%BD%95%E4%B8%BA%E4%BB%A5%E4%B8%8B%E7%9F%A9%E9%98%B5%E7%BC%96%E5%86%99%E4%BB%A3%E7%A0%81%EF%BC%9F.png)
答案1
您可以嵌套matrix
环境:
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{equation}
\mathop{\mathbf{adj}}
\begin{pmatrix}
a_{11} & a_{12} & a_{13} \\
a_{12} & a_{22} & a_{23} \\
a_{31} & a_{32} & a_{33}
\end{pmatrix}
=
\begin{pmatrix}
+
\begin{vmatrix}
a_{22} & a_{23} \\
a_{32} & a_{33}
\end{vmatrix} &
-
\begin{vmatrix}
a_{12} & a_{13} \\
a_{32} & a_{33}
\end{vmatrix} &
+
\begin{vmatrix}
a_{12} & a_{13} \\
a_{22} & a_{23} \\
\end{vmatrix} \\[1.5em]
-
\begin{vmatrix}
a_{21} & a_{23} \\
a_{31} & a_{33}
\end{vmatrix} &
+
\begin{vmatrix}
a_{11} & a_{13} \\
a_{31} & a_{33}
\end{vmatrix}&
-
\begin{vmatrix}
a_{11} & a_{13} \\
a_{21} & a_{23}
\end{vmatrix} \\[1.5em]
+
\begin{vmatrix}
a_{21} & a_{22} \\
a_{31} & a_{32}
\end{vmatrix} &
-
\begin{vmatrix}
a_{11} & a_{12} \\
a_{21} & a_{22}
\end{vmatrix} &
+
\begin{vmatrix}
a_{11} & a_{12} \\
a_{21} & a_{22}
\end{vmatrix}
\end{pmatrix}
\end{equation}
\end{document}
答案2
正如其他答案所建议的,最好使用一些简写。
\documentclass{article}
\usepackage{amsmath}
\newcommand{\minor}[4]{%
\begin{vmatrix} a_{#1} & a_{#2} \\ a_{#3} & a_{#4} \end{vmatrix}
}
\begin{document}
\[
\operatorname{adj}
\begin{pmatrix}
a_{11} & a_{12} & a_{13} \\
a_{21} & a_{22} & a_{23} \\
a_{31} & a_{32} & a_{33}
\end{pmatrix}
=
\begin{pmatrix}
+\minor{22}{23}{32}{33} & -\minor{12}{13}{32}{33} & +\minor{12}{13}{22}{23}
\\ \noalign{\vspace{1ex}}
-\minor{21}{23}{31}{33} & +\minor{11}{13}{31}{33} & -\minor{11}{13}{21}{23}
\\ \noalign{\vspace{1ex}}
+\minor{21}{22}{31}{32} & -\minor{11}{12}{31}{32} & +\minor{11}{12}{21}{22}
\end{pmatrix}
\]
\end{document}
现在的挑战是让 TeX 自己计算伴随。这里有一组宏可以做到这一点。
\documentclass{article}
\usepackage{amsmath,xparse}
\ExplSyntaxOn
\NewDocumentCommand{\adjointmatrix}{O{a}m}
{% #1 = entry, #2 = number of rows
\tl_clear:N \l__amela_matrix_body_tl
\__amela_matrix_adjoint_make:nn { #1 } { #2 }
\begin{pmatrix}
\tl_use:N \l__amela_matrix_body_tl
\end{pmatrix}
}
\cs_new_protected:Nn \__amela_matrix_adjoint_make:nn
{
\int_step_inline:nn { #2 }
{
\int_step_inline:nn { #2 }
{
\int_compare:nF { ####1 = 1 } { \tl_put_right:Nn \l__amela_matrix_body_tl { & } }
\tl_put_right:Nx \l__amela_matrix_body_tl
{
\int_if_odd:nTF { ##1+####1 } { - } { + }
}
\tl_put_right:Nn \l__amela_matrix_body_tl { \begin{vmatrix} }
\__amela_matrix_adjoint_minor:nnnn { #1 } { #2 } { ####1 } { ##1 }
\tl_put_right:Nn \l__amela_matrix_body_tl { \end{vmatrix} }
}
\int_compare:nT { ##1 < #2 }
{
\tl_put_right:Nn \l__amela_matrix_body_tl { \\ \noalign{\vspace{1ex}} }
}
}
}
\cs_new_protected:Nn \__amela_matrix_adjoint_minor:nnnn
{% #1 = entry, #2 = size, #3 = row index, #4 = column index
\tl_clear:N \l__amela_matrix_minor_body_tl
\int_step_inline:nn { #2 }
{
\int_compare:nF { ##1 = #3 }
{
\tl_put_right:Nn \l__amela_matrix_minor_body_tl { \use_none:n } % remove the initial &
\int_step_inline:nn { #2 }
{
\int_compare:nF { ####1 = #4 }
{
\tl_put_right:Nn \l__amela_matrix_minor_body_tl { & #1\sb{##1####1} }
}
}
\tl_put_right:Nn \l__amela_matrix_minor_body_tl { \\ }
}
}
\tl_put_right:NV \l__amela_matrix_body_tl \l__amela_matrix_minor_body_tl
}
\NewDocumentCommand{\squarematrix}{O{a}m}
{% #1 = entry, #2 = number of rows
\tl_clear:N \l__amela_matrix_body_tl
\__amela_matrix_make:nn { #1 } { #2 }
\begin{pmatrix}
\tl_use:N \l__amela_matrix_body_tl
\end{pmatrix}
}
\tl_new:N \l__amela_matrix_body_tl
\tl_new:N \l__amela_matrix_minor_body_tl
\cs_new_protected:Nn \__amela_matrix_make:nn
{
\int_step_inline:nn { #2 }
{
\int_step_inline:nn { #2 }
{
\int_compare:nF { ####1 = 1 } { \tl_put_right:Nn \l__amela_matrix_body_tl { & } }
\tl_put_right:Nn \l__amela_matrix_body_tl { #1\sb{##1 ####1} }
}
\tl_put_right:Nn \l__amela_matrix_body_tl { \\ }
}
}
\ExplSyntaxOff
\begin{document}
\[
\squarematrix{3} \squarematrix[b]{4}
\]
\[
\operatorname{adj}\squarematrix{3}=\adjointmatrix{3}
\]
\[
\operatorname{adj}\squarematrix{2}=\adjointmatrix{2}
\]
\[
\adjointmatrix[b]{4}
\]
\end{document}
这是输出\adjointmatrix{5}
(您需要大尺寸的纸张)。
答案3
有很多种方法可以为您的答案创建完整的代码。我选择了一种简短的经典方法:在每行上放置 3 个矩阵。我给出了一个小的垂直空间[3ex]
。
\documentclass{article}
\usepackage{amsmath}
\DeclareMathOperator{\adj}{adj}
\begin{document}
\[
\adj \begin{pmatrix}
a_{11} & a_{12} & a_{13} \\
a_{12} & a_{22} & a_{23} \\
a_{31} & a_{32} & a_{33}
\end{pmatrix}=
\begin{pmatrix}
+\begin{vmatrix} a_{22} & a_{23} \\ a_{32} & a_{33} \end{vmatrix} &
-\begin{vmatrix} a_{12} & a_{13} \\ a_{32} & a_{33} \end{vmatrix} &
+\begin{vmatrix} a_{12} & a_{13} \\ a_{22} & a_{23} \end{vmatrix} \\[3ex]
-\begin{vmatrix} a_{21} & a_{23} \\ a_{31} & a_{33} \end{vmatrix} &
+\begin{vmatrix} a_{11} & a_{13} \\ a_{31} & a_{33} \end{vmatrix} &
-\begin{vmatrix} a_{11} & a_{13} \\ a_{21} & a_{23} \end{vmatrix} \\[3ex]
+\begin{vmatrix} a_{21} & a_{22} \\ a_{31} & a_{32} \end{vmatrix} &
-\begin{vmatrix} a_{11} & a_{12} \\ a_{21} & a_{22} \end{vmatrix} &
+\begin{vmatrix} a_{11} & a_{12} \\ a_{21} & a_{22} \end{vmatrix}
\end{pmatrix}
\]
\end{document}
这是我的输出:
答案4
我们使用的是 TeX,因此我们可以做一个快捷方式:
$$
\def\subd#1#2#3#4{\left|\matrix{a_{#1#2}&a_{#1#4}\cr a_{#3#2}&a_{#3#4}}\right|}
\def\crs{\cr\noalign{\kern.7ex}}
{\bf adj} \pmatrix{a_{11}&a_{12}&a_{13}\cr a_{21}&a_{22}&a_{23}\cr a_{31}&a_{32}&a_{33}}
= \pmatrix{+\subd 22 33 & -\subd 12 33 & +\subd 12 23 \crs
-\subd 21 33 & +\subd 11 33 & -\subd 11 23 \crs
+\subd 21 32 & -\subd 11 32 & +\subd 11 22}
$$