This worksheet defines new class for working with unevaluated determinant. For pedagogical purpouses.

Class definition

{{{id=70| class LaplaceExpansion(SageObject): """ Class for explaining Laplace expansion and operations with determinant. For pedagogical purpouses only. Objects in this class are lists of pairs ``[A,i]`` where ``A`` is matrix and ``i`` is a number. The value is sum of all terms ``i*det(A)``. Methods defined on this class do not change matrices. AUTHORS: - Robert Marik (03-2010) """ def __init__(self, input): r""" Initialization for LaplaceExpansion class ``input`` is either matrix or list of pairs [matrix,number] """ value = 0 if not isinstance(input,list): input = [[input,1]] for i in input: value += i[1]*(i[0].det()) self.value = value self._data_ = input def _repr_(self): s = '' for i in self._data_: s = s + '+(%s)*determinant(\n%s\n)'%(i[1],i[0]) return "Laplace expansion: \n"+s[1:] def show(self): return html(r'$%s$'%latex(self)) def _latex_(self): save_del = latex.matrix_delimiters() latex.matrix_delimiters("|", "|") expr = '' for i in self._data_: if len(i[0].rows())==1: ii = '(%s)' %latex(i[0][0][0]) else: ii = latex(i[0]) #ii = latex(i[0]) if i[1] == 1: expr = expr + '+%s' % (ii) elif i[1] == 0: expr = expr + '+0' else: expr = expr + '+(%s) %s' % (i[1],ii) latex.matrix_delimiters("(", ")") return expr[1:] def __add__(self,s): return LaplaceExpansion(self._data_+s._data_) def expand(self, row = None, column = None, term = 0, remove_zeros = True): r""" Returns Laplace expansion along given row or column. INPUT: - ``row`` - row for expansion (if ``column`` is None) - ``column`` - column for expansion (if ``row`` is None) - ``term`` - term for expansion, if ``self`` is a Laplace expansion with more terms - ``remove_zeros`` - if True, do not include minors which belong to zero elements Does not change ``self``. If neither row nor column are specified, uses the first row. """ b = self._data_[term] s = [] if row == None and column == None: row = 0 if column == None: col = len(b[0].rows()) for ii in range(col): s = s + [[matrix([[b[0][i][j] for j in range(col) if j!=ii]\ for i in range(col) if i!=row]),(-1)^(row+ii)*b[1]*b[0][row][ii]]] if remove_zeros: s = [w for w in s if w[1] != 0] ans = self._data_[:term]+s+self._data_[term+1:] return LaplaceExpansion(ans) if row == None: col = len(b[0].rows()) for ii in range(col): s = s + [[matrix([[b[0][i][j] for j in range(col) if j!=column]\ for i in range(col) if i!=ii]),(-1)^(column+ii)*b[1]*b[0][ii][column]]] if remove_zeros: s = [w for w in s if w[1] != 0] ans = self._data_[:term]+s+self._data_[term+1:] return LaplaceExpansion(ans) def pivot_on(self, i = 0 , j = 0, term = 0): r""" Cleans elements in column j pivoting with the element in row i Does not change ``self``. """ b = self._data_[term] ans_temp = [vector(s)-vector(b[0][i])*s[j]/b[0][i][j] for s in b[0].rows()] ans_temp[i] = b[0][i] # restore pivot row ans = self._data_[:term]+[[matrix(ans_temp),b[1]]]+self._data_[term+1:] return LaplaceExpansion(ans) def add_multiple_of_row(self, i , j, s, term = 0): r""" Add s times row j to row i in given term. Does not change ``self``. """ b = copy(self._data_[term][0][:]) b.add_multiple_of_row(i,j,s) ans = self._data_[:term]+[[b,self._data_[term][1]]]+self._data_[term+1:] return LaplaceExpansion(ans) def collect(self): r""" Collects all determinants of order 1 at the end. """ s = 0 d = [] for i in self._data_: if len(i[0].rows())!=1: d = d + [i] else: s=s+i[0][0][0]*i[1] if s != 0: d = d + [[matrix([[s]]),1]] return LaplaceExpansion(d) /// }}}

Example

{{{id=74| A=matrix([[2,3,4,1],[1,1,4,0],[7,6,5,1],[1,1,1,0]]) B=LaplaceExpansion(A) B /// \newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle \left|\begin{array}{rrrr} 2 & 3 & 4 & 1 \\ 1 & 1 & 4 & 0 \\ 7 & 6 & 5 & 1 \\ 1 & 1 & 1 & 0 \end{array}\right| }}}

We multiply the first row by $-1$ and add to the third row

{{{id=75| B.add_multiple_of_row(2,0,-1) /// \newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle \left|\begin{array}{rrrr} 2 & 3 & 4 & 1 \\ 1 & 1 & 4 & 0 \\ 5 & 3 & 1 & 0 \\ 1 & 1 & 1 & 0 \end{array}\right| }}}

Laplace expansion along the last column

{{{id=2| C=_.expand(column=3) C /// \newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle (-1) \left|\begin{array}{rrr} 1 & 1 & 4 \\ 5 & 3 & 1 \\ 1 & 1 & 1 \end{array}\right| }}}

We multiply the last row by $-1$ and add to the first row

{{{id=76| C.add_multiple_of_row(0,2,-1) /// \newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle (-1) \left|\begin{array}{rrr} 0 & 0 & 3 \\ 5 & 3 & 1 \\ 1 & 1 & 1 \end{array}\right| }}}

We use Laplace expansion along the first row

{{{id=77| D=_.expand() D /// \newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle (-3) \left|\begin{array}{rr} 5 & 3 \\ 1 & 1 \end{array}\right| }}}

We evaluate the determinat using rule $\begin{vmatrix}5&3\\1&1\end{vmatrix}=5\cdot 1-3\cdot 1=2$ and multiply by $-3$

{{{id=78| D.value /// \newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle -6 }}}

Row operations

{{{id=57| (B.add_multiple_of_row(1,0,-1)).add_multiple_of_row(2,0,-1) /// \newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle \left|\begin{array}{rrrr} 2 & 3 & 4 & 1 \\ -1 & -2 & 0 & -1 \\ 5 & 3 & 1 & 0 \\ 1 & 1 & 1 & 0 \end{array}\right| }}} {{{id=21| B.pivot_on(3,2) /// \newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle \left|\begin{array}{rrrr} -2 & -1 & 0 & 1 \\ -3 & -3 & 0 & 0 \\ 2 & 1 & 0 & 1 \\ 1 & 1 & 1 & 0 \end{array}\right| }}} {{{id=31| B.pivot_on(3,0) /// \newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle \left|\begin{array}{rrrr} 0 & 1 & 2 & 1 \\ 0 & 0 & 3 & 0 \\ 0 & -1 & -2 & 1 \\ 1 & 1 & 1 & 0 \end{array}\right| }}} {{{id=22| B.pivot_on(2,2) /// \newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle \left|\begin{array}{rrrr} -\frac{18}{5} & -\frac{9}{5} & 0 & \frac{1}{5} \\ -\frac{23}{5} & -\frac{19}{5} & 0 & -\frac{4}{5} \\ 7 & 6 & 5 & 1 \\ -\frac{2}{5} & -\frac{1}{5} & 0 & -\frac{1}{5} \end{array}\right| }}} {{{id=26| B.pivot_on(3,1) /// \newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle \left|\begin{array}{rrrr} -1 & 0 & 1 & 1 \\ 0 & 0 & 3 & 0 \\ 1 & 0 & -1 & 1 \\ 1 & 1 & 1 & 0 \end{array}\right| }}} {{{id=25| B /// \newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle \left|\begin{array}{rrrr} 2 & 3 & 4 & 1 \\ 1 & 1 & 4 & 0 \\ 7 & 6 & 5 & 1 \\ 1 & 1 & 1 & 0 \end{array}\right| }}}

Laplace expansion

{{{id=23| B.expand(column=3) /// \newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle (-1) \left|\begin{array}{rrr} 1 & 1 & 4 \\ 7 & 6 & 5 \\ 1 & 1 & 1 \end{array}\right|+(-1) \left|\begin{array}{rrr} 2 & 3 & 4 \\ 1 & 1 & 4 \\ 1 & 1 & 1 \end{array}\right| }}} {{{id=8| BB=(B).expand(row=1) BB /// \newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle (-1) \left|\begin{array}{rrr} 3 & 4 & 1 \\ 6 & 5 & 1 \\ 1 & 1 & 0 \end{array}\right|+\left|\begin{array}{rrr} 2 & 4 & 1 \\ 7 & 5 & 1 \\ 1 & 1 & 0 \end{array}\right|+(-4) \left|\begin{array}{rrr} 2 & 3 & 1 \\ 7 & 6 & 1 \\ 1 & 1 & 0 \end{array}\right| }}} {{{id=9| (BB).expand(column=2,term=1) /// \newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle (-1) \left|\begin{array}{rrr} 3 & 4 & 1 \\ 6 & 5 & 1 \\ 1 & 1 & 0 \end{array}\right|+\left|\begin{array}{rr} 7 & 5 \\ 1 & 1 \end{array}\right|+(-1) \left|\begin{array}{rr} 2 & 4 \\ 1 & 1 \end{array}\right|+(-4) \left|\begin{array}{rrr} 2 & 3 & 1 \\ 7 & 6 & 1 \\ 1 & 1 & 0 \end{array}\right| }}} {{{id=10| (BB).expand(row=0,term=2).expand(row=2) /// \newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle (-1) \left|\begin{array}{rr} 4 & 1 \\ 5 & 1 \end{array}\right|+\left|\begin{array}{rr} 3 & 1 \\ 6 & 1 \end{array}\right|+\left|\begin{array}{rrr} 2 & 4 & 1 \\ 7 & 5 & 1 \\ 1 & 1 & 0 \end{array}\right|+(-8) \left|\begin{array}{rr} 6 & 1 \\ 1 & 0 \end{array}\right|+(12) \left|\begin{array}{rr} 7 & 1 \\ 1 & 0 \end{array}\right|+(-4) \left|\begin{array}{rr} 7 & 6 \\ 1 & 1 \end{array}\right| }}} {{{id=67| (BB).expand(row=0,term=2).expand(row=2).expand(row=0, term=1) /// \newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle (-1) \left|\begin{array}{rr} 4 & 1 \\ 5 & 1 \end{array}\right|+(3) (1)+(-1) (6)+\left|\begin{array}{rrr} 2 & 4 & 1 \\ 7 & 5 & 1 \\ 1 & 1 & 0 \end{array}\right|+(-8) \left|\begin{array}{rr} 6 & 1 \\ 1 & 0 \end{array}\right|+(12) \left|\begin{array}{rr} 7 & 1 \\ 1 & 0 \end{array}\right|+(-4) \left|\begin{array}{rr} 7 & 6 \\ 1 & 1 \end{array}\right| }}} {{{id=11| (_).collect() /// \newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle (-1) \left|\begin{array}{rr} 4 & 1 \\ 5 & 1 \end{array}\right|+\left|\begin{array}{rrr} 2 & 4 & 1 \\ 7 & 5 & 1 \\ 1 & 1 & 0 \end{array}\right|+(-8) \left|\begin{array}{rr} 6 & 1 \\ 1 & 0 \end{array}\right|+(12) \left|\begin{array}{rr} 7 & 1 \\ 1 & 0 \end{array}\right|+(-4) \left|\begin{array}{rr} 7 & 6 \\ 1 & 1 \end{array}\right|+(-3) }}} {{{id=13| (_).expand(term=1,row=2) /// \newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle (-1) \left|\begin{array}{rr} 4 & 1 \\ 5 & 1 \end{array}\right|+\left|\begin{array}{rr} 4 & 1 \\ 5 & 1 \end{array}\right|+(-1) \left|\begin{array}{rr} 2 & 1 \\ 7 & 1 \end{array}\right|+(-8) \left|\begin{array}{rr} 6 & 1 \\ 1 & 0 \end{array}\right|+(12) \left|\begin{array}{rr} 7 & 1 \\ 1 & 0 \end{array}\right|+(-4) \left|\begin{array}{rr} 7 & 6 \\ 1 & 1 \end{array}\right|+(-3) }}} {{{id=29| BBBB=_.expand(term=3, column=1).expand(term=2,column=1).\ expand(term=1,column=1).expand(term=0,column=1) BBBB /// \newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle (5)+(-1) (4)+(-1) (5)+(4)+(7)+(-1) (2)+(8) (1)+(12) \left|\begin{array}{rr} 7 & 1 \\ 1 & 0 \end{array}\right|+(-4) \left|\begin{array}{rr} 7 & 6 \\ 1 & 1 \end{array}\right|+(-3) }}} {{{id=63| BBBB.collect() /// \newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle (12) \left|\begin{array}{rr} 7 & 1 \\ 1 & 0 \end{array}\right|+(-4) \left|\begin{array}{rr} 7 & 6 \\ 1 & 1 \end{array}\right|+(10) }}} {{{id=64| _.expand(term=1,column=1).expand(term=0,column=1) /// \newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle (-12) (1)+(24) (1)+(-4) (7)+(10) }}} {{{id=71| _.collect() /// \newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle (-6) }}} {{{id=65| A.det() /// \newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle -6 }}} {{{id=73| /// }}}