def run_eig(A, verbose = 0): if verbose > 1: print("original matrix (eig):\n", A) n = A.rows E, EL, ER = mp.eig(A, left = True, right = True) if verbose > 1: print("E:\n", E) print("EL:\n", EL) print("ER:\n", ER) eps = mp.exp(0.8 * mp.log(mp.eps)) err0 = 0 for i in xrange(n): B = A * ER[:,i] - E[i] * ER[:,i] err0 = max(err0, mp.mnorm(B)) B = EL[i,:] * A - EL[i,:] * E[i] err0 = max(err0, mp.mnorm(B)) err0 /= n * n if verbose > 0: print("difference (E):", err0) assert err0 < eps
def _compute_roots(w, x, use_mp): # Cf.: # Knockaert, L. (2008). A simple and accurate algorithm for barycentric # rational interpolation. IEEE Signal processing letters, 15, 154-157. if use_mp: from mpmath import mp if use_mp is True: use_mp = 100 mp.dps = use_mp ak = mp.matrix(w) ak /= sum(ak) bk = mp.matrix(x) M = mp.diag(bk) for i in range(M.rows): for j in range(M.cols): M[i, j] -= ak[i] * bk[j] lam = mp.eig(M, left=False, right=False) lam = np.array(lam, dtype=complex) else: # the same procedure in standard double precision ak = w / w.sum() M = np.diag(x) - np.outer(ak, x) lam = scipy.linalg.eigvals(M) # remove one simple root lam = np.delete(lam, np.argmin(abs(lam))) return np.real_if_close(lam)
def run_eig(A, verbose=0): if verbose > 1: print("original matrix (eig):\n", A) n = A.rows E, EL, ER = mp.eig(A, left=True, right=True) if verbose > 1: print("E:\n", E) print("EL:\n", EL) print("ER:\n", ER) eps = mp.exp(0.8 * mp.log(mp.eps)) err0 = 0 for i in xrange(n): B = A * ER[:, i] - E[i] * ER[:, i] err0 = max(err0, mp.mnorm(B)) B = EL[i, :] * A - EL[i, :] * E[i] err0 = max(err0, mp.mnorm(B)) err0 /= n * n if verbose > 0: print("difference (E):", err0) assert err0 < eps
def _compute_roots(w, x, use_mp): # Cf.: # Knockaert, L. (2008). A simple and accurate algorithm for barycentric # rational interpolation. IEEE Signal processing letters, 15, 154-157. # # This version requires solving only a standard eigenvalue problem, but # has troubles when the polynomial has leading 0 coefficients. if _is_mp_array(w) or _is_mp_array(x): use_mp = True if use_mp: assert mpmath, 'mpmath package is not installed' ak = mp.matrix(w) ak /= sum(ak) bk = mp.matrix(x) M = mp.diag(bk) for i in range(M.rows): for j in range(M.cols): M[i, j] -= ak[i] * bk[j] lam = np.array(mp.eig(M, left=False, right=False)) # remove one simple root lam = np.delete(lam, np.argmin(abs(lam))) return lam else: # the same procedure in standard double precision ak = w / w.sum() M = np.diag(x) - np.outer(ak, x) lam = scipy.linalg.eigvals(M) # remove one simple root lam = np.delete(lam, np.argmin(abs(lam))) return np.real_if_close(lam)
def B(matriz): E = mp.eig(matriz, left=False, right=False) E = mp.eig_sort(E) bd = matriz a = 0 for y in range(5): a = a + 1 d = np.identity(100) d[d == 1.0] = np.abs(mp.re(E[y])) d1 = mp.matrix(d) bd = bd + d1 o = mp.eig(bd, left=False, right=False) o = mp.eig_sort(o) if mp.re(o[0]) > 0.001: break B1 = mp.cholesky(bd) return B1, a
def mp_eig(mp_matrix) -> Expression: try: _, ER = mp.eig(mp_matrix) except: return None eigenvalues = ER.tolist() # Sort the eigenvalues in the Mathematica convention: largest first. eigenvalues.sort(key=lambda v: (abs(v[0]), -v[0].real, -(v[0].imag)), reverse=True) eigenvalues = [[from_mpmath(c) for c in row] for row in eigenvalues] return Expression("List", *eigenvalues)
def _eigenvals_eigenvects_mpmath(M): norm2 = lambda v: mp.sqrt(sum(i**2 for i in v)) v1 = None prec = max([x._prec for x in M.atoms(Float)]) eps = 2**-prec while prec < DEFAULT_MAXPREC: with workprec(prec): A = mp.matrix(M.evalf(n=prec_to_dps(prec))) E, ER = mp.eig(A) v2 = norm2([i for e in E for i in (mp.re(e), mp.im(e))]) if v1 is not None and mp.fabs(v1 - v2) < eps: return E, ER v1 = v2 prec *= 2 # we get here because the next step would have taken us # past MAXPREC or because we never took a step; in case # of the latter, we refuse to send back a solution since # it would not have been verified; we also resist taking # a small step to arrive exactly at MAXPREC since then # the two calculations might be artificially close. raise PrecisionExhausted
print("Loaded "+str(cols)+" cols") if data.shape[0] != data.shape[1]: print("Assuming complex values => splitting them!") data = data[:rows/2] + data[rows/2:]*1j if data.shape[0] != data.shape[1]: print("Fatal error, non-square sized matrix!") if 1: print("Computing EV decomposition with numpy") w, v = np.linalg.eig(data) else: print("Computing EV decomposition with multiprecision") mp.prec = 53 # default: 53 w, v = mp.eig(mp.matrix(data)) w = np.matrix(w) v = np.matrix(v) # TODO: fix this conversion w = np.log(w)/time f = filename+"_evalues_complex.csv" print("Writing data to "+f) # simply append the imaginary parts as columns at the end of the real parts! np.savetxt(f, np.column_stack([w.real, w.imag]), delimiter='\t', header=headerstr[1:]) if False: f = filename+"_evectors_complex.csv" print("Writing data to "+f)
X = Feb_80W # pasado # estimar M0 yM1 M0_act = ((Y * Y.T) / 100) M0_in = (M0_act**-1) MP = (M0_act * M0_in) o = MP[0:5, 0:5] o0 = M0_in[0:5, 0:5] M1_act = ((Y * X.T) / 100) M0_ant = ((X * X.T) / 100) A = M1_act * (M0_ant**-1) BBT = M0_act - (A * M1_act.T) #### E = mp.eig(BBT, left=False, right=False) E = mp.eig_sort(E) # reajuste 0 d = np.identity(100) d[d == 1.0] = abs(-24.7174268506955680247383180094514) d1 = mp.matrix(d) BBT0 = BBT + d1 E0 = mp.eig(BBT0, left=False, right=False) E0 = mp.eig_sort(E0) # reajuste 0 d0 = np.identity(100) d0[d0 == 1.0] = abs(-0.00000062521499573893501604735293439538) d2 = mp.matrix(d0) BBT1 = BBT0 + d2