Jordan–Bareiss algorithm

264 Views Asked by At

Jordan–Bareiss Algorithm is an algorithm that find determinant of $n \times n$ square matrix $M$. Its algorithm is given below:

  1. Initialize the sign $s := 1$ and set $c := m^{(-1)}_{00} = 1;$
  2. loop over rows $M_k$ of $M$ for $k ∈ \{1, \dots , n\}$:
    a) find the pivot $m_{ik} \neq 0$ with $i ≥ k$;
    b) if no such $m_{ik}$ exists, then return $0$ and quit;
    c) if $i \neq k$, then swap the rows $M_k$ and $M_i$ and update the sign, setting $s := −s$.
    d) replace every row $M_i$ with $i ∈ \{k + 1, \dots , n\}$ by an expression
    $\hspace{2.4cm}$ $\frac{(m_{kk}·M_i − m_{ik}·M_k)}{c}$
    e) update the divisor $c$ setting $c:= m_{kk}$;
  3. return $\det M = s·m_{nn} $.

Here is my trial using SageMath but its giving wrong answer.

def JordanBarreiss(M):
    assert M.is_square(), "It must be a square matrix"
    n=M.dimensions()[0]
    s=1
    c=1
    for i in [1,..,n-1]:
        for k in [1,..,n-1]:
            if M[i,k] == 0 and i < k:
                return 0
            if i != k:
                M.swap_rows(i,k)
                s=-s
                for j in [k+1,..,n-1]:
                    M[j] = (M[k,k]*M[j] - M[j,k]*M[k])/c
        c = M[k,k]
    return M[n-1,n-1]
A = matrix(QQ, [[5,1,-9,0],[-1,3,-1,1],[8,-2,1,-3],[2,2,1,2]]);
JordanBarreiss(A);

out[110]  0

The expected output M = 5  1  -9   0 
                        0  16 -16  5
                        0  0   196 -30
                        0  0   0   475
where M[n-1,n-1] = 475 (det(M))

Please, it will be highly appreciated if anyone could help to fix this error, Python is also allowed. thanks.

1

There are 1 best solutions below

0
On BEST ANSWER

Now I get it, thanks all

def JordanBarreiss(M):
    """
     JordanBareiss: Function to compute determinant of nxn square matrix M of size this algorithm
     
     INPUT:
        
         - A square matrix M
    
    OUTPUT:
        
        - det(M) 'determinant of the given matrix'

    """
    assert M.is_square(), "Only Square matrix allowed"
    n=M.dimensions()[0]
    c=1
    for k in range(n-1):
        for i in range(k+1,n):
            for j in [k+1,..,n-1]:
                M[i,j] = (M[i,j]*M[k,k] - M[i,k]*M[k,j])/c
        c=M[k,k]
    return M[n-1,n-1]

```