def ludcmp_crout(matrix): """ Decomposes/factorizes square input matrix into a lower and an upper matrix using Crout's algorithm WITHOUT pivoting. NB. It only works for square matrices!!! """ ndim = squaredim(matrix, 'ludcmp_crout') # Copy object instance to new matrix in order for the original instance # not to be destroyed. # Create two new square matrices of the same sized as the input matrix: # one unity matrix (to be the lower matrix), one zero matrix (to be # the upper matrix) copymx = deepcopy(matrix) lower = Matrix() lower.unity(ndim) upper = Matrix() upper.zero(ndim, ndim) permlist = list(range(0, ndim)) # Perform the necessary manipulations: for j in range(0, ndim): iu = 0 while iu <= j: k = 0 summ = 0.0 while k < iu: summ += lower[iu][k]*upper[k][j] k = k + 1 upper[iu][j] = copymx[iu][j] - summ iu = iu + 1 il = j + 1 while il < ndim: k = 0 summ = 0.0 while k < j: summ += lower[il][k]*upper[k][j] k = k + 1 divisor = float(upper[j][j]) if abs(divisor) < TINY: divisor = fsign(divisor)*TINY lower[il][j] = (copymx[il][j]-summ) / divisor il = il + 1 parity = 1.0 return lower, upper, permlist, parity
def ludcmp_crout(matrix): """ Decomposes/factorizes square input matrix into a lower and an upper matrix using Crout's algorithm WITHOUT pivoting. NB. It only works for square matrices!!! """ ndim = squaredim(matrix, 'ludcmp_crout') # Copy object instance to new matrix in order for the original instance # not to be destroyed. # Create two new square matrices of the same sized as the input matrix: # one unity matrix (to be the lower matrix), one zero matrix (to be # the upper matrix) copymx = deepcopy(matrix) lower = Matrix() lower.unity(ndim) upper = Matrix() upper.zero(ndim, ndim) permlist = list(range(0, ndim)) # Perform the necessary manipulations: for j in range(0, ndim): iu = 0 while iu <= j: k = 0 summ = 0.0 while k < iu: summ += lower[iu][k] * upper[k][j] k = k + 1 upper[iu][j] = copymx[iu][j] - summ iu = iu + 1 il = j + 1 while il < ndim: k = 0 summ = 0.0 while k < j: summ += lower[il][k] * upper[k][j] k = k + 1 divisor = float(upper[j][j]) if abs(divisor) < TINY: divisor = fsign(divisor) * TINY lower[il][j] = (copymx[il][j] - summ) / divisor il = il + 1 parity = 1.0 return lower, upper, permlist, parity
def inverted(matrix, pivoting=True): """ Only square matrices can be inverted! """ ndim = squaredim(matrix, 'inverted') # First: LU-decompose matrix to be inverted if pivoting: lower, upper, permlist, parity = ludcmp_crout_piv(matrix) else: lower, upper, permlist, parity = ludcmp_crout(matrix) # Create unity matrix unitymatrix = Matrix() unitymatrix.unity(ndim) # Loop over the columns in unity matrix and substitute # (uses the fact that rows and columns are the same in a unity matrix) columns = Matrix() columns.zero(ndim, ndim) for k in range(0, ndim): columns[k] = lusubs(lower, upper, unitymatrix[k], permlist) # preparations below for changing lusubs to handling column vector # instead of list #row = Matrix([unitymatrix[k]]) #column = transpose(row) #columns[k] = lusubs(lower, upper, column, permlist) #del column # Transpose matrix to get inverse newmatrix = ndim * [float('nan')] for k in range(0, ndim): # List comprehension is used for the innermost loop newmatrix[k] = array('d', [row[k] for row in columns]) imatrix = Matrix(newmatrix) del newmatrix return imatrix
def inverted(matrix, pivoting=True): """ Only square matrices can be inverted! """ ndim = squaredim(matrix, 'inverted') # First: LU-decompose matrix to be inverted if pivoting: lower, upper, permlist, parity = ludcmp_crout_piv(matrix) else: lower, upper, permlist, parity = ludcmp_crout(matrix) # Create unity matrix unitymatrix = Matrix() unitymatrix.unity(ndim) # Loop over the columns in unity matrix and substitute # (uses the fact that rows and columns are the same in a unity matrix) columns = Matrix() columns.zero(ndim, ndim) for k in range(0, ndim): columns[k] = lusubs(lower, upper, unitymatrix[k], permlist) # preparations below for changing lusubs to handling column vector # instead of list #row = Matrix([unitymatrix[k]]) #column = transpose(row) #columns[k] = lusubs(lower, upper, column, permlist) #del column # Transpose matrix to get inverse newmatrix = ndim*[float('nan')] for k in range(0, ndim): # List comprehension is used for the innermost loop newmatrix[k] = array('d', [row[k] for row in columns]) imatrix = Matrix(newmatrix) del newmatrix return imatrix
def corrmatrix(inputmatrix): """ Computes the correlation matrix of the input matrix. Each row is assumed to contain the vector for one parameter. """ ndim = len(inputmatrix) # = the number of rows/parameters # First create unity output matrix corrmatrix = Matrix() corrmatrix.unity(ndim) # Then fill it with correlation coefficients for k in range(0, ndim): kp1 = k + 1 for j in range(0, kp1): if j != k: #amk,amj,vk,vj,covkj, rhokj = covar(inputmatrix[k], \ # inputmatrix[j]) #corrmatrix[k][j] = corrmatrix[j][k] = rhokj corrmatrix[k][j] = corrmatrix[j][k] = \ covar(inputmatrix[k], inputmatrix[j])[5] # = rhokj return corrmatrix