daily pastebin goal
26%
SHARE
TWEET

Untitled

a guest Nov 22nd, 2017 43 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def my_det(X):
  2.     '''
  3.    Parameters
  4.    ----------
  5.    X : array_like
  6.    
  7.    Returns
  8.    -------
  9.    det : float
  10.        Determinant of `a`.
  11.    '''
  12.    
  13.     # Your code here
  14.     X1 = X.copy()
  15.     if X1.shape[0] != X1.shape[1]:
  16.         raise ValueError('Not square')
  17.     det = 1
  18.     for j in range(X1.shape[1]):
  19.         k = j
  20.         for i in range(j + 1, X1.shape[0]):
  21.             if abs(X1[i, j]) > abs(X1[k, j]):
  22.                 k = j
  23.         if X1[k, j] == 0:
  24.             det = 0
  25.             return 0
  26.         if X1[j, j] == 0:
  27.             det = 0
  28.             return 0
  29.         X1[k,:], X1[j,:] = X1[j,:].copy(), X1[k,:].copy()
  30.         if k != j:
  31.             det = -det
  32.         try:
  33.             det *= X1[j, j]
  34.         except RuntimeError:
  35.             return 0
  36.         if abs(det) < 1e-7:
  37.             return 0
  38.         for i in range(j + 1, X1.shape[0]):
  39.             try:
  40.                 X1[j, i] /= X1[j, j]
  41.             except RuntimeError:
  42.                 return inf
  43.         for i in range(X1.shape[0]):
  44.             tmp = X1[i, j]
  45.             if j != i and X1[i, j] != 0:
  46.                 X1[i,j+1:] -= X1[j,j+1:] * tmp
  47.                 #for k in range(j + 1, X1.shape[0]):
  48.                 #    X1[i, k] -= X1[j, k] * X1[i, j]
  49.     return det
RAW Paste Data
Top