def pinv2(a, cond=None): """ pinv2(a, cond=None) -> a_pinv Compute the generalized inverse of A using svd. """ a = asarray_chkfinite(a) u, s, vh = decomp.svd(a) t = u.typecode() if cond in [None,-1]: cond = {0: feps*1e3, 1: eps*1e6}[_array_precision[t]] m,n = a.shape cutoff = cond*scipy_base.maximum.reduce(s) psigma = zeros((m,n),t) for i in range(len(s)): if s[i] > cutoff: psigma[i,i] = 1.0/conjugate(s[i]) #XXX: use lapack/blas routines for dot return transpose(conjugate(dot(dot(u,psigma),vh)))
def rsf2csf(T, Z): """Convert real schur form to complex schur form. Description: If A is a real-valued matrix, then the real schur form is quasi-upper triangular. 2x2 blocks extrude from the main-diagonal corresponding to any complex-valued eigenvalues. This function converts this real schur form to a complex schur form which is upper triangular. """ Z,T = map(asarray_chkfinite, (Z,T)) if len(Z.shape) !=2 or Z.shape[0] != Z.shape[1]: raise ValueError, "matrix must be square." if len(T.shape) !=2 or T.shape[0] != T.shape[1]: raise ValueError, "matrix must be square." if T.shape[0] != Z.shape[0]: raise ValueError, "matrices must be same dimension." N = T.shape[0] arr = scipy_base.array t = _commonType(Z, T, arr([3.0],'F')) Z, T = _castCopy(t, Z, T) conj = scipy_base.conj dot = scipy_base.dot r_ = scipy_base.r_ transp = scipy_base.transpose for m in range(N-1,0,-1): if abs(T[m,m-1]) > eps*(abs(T[m-1,m-1]) + abs(T[m,m])): k = slice(m-1,m+1) mu = eigvals(T[k,k]) - T[m,m] r = basic.norm([mu[0], T[m,m-1]]) c = mu[0] / r s = T[m,m-1] / r G = r_[arr([[conj(c),s]],typecode=t),arr([[-s,c]],typecode=t)] Gc = conj(transp(G)) j = slice(m-1,N) T[k,j] = dot(G,T[k,j]) i = slice(0,m+1) T[i,k] = dot(T[i,k], Gc) i = slice(0,N) Z[i,k] = dot(Z[i,k], Gc) T[m,m-1] = 0.0; return T, Z
def pade(an, m): """Given Taylor series coefficients in an, return a Pade approximation to the function as the ratio of two polynomials p / q where the order of q is m. """ an = asarray(an) N = len(an) - 1 n = N-m if (n < 0): raise ValueError, \ "Order of q <m> must be smaller than len(an)-1." Akj = eye(N+1,n+1) Bkj = zeros((N+1,m),'d') for row in range(1,m+1): Bkj[row,:row] = -(an[:row])[::-1] for row in range(m+1,N+1): Bkj[row,:] = -(an[row-m:row])[::-1] C = hstack((Akj,Bkj)) pq = dot(linalg.inv(C),an) p = pq[:n+1] q = r_[1.0,pq[n+1:]] return poly1d(p[::-1]), poly1d(q[::-1])
def type1(self, x): return sb.dot(x, self.obj)