4

Is it possible to create a function to extract a submatrix from a matrix?, for example

\[A=\begin{pmatrix}
    5 & 0 & 0\\
    0 & 2 & -5\\
    6 & 1 & -2
\end{pmatrix}\]

function c{1,1} (the submatrix that removes the first row and first column) and get

\[\begin{pmatrix}
    2 & -5\\
    1 & -2
\end{pmatrix}\]

My goal is to write the submatrices and their determinants in a simpler way so as not to write them one by one. Any solution or idea?

Zaragosa
  • 599
  • 1
    You can do this using python or other scripting language. But I doubt that this is possible using latex. – anandogc Sep 06 '23 at 03:16
  • 1
    You can create a macrix in the form \csname foo\i.\j\endcsname which can be manipulated easily. Printing it would be a separate step. – John Kormylo Sep 06 '23 at 03:28
  • I would have done something similar to dexteritas' answer but locally with PGFKeys instead of etoolbox (combining storing a matrix and building another one), though at the end, they would be pretty similar. Are you okay with a comma-separated list of rows of comma-separated entries per row or do you need the LaTeX-like input with & and \\? – Qrrbrbirlbel Sep 06 '23 at 12:31

2 Answers2

5

I made some commands to store a matrix and to show the matrix, an element of the matrix, or a submatrix. The elements of the matrix are saved <matrix name>-<y>-<x>, so you could define further commands with that.

Result

enter image description here

Code

\documentclass{article}

\usepackage{etoolbox} \usepackage{pgffor} \usepackage{booktabs}

\def\dmname{} \newcounter{dmx} \newcounter{dmy} \newcommand{\dmlines}[1]{% \setcounter{dmx}{0} \forcsvlist{\dmelements}{#1} \stepcounter{dmy} } \newcommand{\dmelements}[1]{% \csdef{\dmname-\thedmy-\thedmx}{#1}% \stepcounter{dmx}% } \newcommand{\definematrix}[2]{% % #1 = name % #2 = matrix \gdef\dmname{#1}% \setcounter{dmy}{0}% \forcsvlist{\dmlines}{#2}% \csxdef{\dmname-w}{\thedmx}% \csxdef{\dmname-h}{\thedmy}% }

\newcommand{\getmatrixelement}[3]{% % #1 = name % #2 = y % #3 = x \csuse{#1-#2-#3}% }

\newcommand{\getsubmatrix}[5]{% % #1 = name % #2 = y % #3 = x % #4 = y2 % #5 = x2 \def\dmtablecontent{}% \foreach \y in {#2, ..., #4} {% \foreach \x in {#3, ..., #5} {% \xappto\dmtablecontent{\csuse{#1-\y-\x}}% \ifnumless{\x}{#5}{% \xappto\dmtablecontent{&}% }{}% }% \xappto\dmtablecontent{\}% }% % \begin{tabular}{*{\the\numexpr#5-#3+1\relax}{c}}% \dmtablecontent% \end{tabular}% }

\newcommand{\getmatrixwidth}[1]{% % #1 = name \csuse{#1-w}% }

\newcommand{\getmatrixheight}[1]{% % #1 = name \csuse{#1-h}% }

\newcommand{\getmatrix}[1]{% % #1 = name \getsubmatrix{#1}{0}{0} {\the\numexpr\getmatrixheight{#1}-1\relax} {\the\numexpr\getmatrixwidth{#1}-1\relax}% }

\newcommand{\getmatrixwithoutrc}[3]{% % shows the matrix without the given row and column % #1 = name % #2 = row % #3 = column \def\dmtablecontent{}% \def\dmymax{\the\numexpr\getmatrixheight{#1}-1\relax}% \def\dmxmax{\the\numexpr\getmatrixwidth{#1}-1\relax}% \foreach \y in {0, ..., \dmymax} {% \ifnumequal{\y}{#2}{}{% \foreach \x in {0, ..., \dmxmax} {% \ifnumequal{\x}{#3}{}{% \xappto\dmtablecontent{\csuse{#1-\y-\x}}% \ifnumless{\x}{\dmxmax}{% \xappto\dmtablecontent{&}% }{}% }% }% \xappto\dmtablecontent{\}% }% }% % \begin{tabular}{*{\getmatrixwidth{#1}}{c}} \dmtablecontent \end{tabular} }

\begin{document}

\definematrix{a}{{1, 2}, {3, 4}} \definematrix{b}{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}

\renewcommand{\arraystretch}{1.5} \begin{tabular}{ll} \toprule \textbf{Result} & \textbf{Command}\ \midrule & \verb|\definematrix{a}{{1, 2}, {3, 4}}|\ & \verb|\definematrix{b}{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}| \ $\getmatrixheight{a} \times \getmatrixwidth{a}$ & \verb|$\getmatrixheight{a} \times \getmatrixwidth{a}$| \ \getmatrix{a} & \verb|\getmatrix{a}| \ \getmatrixelement{a}{0}{0} & \verb|\getmatrixelement{a}{0}{0}| \ \getmatrixelement{a}{1}{0} & \verb|\getmatrixelement{a}{1}{0}| \ \getsubmatrix{a}{0}{1}{1}{1} & \verb|\getsubmatrix{a}{0}{1}{1}{1}| \ \getmatrix{b} & \verb|\getmatrix{b}| \ \getsubmatrix{b}{1}{1}{2}{2} & \verb|\getsubmatrix{b}{1}{1}{2}{2}| \ \getmatrixwithoutrc{b}{1}{1} & \verb|\getmatrixwithoutrc{b}{1}{1}| \ \bottomrule \end{tabular}

\end{document}

dexteritas
  • 9,161
3

Here's a sagetex solution using SAGE, a computer algebra system (CAS). Documentation on some matrix basics is here. The complete documentation for matrices is available in PDF form here. With 685 pages of documentation you'll find SAGE can do most anything you want.

\documentclass{article}
\usepackage{sagetex,amsmath,amsfonts}
\linespread{2.0}
\begin{document}
\begin{sagesilent}
latex.matrix_delimiters(left='[', right=']')
A=matrix([[5,0,0],[0,2,-5],[6,1,-2]])
B = matrix(4,[0..15])
C= B.delete_rows([0,3]).delete_columns([1,2])
D= A.delete_rows([0]).delete_columns([0])
\end{sagesilent}
Consider the matrices below:  \[A=\sage{A} \hspace{2cm} B=\sage{B}\]

The entry $A_{1,1}=\sage{A[0][0]}$ because SAGE is Python based and indices start with $0$. We can create submatrices $C=\sage{C}$ and $D=\sage{D}$ by deleting rows and columns. SAGE can calculate $C \cdot D = \sage{CD}$ and its determinant: \begin{sagesilent} latex.matrix_delimiters(left='|', right='|') \end{sagesilent} $det(C \cdot D)=\sage{CD}=\sage{det(C*D)}$ \end{document}

The result, running in Cocalc: enter image description here

The most important thing to remember is that SAGE, which is Python based and gives you access to Python, has a default starting index of 0. So removing the first row and column from your matrix is given by: D= A.delete_rows([0]).delete_columns([0]). What surrounds your matrix in LaTeX are the delimiters, documentation for changing them in SAGE is here. The code latex.matrix_delimiters(left='|', right='|') changed the delimiters so I could show the determinant in LaTeX.

SAGE is not part of LaTeX. The easiest way to get started is with a free Cocalc account.

DJP
  • 12,451