9

I want to demonstrate examples of Gaussian elimination/the Gauss-Jordan method as shown below. There are some things that I like about what I have right now.

  • Each column is the same width from array to array.
  • The notation for row operations is consistent with the textbook that I am using.
  • There is a clear break between steps.
  • I don't have to do a huge amount of manual tuning.

However, there are also a few things that I would like to change.

  • Having to use \\[3pt] instead of just \\ to make sure that the spacing is consistent, and fractions on adjacent lines do not crash into one another.
  • In a long example (like the one below), the sequence of steps cannot break across a page, which causes the array to run off the bottom of the page unless I manually insert a break (which may mess up the consistent column widths).
  • The hackish way in which the horizontal lines between matrices are obtained.
  • I usually prefer to use brackets around my matrices.
  • The right border is implemented in a very hackish way.

Any advice on how to implement these changes while maintaining the aspects that I like would be greatly appreciated. I am aware of the gauss package, but I'm not sure if I can use it to get things in the "like" list.

\documentclass{article}

\begin{document}

\[
    \begin{array}{rr@{}|rrrrrrr|l}
        & & 2 & 1 & 1 & 3 & 0 & 4 & 1
        & \frac{1}{2} R_{1}
        \\[3pt]
        & & 4 & 2 & 4 & 4 & 1 & 5 & 5
        \\[3pt]
        & & 2 & 1 & 3 & 1 & 0 & 4 & 3
        \\[3pt]
        & & 6 & 3 & 4 & 8 & 1 & 9 & 5
        \\[3pt]
        & & 0 & 0 & 3 & -3 & 0 & 0 & 3
        \\[3pt]
        & & 8 & 4 & 2 & 14 & 1 & 13 & 3
        \\ \\[-1em] \hline \\[-1em]
        %%%%%%%%%%%%%%%%%%%%%%%%%
        & & 1 & \frac{1}{2} & \frac{1}{2} & \frac{3}{2} & 0 & 2 & \frac{1}{2}
        \\[3pt]
        & & 4 & 2 & 4 & 4 & 1 & 5 & 5
        & R_{2} - 4 R_{1}
        \\[3pt]
        & & 2 & 1 & 3 & 1 & 0 & 4 & 3
        & R_{3} - 2 R_{1}
        \\[3pt]
        & & 6 & 3 & 4 & 8 & 1 & 9 & 5
        & R_{4} - 6 R_{1}
        \\[3pt]
        & & 0 & 0 & 3 & -3 & 0 & 0 & 3
        \\[3pt]
        & & 8 & 4 & 2 & 14 & 1 & 13 & 3
        & R_{6} - 8 R_{1}
        \\ \\[-1em] \hline \\[-1em]
        %%%%%%%%%%%%%%%%%%%%%%%%%
        & & 1 & \frac{1}{2} & \frac{1}{2} & \frac{3}{2} & 0 & 2 & \frac{1}{2}
        \\[3pt]
        & & 0 & 0 & 2 & -2 & 1 & -3 & 3
        & \frac{1}{2} R_{2}
        \\[3pt]
        & & 0 & 0 & 2 & -2 & 0 & 0 & 2
        \\[3pt]
        & & 0 & 0 & 1 & -1 & 1 & -3 & 2
        \\[3pt]
        & & 0 & 0 & 3 & -3 & 0 & 0 & 3
        \\[3pt]
        & & 0 & 0 & -2 & 2 & 1 & -3 & -1
        \\ \\[-1em] \hline \\[-1em]
        %%%%%%%%%%%%%%%%%%%%%%%%%
        & & 1 & \frac{1}{2} & \frac{1}{2} & \frac{3}{2} & 0 & 2 & \frac{1}{2}
        & R_{1} - \frac{1}{2} R_{2}
        \\[3pt]
        & & 0 & 0 & 1 & -1 & \frac{1}{2} & -\frac{3}{2} & \frac{3}{2}
        \\[3pt]
        & & 0 & 0 & 2 & -2 & 0 & 0 & 2
        & R_{3} - 2 R_{2}
        \\[3pt]
        & & 0 & 0 & 1 & -1 & 1 & -3 & 2
        & R_{4} - R_{2}
        \\[3pt]
        & & 0 & 0 & 3 & -3 & 0 & 0 & 3
        & R_{5} - 3 R_{2}
        \\[3pt]
        & & 0 & 0 & -2 & 2 & 1 & -3 & -1
        & R_{6} + 2 R_{2}
        \\ \\[-1em] \hline \\[-1em]
        %%%%%%%%%%%%%%%%%%%%%%%%%
        & & 1 & \frac{1}{2} & 0 & 2 & -\frac{1}{4} & \frac{11}{4} & -\frac{1}{4}
        \\[3pt]
        & & 0 & 0 & 1 & -1 & \frac{1}{2} & -\frac{3}{2} & \frac{3}{2}
        \\[3pt]
        & & 0 & 0 & 0 & 0 & -1 & 3 & -1
        & -R_{3}
        \\[3pt]
        & & 0 & 0 & 0 & 0 & \frac{1}{2} & -\frac{3}{2} & \frac{1}{2}
        \\[3pt]
        & & 0 & 0 & 0 & 0 & -\frac{3}{2} & \frac{9}{2} & -\frac{3}{2}
        \\[3pt]
        & & 0 & 0 & 0 & 0 & 2 & -6 & 2
        \\ \\[-1em] \hline \\[-1em]
        %%%%%%%%%%%%%%%%%%%%%%%%%
        & & 1 & \frac{1}{2} & 0 & 2 & -\frac{1}{4} & \frac{11}{4} & -\frac{1}{4}
        & R_{1} + \frac{1}{4} R_{3}
        \\[3pt]
        & & 0 & 0 & 1 & -1 & \frac{1}{2} & -\frac{3}{2} & \frac{3}{2}
        & R_{2} - \frac{1}{2} R_{3}
        \\[3pt]
        & & 0 & 0 & 0 & 0 & 1 & -3 & 1
        \\[3pt]
        & & 0 & 0 & 0 & 0 & \frac{1}{2} & -\frac{3}{2} & \frac{1}{2}
        & R_{4} - \frac{1}{2} R_{3}
        \\[3pt]
        & & 0 & 0 & 0 & 0 & -\frac{3}{2} & \frac{9}{2} & -\frac{3}{2}
        & R_{5} + \frac{3}{2} R_{3}
        \\[3pt]
        & & 0 & 0 & 0 & 0 & 2 & -6 & 2
        & R_{6} - 2 R_{3}
        \\ \\[-1em] \hline \\[-1em]
        %%%%%%%%%%%%%%%%%%%%%%%%%
        & & 1 & \frac{1}{2} & 0 & 2 & 0 & 2 & 0
        \\[3pt]
        & & 0 & 0 & 1 & -1 & 0 & 0 & 1
        \\[3pt]
        & & 0 & 0 & 0 & 0 & 1 & -3 & 1
        \\[3pt]
        & & 0 & 0 & 0 & 0 & 0 & 0 & 0
        \\[3pt]
        & & 0 & 0 & 0 & 0 & 0 & 0 & 0
        \\[3pt]
        & & 0 & 0 & 0 & 0 & 0 & 0 & 0
    \end{array}
\]

