def solve(A, b, x0, eps=1E-8, iter_max=1000): 'Solve system Ax=b with Jacobi method.' # Checks of input m, n = A.shape assert m == n assert b.shape == (n, ) # To prevent some integer division surprises with inverse A = A.astype('float', copy=False) b = b.astype('float', copy=False) # Get Jacobi matrix # If the diagonal is all zero, there is no way to continues if la.norm(A.diagonal()) < 1E-15: print 'Is diag(A) all zeros?' return x0 else: diag_inv = np.array([1./d if abs(d) > 1E-14 else 0 for d in A.diagonal()]) D_inv = np.diag(diag_inv) E = A - np.diag(A.diagonal()) if la.norm(E) < 1E-13: E = np.eye(n) B = -D_inv.dot(E) # compute the rhs constant term z = D_inv.dot(b) n_iters = 0 r = b - A.dot(x0) # Initialize residuum r_norm = r.dot(r) r_norm0 = r_norm # Remember size for stopping loop # Compare the spectra A_vals = la.eigvals(A) B_vals = la.eigvals(B) print 'A eigenvalues', A_vals print 'B eigenvalues', B_vals if not np.all(np.abs(B_vals) < 1): print '\tSome eigenvalues of B are greate than one in magnitude!' while n_iters < iter_max and r_norm > eps*r_norm0: n_iters += 1 # Get the new solution x0 = B.dot(x0) + z # Compute norm of residuum r = b - A.dot(x0) r_norm = r.dot(r) return x0, n_iters
def linear_algebra(): """ Use the `numpy.linalg` library to do Linear Algebra For a reference on math, see 'Linear Algebra explained in four pages' http://minireference.com/static/tutorials/linear_algebra_in_4_pages.pdf """ ### Setup two vectors x = np.array([1, 2, 3, 4]) y = np.array([5, 6, 7, 8]) ### Vector Operations include addition, subtraction, scaling, norm (length), # dot product, and cross product print np.vdot(x, y) # Dot product of two vectors ### Setup two arrays / matrices a = np.array([[1, 2], [3, 9]]) b = np.array([[2, 4], [5, 6]]) ### Dot Product of two arrays print np.dot(a, b) ### Solving system of equations (i.e. 2 different equations with x and y) print LA.solve(a, b) ### Inverse of a matrix undoes the effects of the Matrix # The matrix multipled by the inverse matrix returns the # 'identity matrix' (ones on the diagonal and zeroes everywhere else); # identity matrix is useful for getting rid of the matrix in some equation print LA.inv(a) # return inverse of the matrix print "\n" ### Determinant of a matrix is a special way to combine the entries of a # matrix that serves to check if matrix is invertible (!=0) or not (=0) print LA.det(a) # returns the determinant of the array print "\n" # e.g. 3, means that it is invertible ### Eigenvectors is a special set of input vectors for which the action of # the matrix is described as simple 'scaling'. When a matrix is multiplied # by one of its eigenvectors, the output is the same eigenvector multipled # by a constant (that constant is the 'eigenvalue' of the matrix) print LA.eigvals(a) # comput the eigenvalues of a general matrix print "\n" print LA.eigvalsh(a) # Comput the eigenvalues of a Hermitian or real symmetric matrix print "\n" print LA.eig(a) # return the eigenvalues for a square matrix print "\n" print LA.eigh(a) # return the eigenvalues or eigenvectors of a Hermitian or symmetric matrix print "\n"
def ismassmatrix(M, semi=False): """ Check whether M is a valid mass matrix. :param M: the mass matrix to check :type M: (6,6)-array :param bool semi: if set to ``True``, positive *semi*-definite matrices are also considered valid. :return: ``True`` if M is correctly shaped and symmetric positive definite. """ common = M.shape == (6, 6) and allclose(M, M.T) and allclose(M[3:6, 3:6], M[3, 3] * eye(3)) if semi: return common and (eigvals(M) >= 0.0).all() else: return common and (eigvals(M) > 0.0).all()
def updateD_H(self, x): """ Compute Hessian for update of D See [2] for derivation of Hessian """ self.precompute(x) H = zeros((len(x), len(x))) Ai = zeros(self.A.shape[0]) Aj = zeros(Ai.shape) for i in range(len(x)): Ai = self.A[:, i] ti = dot(self.AD, outer(self.R[:, i], Ai)) + dot(outer(Ai, self.R[i, :]), self.ADt) for j in range(i, len(x)): Aj = self.A[:, j] tj = outer(Ai, Aj) H[i, j] = ( self.E * (self.R[i, j] * tj + self.R[j, i] * tj.T) - ti * ( dot(self.AD, outer(self.R[:, j], Aj)) + dot(outer(Aj, self.R[j, :]), self.ADt) ) ).sum() H[j, i] = H[i, j] H *= -2 e = eigvals(H).min() H = H + (eye(H.shape[0]) * e) return H
def _hessx(xi, i): hess = numpy.zeros((k, k)) for j in range(l): gij = snp_matrix[i, j] if gij == geosnp.MISSING: continue yj = Y[j] qj, aj, bj = yj[0], yj[1 : k + 1], yj[-1] qnf = (qj * sum(xi ** 2.0)) + aj.dot(xi) + bj fij = 1.0 / (1.0 + math.exp(qnf)) term = 2.0 * sum(qj * xi) + aj hess -= fij * (1.0 - fij) * numpy.outer(term, term) + (gij - 2.0 * fij) * (2.0 * qj) # flip for NLL hess = -2.0 * hess # as NLL wrt X is not necessarily convex, we may need to alter the # hessian so that newton method still converges to local optimum # algorithm 6.3 from Numerical Opt Nocedal,Wright 1999 beta = linalg.norm(hess, "fro") tau = 0 if min(numpy.diag(hess)) > 0 else beta eye = numpy.eye(k) while True: hess = hess + (tau * eye) # test for Positive Definiteness if min(linalg.eigvals(hess)) > 0: break else: tau = max(2 * tau, beta / 2) return hess
def too_small_check(ccov, eigcut = 10**(-10)): """Check to see if the matrix eigenvalues are too small. This can cause problems when computing chi^2 due to precision loss """ testeig = eigvals(ccov) flag = 0 for entry in testeig: if entry < eigcut: flag = 1 print "***Warning***" print "Range selected has a covariance matrix with" print "very small eigenvalues. This can cause problems" print "in computing chi^2, as well as quantities derived" print "from chi^2. The cuttoff is set at:", eigcut print "Problematic eigenvalue = ", entry break if flag == 1: print "List of eigenvalues of covariance matrix:" for entry in testeig: print entry while True: print "Continue? (y/n)" cresp = str(raw_input()) if (cresp == "n" or cresp == "no" or cresp == "No" or cresp == "N"): sys.exit(0) if (cresp == "y" or cresp == "yes" or cresp == "Yes" or cresp == "Y"): break else: print "Sorry, I didn't understand that." continue return 0
def is_chaotic(J): """Determines whether the Jacobian matrix is chaotic Arguments ========== J = string, list of lists, array of arrays, or matrix Returns ======= bool = True if chaotic, False otherwise """ # start your code here margin = 0.0001 J = np.mat(J) eigens = linalg.eigvals(J) less = 0 equal = 0 more = 0 for eigen in eigens: if abs(eigen - 1) < margin: equal = equal + 1 else: if eigen < 1: less = less + 1 if eigen > 1: more = more + 1 if less == 1 and equal == 1 and more == 1: return True else: return False
def speigen_range(matrix, retry=True, coerce=True): """ Construct the eigenrange of a potentially sparse matrix. """ if spar.issparse(matrix): try: emax = spla.eigs(matrix, k=1, which='LR')[0] except (spla.ArpackNoConvergence, spla.ArpackError) as e: rowsums = np.unique(np.asarray(matrix.sum(axis=1)).flatten()) if np.allclose(rowsums, np.ones_like(rowsums)): emax = np.array([1]) else: Warn('Maximal eigenvalue computation failed to converge' ' and matrix is not row-standardized.') raise e emin = spla.eigs(matrix, k=1, which='SR')[0] if coerce: emax = emax.real.astype(float) emin = emin.real.astype(float) else: try: eigs = nla.eigvals(matrix) emin, emax = eigs.min().astype(float), eigs.max().astype(float) except Exception as e: Warn('Dense eigenvector computation failed!') if retry: Warn('Retrying with sparse matrix...') spmatrix = spar.csc_matrix(matrix) speigen_range(spmatrix) else: Warn('Bailing...') raise e return emin, emax
def importance_pca(data, kpi, max_features=10): """ :param data: dataframe containing training data :param kpi: Name of the current kpi :param max_features: maximum number of features to return :return: list of the best metrics The function does not use scikit-learn PCA implementation, because it is more difficult to associate each feature with its eigenvalue in the correlation matrix, also we are not interested in the projection vectors, but only in the eigenvalues. """ columns = data[[col for col in set(data.columns) - {kpi}]].columns # correlation matrix and eigenvalues calculation corr = data[columns].corr().fillna(value=0).values eigenvalues = linalg.eigvals(corr) normalized_eigenvalues = normalize_series(eigenvalues) scaled_eigenvalues = normalized_eigenvalues / sum(normalized_eigenvalues) ranked_columns = [ (eig, columns[n]) for n, eig in enumerate(scaled_eigenvalues) ] return [(j, i) for i, j in sorted( ranked_columns, reverse=True )][: max_features]
def test_eigenvector_v_katz_random(self): G = nx.gnp_random_graph(10,0.5, seed=1234) l = float(max(eigvals(nx.adjacency_matrix(G).todense()))) e = nx.eigenvector_centrality_numpy(G) k = nx.katz_centrality_numpy(G, 1.0/l) for n in G: assert_almost_equal(e[n], k[n])
def Aii_dpf(self, i, theta, phi, A=None, no_psd=False, byhand=False): """computes a single component of A in the dominant polarization frame. If A is supplied, it converts to the dominant polarizatoin frame""" if A==None: A = self.A(theta, phi, 0.0, no_psd=no_psd) if no_psd: npix, npol, npol = np.shape(A) a = np.empty((npix,1,npol,npol),float) a[:,0,:,:] = A A = a if byhand: a = A[:,:,0,0] b = A[:,:,0,1] c = A[:,:,1,1] dpfA = np.zeros_like(A, float) x = ((a-c)**2 + 4*b**2)**0.5 y = a+c if no_psd: return 0.5*(y[:,0] + (-1)**i * x[:,0]) else: return 0.5*(y + (-1)**i * x) else: vals = linalg.eigvals(A)[:,:,::-1] ### order by decreasing eigenvalue if no_psd: return vals[:,0,i] else: return vals[:,:,i]
def diagonalization(p): for i in range(M): for j in range(M): Ha[i,j]=np.dot(p[i],np.dot(H,p[j])) eigenvals=LA.eigvals(Ha) return(Ha,eigenvals)
def lanczos_eig(A): m,n = np.shape(A) Q = np.zeros((m,n)) beta = 0 x0 = np.random.randn(n) Q[:,0] = x0/(la.norm(x0)) alpha = np.zeros(n) gama = [] U = np.zeros((m,n)) H = np.zeros((m,n)) for i in range(0,n): U[:,i] = np.dot(A,Q[:,i]) alpha = np.dot(Q[:,i].T,U[:,i]) U[:,i] = U[:,i]-beta*Q[:,i-1]-alpha*Q[:,i] beta = la.norm(U[:,i]) H[i,i] = alpha if(i+1<n): H[i+1,i] = beta H[i,i+1] = beta if beta == 0: break if(i+1<n): Q[:,i+1]=U[:,i]/beta gama.append(np.copy(la.eigvals(H).T)) return gama
def EV(sasb_n, sasb_nm1, sa_n, sa_nm1,debug1=False,debug2=False): try: dim = len(sa_n) sasb_n = sasb_n.reshape(dim , dim) sasb_nm1=sasb_nm1.reshape(dim , dim) dsdk_n =sasb_n -outer(sa_n,sa_n) dsdk_nm1=sasb_nm1-outer(sa_nm1,sa_n) T=dot(dsdk_nm1,inv(dsdk_n)) if debug2: print(cond(T)) #evs=sort(abs(eigvals(T))) evs=sort(eigvals(T)) if debug1: val,vec=eig(T) print(val) print('-----------------') for i in range(len(val)): print(val[i],vec[:,i]) print('-----------------') lambda_ =max(abs(evs)) lambda_test =evs[-1] #assert(lambda_==lambda_test) if(len(evs)>1): lambda_2=evs[-2] else: lambda_2=0 lambda_=RawSanitize(lambda_) return lambda_, lambda_2 except np.linalg.linalg.LinAlgError: return 0,0
def point_is_stable(self, point, tol=1e-5): """ returns true if a given steady state is stable """ if not self.point_is_steady_state(point, tol): raise ValueError('Supplied point is not a steady state') jacobian = self.jacobian(point, tol) x_check, y_check = False, False #< checked direction # check special boundary cases if 'left' in self.region_constraint and np.abs(point[0] - self.region[0]) < 1e-8: x_check = True elif 'right' in self.region_constraint and np.abs(point[0] - self.region[2]) < 1e-8: x_check = True if 'bottom' in self.region_constraint and np.abs(point[1] - self.region[1]) < 1e-8: y_check = True elif 'top' in self.region_constraint and np.abs(point[1] - self.region[3]) < 1e-8: y_check = True # check the remaining directions if x_check and y_check: return True # both x and y direction are stable elif x_check: return jacobian[1, 1] < 0 # y direction has to be tested elif y_check: return jacobian[0, 0] < 0 # x direction has to be tested else: return all(eigvals(jacobian) < 0) # both directions have to be tested
def calculate_beta(self, column_name): # it doesn't make much sense to calculate beta for less than two days, # so return nan. algorithm_returns = self.prices[column_name] algorithm_returns = (algorithm_returns-algorithm_returns.shift(1))/algorithm_returns.shift(1) if (column_name==self.benchmark_code_str): self.prices_return[column_name]=self.prices_return[column_name]/100 else: self.prices_return[column_name]=algorithm_returns algorithm_returns = algorithm_returns[1:] benchmark_returns = self.prices_return[self.benchmark_code_str] benchmark_returns = benchmark_returns[1:] if len(algorithm_returns) < 2: return np.nan returns_matrix = np.vstack([algorithm_returns,benchmark_returns]) C = np.cov(returns_matrix, ddof=1) # If there are missing benchmark values, then we can't calculate the # beta. if not np.isfinite(C).all(): return np.nan eigen_values = la.eigvals(C) condition_number = max(eigen_values) / min(eigen_values) algorithm_covariance = C[0][1] benchmark_variance = C[1][1] beta = algorithm_covariance / benchmark_variance #print beta return beta
def bisect_gFB(s, tres, Q11, Q22, Q12, Q21, k1, k2): """ Find number of eigenvalues of H(s) that are equal to or less than s. Parameters ---------- s : float Laplace transform argument. tres : float Time resolution (dead time). Q11 : array_like, shape (k1, k1) Q22 : array_like, shape (k2, k2) Q21 : array_like, shape (k2, k1) Q12 : array_like, shape (k1, k2) Q11, Q12, Q22, Q21 - submatrices of Q. k1 : int A number of open/shut states in kinetic scheme. k2 : int A number of shut/open states in kinetic scheme. Returns ------- ng : int """ h = qml.H(s, tres, Q11, Q22, Q12, Q21, k2) eigval = nplin.eigvals(h) ng = (eigval <= s).sum() return ng
def A_dpf(self, theta, phi, A=None, no_psd=False, byhand=False): """computes A in the dominant polarization frame. If A is supplied, it converts A to the dominant polarization frame""" if A==None: A = self.A(theta, phi, 0.0, no_psd=no_psd) if no_psd: npix, npol, npol = np.shape(A) a = np.empty((npix, 1, npol, npol), float) a[:,0,:,:] = A A = a if byhand: a = A[:,:,0,0] b = A[:,:,0,1] c = A[:,:,1,1] dpfA = np.zeros_like(A, float) x = ((a-c)**2 + 4*b**2)**0.5 y = a+c dpfA[:,:,0,0] = 0.5*(y + x) dpfA[:,:,1,1] = 0.5*(y - x) else: dpfA=np.zeros_like(A, float) vals = linalg.eigvals(A)[:,:,::-1] ### order by decreasing eigenvalue for i in xrange(self.Np): dpfA[:,:,i,i] = vals[:,:,i] if no_psd: return dpfA[:,0,:,:] else: return dpfA
def _compressibility(self, A, TdAdT, dAdn, B, dBdn): """Calculate the compressibility from the parameters A and B""" c = np.array([-1, A - B * (1 + B), -(A * B)], dtype=np.float) self._companionmatrix[0, :] = -c result = la.eigvals(self._companionmatrix) result = result[result.imag < ERROR_TOL].real return np.array((result.max(), result.min()), dtype=phase_struct)
def polyroots(cs): """Roots of a polynomial. Compute the roots of the Chebyshev series `cs`. The argument `cs` is a sequence of coefficients ordered from low to high. i.e., [1,2,3] is the polynomial ``1 + 2*x + 3*x**2``. Parameters ---------- cs : array_like of shape(M,) 1D array of polynomial coefficients ordered from low to high. Returns ------- out : ndarray An array containing the complex roots of the polynomial series. Examples -------- """ # cs is a trimmed copy [cs] = pu.as_series([cs]) if len(cs) <= 1 : return np.array([], dtype=cs.dtype) if len(cs) == 2 : return np.array([-cs[0]/cs[1]]) n = len(cs) - 1 cmat = np.zeros((n,n), dtype=cs.dtype) cmat.flat[n::n+1] = 1 cmat[:,-1] -= cs[:-1]/cs[-1] roots = la.eigvals(cmat) roots.sort() return roots
def __init__(self,ninput,nnodes,conn_input=0.4,conn_recurrent=0.2,gamma=numpy.tanh,frac_exc=0.5): self.ninput=ninput self.nnodes=nnodes self.gamma=gamma self.conn_recurrent=conn_recurrent self.conn_input=conn_input self.frac_exc=frac_exc w_echo = numpy.array( [[self.connection_weight(i,j) for j in range(self.nnodes)] for i in range(self.nnodes)]) w_input=numpy.array( [[self.input_weight(i,j) for j in range(self.ninput)] for i in range(self.nnodes)]) w_add = numpy.array( [self.add_weight(i) for i in range(self.nnodes)]) eigenvalues=linalg.eigvals(w_echo) spectral_radius=max([abs (a) for a in eigenvalues]) w_echo=(0.95/spectral_radius)*w_echo self.w_echo = w_echo self.w_input = w_input self.w_add = w_add
def ismassmatrix(M, semi=False): """Check whether M is a valid mass matrix. Return ``True`` if M is correctly shaped and symmetric positive definite. When ``semi`` is set to ``True``, positive *semi*-definite matrices are also considered valid. """ common = M.shape == (6,6) and allclose(M, M.T) and \ allclose(M[3:6, 3:6], M[3,3]*eye(3)) if semi: return common and (eigvals(M) >= 0.).all() else: return common and (eigvals(M) > 0.).all()
def calculate_beta(self): """ .. math:: \\beta_a = \\frac{\mathrm{Cov}(r_a,r_p)}{\mathrm{Var}(r_p)} http://en.wikipedia.org/wiki/Beta_(finance) """ #it doesn't make much sense to calculate beta for less than two days, #so return none. if len(self.algorithm_returns) < 2: return 0.0, 0.0, 0.0, 0.0, [] returns_matrix = np.vstack([self.algorithm_returns, self.benchmark_returns]) C = np.cov(returns_matrix) eigen_values = la.eigvals(C) condition_number = max(eigen_values) / min(eigen_values) algorithm_covariance = C[0][1] benchmark_variance = C[1][1] beta = C[0][1] / C[1][1] return ( beta, algorithm_covariance, benchmark_variance, condition_number, eigen_values )
def legroots(cs): """ Compute the roots of a Legendre series. Returns the roots (a.k.a "zeros") of the Legendre series represented by `cs`, which is the sequence of coefficients from lowest order "term" to highest, e.g., [1,2,3] is the series ``L_0 + 2*L_1 + 3*L_2``. Parameters ---------- cs : array_like 1-d array of Legendre series coefficients ordered from low to high. maxiter : int, optional Maximum number of iterations of Newton to use in refining the roots. Returns ------- out : ndarray Sorted array of the roots. If all the roots are real, then so is the dtype of ``out``; otherwise, ``out``'s dtype is complex. See Also -------- polyroots chebroots Notes ----- The root estimates are obtained as the eigenvalues of the companion matrix, Roots far from the real interval [-1, 1] in the complex plane may have large errors due to the numerical instability of the Lengendre series for such values. Roots with multiplicity greater than 1 will also show larger errors as the value of the series near such points is relatively insensitive to errors in the roots. Isolated roots near the interval [-1, 1] can be improved by a few iterations of Newton's method. The Legendre series basis polynomials aren't powers of ``x`` so the results of this function may seem unintuitive. Examples -------- >>> import numpy.polynomial.legendre as leg >>> leg.legroots((1, 2, 3, 4)) # 4L_3 + 3L_2 + 2L_1 + 1L_0 has only real roots array([-0.85099543, -0.11407192, 0.51506735]) """ # cs is a trimmed copy [cs] = pu.as_series([cs]) if len(cs) < 2: return np.array([], dtype=cs.dtype) if len(cs) == 2: return np.array([-cs[0]/cs[1]]) m = legcompanion(cs) r = la.eigvals(m) r.sort() return r
def isStable(self, u): A=self.df(u) eigenwaarden=list(linalg.eigvals(A)) #print(eigenwaarden) for i in range(len(eigenwaarden)): if eigenwaarden[i]>=0: return False return True
def _is_sympsd(M): 'Check that the matrix M is symmetric positive semi-definite' is_sympsd = False if _is_symmetric(M): minevM = min(linalg.eigvals(M)) if (minevM >= 0.): is_sympsd = True return is_sympsd
def run_experiment(niter=100): K = 100 results = [] for _ in xrange(niter): mat = np.random.randn(K, K) max_eigenvalue = np.abs(eigvals(mat)).max() results.append(max_eigenvalue) return results
def do(self, a, b): d = linalg.det(a) if asarray(a).dtype.type in (single, double): ad = asarray(a).astype(double) else: ad = asarray(a).astype(cdouble) ev = linalg.eigvals(ad) assert_almost_equal(d, multiply.reduce(ev))
def test_stability(self, Q): """ Stability test for a given matrix Q. """ sr = np.max(np.abs(eigvals(Q))) if not sr < 1 / self.beta: msg = "Spectral radius condition failed with radius = %f" % sr raise ValueError(msg)
def lagroots(c): """ Compute the roots of a Laguerre series. Return the roots (a.k.a. "zeros") of the polynomial .. math:: p(x) = \\sum_i c[i] * L_i(x). Parameters ---------- c : 1-D array_like 1-D array of coefficients. Returns ------- out : ndarray Array of the roots of the series. If all the roots are real, then `out` is also real, otherwise it is complex. See Also -------- polyroots, legroots, chebroots, hermroots, hermeroots Notes ----- The root estimates are obtained as the eigenvalues of the companion matrix, Roots far from the origin of the complex plane may have large errors due to the numerical instability of the series for such values. Roots with multiplicity greater than 1 will also show larger errors as the value of the series near such points is relatively insensitive to errors in the roots. Isolated roots near the origin can be improved by a few iterations of Newton's method. The Laguerre series basis polynomials aren't powers of `x` so the results of this function may seem unintuitive. Examples -------- >>> from numpy.polynomial.laguerre import lagroots, lagfromroots >>> coef = lagfromroots([0, 1, 2]) >>> coef array([ 2., -8., 12., -6.]) >>> lagroots(coef) array([-4.4408921e-16, 1.0000000e+00, 2.0000000e+00]) """ # c is a trimmed copy [c] = pu.as_series([c]) if len(c) <= 1: return np.array([], dtype=c.dtype) if len(c) == 2: return np.array([1 + c[0]/c[1]]) # rotated companion matrix reduces error m = lagcompanion(c)[::-1,::-1] r = la.eigvals(m) r.sort() return r
def waterpouring(Mt, SNR_dB, H_chan): SNR = 10**(SNR_dB / 10) r = LA.matrix_rank(H_chan) H_sq = np.dot(H_chan, np.matrix(H_chan, dtype=complex).H) lambdas = LA.eigvals(H_sq) lambdas = np.sort(lambdas)[::-1] p = 1 gammas = np.zeros((r, 1)) flag = True while flag == True: lambdas_r_p_1 = lambdas[0:(r - p + 1)] inv_lambdas_sum = np.sum(1 / lambdas_r_p_1) mu = (Mt / (r - p + 1)) * (1 + (1 / SNR) * inv_lambdas_sum) for idx, item in enumerate(lambdas_r_p_1): gammas[idx] = mu - (Mt / (SNR * item)) if gammas[r - p] < 0: #due to Python starts from 0 gammas[r - p] = 0 #due to Python starts from 0 p = p + 1 else: flag = False res = [] for gamma in gammas: res.append(float(gamma)) return np.array(res)
def extrackt_bbox(self): """ Bounding box extraction function. The algorithms creates the minimal bounding box around the ellipse defined by v_hat. Returns ------- bbox_extr: array_like struct containing the time series of bounding boxes """ v_hat = self._ggiw_post['v'] v_hat /= np.maximum( 1.0, self._ggiw_post['nu'][:, None, None] - 2 * self._1_d_1) bbox_extr = np.zeros(self._steps, dtype=self._dt_bbox) bbox_extr['ts'] = np.arange(self._steps, dtype='i4') - 1 bbox_extr['orientation'] = 0.5 * np.arctan2( 2 * v_hat[:, 0, 1], v_hat[:, 0, 0] - v_hat[:, 1, 1]) bbox_extr['dimension'] = 3.0 * np.sqrt(la.eigvals(v_hat)) bbox_extr['center_xy'] = self._ggiw_post['m'][:, :self._d] return bbox_extr
def chebroots(cs): """Roots of a Chebyshev series. Compute the roots of the Chebyshev series `cs`. The argument `cs` is a sequence of coefficients ordered from low to high. i.e., [1,2,3] is the series ``T_0 + 2*T_1 + 3*T_2``. Parameters ---------- cs : array_like 1D array of Chebyshev coefficients ordered from low to high. Returns ------- out : ndarray An array containing the complex roots of the chebyshev series. Examples -------- """ # cs is a trimmed copy [cs] = pu.as_series([cs]) if len(cs) <= 1: return np.array([], dtype=cs.dtype) if len(cs) == 2: return np.array([-cs[0] / cs[1]]) n = len(cs) - 1 cmat = np.zeros((n, n), dtype=cs.dtype) cmat.flat[1::n + 1] = .5 cmat.flat[n::n + 1] = .5 cmat[1, 0] = 1 cmat[:, -1] -= cs[:-1] * (.5 / cs[-1]) roots = la.eigvals(cmat) roots.sort() return roots
def form_PI_cl(data): A = np.array([[1.0]]) B = np.array([[1.0]]) for i in range(N): C = np.array([[dt * data['Ki_fit'][i]]]) D = np.array([[data['Kp_fit'][i]]]) pi_block = ss(A, B, C, D) bike_block = ss(data['A_cl'][i], data['B_cl'][i], data['C_cl'][i], 0) pc = series(pi_block, bike_block) cl = feedback(pc, 1, sign=-1) data['yr_cl_evals'][i] = la.eigvals(cl.A) assert (np.all(abs(data['yr_cl_evals'][i]) < 1.0)) data['A_yr_cl'][i] = cl.A data['B_yr_cl'][i] = cl.B data['C_yr_cl'][i] = cl.C assert (cl.D == 0) num, den = ss2tf(cl.A, cl.B, cl.C, cl.D) data['w_psi_r_to_psi_dot'][i], y = freqz(num[0], den) data['w_psi_r_to_psi_dot'][i] /= (dt * 2.0 * np.pi) data['mag_psi_r_to_psi_dot'][i] = 20.0 * np.log10(abs(y)) data['phase_psi_r_to_psi_dot'][i] = np.unwrap( np.angle(y)) * 180.0 / np.pi
def cost_function(normalized_gains_array, robot, q0, v0, nominal_gains_array): MAX_REAL_EIG_VAL = -3 MAX_NORMALIZED_GAIN = 2.0 W_UNSTABLE = 1e3 W_GAINS = 100.0 gains_array = denormalize_gains_array(normalized_gains_array, nominal_gains_array); H = compute_closed_loop_transition_matrix(gains_array, robot, q0, v0, update=False); ei = eigvals(H); cost = 0.0 for i in range(ei.shape[0]): ei_real, ei_imag = np.real(ei[i]), np.imag(ei[i]) if(ei_real<0.0): cost += (ei_imag/ei_real)**2 cost += max(ei_real, MAX_REAL_EIG_VAL) else: cost += W_UNSTABLE*(ei_real**2) for g in normalized_gains_array: if(g>MAX_NORMALIZED_GAIN): cost += W_GAINS*(g-MAX_NORMALIZED_GAIN)**2 return cost
def main(): N=512 t=1 dt=1 j=t+dt intra=np.array([[0,t],[t,0]]) inter=np.array([[0,0],[j,0]]) inter_T=inter.T H=np.kron(np.eye(N), intra) H +=np.kron(np.eye(N, k=1), inter) H +=np.kron(np.eye(N, k=-1), inter_T) H +=np.kron(np.eye(N, k=N-1), inter_T) H +=np.kron(np.eye(N, k=-N+1), inter) print(inter) print(intra) print(H) Hmfft=mfft(H,2) Hmfft2=mfft(Hmfft.conj().T,2).conj().T Hmfft3=fftshift(Hmfft2)/N print(Hmfft3) eigenwerte=[] for l in range(N): block0=Hmfft3[l*2:2+l*2:,l*2:2+l*2:] eigenwerte.extend(LA.eigvals(block0)) print(eigenwerte) x=np.arange(N*2) y=np.array(eigenwerte) plt.plot(x,y, 'ro') plt.show()
def multCheb(coeffs): """Finds the zeros of a 1-D chebyshev polynomial using a multiplication matrix. Parameters ---------- coeffs : numpy array The coefficients of the polynomial. Returns ------- zero : numpy array An array of the zeros. """ n = len(coeffs) - 1 mMatrix = np.zeros((n,n), dtype=coeffs.dtype) mMatrix[1][0] = 1 bot = mMatrix.reshape(-1)[1::n+1] bot[...] = 1/2 bot = mMatrix.reshape(-1)[2*n+1::n+1] bot[...] = 1/2 #print(coeffs[-1]) mMatrix[:,-1] -= .5*coeffs[:-1]/coeffs[-1] zeros = la.eigvals(mMatrix) return zeros
def update_clustering_components(self, mask, assignments): cluster_idx = 1 cluster_D = np.where(mask == cluster_idx)[0].shape[0] cluster_X = self.X[:, np.where(mask == cluster_idx)[0]] if cluster_D == 1: covar_scale = np.var(cluster_X) else: covar_scale = np.median(LA.eigvals(np.cov(cluster_X.T))) mu_scale = np.amax(cluster_X) - covar_scale # Intialize prior m_0 = cluster_X.mean(axis=0) k_0 = 1.0 / self.h1 # k_0 = covar_scale ** 2 / mu_scale ** 2 v_0 = cluster_D + 2 # S_0 = 1./100 / covar_scale * np.eye(cluster_D) S_0 = 1. * np.eye(cluster_D) cluster_kernel_prior = NIW(m_0, k_0, v_0, S_0) if self.covariance_type == "full": components = GaussianComponents(cluster_X, cluster_kernel_prior, assignments, self.K_max) elif self.covariance_type == "diag": components = GaussianComponentsDiag(cluster_X, cluster_kernel_prior, assignments, self.K_max) elif self.covariance_type == "fixed": components = GaussianComponentsFixedVar(cluster_X, cluster_kernel_prior, assignments, self.K_max) else: assert False, "Invalid covariance type." return components
def mpi(): print( "1. Преобразовать систему к виду, необходимому для применения метода простых итераций. " "Проверить условия сходимости МПИ") print("\nПреобразованная матрица B: ") new_B = replace_b() print(tabulate(new_B, tablefmt='fancy_grid')) print("\nПреобразованная матрица A: ") new_A = replace_a() print(tabulate(new_A, tablefmt='fancy_grid')) W = linalg.eigvals(new_A) # print(W) flag = 0 for i in range(0, n): if abs(W[i]) < 1: flag += 1 if flag == n and linalg.norm(new_A) < 1: print("\nРешения собственных значений матрицы по модулю < 1") print("\nНорма матрицы", linalg.norm(new_A), " < 1") print( "\nСледоательно, необходимое и достаточное условия МПИ выполняеются" ) else: print("\nУсловия не выполняются")
def ellfit2(XYZ): #EIGENVALUES from numpy import linalg as LA #m = [1.0] * len(XYZ) #TO BE MODIFIED #for i in range(len(XYZ)): # m[i] = i r = [0] * len(XYZ) x = XYZ[:, 0] y = XYZ[:, 1] z = XYZ[:, 2] M = np.sum(m) S = (3, 3) S = np.zeros(S) for i in range(len(XYZ)): S[0, 0] += m[i] * x[i] * x[i] / M S[1, 1] += m[i] * y[i] * y[i] / M S[2, 2] += m[i] * z[i] * z[i] / M S[0, 1] += m[i] * x[i] * y[i] / M S[1, 2] += m[i] * y[i] * z[i] / M S[0, 2] += m[i] * x[i] * z[i] / M S[1, 0] = S[0, 1] S[2, 1] = S[1, 2] S[2, 0] = S[0, 2] eigenValues = LA.eigvals(S) return eigenValues
def getGraph(p, d): # construct a graph from scale free distribution # paper: The joint graphical lasso for inverse covarianceestimation across multiple classes # reference: https://rss.onlinelibrary.wiley.com/doi/epdf/10.1111/rssb.12033 # p: number of nodes # d: out degree of each node Rnd = snap.TRnd(10) UGraph = snap.GenPrefAttach(p, d, Rnd) S = np.zeros((p, p)) for EI in UGraph.Edges(): # generate a random number in (-0.4, -0.1)U(0.1,0.4) # method: https://stats.stackexchange.com/questions/270856/how-to-randomly-generate-random-numbers-in-one-of-two-intervals r = np.random.uniform(0, 0.6) S[EI.GetSrcNId(), EI.GetDstNId( )] = r - 0.4 if r < 0.3 else r - 0.2 # assign values to edges S0 = S.copy() # orginal half graph without standardizing it into a PD matrix S = S + S.T S = S / (1.5 * np.sum(np.absolute(S), axis=1)[:, None]) A = (S + S.T) / 2 + np.matrix(np.eye(p)) # check if A is PD print(np.all(alg.eigvals(A) > 0)) return S0, A
def calculate_beta(self): """ .. math:: \\beta_a = \\frac{\mathrm{Cov}(r_a,r_p)}{\mathrm{Var}(r_p)} http://en.wikipedia.org/wiki/Beta_(finance) """ # it doesn't make much sense to calculate beta for less than two days, # so return nan. if len(self.algorithm_returns) < 2: return np.nan, np.nan, np.nan, np.nan, [] returns_matrix = np.vstack([self.algorithm_returns, self.benchmark_returns]) C = np.cov(returns_matrix, ddof=1) # If there are missing benchmark values, then we can't calculate the # beta. if not np.isfinite(C).all(): return np.nan, np.nan, np.nan, np.nan, [] eigen_values = la.eigvals(C) condition_number = max(eigen_values) / min(eigen_values) algorithm_covariance = C[0][1] benchmark_variance = C[1][1] beta = algorithm_covariance / benchmark_variance return ( beta, algorithm_covariance, benchmark_variance, condition_number, eigen_values )
def tst_covariance_matrix(): A = matrix.sqr((1.0, 2.0, 3.0, 3.0, 1.0, 2.0, 2.0, 3.0, 1.0)) s0 = (0.2, -0.1, 1) r = (0.1, 0.2, -0.3) cov = CovarianceMatrix(A, s0, r, use_wavelength_spread=False) assert cov.use_mosaic_block_angular_spread == True assert cov.use_wavelength_spread == False assert cov.num_parameters() == 7 parameters = (0.1, 0.001, 0.2, 0.002, 0.003, 0.3) cov.compose(reciprocal_lattice_point_spread=parameters, mosaic_block_angular_spread=0.01) assert all( abs(p1 - p2) < 1e-7 for p1, p2 in zip( cov.reciprocal_lattice_point_spread().parameters, parameters)) assert abs(cov.mosaic_block_angular_spread().parameter - 0.01) < 1e-7 cov = CovarianceMatrix(A, s0, r) cov.parameters = flex.double( (0.1, 0.001, 0.2, 0.002, 0.004, 0.002, 0.002, 0.0003)) sigma = matrix.sqr(cov.covariance) from numpy.linalg import eigvals eigen_values = eigvals(sigma.as_list_of_lists()) assert all(e > 0 for e in eigen_values) print("OK")
def canonical(U): """Canonical local invariants of a two-qubit gate. Returns a vector of three real canonical local invariants for the U(4) matrix U, normalized to the range [0,1]. Uses the algorithm in :cite:`Childs`. """ # Ville Bergholm 2004-2010 sigma = kron(sy, sy) U_flip = dot(dot(sigma, U.transpose()), sigma) # spin flipped U temp = dot(U, U_flip) / sqrt(complex(det(U))) Lambda = eigvals(temp) #[exp(i*2*phi_1), etc] # logarithm to the branch (-1/2, 3/2] Lambda = angle(Lambda) / pi # divide pi away for k in range(len(Lambda)): if Lambda[k] <= -0.5: Lambda[k] += 2 S = Lambda / 2 S = sort(S)[::-1] # descending order n = int(round(sum(S))) # sum(S) must be an integer # take away extra translations-by-pi S -= r_[ones(n), zeros(4 - n)] # put the elements in the correct order S = roll(S, -n) M = [[1, 1, 0], [1, 0, 1], [0, 1, 1]] # scaled by factor 2 c = dot(M, S[:3]) # now 0.5 >= c[0] >= c[1] >= |c[2]| # and into the Berkeley chamber using a translation and two Weyl reflections if c[2] < 0: c[0] = 1 - c[0] c[2] = -c[2] return c
def plot_stability_region(stabfn, A, dt, bool): if bool == True: x = linspace(-4, 4, 100) X = meshgrid(x, x) z = X[0] + 1j * X[1] Rlevel = abs(stabfn(z)) plt.figure(figsize=(8, 8)) plt.contourf(x, x, Rlevel, [1, 1000]) plt.contour(x, x, Rlevel, [1, 1000]) plt.xlabel(r'Re') plt.ylabel(r'Im') plt.plot([0, 0], [-4, 4], '-k') plt.plot([-4, 4], [0, 0], '-k') plt.axes().set_aspect('equal') print("#####################") print("Computing EigenValues") print("#####################") evals = eigvals(A * dt) Re = [i.real for i in evals] Im = [i.imag for i in evals] if bool == True: plt.scatter(Re, Im, color='red', marker='^') else: plt.scatter(Re, Im, color='yellow', marker='x')
def time_eigvals(self, size, contig, module): if module == 'numpy': nl.eigvals(self.a) else: sl.eigvals(self.a)
# Since the square of the momentum operator is real, we can discard the complex part def P2(N): Pmat = P(N) #ans_complex = P(N).dot(P(N)) ans_complex = Pmat.dot(Pmat) return np.array(ans_complex, dtype=float) # This shows that the harmonic oscillator Hamiltonian is diagonal in the basis of its # eigenvectors print(P2(5) + X(5).dot(X(5))) def H4(N): matP2 = P2(N+4) matX = X(N+4) ans = matP2 + matX.dot(matX).dot(matX).dot(matX) return ans[:N, :N] ref = 1.06036209048418289964704601 varX, varY = [], [] for N in range(2, 51): vals = eigvals(H4(N)) val = min(vals) err = -np.log10(np.abs(ref - val)) varX.append(N) varY.append(err) print('{:2d}{:18.12f}{:8.1f}'.format(N, val, err))
curve = plt.plot(risk_data, ret_data, 'g-', label='Curve') MVP = plt.plot(MVP_sigma, MVP_ret, '*-', label='Minimum Variance Portfolio') Opportunity_Set = plt.scatter(sigmas, returns, label='Opportunity Set') # Plot the Individual Stocks' Points for i in range(n): plt.plot(sqrt(cov[i, i]).value, mu[i], 'o--', label='stock ' + str(i)) plt.legend(loc='upper right') plt.show() # Compute the 95 % VaR and 95 % ES for each of the combination of weights VaRs = -returns + norm.ppf(0.95) * sigmas ESs = -returns + sigmas * norm.pdf(norm.ppf(0.95)) / (1 - 0.95) smallest_VaR = np.argmin(VaRs) smallest_ES = np.argmin(ESs) print('The Allocation for the smallest VaR is \n', weights[smallest_VaR]) print('The Allocation for the smallest ES is \n', weights[smallest_ES]) # Determine whether the correlation coefficient matrix is positive definite or not eigenvalues = LA.eigvals(rho) print('Eigenvalue\n') print(eigenvalues) T = np.size(eigenvalues) for i in range(T): if eigenvalues[i] < 0: print('It is not a Positive Definite Matrix\n') break elif i == np.size(eigenvalues) and eigenvalues[i] > 0: print('It is a Positive Definite Matrix\n')
def QR(A): m, n = A.shape Q = np.copy(A).astype(float) R = np.identity(n) for i in range(n): R[i, i] = np.dot(Q[:, i], Q[:, i])**0.5 Q[:, i] = Q[:, i] / R[i, i] for j in range(i + 1, n): R[i, j] = np.dot(Q[:, i], Q[:, j]) Q[:, j] = Q[:, j] - R[i, j] * Q[:, i] return Q, R def eigen_QR(A, eps, Qk=np.identity(len(A))): n = A.shape[1] Q, R = QR(A) B = np.dot(R, Q) Qk = np.dot(Qk, Q) for i in range(n - 1): if (max(abs(B[i, i + 1:n])) > eps): return eigen_QR(B, eps, Qk=Qk) return np.diagonal(B), Qk.T print( "Eigenvalues and eigenvectors {Rows are eigenvectors} (by QR algorithm):") val, vec = eigen_QR(A, 1e-6) print(val, vec) print("\nEigenvalues (with built-in numpy eigenvals function):") print(linalg.eigvals(A))
def roots(p): """ Return the roots of a polynomial with coefficients given in p. The values in the rank-1 array `p` are coefficients of a polynomial. If the length of `p` is n+1 then the polynomial is described by:: p[0] * x**n + p[1] * x**(n-1) + ... + p[n-1]*x + p[n] Parameters ---------- p : array_like Rank-1 array of polynomial coefficients. Returns ------- out : ndarray An array containing the complex roots of the polynomial. Raises ------ ValueError When `p` cannot be converted to a rank-1 array. See also -------- poly : Find the coefficients of a polynomial with a given sequence of roots. polyval : Evaluate a polynomial at a point. polyfit : Least squares polynomial fit. poly1d : A one-dimensional polynomial class. Notes ----- The algorithm relies on computing the eigenvalues of the companion matrix [1]_. References ---------- .. [1] R. A. Horn & C. R. Johnson, *Matrix Analysis*. Cambridge, UK: Cambridge University Press, 1999, pp. 146-7. Examples -------- >>> coeff = [3.2, 2, 1] >>> np.roots(coeff) array([-0.3125+0.46351241j, -0.3125-0.46351241j]) """ # If input is scalar, this makes it an array p = atleast_1d(p) if len(p.shape) != 1: raise ValueError("Input must be a rank-1 array.") # find non-zero array entries non_zero = NX.nonzero(NX.ravel(p))[0] # Return an empty array if polynomial is all zeros if len(non_zero) == 0: return NX.array([]) # find the number of trailing zeros -- this is the number of roots at 0. trailing_zeros = len(p) - non_zero[-1] - 1 # strip leading and trailing zeros p = p[int(non_zero[0]):int(non_zero[-1]) + 1] # casting: if incoming array isn't floating point, make it floating point. if not issubclass(p.dtype.type, (NX.floating, NX.complexfloating)): p = p.astype(float) N = len(p) if N > 1: # build companion matrix and find its eigenvalues (the roots) A = diag(NX.ones((N - 2, ), p.dtype), -1) A[0, :] = -p[1:] / p[0] roots = eigvals(A) else: roots = NX.array([]) # tack any zeros onto the back of the array roots = hstack((roots, NX.zeros(trailing_zeros, roots.dtype))) return roots
def poly(seq_of_zeros): """ Find the coefficients of a polynomial with the given sequence of roots. Returns the coefficients of the polynomial whose leading coefficient is one for the given sequence of zeros (multiple roots must be included in the sequence as many times as their multiplicity; see Examples). A square matrix (or array, which will be treated as a matrix) can also be given, in which case the coefficients of the characteristic polynomial of the matrix are returned. Parameters ---------- seq_of_zeros : array_like, shape (N,) or (N, N) A sequence of polynomial roots, or a square array or matrix object. Returns ------- c : ndarray 1D array of polynomial coefficients from highest to lowest degree: ``c[0] * x**(N) + c[1] * x**(N-1) + ... + c[N-1] * x + c[N]`` where c[0] always equals 1. Raises ------ ValueError If input is the wrong shape (the input must be a 1-D or square 2-D array). See Also -------- polyval : Evaluate a polynomial at a point. roots : Return the roots of a polynomial. polyfit : Least squares polynomial fit. poly1d : A one-dimensional polynomial class. Notes ----- Specifying the roots of a polynomial still leaves one degree of freedom, typically represented by an undetermined leading coefficient. [1]_ In the case of this function, that coefficient - the first one in the returned array - is always taken as one. (If for some reason you have one other point, the only automatic way presently to leverage that information is to use ``polyfit``.) The characteristic polynomial, :math:`p_a(t)`, of an `n`-by-`n` matrix **A** is given by :math:`p_a(t) = \\mathrm{det}(t\\, \\mathbf{I} - \\mathbf{A})`, where **I** is the `n`-by-`n` identity matrix. [2]_ References ---------- .. [1] M. Sullivan and M. Sullivan, III, "Algebra and Trignometry, Enhanced With Graphing Utilities," Prentice-Hall, pg. 318, 1996. .. [2] G. Strang, "Linear Algebra and Its Applications, 2nd Edition," Academic Press, pg. 182, 1980. Examples -------- Given a sequence of a polynomial's zeros: >>> np.poly((0, 0, 0)) # Multiple root example array([1, 0, 0, 0]) The line above represents z**3 + 0*z**2 + 0*z + 0. >>> np.poly((-1./2, 0, 1./2)) array([ 1. , 0. , -0.25, 0. ]) The line above represents z**3 - z/4 >>> np.poly((np.random.random(1.)[0], 0, np.random.random(1.)[0])) array([ 1. , -0.77086955, 0.08618131, 0. ]) #random Given a square array object: >>> P = np.array([[0, 1./3], [-1./2, 0]]) >>> np.poly(P) array([ 1. , 0. , 0.16666667]) Or a square matrix object: >>> np.poly(np.matrix(P)) array([ 1. , 0. , 0.16666667]) Note how in all cases the leading coefficient is always 1. """ seq_of_zeros = atleast_1d(seq_of_zeros) sh = seq_of_zeros.shape if len(sh) == 2 and sh[0] == sh[1] and sh[0] != 0: seq_of_zeros = eigvals(seq_of_zeros) elif len(sh) == 1: dt = seq_of_zeros.dtype # Let object arrays slip through, e.g. for arbitrary precision if dt != object: seq_of_zeros = seq_of_zeros.astype(mintypecode(dt.char)) else: raise ValueError("input must be 1d or non-empty square 2d array.") if len(seq_of_zeros) == 0: return 1.0 dt = seq_of_zeros.dtype a = ones((1, ), dtype=dt) for k in range(len(seq_of_zeros)): a = NX.convolve(a, array([1, -seq_of_zeros[k]], dtype=dt), mode='full') if issubclass(a.dtype.type, NX.complexfloating): # if complex roots are all complex conjugates, the roots are real. roots = NX.asarray(seq_of_zeros, complex) pos_roots = sort_complex(NX.compress(roots.imag > 0, roots)) neg_roots = NX.conjugate( sort_complex(NX.compress(roots.imag < 0, roots))) if (len(pos_roots) == len(neg_roots) and NX.alltrue(neg_roots == pos_roots)): a = a.real.copy() return a
def get_min_autovalor(A): return min(eigvals(A))
def is_positive_definite(matrix): """ Checks if `matrix` is positive definite (if all its eigenvalues are positive) """ return (eigvals(matrix) > 0).all()
for j in np.arange(1, i)])) / (R - r) return np.array([[x[max(i + 1 - j, 0)] for j in np.arange(n - 1)] for i in np.arange(n - 1)]) N = 100 tabEXACT_u = np.ones(N + 1) tab_n = np.arange(N + 1) u = 1 n = 2 b = True while b: r = u * R / n ρ = r / (R - (n - 1) * r) mat = mat_exacte(n, R, u) s = max(la.eigvals(mat)) if s < 1: tabEXACT_u[n] = u n += 1 else: u -= 0.0001 if u < 0 or n > N: b = False tabSFA_u = np.ones(N + 1) u = 1 n = 2 b = True while b: r = u * R / n ρ = r / (R - (n - 1) * r)
def eigenvalues_sorted(m: _tarray) -> _tarray: ev = _npl.eigvals(m) ev = _np.sort(_np.abs(ev)) return ev
def __init__(self, y, x, w, method='full', epsilon=0.0000001): # set up main regression variables and spatial filters self.y = y self.x = x self.n, self.k = self.x.shape self.method = method self.epsilon = epsilon #W = w.full()[0] #Wsp = w.sparse ylag = weights.lag_spatial(w, y) # b0, b1, e0 and e1 xtx = spdot(self.x.T, self.x) xtxi = la.inv(xtx) xty = spdot(self.x.T, self.y) xtyl = spdot(self.x.T, ylag) b0 = spdot(xtxi, xty) b1 = spdot(xtxi, xtyl) e0 = self.y - spdot(x, b0) e1 = ylag - spdot(x, b1) methodML = method.upper() # call minimizer using concentrated log-likelihood to get rho if methodML in ['FULL', 'LU', 'ORD']: if methodML == 'FULL': W = w.full()[0] # moved here res = minimize_scalar(lag_c_loglik, 0.0, bounds=(-1.0, 1.0), args=( self.n, e0, e1, W), method='bounded', tol=epsilon) elif methodML == 'LU': I = sp.identity(w.n) Wsp = w.sparse # moved here W = Wsp res = minimize_scalar(lag_c_loglik_sp, 0.0, bounds=(-1.0,1.0), args=(self.n, e0, e1, I, Wsp), method='bounded', tol=epsilon) elif methodML == 'ORD': # check on symmetry structure if w.asymmetry(intrinsic=False) == []: ww = symmetrize(w) WW = np.array(ww.todense()) evals = la.eigvalsh(WW) W = WW else: W = w.full()[0] # moved here evals = la.eigvals(W) res = minimize_scalar(lag_c_loglik_ord, 0.0, bounds=(-1.0, 1.0), args=( self.n, e0, e1, evals), method='bounded', tol=epsilon) else: # program will crash, need to catch print(("{0} is an unsupported method".format(methodML))) self = None return self.rho = res.x[0][0] # compute full log-likelihood, including constants ln2pi = np.log(2.0 * np.pi) llik = -res.fun - self.n / 2.0 * ln2pi - self.n / 2.0 self.logll = llik[0][0] # b, residuals and predicted values b = b0 - self.rho * b1 self.betas = np.vstack((b, self.rho)) # rho added as last coefficient self.u = e0 - self.rho * e1 self.predy = self.y - self.u xb = spdot(x, b) self.predy_e = inverse_prod( w.sparse, xb, self.rho, inv_method="power_exp", threshold=epsilon) self.e_pred = self.y - self.predy_e # residual variance self._cache = {} self.sig2 = self.sig2n # no allowance for division by n-k # information matrix # if w should be kept sparse, how can we do the following: a = -self.rho * W spfill_diagonal(a, 1.0) ai = spinv(a) wai = spdot(W, ai) tr1 = wai.diagonal().sum() #same for sparse and dense wai2 = spdot(wai, wai) tr2 = wai2.diagonal().sum() waiTwai = spdot(wai.T, wai) tr3 = waiTwai.diagonal().sum() ### to here wpredy = weights.lag_spatial(w, self.predy_e) wpyTwpy = spdot(wpredy.T, wpredy) xTwpy = spdot(x.T, wpredy) # order of variables is beta, rho, sigma2 v1 = np.vstack( (xtx / self.sig2, xTwpy.T / self.sig2, np.zeros((1, self.k)))) v2 = np.vstack( (xTwpy / self.sig2, tr2 + tr3 + wpyTwpy / self.sig2, tr1 / self.sig2)) v3 = np.vstack( (np.zeros((self.k, 1)), tr1 / self.sig2, self.n / (2.0 * self.sig2 ** 2))) v = np.hstack((v1, v2, v3)) self.vm1 = la.inv(v) # vm1 includes variance for sigma2 self.vm = self.vm1[:-1, :-1] # vm is for coefficients only
def featureDetection(color_imgs, imgs, window_size=5, k=0.05, threshold=None, local=False): """ Detect features and return (x, y) coordinates of keypoints. Saving new images with red dots highlighting the keypoints. Args: color_imgs imgs: gray images for detecting features window_size (int): window size of the gaussian filter k (float): recommended value 0.04 ~ 0.06 threshold: set threshold for keypoints, if None then output first 5000 keypoints local: output local keypoints instead of global keypoints if it is set to True Returns: list of list of tuples (coordinates of the detected keypoints in each image) """ offset = (window_size - 1) // 2 sigma = (window_size + 1) / 3 cornerlist = [[] for img in imgs] descriptionlist = [[] for img in imgs] x, y = np.mgrid[-offset:offset + 1, -offset:offset + 1] gaussian = np.exp(-(x**2 + y**2) / 2 / sigma**2) g = lambda window: (window * gaussian).sum() with open("log", "w") as f: for i, (color_img, img) in enumerate(tqdm(zip(color_imgs, imgs))): h, w = img.shape #Ix, Iy = np.gradient(img) # buggy gradient print(img) print(i) Ix = cv2.Sobel(img, cv2.CV_64F, 1, 0) Iy = cv2.Sobel(img, cv2.CV_64F, 0, 1) Ixx = Ix**2 Iyy = Iy**2 Ixy = Ix * Iy R = np.zeros(img.shape) for x in range(offset, h - offset): for y in range(offset, w - offset): M = np.array(((g(Ixx[x - offset:x + offset + 1, y - offset:y + offset + 1]), g(Ixy[x - offset:x + offset + 1, y - offset:y + offset + 1])), (g(Ixy[x - offset:x + offset + 1, y - offset:y + offset + 1]), g(Iyy[x - offset:x + offset + 1, y - offset:y + offset + 1])))) eigs = LA.eigvals(M) R[x, y] = eigs[0] * eigs[1] - k * ((eigs[0] + eigs[1])**2) cornerlist[i] = [(R[x, y], (x, y)) for x in range(offset, h - offset) for y in range(offset, w - offset)] if local: cornerlist[i] = [ (r, (x, y)) for (r, (x, y)) in cornerlist[i] if r == np.amax(R[x - offset:x + offset, y - offset:y + offset]) and r - np.amin(R[x - offset:x + offset, y - offset:y + offset]) >= threshold ] cornerlist[i] = [(x, y) for r, (x, y) in cornerlist[i] if r >= threshold] descriptionlist[i] = [ feature_describing(img, Ix, Iy, (x, y)) for (x, y) in cornerlist[i] ] for x, y in cornerlist[i]: color_img.itemset((x, y, 0), 0) color_img.itemset((x, y, 1), 0) color_img.itemset((x, y, 2), 255) print(len(cornerlist[i])) cv2.imwrite(os.path.join(dir_name, f"feature{i}.png"), color_img) return cornerlist, descriptionlist
covfile = np.genfromtxt(infile) cov = np.zeros((ndata, ndata)) print ndata, n2pt, int(np.max(covfile[:, 0]) + 1) for i in range(0, covfile.shape[0]): cov[int(covfile[i, 0]), int(covfile[i, 1])] = covfile[i, 8] + covfile[i, 9] cov[int(covfile[i, 1]), int(covfile[i, 0])] = covfile[i, 8] + covfile[i, 9] cor = np.zeros((ndata, ndata)) for i in range(0, ndata): for j in range(0, ndata): if (cov[i, i] * cov[j, j] > 0): cor[i, j] = cov[i, j] / math.sqrt(cov[i, i] * cov[j, j]) a = np.sort(LA.eigvals(cor)) print "min+max eigenvalues full cor:" print np.min(a), np.max(a) print "neg eigenvalues full cor:" for i in range(0, ndata): if (a[i] < 0.0): print a[i] b = np.sort(LA.eigvals(cov)) print "min+max eigenvalues full cov:" print np.min(b), np.max(b) print "neg eigenvalues full cov:" for i in range(0, ndata): if (b[i] < 0.0): print b[i] # outfile = infile+"_shear" # f = open(outfile, "w")
def vectfit_step(f, s, poles): """ f = complex data to fit s = j*frequency poles = initial poles guess note: All complex poles must come in sequential complex conjugate pairs returns adjusted poles """ N = len(poles) Ns = len(s) cindex = zeros(N) # cindex is: # - 0 for real poles # - 1 for the first of a complex-conjugate pair # - 2 for the second of a cc pair for i, p in enumerate(poles): if p.imag != 0: if i == 0 or cindex[i - 1] != 1: assert cc(poles[i]) == poles[i + 1], ( "Complex poles must come in conjugate pairs: %s, %s" % (poles[i], poles[i + 1])) cindex[i] = 1 else: cindex[i] = 2 # First linear equation to solve. See Appendix A A = zeros((Ns, 2 * N + 2), dtype=np.complex64) for i, p in enumerate(poles): if cindex[i] == 0: A[:, i] = 1 / (s - p) elif cindex[i] == 1: A[:, i] = 1 / (s - p) + 1 / (s - cc(p)) elif cindex[i] == 2: A[:, i] = 1j / (s - p) - 1j / (s - cc(p)) else: raise RuntimeError("cindex[%s] = %s" % (i, cindex[i])) A[:, N + 2 + i] = -A[:, i] * f A[:, N] = 1 A[:, N + 1] = s # Solve Ax == b using pseudo-inverse b = f A = vstack((real(A), imag(A))) b = concatenate((real(b), imag(b))) x, residuals, rnk, s = lstsq(A, b, rcond=-1) residues = x[:N] d = x[N] h = x[N + 1] # We only want the "tilde" part in (A.4) x = x[-N:] # Calculation of zeros: Appendix B A = diag(poles) b = ones(N) c = x for i, (ci, p) in enumerate(zip(cindex, poles)): if ci == 1: x, y = real(p), imag(p) A[i, i] = A[i + 1, i + 1] = x A[i, i + 1] = -y A[i + 1, i] = y b[i] = 2 b[i + 1] = 0 #cv = c[i] #c[i,i+1] = real(cv), imag(cv) H = A - outer(b, c) H = real(H) new_poles = sort(eigvals(H)) unstable = real(new_poles) > 0 #new_poles[unstable] -= 2*real(new_poles)[unstable] return new_poles
def calc_bott(gyro_lattice, cp, psub=None, check=False, verbose=False, attribute_evs=False, save_eigv=False, force_hdf5_eigv=True): """Compute the bott index for a gyro_lattice Parameters ---------- gyro_lattice : GyroLattice instance The gyro network of which to compute the Bott index psub : (#states with freq > omegac) x len(gyro_lattice.lattice.xy)*2 complex array (optional) projection operator, if already calculated previously. If None, this function calculates this. check : bool (optional) Display intermediate results verbose : bool (optional) Print more output on command line attribute_evs : bool (optional) Attribute the eigval and eigvect to the GyroLattice instance gyro_lattice, if projector pp is not supplied Returns ------- bott : float The computed Bott index for this system """ lat = gyro_lattice.lattice xy = lat.xy lat.get_PV(attribute=True) omegac = cp['omegac'] if psub is None: print 'bott_mgyrofns: Computing projector with omegac = ', omegac psub = calc_small_projector(gyro_lattice, omegac, attribute=attribute_evs, save_eigv=save_eigv, force_hdf5_eigv=force_hdf5_eigv) else: print 'bott_mgyrofns: using supplied psub...' # print 'psub = ', psub # sys.exit() ####################################################### # if lp['check']: # print 'min diff eigval = ', np.abs(np.diff(eigval)).min() # close = np.where(np.abs(np.diff(eigval)) < 1e-14)[0] # print 'diff eigval = ', np.diff(eigval) # print 'close -> ', close # plt.plot(np.arange(len(eigval)), np.real(eigval), 'ro-', label=r'Re($e_i$)') # plt.plot(np.arange(len(eigval)), np.imag(eigval), 'b.-', label=r'Im($e_i$)') # plt.legend() # plt.xlabel('eigenvalue index', fontsize=fsfs) # plt.ylabel('eigenvalue', fontsize=fsfs) # plt.title('Eigenvalues of the Haldane Model', fontsize=fsfs) # plt.show() # # # Look at the matrix of eigenvectors # ss = eigvect # sum0 = np.abs(np.sum(np.abs(ss ** 2), axis=0)) # sum1 = np.abs(np.sum(np.abs(ss ** 2), axis=1)) # print 'sum0 = ', sum0 # print 'sum1 = ', sum1 # ss1 = ss.conj().T # nearI = np.dot(ss, ss1) ####################################################### # Get U = P exp(iX) P and V = P exp(iY) P # double the position vectors in x and y xx = np.repeat(xy[:, 0], 2) yy = np.repeat(xy[:, 1], 2) # rescale the position vectors in x and y xsize = np.sqrt(lat.PV[0][0]**2 + lat.PV[0][1]**2) ysize = np.sqrt(lat.PV[1][0]**2 + lat.PV[1][1]**2) theta = (xx - np.min(xy[:, 0])) / xsize * 2. * np.pi phi = (yy - np.min(xy[:, 1])) / ysize * 2. * np.pi print 'np.shape(theta) = ', np.shape(theta) print 'np.shape(phi) = ', np.shape(phi) print 'np.shape(psub) = ', np.shape(psub) uu = np.dot( psub, np.dot( np.exp(1j * theta) * np.identity(len(xx)), psub.conj().transpose())) vv = np.dot( psub, np.dot( np.exp(1j * phi) * np.identity(len(yy)), psub.conj().transpose())) # This is the way using the full projector P = S M S^{-1} # uu = np.dot(pp, np.dot(np.exp(1j * theta) * np.identity(len(xx)), pp)) # vv = np.dot(pp, np.dot(np.exp(1j * phi) * np.identity(len(xx)), pp)) # Could multiply the eigenvalues of VUUtVt to get the determinant of the product, # but instead add logs of evs. # Log Det = Trace Log # if Log e^A = A, then this holds. Wikipedia says this holds for Lie groups # Wikipedia Matrix Exponential > Jacobi's Formula says that # det e^A = e^{tr(A)} # Compute the Bott index if verbose: print 'diagonalizing (V U Vt Ut)...' ev = la.eigvals(np.dot(vv, np.dot(uu, np.dot(vv.conj().T, uu.conj().T)))) # Consider only eigvals near the identity -- should always automatically # be satisfied given that all included states are orthonormal. # ev = ev[np.abs(ev) > 1e-9] # Perhaps sensitive to errors during product, so this is commented out # tr = np.prod(ev) bott = np.imag(np.sum(np.log(ev))) / (2. * np.pi) if verbose: print 'bott = ', bott return bott