py_det_of_minor

View page source

Python Determinant of a Minor

Syntax

      d = cmpad::det_of_minor ( a , n , m , r , c )

Prototype

def det_of_minor(a, n, m, r, c) :
   assert m <= n
   assert len(a) == n * n
   assert len(r) == n + 1
   assert len(c) == n + 1
   # ...
   assert type(d) == type(a[0])
   return d

Purpose

This function computes the determinant of a minor of the matrix \(A\) using expansion by minors. It is for example and testing purposes only. Expansion by minors is chosen as an example because it uses a lot of floating point operations yet does not require much source code. This is not an efficient method for computing a determinant; for example, using an LU factorization would be faster.

Minor

The elements of the \(m \times m\) minor \(M\) of the matrix \(A\) are defined, for \(i = 0 , \ldots , m-1\) and \(j = 0 , \ldots , m-1\), by

\[M_{i,j} = A_{R(i), C(j)}\]

where the function \(R(i)\) is defined by the argument r and \(C(j)\) is defined by the argument c .

Determinant of A

If the following conditions hold, the minor is the entire matrix \(A\) and hence det_of_minor will return the determinant of \(A\):

  1. \(m = n\).

  2. for \(i = 0 , \ldots , n-1\), \(r[i] = i+1\), and \(r[n] = 0\).

  3. for \(j = 0 , \ldots , n-1\), \(c[j] = j+1\), and \(c[n] = 0\).

Scalar

We use Scalar for the type of the elements of a . If y is a Scalar and x is a Scalar or float , the type Scalar must support the following operations:

Syntax

Description

Result Type

x = y

set value of x to current value of y Scalar

x + y

value of x plus y

Scalar

x - y

value of x minus y

Scalar

x * y

value of x times value of y

Scalar

a

The elements of the \(n \times n\) matrix \(A\) are defined, for \(i = 0 , \ldots , n-1\) and \(j = 0 , \ldots , n-1\), by

\[A_{i,j} = a[ i * n + j]\]

n

This is the number of rows (and columns) in the square matrix \(A\).

m

This is the number of rows (and columns) in the square minor \(M\).

r

This defines the function \(R(i)\) which specifies the rows of the minor \(M\). To be specific, the function \(R(i)\) for \(i = 1, \ldots , m-1\) is defined by

\[\begin{split}\begin{eqnarray} R(0) & = & r[n] \\ R(i) & = & r[ R(i-1) ] \end{eqnarray}\end{split}\]

All the elements of r have value less than or equal n ; \(R(i) < n\) and \(r[ R(m-1) ] = n\) . The elements of vector r are modified during the computation, and restored to their original value before the return from det_of_minor .

c

This defines the function \(C(i)\) which specifies the columns of the minor \(M\). To be specific, the function \(C(i)\) for \(j = 1, \ldots , m-1\) is defined by

\[\begin{split}\begin{eqnarray} C(0) & = & c[n] \\ C(j) & = & c[ C(j-1) ] \end{eqnarray}\end{split}\]

All the elements of c must have value less than or equal n ; \(C(j) < n\) and \(c[ C(m-1) ] = n\) . The elements of vector c are modified during the computation, and restored to their original value before the return from det_of_minor .

d

The return value d is equal to the determinant of the minor \(M\).

Name

Title

det_of_minor.py

Python det_of_minor: Source Code

xam_det_of_minor.py

Python Example and Test of det_of_minor

Example

det_of_minor.py contains an example and test of det_of_minor .