def test_svd_c_rand(): for i in xrange(5): full = mp.rand() > 0.5 m = 1 + int(mp.rand() * 10) n = 1 + int(mp.rand() * 10) A = (2 * mp.randmatrix(m, n) - 1) + 1j * (2 * mp.randmatrix(m, n) - 1) if mp.rand() > 0.5: A *= 10 for x in xrange(m): for y in xrange(n): A[x,y]=int(mp.re(A[x,y])) + 1j * int(mp.im(A[x,y])) run_svd_c(A, full_matrices=full, verbose=False)
def test_eig_dyn(): v = 0 for i in xrange(5): n = 1 + int(mp.rand() * 5) if mp.rand() > 0.5: # real A = 2 * mp.randmatrix(n, n) - 1 if mp.rand() > 0.5: A *= 10 for x in xrange(n): for y in xrange(n): A[x,y] = int(A[x,y]) else: A = (2 * mp.randmatrix(n, n) - 1) + 1j * (2 * mp.randmatrix(n, n) - 1) if mp.rand() > 0.5: A *= 10 for x in xrange(n): for y in xrange(n): A[x,y] = int(mp.re(A[x,y])) + 1j * int(mp.im(A[x,y])) run_hessenberg(A, verbose = v) run_schur(A, verbose = v) run_eig(A, verbose = v)
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
R = np.ndarray(f.size) I = np.ndarray(f.size) Gr = np.ndarray(f.size) Gi = np.ndarray(f.size) for n in range(f.size): Zap[n] = ZapA[n]*( i0(gammac[n]*a) / i1(gammac[n]*a) ) Zbp[n] = ZbpA[n] * ((i0(gammac[n]*b) * k1(gammac[n]*c) + k0(gammac[n]*b)*i1(gammac[n]*c)) / ( i1(gammac[n]*c)*k1(gammac[n]*b) - i1(gammac[n]*b)*k1(gammac[n]*c) ) ) ZL[n] = 1j*omega[n]*Lp YC[n] = 1j*omega[n]*Cp Zp[n] = Zap[n] + Zbp[n] + ZL[n] Yp[n] = YC[n] # + G[n] Zc[n] = mp.sqrt(Zp[n]/Yp[n]) A[n] = mp.fabs(Zc[n]) R[n] = mp.re(Zc[n]) I[n] = mp.im(Zc[n]) Gr[n] = mp.re(mp.sqrt(Zp[n]*Yp[n])) Gi[n] = mp.im(mp.sqrt(Zp[n]*Yp[n])) fig = plt.figure() plt.plot(f/10**6, A, label='Magnitude', color='red') plt.xlabel('Frequency [MHz]') plt.ylabel('Magnitude of Z$_{c}$ [$\Omega$]') plt.legend() plt.xlim([-0.3,100]) #plt.show() fig2 = plt.figure() plt.plot(f/10**6, R, label='Real Part', color='red') plt.xlabel('Frequency [MHz]') plt.ylabel('Real Part of Z$_{c}$ [$\Omega$]') plt.legend()
from mpmath import mp mp.dps = 5 wx = mp.mpf('9'); wy = mp.mpf('6') phi = (1 + mp.sqrt(5)) / 2 phi = mp.power(phi, 2) coeffs = [ 6, 7 * wx + 2 * wy, wx * ((3 - phi)*wy + 2*wx), (1 - phi) * mp.power(wx, 2) * wy ] roots = mp.polyroots(coeffs, 15) b = [root for root in roots if mp.im(root) == mp.mpf(0)][0] bottom = b + mp.power(b, 2) / (wx + b) print( "Print dimensions: {wx!s} x {wy!s}\n" "Resulting border: {b!s}\n" "Resulting bottom border: {bottom!s}" .format(wx=wx, wy=wy, b=b, bottom=bottom) ) v = dict( mat_l=0 - b, mat_t=b, mat_w=wx + 2 * b, mat_h=-1 * (wy + b + bottom), )
def mp_to_complex(mpcplx): mpreal = mp.re(mpcplx) mpimag = mp.im(mpcplx) flreal = np.float(mp.nstr(mpreal, mp.dps)) flimag = np.float(mp.nstr(mpimag, mp.dps)) return flreal + 1j * flimag
def mp_im(mpcarr): imarr = mp.zero * np.zeros_like(mpcarr) for i in range(len(mpcarr)): imarr[i] = mp.im(mpcarr[i]) return imarr
def plots(self, re_lerp, im_lerp): return [[(re_lerp(mp.re(z)), im_lerp(mp.im(z))) for z in zs] for t, zs in self.pts]