Python numpy.linalg.solve() Examples
The following are 30
code examples of numpy.linalg.solve().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
numpy.linalg
, or try the search function
.
Example #1
Source File: test_linalg.py From vnpy_crypto with MIT License | 6 votes |
def test_0_size_k(self): # test zero multiple equation (K=0) case. class ArraySubclass(np.ndarray): pass a = np.arange(4).reshape(1, 2, 2) b = np.arange(6).reshape(3, 2, 1).view(ArraySubclass) expected = linalg.solve(a, b)[:, :, 0:0] result = linalg.solve(a, b[:, :, 0:0]) assert_array_equal(result, expected) assert_(isinstance(result, ArraySubclass)) # test both zero. expected = linalg.solve(a, b)[:, 0:0, 0:0] result = linalg.solve(a[:, 0:0, 0:0], b[:, 0:0, 0:0]) assert_array_equal(result, expected) assert_(isinstance(result, ArraySubclass))
Example #2
Source File: Utilities2D.py From laplacian-meshes with GNU General Public License v3.0 | 6 votes |
def getBarycentricCoords(A, B, C, X, checkValidity = True): T = np.array( [ [A.x - C.x, B.x - C.x ], [A.y - C.y, B.y - C.y] ] ) y = np.array( [ [X.x - C.x], [X.y - C.y] ] ) lambdas = linalg.solve(T, y) lambdas = lambdas.flatten() lambdas = np.append(lambdas, 1 - (lambdas[0] + lambdas[1])) if checkValidity: if (lambdas[0] < 0 or lambdas[1] < 0 or lambdas[2] < 0): print "ERROR: Not a convex combination; lambda = %s"%lambdas print "pointInsideConvexPolygon2D = %s"%pointInsideConvexPolygon2D([A, B, C], X, 0) plt.plot([A.x, B.x, C.x, A.x], [A.y, B.y, C.y, A.y], 'r') plt.hold(True) plt.plot([X.x], [X.y], 'b.') plt.show() assert (lambdas[0] >= 0 and lambdas[1] >= 0 and lambdas[2] >= 0) else: lambdas[0] = max(lambdas[0], 0) lambdas[1] = max(lambdas[1], 0) lambdas[2] = max(lambdas[2], 0) return lambdas
Example #3
Source File: test_linalg.py From recruit with Apache License 2.0 | 6 votes |
def test_0_size_k(self): # test zero multiple equation (K=0) case. class ArraySubclass(np.ndarray): pass a = np.arange(4).reshape(1, 2, 2) b = np.arange(6).reshape(3, 2, 1).view(ArraySubclass) expected = linalg.solve(a, b)[:, :, 0:0] result = linalg.solve(a, b[:, :, 0:0]) assert_array_equal(result, expected) assert_(isinstance(result, ArraySubclass)) # test both zero. expected = linalg.solve(a, b)[:, 0:0, 0:0] result = linalg.solve(a[:, 0:0, 0:0], b[:, 0:0, 0:0]) assert_array_equal(result, expected) assert_(isinstance(result, ArraySubclass))
Example #4
Source File: gw_iter.py From pyscf with Apache License 2.0 | 6 votes |
def si_c_check (self, tol = 1e-5): """ This compares np.solve and LinearOpt-lgmres methods for solving linear equation (1-v\chi_{0}) * W_c = v\chi_{0}v """ import time import numpy as np ww = 1j*self.ww_ia t = time.time() si0_1 = self.si_c(ww) #method 1: numpy.linalg.solve t1 = time.time() - t print('numpy: {} sec'.format(t1)) t2 = time.time() si0_2 = self.si_c2(ww) #method 2: scipy.sparse.linalg.lgmres t3 = time.time() - t2 print('lgmres: {} sec'.format(t3)) summ = abs(si0_1 + si0_2).sum() diff = abs(si0_1 - si0_2).sum() if diff/summ < tol and diff/si0_1.size < tol: print('OK! scipy.lgmres methods and np.linalg.solve have identical results') else: print('Results (W_c) are NOT similar!') return [[diff/summ] , [np.amax(abs(diff))] ,[tol]] #@profile
Example #5
Source File: test_linalg.py From lambda-packs with MIT License | 6 votes |
def test_0_size_k(self): # test zero multiple equation (K=0) case. class ArraySubclass(np.ndarray): pass a = np.arange(4).reshape(1, 2, 2) b = np.arange(6).reshape(3, 2, 1).view(ArraySubclass) expected = linalg.solve(a, b)[:, :, 0:0] result = linalg.solve(a, b[:, :, 0:0]) assert_array_equal(result, expected) assert_(isinstance(result, ArraySubclass)) # test both zero. expected = linalg.solve(a, b)[:, 0:0, 0:0] result = linalg.solve(a[:, 0:0, 0:0], b[:, 0:0, 0:0]) assert_array_equal(result, expected) assert_(isinstance(result, ArraySubclass))
Example #6
Source File: test_linalg.py From auto-alt-text-lambda-api with MIT License | 6 votes |
def test_0_size_k(self): # test zero multiple equation (K=0) case. class ArraySubclass(np.ndarray): pass a = np.arange(4).reshape(1, 2, 2) b = np.arange(6).reshape(3, 2, 1).view(ArraySubclass) expected = linalg.solve(a, b)[:, :, 0:0] result = linalg.solve(a, b[:, :, 0:0]) assert_array_equal(result, expected) assert_(isinstance(result, ArraySubclass)) # test both zero. expected = linalg.solve(a, b)[:, 0:0, 0:0] result = linalg.solve(a[:, 0:0, 0:0], b[:, 0:0, 0:0]) assert_array_equal(result, expected) assert_(isinstance(result, ArraySubclass))
Example #7
Source File: test_linalg.py From Computable with MIT License | 6 votes |
def test_0_size_k(self): # test zero multiple equation (K=0) case. class ArraySubclass(np.ndarray): pass a = np.arange(4).reshape(1, 2, 2) b = np.arange(6).reshape(3, 2, 1).view(ArraySubclass) expected = linalg.solve(a, b)[:,:, 0:0] result = linalg.solve(a, b[:,:, 0:0]) assert_array_equal(result, expected) assert_(isinstance(result, ArraySubclass)) # test both zero. expected = linalg.solve(a, b)[:, 0:0, 0:0] result = linalg.solve(a[:, 0:0, 0:0], b[:,0:0, 0:0]) assert_array_equal(result, expected) assert_(isinstance(result, ArraySubclass))
Example #8
Source File: test_linalg.py From Mastering-Elasticsearch-7.0 with MIT License | 6 votes |
def test_0_size_k(self): # test zero multiple equation (K=0) case. class ArraySubclass(np.ndarray): pass a = np.arange(4).reshape(1, 2, 2) b = np.arange(6).reshape(3, 2, 1).view(ArraySubclass) expected = linalg.solve(a, b)[:, :, 0:0] result = linalg.solve(a, b[:, :, 0:0]) assert_array_equal(result, expected) assert_(isinstance(result, ArraySubclass)) # test both zero. expected = linalg.solve(a, b)[:, 0:0, 0:0] result = linalg.solve(a[:, 0:0, 0:0], b[:, 0:0, 0:0]) assert_array_equal(result, expected) assert_(isinstance(result, ArraySubclass))
Example #9
Source File: model.py From nusa with MIT License | 6 votes |
def solve(self): # known and unknown values self.VU = [node[key] for node in self.U.values() for key in ("ux",)] self.VF = [node[key] for node in self.F.values() for key in ("fx",)] knw = [pos for pos,value in enumerate(self.VU) if not value is np.nan] unknw = [pos for pos,value in enumerate(self.VU) if value is np.nan] # Matrices to solve self.K2S = np.delete(np.delete(self.KG,knw,0),knw,1) self.F2S = np.delete(self.VF,knw,0) # For displacements self.solved_u = la.solve(self.K2S,self.F2S) # Updating U (displacements vector) for k,ic in enumerate(unknw): nd, var = self.index2key(ic) self.U[nd][var] = self.solved_u[k] self.nodes[ic].ux = self.solved_u[k] # For nodal forces/reactions self.NF = self.F.copy() self.VU = [node[key] for node in self.U.values() for key in ("ux",)] nf_calc = np.dot(self.KG, self.VU) for k,ic in enumerate(range(self.get_number_of_nodes())): nd, var = self.index2key(ic, ("fx",)) self.NF[nd][var] = nf_calc[k] self.nodes[ic].fx = nf_calc[k]
Example #10
Source File: recsys.py From modl with BSD 2-Clause "Simplified" License | 6 votes |
def _single_sample_update(self, X, i, w): n_features = X.shape[1] if X.indptr[i + 1] - X.indptr[i] != 0: X_subset = X.data[X.indptr[i]:X.indptr[i + 1]] subset = X.indices[X.indptr[i]:X.indptr[i + 1]] len_subset = subset.shape[0] reduction = n_features / len_subset self.feature_n_iter_[subset] += 1 components_subset = self.components_[:, subset] Dx = components_subset.dot(X_subset) G = components_subset.dot(components_subset.T) G.flat[::self.n_components + 1] += self.alpha / reduction self.code_[i] = linalg.solve(G, Dx) code = self.code_[i] w_B = np.minimum(1, w * self.n_iter_ / self.feature_n_iter_[subset]) self.B_[:, subset] *= 1 - w_B self.B_[:, subset] += np.outer(code, X_subset * w_B)
Example #11
Source File: test_dict_fact.py From modl with BSD 2-Clause "Simplified" License | 6 votes |
def enet_regression_multi_gram_(G, Dx, X, code, l1_ratio, alpha, positive): batch_size = code.shape[0] if l1_ratio == 0: n_components = G.shape[1] for i in range(batch_size): G.flat[::n_components + 1] += alpha code[i] = linalg.solve(G[i], Dx[i]) G.flat[::n_components + 1] -= alpha else: # Unused but unfortunate API random_state = check_random_state(0) for i in range(batch_size): cd_fast.enet_coordinate_descent_gram( code[i], alpha * l1_ratio, alpha * ( 1 - l1_ratio), G[i], Dx[i], X[i], 100, 1e-2, random_state, False, positive) return code
Example #12
Source File: test_dict_fact.py From modl with BSD 2-Clause "Simplified" License | 6 votes |
def enet_regression_single_gram_(G, Dx, X, code, code_l1_ratio, code_alpha, code_pos): batch_size = code.shape[0] if code_l1_ratio == 0: n_components = G.shape[0] G = G.copy() G.flat[::n_components + 1] += code_alpha code[:] = linalg.solve(G, Dx.T).T G.flat[::n_components + 1] -= code_alpha else: # Unused but unfortunate API random_state = check_random_state(0) for i in range(batch_size): cd_fast.enet_coordinate_descent_gram( code[i], code_alpha * code_l1_ratio, code_alpha * ( 1 - code_l1_ratio), G, Dx[i], X[i], 100, 1e-2, random_state, False, code_pos) return code
Example #13
Source File: test_linalg.py From GraphicDesignPatternByPython with MIT License | 6 votes |
def test_0_size_k(self): # test zero multiple equation (K=0) case. class ArraySubclass(np.ndarray): pass a = np.arange(4).reshape(1, 2, 2) b = np.arange(6).reshape(3, 2, 1).view(ArraySubclass) expected = linalg.solve(a, b)[:, :, 0:0] result = linalg.solve(a, b[:, :, 0:0]) assert_array_equal(result, expected) assert_(isinstance(result, ArraySubclass)) # test both zero. expected = linalg.solve(a, b)[:, 0:0, 0:0] result = linalg.solve(a[:, 0:0, 0:0], b[:, 0:0, 0:0]) assert_array_equal(result, expected) assert_(isinstance(result, ArraySubclass))
Example #14
Source File: test_linalg.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 6 votes |
def test_0_size_k(self): # test zero multiple equation (K=0) case. class ArraySubclass(np.ndarray): pass a = np.arange(4).reshape(1, 2, 2) b = np.arange(6).reshape(3, 2, 1).view(ArraySubclass) expected = linalg.solve(a, b)[:, :, 0:0] result = linalg.solve(a, b[:, :, 0:0]) assert_array_equal(result, expected) assert_(isinstance(result, ArraySubclass)) # test both zero. expected = linalg.solve(a, b)[:, 0:0, 0:0] result = linalg.solve(a[:, 0:0, 0:0], b[:, 0:0, 0:0]) assert_array_equal(result, expected) assert_(isinstance(result, ArraySubclass))
Example #15
Source File: test_linalg.py From pySINDy with MIT License | 6 votes |
def test_0_size_k(self): # test zero multiple equation (K=0) case. class ArraySubclass(np.ndarray): pass a = np.arange(4).reshape(1, 2, 2) b = np.arange(6).reshape(3, 2, 1).view(ArraySubclass) expected = linalg.solve(a, b)[:, :, 0:0] result = linalg.solve(a, b[:, :, 0:0]) assert_array_equal(result, expected) assert_(isinstance(result, ArraySubclass)) # test both zero. expected = linalg.solve(a, b)[:, 0:0, 0:0] result = linalg.solve(a[:, 0:0, 0:0], b[:, 0:0, 0:0]) assert_array_equal(result, expected) assert_(isinstance(result, ArraySubclass))
Example #16
Source File: small.py From gdshelpers with GNU Lesser General Public License v3.0 | 6 votes |
def find_line_intersection(r1, angle1, r2, angle2): """ Find intersection between two lines defined by point and direction. :param r1: Origin of the first line. :param angle1: Angle of the first line. :param r2: Origin of the second line. :param angle2: Angle of the second line. :return: Tuple of point of intersection and distances from the origins. :rtype: tuple """ u1 = np.array([np.cos(angle1), np.sin(angle1)]) u2 = np.array([np.cos(angle2), np.sin(angle2)]) a = np.array([[u1[0], -u2[0]], [u1[1], -u2[1]]]) b = r2 - r1 # noinspection PyTypeChecker x = linalg.solve(a, b) return r1 + u1 * x[0], x
Example #17
Source File: gw_iter.py From pyscf with Apache License 2.0 | 5 votes |
def si_c2(self,ww): """ This computes the correlation part of the screened interaction using LinearOpt and lgmres lgmres method is much slower than np.linalg.solve !! """ import numpy as np from scipy.sparse.linalg import lgmres from scipy.sparse.linalg import LinearOperator rf0 = si0 = self.rf0(ww) for iw,w in enumerate(ww): k_c = np.dot(self.kernel_sq, rf0[iw,:,:]) b = np.dot(k_c, self.kernel_sq) self.comega_current = w k_c_opt = LinearOperator((self.nprod,self.nprod), matvec=self.gw_vext2veffmatvec, dtype=self.dtypeComplex) for m in range(self.nprod): si0[iw,m,:],exitCode = lgmres(k_c_opt, b[m,:], atol=self.gw_iter_tol, maxiter=self.maxiter) if exitCode != 0: print("LGMRES has not achieved convergence: exitCode = {}".format(exitCode)) #np.allclose(np.dot(k_c, si0), b, atol=1e-05) == True #Test return si0
Example #18
Source File: test_linalg.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def do(self, a, b, tags): x = linalg.solve(a, b) assert_almost_equal(b, dot_generalized(a, x)) assert_(consistent_subclass(x, b))
Example #19
Source File: beamformer.py From pb_bss with MIT License | 5 votes |
def get_mvdr_vector_merl(target_psd_matrix, noise_psd_matrix): """ Returns the MVDR beamforming vector. This implementation is based on a variant described in https://www.merl.com/publications/docs/TR2016-072.pdf It selects a reference channel that maximizes the post-SNR. :param target_psd_matrix: Target PSD matrix with shape (..., bins, sensors, sensors) :param noise_psd_matrix: Noise PSD matrix with shape (..., bins, sensors, sensors) :return: Set of beamforming vectors with shape (..., bins, sensors) """ G = np.linalg.solve(noise_psd_matrix, target_psd_matrix) lambda_ = np.trace(G, axis1=-2, axis2=-1) h = G / lambda_[..., None, None] nom = np.sum( np.einsum('...fac,fab,...fbc->c', h.conj(), target_psd_matrix, h) ) denom = np.sum( np.einsum('...fac,fab,...fbc->c', h.conj(), noise_psd_matrix, h) ) h_idx = np.argmax(nom/denom) return h[..., h_idx]
Example #20
Source File: beamformer.py From pb_bss with MIT License | 5 votes |
def get_mvdr_vector(atf_vector, noise_psd_matrix): """ Returns the MVDR beamforming vector. :param atf_vector: Acoustic transfer function vector with shape (..., bins, sensors) :param noise_psd_matrix: Noise PSD matrix with shape (bins, sensors, sensors) :return: Set of beamforming vectors with shape (..., bins, sensors) """ assert noise_psd_matrix is not None while atf_vector.ndim > noise_psd_matrix.ndim - 1: noise_psd_matrix = np.expand_dims(noise_psd_matrix, axis=0) # Make sure matrix is hermitian noise_psd_matrix = 0.5 * ( noise_psd_matrix + np.conj(noise_psd_matrix.swapaxes(-1, -2)) ) try: numerator = solve(noise_psd_matrix, atf_vector) except np.linalg.LinAlgError: bins = noise_psd_matrix.shape[0] numerator = np.empty_like(atf_vector) for f in range(bins): numerator[f], *_ = np.linalg.lstsq(noise_psd_matrix[f], atf_vector[..., f, :]) denominator = np.einsum('...d,...d->...', atf_vector.conj(), numerator) beamforming_vector = numerator / np.expand_dims(denominator, axis=-1) return beamforming_vector
Example #21
Source File: test_linalg.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def test_0_size(self): class ArraySubclass(np.ndarray): pass # Test system of 0x0 matrices a = np.arange(8).reshape(2, 2, 2) b = np.arange(6).reshape(1, 2, 3).view(ArraySubclass) expected = linalg.solve(a, b)[:, 0:0, :] result = linalg.solve(a[:, 0:0, 0:0], b[:, 0:0, :]) assert_array_equal(result, expected) assert_(isinstance(result, ArraySubclass)) # Test errors for non-square and only b's dimension being 0 assert_raises(linalg.LinAlgError, linalg.solve, a[:, 0:0, 0:1], b) assert_raises(ValueError, linalg.solve, a, b[:, 0:0, :]) # Test broadcasting error b = np.arange(6).reshape(1, 3, 2) # broadcasting error assert_raises(ValueError, linalg.solve, a, b) assert_raises(ValueError, linalg.solve, a[0:0], b[0:0]) # Test zero "single equations" with 0x0 matrices. b = np.arange(2).reshape(1, 2).view(ArraySubclass) expected = linalg.solve(a, b)[:, 0:0] result = linalg.solve(a[:, 0:0, 0:0], b[:, 0:0]) assert_array_equal(result, expected) assert_(isinstance(result, ArraySubclass)) b = np.arange(3).reshape(1, 3) assert_raises(ValueError, linalg.solve, a, b) assert_raises(ValueError, linalg.solve, a[0:0], b[0:0]) assert_raises(ValueError, linalg.solve, a[:, 0:0, 0:0], b)
Example #22
Source File: test_linalg.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def test_types(self): def check(dtype): x = np.array([[1, 0.5], [0.5, 1]], dtype=dtype) assert_equal(linalg.solve(x, x).dtype, dtype) for dtype in [single, double, csingle, cdouble]: check(dtype)
Example #23
Source File: test_linalg.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def do(self, a, b, tags): x = linalg.solve(a, b) assert_almost_equal(b, dot_generalized(a, x)) assert_(consistent_subclass(x, b))
Example #24
Source File: statesp.py From python-control with BSD 3-Clause "New" or "Revised" License | 5 votes |
def horner(self, s): """Evaluate the systems's transfer function for a complex variable Returns a matrix of values evaluated at complex variable s. """ resp = np.dot(self.C, solve(s * eye(self.states) - self.A, self.B)) + self.D return array(resp) # Method for generating the frequency response of the system
Example #25
Source File: recsys.py From modl with BSD 2-Clause "Simplified" License | 5 votes |
def _refit(self, X): n_samples, n_features = X.shape for i in range(n_samples): X_subset = X.data[X.indptr[i]:X.indptr[i + 1]] subset = X.indices[X.indptr[i]:X.indptr[i + 1]] len_subset = subset.shape[0] reduction = n_features / len_subset components_subset = self.components_[:, subset] Dx = components_subset.dot(X_subset) G = components_subset.dot(components_subset.T) G.flat[::self.n_components + 1] += self.alpha / reduction self.code_[i] = linalg.solve(G, Dx)
Example #26
Source File: statesp.py From python-control with BSD 3-Clause "New" or "Revised" License | 5 votes |
def dcgain(self): """Return the zero-frequency gain The zero-frequency gain of a continuous-time state-space system is given by: .. math: G(0) = - C A^{-1} B + D and of a discrete-time state-space system by: .. math: G(1) = C (I - A)^{-1} B + D Returns ------- gain : ndarray An array of shape (outputs,inputs); the array will either be the zero-frequency (or DC) gain, or, if the frequency response is singular, the array will be filled with np.nan. """ try: if self.isctime(): gain = np.asarray(self.D-self.C.dot(np.linalg.solve(self.A, self.B))) else: gain = self.horner(1) except LinAlgError: # eigenvalue at DC gain = np.tile(np.nan, (self.outputs, self.inputs)) return np.squeeze(gain) # TODO: add discrete time check
Example #27
Source File: _experimental.py From nusa with MIT License | 5 votes |
def solve(self): # Solve LS self.VU = [node[key] for node in self.U.values() for key in ("ux","uy")] self.VF = [node[key] for node in self.F.values() for key in ("fx","fy")] knw = [pos for pos,value in enumerate(self.VU) if not value is np.nan] unknw = [pos for pos,value in enumerate(self.VU) if value is np.nan] self.K2S = np.delete(np.delete(self.KG,knw,0),knw,1) self.F2S = np.delete(self.VF,knw,0) # For displacements self.solved_u = la.solve(self.K2S,self.F2S) for k,ic in enumerate(unknw): nd, var = self.index2key(ic) self.U[nd][var] = self.solved_u[k] # Updating nodes displacements for nd in self.nodes.values(): if np.isnan(nd.ux): nd.ux = self.U[nd.label]["ux"] if np.isnan(nd.uy): nd.uy = self.U[nd.label]["uy"] # For nodal forces/reactions self.NF = self.F.copy() self.VU = [node[key] for node in self.U.values() for key in ("ux","uy")] nf_calc = np.dot(self.KG, self.VU) for k in range(2*self.get_number_of_nodes()): nd, var = self.index2key(k, ("fx","fy")) self.NF[nd][var] = nf_calc[k] cnlab = np.floor(k/float(self.dof)) if var=="fx": self.nodes[cnlab].fx = nf_calc[k] elif var=="fy": self.nodes[cnlab].fy = nf_calc[k]
Example #28
Source File: model.py From nusa with MIT License | 5 votes |
def solve(self): # Solve LS self.VU = [node[key] for node in self.U.values() for key in ("uy","ur")] self.VF = [node[key] for node in self.F.values() for key in ("fy","m")] knw = [pos for pos,value in enumerate(self.VU) if not value is np.nan] unknw = [pos for pos,value in enumerate(self.VU) if value is np.nan] self.K2S = np.delete(np.delete(self.KG,knw,0),knw,1) self.F2S = np.delete(self.VF,knw,0) # For displacements self.solved_u = la.solve(self.K2S,self.F2S) for k,ic in enumerate(unknw): nd, var = self.index2key(ic) self.U[nd][var] = self.solved_u[k] # Updating nodes displacements for nd in self.nodes.values(): if np.isnan(nd.uy): nd.uy = self.U[nd.label]["uy"] if np.isnan(nd.ur): nd.ur = self.U[nd.label]["ur"] # For nodal forces/reactions self.NF = self.F.copy() self.VU = [node[key] for node in self.U.values() for key in ("uy","ur")] nf_calc = np.dot(self.KG, self.VU) for k in range(2*self.get_number_of_nodes()): nd, var = self.index2key(k, ("fy","m")) self.NF[nd][var] = nf_calc[k] cnlab = np.floor(k/float(self.dof)) if var=="fy": self.nodes[cnlab].fy = nf_calc[k] elif var=="m": self.nodes[cnlab].m = nf_calc[k]
Example #29
Source File: model.py From nusa with MIT License | 5 votes |
def solve(self): # known and unknown values self.VU = [node[key] for node in self.U.values() for key in ("ux",)] self.VF = [node[key] for node in self.F.values() for key in ("fx",)] knw = [pos for pos,value in enumerate(self.VU) if not value is np.nan] unknw = [pos for pos,value in enumerate(self.VU) if value is np.nan] if len(unknw)==1: _k = unknw[0] _rowtmp = self.KG[_k,:] _ftmp = self.VF[_k] _fk = _ftmp - np.dot(np.delete(_rowtmp,_k), np.delete(self.VU,_k)) _uk = _fk / self.KG[_k, _k] # Then self.solved_u = np.array([_uk]) else: # "Normal" case self.K2S = np.delete(np.delete(self.KG,knw,0),knw,1) self.F2S = np.delete(self.VF,knw,0) self.solved_u = la.solve(self.K2S,self.F2S) # For displacements # Updating U (displacements vector) for k,ic in enumerate(unknw): nd, var = self.index2key(ic) self.U[nd][var] = self.solved_u[k] self.nodes[ic].ux = self.solved_u[k] # For nodal forces/reactions self.NF = self.F.copy() self.VU = [node[key] for node in self.U.values() for key in ("ux",)] nf_calc = np.dot(self.KG, self.VU) for k,ic in enumerate(range(self.get_number_of_nodes())): nd, var = self.index2key(ic, ("fx",)) self.NF[nd][var] = nf_calc[k] self.nodes[ic].fx = nf_calc[k]
Example #30
Source File: test_linalg.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_types(self, dtype): x = np.array([[1, 0.5], [0.5, 1]], dtype=dtype) assert_equal(linalg.solve(x, x).dtype, dtype)