def varimax(Phi, gamma=1.0, q=20, tol=1e-6, normalize=False): p, k = Phi.shape R = np.eye(k) d = 0 if normalize: norm_val = np.sqrt(np.dot(Phi, Phi.T).diagonal()) norm_val = np.repeat(norm_val, k).reshape(p, k) Phi = Phi / norm_val for i in xrange(q): d_old = d Lambda = np.dot(Phi, R) u, s, vh = np.svd( np.dot( Phi.T, np.asarray(Lambda)**3 - (gamma / p) * dot(Lambda, np.diag(np.diag(np.dot(Lambda.T, Lambda)))))) R = np.dot(u, vh) d = np.sum(s) if d_old != 0 and d / d_old < 1 + tol: break if normalize: return np.dot(Phi, R) * norm_val, R else: return np.dot(Phi, R), R
def compress(self,m): #remain m largest value o=np.array([[[1.]]]) for l in range(self.L): self.Ws[l]=np.tensordot(o,self.Ws[l],1) self.Ws[l].reshape(-1,self.Ws[l][-1]) #blockize U,S,Vdag=np.svd(self.Ws[l],full_matrices=False) W=U.reshape(-1,self.d,self.d,U.shape[-1]) S.sort() S=S[:m] #renormalize? o=np.tensordot(np.diag(S),Vdag,1)
def dense(A): #A is n(1) x ... x n(d) tensor #U is d cell array with U(k) being the left singular vector of a's mode-k unfolding #S is n(1) x ... x n(d) tensor : A x1 U(1) x2 U(2) ... xd U(d) S = A for k,_ in range(A.size()): C = A[k,:].numpy() ##may need to make sure columns are arranged in increasing order [U,_,_] = svd(C) t_U = numpy.transpose(U) S(k,:).append(numpy.tensordot(S,t_U,k)) return S, t_U
def svd_speed_test(mat): print('using numpy svd') t = time() np.svd(mat, full_matrices=False, compute_uv=True) print('time elapsed for numpy svd: {}'.format(time() - t)) print('using scipy sparse svds') t = time() sp.svds(mat, 10) print('time elapsed for scipy svds: {}'.format(time() - t)) print('using scipy svd') t = time() scipysvd(mat ,full_matrices=False, compute_uv=True) print('time elapsed for scipy svd: {}'.format(time() - t)) print('using scikit-learn randomized svd') t = time() randomized_svd(mat, n_components=10) print('time elapsed for scikit-learn randomized svd: {}'.format(time() - t)) return time
def fit(self, x, U_dim, class_ids): x_hat = np.zeros_like(x) u_ids = np.uniqe(class_ids) M = np.sqrt(len(u_ids)) for i in u_ids: idx = np.nonzero(i == class_ids) N = np.sqrt(len(idx)) mu_i = np.mean(x[idx, :], axis=0) xx[idx, :] = (x[idx, :] - mu_i) / N xx /= M _, s, Vt = np.svd(xx, full_matrices=False, overwrite_a=True) idx = (np.argsort(s)[::-1])[:U_dim] self.U = Vt[idx, :]
def xyz_svd(xyz_array): """ Calculates the SVD solution given a Numpy array. # modified after: # http://stackoverflow.com/questions/15959411/best-fit-plane-algorithms-why-different-results-solved """ try: result = np.svd(xyz_array) except: result = None return dict(result=result)
def pca_svd(data, headers, normalize=True): # assign to A the desired data. Use either normalize_columns_separately # or get_data, depending on the value of the normalize argument. A = data.getNumCol(headers) if normalize == True: A = self.normalize_columns_separately(headers, data) # assign to m the mean values of the columns of A m = np.matrix(A.mean(axis=0)) # assign to D the difference matrix A - m D = A.copy() for r in range(A.shape[0]): D[r] = D[r] - mu # assign to U, S, V the result of running np.svd on D, with full_matrices=False (U, S, V) = np.svd(D, full_matrices=False)
def gplvm(Y, L, kernel, optimizer): # normalize data Y = (Y - np.mean(Y, 0)) / np.sqrt(var(Y, 0)) # initialize X U, S, V = np.svd(Y) X = U[:, :L] / 10 N = len(Y) # kernel hyperparameter init = [np.log(1), np.log(0.1), np.log(1e-2)] # optimize log likelihood print('optimizing: optimizer = %s' % optimizer) if optimizer in optimizers: x = optimizers[optimizer](X, Y, L, kernel, init) return x.reshape(N, L) else: print('unknown optimizer! [%s]' % '|'.join(optimizers.keys())) sys.exit(0)
def compute_P_from_essential(E): """ Computes the second camera matrix (assuming P1 = [I 0]) from an essential matrix. Output is a list of four possible camera matrices. """ # make sure E is rank 2 U,S,V = np.svd(E) if np.det(np.dot(U,V))<0: V = -V E = dot(U,dot(diag([1,1,0]),V)) # create matrices (Hartley p 258) Z = skew([0,0,-1]) W = array([[0,-1,0],[1,0,0],[0,0,1]]) # return all four solutions P2 = [vstack((dot(U,dot(W,V)).T,U[:,2])).T, vstack((dot(U,dot(W,V)).T,-U[:,2])).T, vstack((dot(U,dot(W.T,V)).T,U[:,2])).T, vstack((dot(U,dot(W.T,V)).T,-U[:,2])).T] return P2
def nullspace(A, atol=1e-13, rtol=0): """Compute an approximate basis for the nullspace of A. The algorithm used by this function is based on the singular value decomposition of `A`. Parameters ---------- A : ndarray A should be at most 2-D. A 1-D array with length k will be treated as a 2-D with shape (1, k) atol : float The absolute tolerance for a zero singular value. Singular values smaller than `atol` are considered to be zero. rtol : float The relative tolerance. Singular values less than rtol*smax are considered to be zero, where smax is the largest singular value. If both `atol` and `rtol` are positive, the combined tolerance is the maximum of the two; that is:: tol = max(atol, rtol * smax) Singular values smaller than `tol` are considered to be zero. Return value ------------ ns : ndarray If `A` is an array with shape (m, k), then `ns` will be an array with shape (k, n), where n is the estimated dimension of the nullspace of `A`. The columns of `ns` are a basis for the nullspace; each element in numpy.dot(A, ns) will be approximately zero. """ A = np.atleast_2d(A) u, s, vh = np.svd(A) tol = max(atol, rtol * s[0]) nnz = (s >= tol).sum() ns = vh[nnz:].conj().T return ns
def _calculate_AB(self, x_hat, y_hat): A = np.zeros((self.num_vars, self.num_vars)) B = np.zeros((self.num_vars, self.num_vars)) for i in range(self.num_samples): x_sample = x_hat[:, i].reshape((self.num_vars, 1)) y_sample = y_hat[:, i].reshape((self.num_vars, 1)) diff = x_sample - y_sample xsqr = x_sample @ x_sample.T ysqr = y_sample @ y_sample.T grp_A = diff @ diff.T grp_B = xsqr + ysqr A += grp_A B += grp_B A /= self.weights_total B /= 2*self.weights_total a, b, c = np.svd(B) # A = np.cov(x_hat - y_hat) # B = (np.cov(x_hat) + np.cov(y_hat))/(2*self.weights_total) return(A, B)
def varimax(Phi, q=20, tol=1e-6): # adapted from wikipedia https://en.wikipedia.org/wiki/Talk%3AVarimax_rotation # Compute the varimax rotation matrix # Parameters: # - Phi: loading # - q: max number of iterations # - tol: convergence treshold p, k = Phi.shape R = np.eye(k) d = 0 for i in range(q): d_old = d Lambda = np.dot(Phi, R) u, s, vh = np.svd( np.dot( Phi.T, np.asarray(Lambda)**3 - (1 / p) * np.dot(Lambda, np.diag(np.diag(np.dot(Lambda.T, Lambda)))))) R = np.dot(u, vh) d = np.sum(s) if d_old != 0 and d / d_old < 1 + tol: break return np.dot(Phi, R)
def fbg(A, B, p, q=None): """ %FBG Feedback gain matrices. % % FUNCTION CALLS (2): % % (1) L = fbg(A,B,p); % % This calculates a matrix L such that the eigenvalues of A-B*L are % those specified in the vector p. Any complex eigenvalues in the % vector p must appear in consecutive complex-conjugate pairs. % The numbers in the vector p must be distinct (see below for repeated roots). % % (2) L = fbg(A,B,p,q); % % This form allows the user to specify repeated eigenvalues by indicating % the desired multiplicity in the vector q. For example, to have a single % eigenvalue at -2, another at -3 with multiplicity 3, and complex conjugate % eigenvalues at -1+j, -1-j with multiplicity 2, set p=[-2 -3 -1+j -1-j], % and q=[1 3 2 2]. The multiplicity of any eigenvalue cannot be greater % than the number of columns of B. """ if q is None: q = np.ones((1, len(p))) (n, m) = B.shape I = np.eye(n) npp = len(p) cvv = [n.imag==0 for n in p ] npoles = int(npp - sum([ not n for n in cvv])/2) for i in range(0, npp): if i < npoles: if not cvv[i]: cvv[i+1]=[] p[i+1]=[] q[i+1]=[] if n <= m: d1=[] d2=[] for i in range(0, npoles): if cvv[i]: d1.extend([p[i]]) if i < npoles: d2.extend([0]) else: d1.extend([p[i].real, p[i].real]) if i < np: d2.extend([p[i].imag, 0]) else: d2.extend([p[i].imag]) if n > 1: L = LA.lstsq(B, A - (np.diag(d1) + np.diag(d2, 1) - np.diag(d2, -1)))[0] else: L = LA.lstsq(B, A - d1)[0] return L sq = sum(q) AT=[] ATT=[] X=[] X1=[] Y=[] Y1=[] cv=[] Xb=[] for i in range(0, npoles): print(cvv[i]) cv.extend([cvv[i] * n for n in [1]*q[i]]) print(cv) for i in range(0, npoles): T = np.null(np.concatenate((p[i]*I-A, B), axis=1)) TT = np.orth(T[1:n,:]) AT = np.concatenate((AT, T), axis=1) ATT = np.concatenate((ATT, TT), axis=1) X[:, i] = TT[:, 1:q[i]] if q[i] == 1 and i > 1: cvt = cv[1:i] In = find(cvt==0) c = cond(np.concatenate((X, np.conj(X[:,In])), axis=1)) for j in range(1, m+1): Y = np.concatenate((X[:,1:i-1], TT[:,j]), axis=1) cc = np.cond(np.concatenate((Y, np.conj(Y[:,In])), axis=1)) if cc < c: c = cc X[:, i] = TT[:, j] Xt = X cd = 1.e15 if m == n: # can calculate L to get orthogonal eigenvectors Ab = np.zeros((n, n)) for i in range(0, npoles): Ab[i, i] = p[i] if not cv[i]: Ab[i, i+1] = -p[i].imag Ab[i+1, i] = p[i].imag Ab[i+1, i+1] = p[i].real L = LA.lstsq(B, (A-Ab))[0] return L if m > 1: for k in range(5): X2 = [] kk = 0 for i in range(0, npoles): Pr = ATT[:,(i-1)*m+1:i*m] Pr = Pr * np.transpose(Pr) for j in range(0, q[i]): kk = kk + 1 S = np.concatenate((Xt[:,1:kk-1], Xt[:,kk+1:sq]), axis=1) S = np.concatenate((S, np.conj(S)), axis=1) if not cv[kk]: S = np.concatenate((S, np.conj(Xt[:, kk])), axis=1) (Us, Ss, Vs) = np.svd(S) Xt[:,kk] = Pr * Us[:,n] Xt[:,kk] = Xt[:, kk] / np.norm(Xt[:, kk]) if not cv(kk): X2 = np.concatenate((X2, np.conj(Xt[:,kk])), axis=1) c = np.cond(np.concatenate((Xt, X2), axis=1)); if c < cd: Xtf = Xt cd = c else: Xtf = X kkk = 0 X1=[] X2=[] for i in range(0, npoles): for j in range(0, q[i]): kkk = kkk + 1 if cv[kkk]: x = Xtf[:,kkk].real y = Xtf[:,kkk].imag if np.norm(x) > np.norm(y): Xtf[:,kkk] = x/np.norm(x) else: Xtf[:,kkk] = y/np.norm(y) a = LA.lstsq(AT[1:n, i*m+1:(i+1)*m], Xtf[:,kkk])[0] t = AT[n+1:n+m, i*m+1:(i+1)*m] * a x = t.imag Xb = np.concatenate((Xb, t.real), axis=1) if not cv[kkk]: X2 = np.concatenate((X2, x), axis=1) X1 = np.concatenate((X1, Xtf[:,kkk].imag), axis=1) Xtf[:,kkk] = Xtf[:,kkk].real L = np.concatenate((Xb, X2), axis=1)/np.concatenate((Xtf, X1), axis=1) return L
def system_misstoa_ransac_bundle(d, sys): dobundle = 1 sol = None for kk in range(1, 2): sol, manrin = tm_ransac5rows(d, sys) if dobundle: sol, res0, res, d2calc = tm_bundle_rank(sol, d) sol = tm_ransac_more_rows(d, sol, sys) if dobundle: sol, res0, res, d2calc = tm_bundle_rank(sol, d) sol = tm_ransac_more_cols(d, sol, sys) if dobundle: sol, res0, res, d2calc = tm_bundle_rank(sol, d) sol = tm_ransac_more_rows(d, sol, sys) if dobundle: sol, res0, res, d2calc = tm_bundle_rank(sol, d) sol = tm_ransac_more_cols(d, sol, sys) if dobundle: sol, res0, res, d2calc = tm_bundle_rank(sol, d) Bhat = sol.Bhat D = 3 u, s, v = np.svd(Bhat[2:, 2:]) xr = u[:, 0:D - 1] yr = (s[0:D - 1, 0:D - 1]) * (v[:, 0:D - 1]) auxvar1 = np.zeros((D, 1)) xtp = np.concatenate((auxvar1, xr)) yt = np.concatenate((auxvar1, yr)) xt = xtp / (-2) # maybe np.divide(xtp,-2) Bhatcol1 = Bhat[:, 0] nr_of_unknowns = (D * (D + 1)) / 2 + D + 1 xv = [None] * nr_of_unknowns for i in range(0, nr_of_unknowns): xv[i] = Multipol.multipol(1, np.zeros(nr_of_unknowns, 1)) bv = None Cv = None if D == 3: Cv1 = np.concatenate((xv[1], xv[2], xv[3]), 1) Cv2 = np.concatenate((xv[2], xv[4], xv[5]), 1) Cv3 = np.concatenate((xv[3], xv[5], xv[6]), 1) Cv = np.concatenate((Cv1, Cv2, Cv3)) bv = np.concatenate((xv[6], xv[7], xv[9]), 1) bv = bv.conj().T elif D == 2: Cv1 = np.concatenate((xv[1], xv[2]), 1) Cv2 = np.concatenate((xv[2], xv[3]), 1) Cv = np.concatenate((Cv1, Cv2)) bv = np.concatenate((xv[3], xv[5]), 1) bv = bv.conj().T eqs = [None] * xt.shape[1] for i in range(1, xt.shape[1]): eqs[i - 1] = (-2 * (xt[:, i]).conj().T * bv + (xt[:, i]).conj().T * Cv * xt[:, i]) - Bhatcol1[i] eqs_linear = eqs cfm_linear, mons_linear = polynomials2matrix(eqs_linear) cfm_linear = np.asarray(cfm_linear) cfm_linear = cfm_linear / np.tile(sqrt(sum(cfm_linear**2, 2)), (1, cfm_linear.shape[1])) cfm_linear0 = cfm_linear AA = cfm_linear0[:, 1:-2] bb = cfm_linear0[:, -1] zz0 = -np.linalg.pinv(AA) * bb H = interpolate.interp1d(Cv, (np.append(zz0, 0)).reshape(10, 1)) b = interpolate.interp1d(bv, (np.append( zz0, 0, )).reshape(10, 1)) if min(np.linalg.eig(H)): L = np.linalg.cholesky(np.linalg.inv(H)).T else: mins = min(np.linalg.eig(H)) H = H + (-mins + 0.1) * np.eye(3) L = np.linalg.cholesky(np.linalg.inv(H)).T r00 = np.linalg.inv(L.conj().T) * xt s00 = L * (yt + np.tile(b, (1, Bhat.shape[1]))) r0 = np.zeros(3, d.size[0]) s0 = np.zeros(3, d.size[1]) r0[:, sol.rows] = r00 s0[:, sol.cols] = s00 toa_calc_d_from_xy(r0, s0) inliers = sol.inlmatrix == 1 r1, s1, res, jec = toa_3D_bundle(d, r0, s0, inliers) r, s = toa_normalize(r1, s1) return r, s, inliers