\end{document}
Stirling
  • 1,419
  • You can lose the \[3pt] by using @(\mathstrut} or possibly something larger. You can break into multiple matrices by using p{20pt} (need to fine tune) instead of r. – John Kormylo Jan 29 '14 at 03:35
  • Ignore my previous comment. Instead, see http://tex.stackexchange.com/questions/136239/automatic-way-to-match-matrix-heights/136242#136242 – John Kormylo Jan 29 '14 at 04:19
  • Also see http://tex.stackexchange.com/questions/21326/how-to-make-equal-width-columns?rq=1 – John Kormylo Jan 29 '14 at 04:26

2 Answers2

5

Is this where you are headed?

first two matrices

\documentclass{article}
\usepackage{mathtools}
\usepackage{array}

\newcolumntype{R}[1]{>{\hbox to #1\bgroup\hfill$}c<{$\egroup}}

\begin{document}
\renewcommand\arraystretch{1.2}

\begin{align*}
  \left[ \begin{array
    {R{1.5em}R{1.5em}R{1.5em}R{1.5em}R{1.5em}R{1.5em}R{1.5em}}
    2 & 1 & 1 & 3 & 0 & 4 & 1 \\
    4 & 2 & 4 & 4 & 1 & 5 & 5 \\
    2 & 1 & 3 & 1 & 0 & 4 & 3 \\
    6 & 3 & 4 & 8 & 1 & 9 & 5 \\
    0 & 0 & 3 & -3 & 0 & 0 & 3 \\
    8 & 4 & 2 & 14 & 1 & 13 & 3
  \end{array} \right] &\quad 
  \begin{array}{l}
    \frac{1}{2} R_1 \\
    \\ \\ \\ \\ \\
  \end{array}
\\
  \left[ \begin{array}
    {R{1.5em}R{1.5em}R{1.5em}R{1.5em}R{1.5em}R{1.5em}R{1.5em}}
    1 & \frac{1}{2} & \frac{1}{2} & \frac{3}{2} & 0 & 2 & \frac{1}{2} \\
    4 & 2 & 4 & 4 & 1 & 5 & 5 \\
    2 & 1 & 3 & 1 & 0 & 4 & 3 \\
    6 & 3 & 4 & 8 & 1 & 9 & 5 \\
    0 & 0 & 3 & -3 & 0 & 0 & 3 \\
    8 & 4 & 2 & 14 & 1 & 13 & 3
  \end{array} \right] &\quad 
  \begin{array}{l}
    \\
    R_{2} - 4 R_{1} \\
    R_{3} - 2 R_{1} \\
    R_{4} - 6 R_{1} \\
    \\
    R_{6} - 8 R_{1}
  \end{array}
\end{align*}
\end{document} 
John Kormylo
  • 79,712
  • 3
  • 50
  • 120
4

I used John Kormylo's response to construct the following macros -- the formatting is all the same as his answer, but the macros are easier to use.

\documentclass{article}
    \usepackage{amsmath}
    \usepackage{array}
    \allowdisplaybreaks

    \makeatletter
    \newcounter{elimination@steps}
    \newcolumntype{R}[1]{>{\raggedleft\arraybackslash$}p{#1}<{$}}
    \def\elimination@num@rights{}
    \def\elimination@num@variables{}
    \def\elimination@col@width{}
    \newenvironment{elimination}[4][0]
    {
        \setcounter{elimination@steps}{0}
        \def\elimination@num@rights{#1}
        \def\elimination@num@variables{#2}
        \def\elimination@col@width{#3}
        \renewcommand{\arraystretch}{#4}
        \start@align\@ne\st@rredtrue\m@ne
    }
    {
        \endalign
        \ignorespacesafterend
    }
    \newcommand{\eliminationstep}[2]
    {
        \ifnum\value{elimination@steps}>0\\\Rightarrow\quad\fi
        \left[
            \ifnum\elimination@num@rights>0
                \begin{array}
                {@{}*{\elimination@num@variables}{R{\elimination@col@width}}
                |@{}*{\elimination@num@rights}{R{\elimination@col@width}}}
            \else
                \begin{array}
                {@{}*{\elimination@num@variables}{R{\elimination@col@width}}}
            \fi
                #1
            \end{array}
        \right]
        & \quad
        \begin{array}{l}
            #2
        \end{array}
        \addtocounter{elimination@steps}{1}
    }
    \makeatother

\begin{document}

\begin{elimination}[3]{3}{1.75em}{1.1}
    \eliminationstep
    {
        4 & -8 & 5 & 1 & 0 & 0 \\
        4 & -7 & 4 & 0 & 1 & 0 \\
        3 & -4 & 2 & 0 & 0 & 1
    }
    {
        \\
        R_{2} - R_{1} \\
        R_{3} - \frac{3}{4} R_{1}
    }
    \eliminationstep
    {
        4 & -8 & 5 & 1 & 0 & 0 \\
        0 &  1 & -1 & -1 & 1 & 0 \\
        0 &  2 & -\frac{7}{4} & -\frac{3}{4} & 0 & 1
    }
    {
        \\
        \\
        R_{3} - 2 R_{2} \\
    }
    \eliminationstep
    {
        4 & -8 & 5 & 1 & 0 & 0 \\
        0 &  1 & -1 & -1 & 1 & 0 \\
        0 &  0 & \frac{1}{4} & \frac{5}{4} & -2 & 1
    }
    {
        \\
        \\
        R_{3} - 2 R_{2} \\
    }
\end{elimination}

\begin{elimination}{6}{1.75em}{1.1}
    \eliminationstep
    {
        4 & -8 & 5 & 1 & 0 & 0 \\
        4 & -7 & 4 & 0 & 1 & 0 \\
        3 & -4 & 2 & 0 & 0 & 1
    }
    {
        \\
        R_{2} - R_{1} \\
        R_{3} - \frac{3}{4} R_{1}
    }
    \eliminationstep
    {
        4 & -8 & 5 & 1 & 0 & 0 \\
        0 &  1 & -1 & -1 & 1 & 0 \\
        0 &  2 & -\frac{7}{4} & -\frac{3}{4} & 0 & 1
    }
    {
        \\
        \\
        R_{3} - 2 R_{2} \\
    }
    \eliminationstep
    {
        4 & -8 & 5 & 1 & 0 & 0 \\
        0 &  1 & -1 & -1 & 1 & 0 \\
        0 &  0 & \frac{1}{4} & \frac{5}{4} & -2 & 1
    }
    {
        \\
        \\
        R_{3} - 2 R_{2} \\
    }
\end{elimination}

\end{document}
Stirling
  • 1,419