
我正在嘗試創建一個大的 nxn 矩陣,但我無法找到一種比手動操作更容易的技術,有什麼想法嗎?
答案1
這將列印具有指定大小的隨機矩陣。
鍵是size
(強制),lb
用於隨機整數的下限(預設 0),ub
用於隨機整數的上限(預設 20)。
\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\bigmatrix}{m}
{
\group_begin:
\keys_set:nn { john/bigmatrix } { #1 }
\john_bigmatrix:
\group_end:
}
\tl_new:N \l__john_bigmatrix_tl
\keys_define:nn { john/bigmatrix }
{
size .int_set:N = \l__john_bigmatrix_size_int,
lb .int_set:N = \l__john_bigmatrix_lb_int,
ub .int_set:N = \l__john_bigmatrix_ub_int,
lb .initial:n = 0,
ub .initial:n = 20,
}
\cs_new_protected:Nn \john_bigmatrix:
{
\int_compare:nT { \l__john_bigmatrix_size_int > \value{MaxMatrixCols} }
{
\setcounter{MaxMatrixCols}{\l__john_bigmatrix_size_int}
}
\int_step_function:nN { \l__john_bigmatrix_size_int } \__john_bigmatrix_row:n
\begin{bmatrix}
\l__john_bigmatrix_tl
\end{bmatrix}
}
\cs_new_protected:Nn \__john_bigmatrix_row:n
{
\tl_put_right:Nx \l__john_bigmatrix_tl
{
\int_rand:nn { \l__john_bigmatrix_lb_int } { \l__john_bigmatrix_ub_int }
}
\prg_replicate:nn { \l__john_bigmatrix_size_int - 1 }
{
\tl_put_right:Nx \l__john_bigmatrix_tl
{
&
\int_rand:nn { \l__john_bigmatrix_lb_int } { \l__john_bigmatrix_ub_int }
}
}
\tl_put_right:Nn \l__john_bigmatrix_tl { \\ }
}
\ExplSyntaxOff
\begin{document}
$\bigmatrix{size=5}$ $\bigmatrix{size=6,lb=-12,ub=12}$
\bigskip
$\bigmatrix{size=15,ub=50}$
\end{document}
答案2
以 Mathematica 為例,
IdentityMatrix[10] // TeXForm
並複製 LaTeX 的輸出,如下所示。
\documentclass[border=12pt,12pt]{standalone}
\usepackage{amsmath}
\begin{document}
$A=
\begin{pmatrix}
1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\
\end{pmatrix}
$
\end{document}
答案3
使用電腦代數系統 Sage 以及sagetex
軟體包。首先,這是程式碼:
\documentclass{article}
\usepackage{sagetex}
\begin{document}
\begin{sagesilent}
latex.matrix_delimiters(left='[', right=']')
A = Matrix([[0,-1,-1],[-1,-1,0],[-1,0,1],[1,0,0],[0,0,-1],[-1,2,1]])
B = Matrix.identity(4)
C = random_matrix(ZZ,4,3)
D = random_matrix(QQ,3,4)
\end{sagesilent}
The matrix $A=\sage{A}$ was input by hand. The matrix $B=\sage{B}$ is defined in Sage.
The matrix $C=\sage{C}$ is $4 \times 4$ matrix consisting of integers determined
at random. The matrix $D=\sage{D}$ is a $3 \times 4$ matrix consisting of rational
numbers determined randomly.
Computing $C \cdot D= \sage{C*D}$ is easy. You can compute use Sage to test if
matrices are singular or nonsingular and even calculate their inverses.
Sage will take care of the calculations but
you'll have to spend time making the output look a little nicer.
\end{document}
接下來,這是輸出。由於我的一些矩陣構造是隨機的,因此它看起來應該與您運行的相同程式碼不同。
最後,基本結構是 C = random_matrix(ZZ,4,3) 其中
- C 是您定義的矩陣
- 4 是行數
- 3 是列數
- ZZ 表示條目為整數,QQ 表示有理數,RR 表示實數,CC 表示複數。您也可以使用有限域。請參閱文件。
請注意,我已經展示了您如何逐項定義矩陣 A,而 B 展示了 Sage 如何為您建立 4x4 單位矩陣。設定好矩陣後,Sage 也會進行計算。這可以防止粗心的錯誤滲透到您的文件中。 Sage 不是 LaTeX 發行版的一部分,但您可以使用免費的 Cocalc 帳戶在線訪問它這裡。您可以在電腦上安裝 Sage,這樣您就不需要 Cocalc。啟動和運行更加困難。在 SAGE 中使用矩陣的一些重要文件是這裡,這裡,這裡, 和這裡。 Sage 對於大矩陣沒有問題,但在頁面上顯示它們就會出現問題。在程式碼中使用 \usepackage{fullpage} 可以釋放空間,以便我列印 20 x 20 矩陣。
答案4
常態隨機數矩陣使用knitr
:
\documentclass{article}
\usepackage{amsmath}
<<bmatrix,echo=F>>=
options(digits=2)
bmatrix <- function(matr) {
printmrow <- function(x) {cat(cat(x,sep=" & "),"\\\\ \n")}
cat("\\begin{bmatrix}","\n")
body <- apply(matr,1,printmrow)
cat("\\end{bmatrix}")}
@
\begin{document}
\[ A =
<<echo=F,results='asis'>>=
bmatrix(round(matrix(rnorm(6), 2 ,3),3))
@
\]
\[ B =
<<echo=F,results='asis'>>=
bmatrix(round(matrix(abs(rnorm(120)), 12 ,10),1))
@
\]
\setcounter{MaxMatrixCols}{12}
\[ C =
<<echo=F,results='asis'>>=
bmatrix(round(matrix(abs(rnorm(144)), 12 ,12),1))
@
\]
\end{document}