
Estou tentando criar uma matriz nxn grande, mas não consigo encontrar uma técnica que facilite isso em vez de fazê-lo manualmente, alguma ideia?
Responder1
Isso imprime uma matriz aleatória com o tamanho especificado.
As chaves são size
(obrigatórias), lb
para o limite inferior dos inteiros aleatórios (padrão 0), ub
para o limite superior dos inteiros aleatórios (padrão 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}
Responder2
Use o Mathematica, por exemplo,
IdentityMatrix[10] // TeXForm
E copie a saída para LaTeX da seguinte maneira.
\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}
Responder3
Use o sistema de álgebra computacional Sage junto com o sagetex
pacote. Primeiro, aqui está o código:
\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}
A seguir, aqui está a saída. Como algumas das minhas construções de matrizes são aleatórias, elas devem parecer diferentes da execução do mesmo código.
Finalmente, a construção básica é C = random_matrix(ZZ,4,3) onde
- C é a matriz que você está definindo
- 4 é o número de linhas
- 3 é o número de colunas
- ZZ significa que as entradas são inteiras, QQ para racionais, RR para reais, CC para complexas. Você também pode trabalhar com campos finitos. Veja a documentação.
Observe que mostrei como a matriz A pode ser definida por você, entrada por entrada, enquanto B mostra como o Sage criará a matriz identidade 4x4 para você. Depois de configurar suas matrizes, o Sage também fará os cálculos. Isso evita que erros descuidados se insinuem no seu documento. Sage não faz parte da distribuição LaTeX, mas você pode acessá-lo online com uma conta Cocalc gratuitaaqui. É possível instalar o Sage no seu computador para que você não precise do Cocalc. Isso é mais difícil de colocar em funcionamento. Alguma documentação importante para trabalhar com matrizes no SAGE éaqui,aqui,aqui, eaqui. O Sage não tem problemas com matrizes grandes, mas exibi-las na página torna-se problemático. Usar \usepackage{fullpage} em seu código pode liberar espaço para que eu imprima uma matriz 20 por 20.
Responder4
Matrizes de números aleatórios normais usando 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}