6

I would like to have a macro for gaussian elimination, but I don't like the notation of the gauss package. I've found a great macro here, but I would like to have several steps in one line. When I just remove \\ it doesn't look good, the spaces between matrices and row operations are not equal and the line doesn't wrap: matrix Also, I would like to decrease spaces between matrices and row operations. How to do that?

Source:

\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\sim\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]
    & 
    \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_{1} \\
        -\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
    }
    {
        \\
        \\
        -2R_{2} \\
    }
    \eliminationstep
    {
        4 & -8 & 5 & 1 & 0 & 0 \\
        0 &  1 & -1 & -1 & 1 & 0 \\
        0 &  0 & \frac{1}{4} & \frac{5}{4} & -2 & 1
    }
    {
        \\
        \\
        +2R_{2} \\
    }
\end{elimination}

\end{document}
koz4k
  • 63

1 Answers1

2

If you put more than one & in align, it assumes you are putting several equations together, so every other & expands like \hfil. You need 2 &s per step.

gauss

\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{mathtools}
\usepackage{array}

\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\sim\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]
    & 
    \begin{array}{l}
        #2
    \end{array}
    &%                                    moved second & here
    \addtocounter{elimination@steps}{1}
}
\makeatother

\begin{document}

\begin{elimination}[3]{3}{1.1em}{1.1}% Decreased from 1.75em
    \eliminationstep
    {
        4 & -8 & 5 & 1 & 0 & 0 \\
        4 & -7 & 4 & 0 & 1 & 0 \\
        3 & -4 & 2 & 0 & 0 & 1
    }
    {
        \\
        -R_{1} \\
        -\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
    }
    {
        \\
        \\
        -2R_{2}
    }
\\[10pt]%                    increased spacing between rows
    \eliminationstep
    {
        4 & -8 & 5 & 1 & 0 & 0 \\
        0 &  1 & -1 & -1 & 1 & 0 \\
        0 &  0 & \frac{1}{4} & \frac{5}{4} & -2 & 1
    }
    {
        \\
        \\
        +2R_{2}
    }
\end{elimination}

\end{document}
John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • Okay, but now there is a big gap between row operations and similarity sign, also the line doesn't break when the matrices don't fit and when I write \ explicitly it breaks everything. Is there any way to fix that? – koz4k Mar 08 '14 at 22:09
  • I fixed the explicit line break. Personally, I would keep the big gap and lose the \sim, but instead of align you would need to use another array and add the number of steps per row as an argument. Oh, reading the manual I noticed that \allowdisplayvreaks won't break an array, or aligned, or ... – John Kormylo Mar 09 '14 at 04:41