def sparse_matlab(i, j, v, m, n): from scipy.sparse import csr_matrix as sp A = sp((m, n)) try: ni = i.shape[1] except IndexError: ni = 1 try: v.shape[0] except AttributeError: v = v * np.ones((ni, i.shape[0])) try: vn2 = v.shape[0] except IndexError: v = v[:, None] vn2 = 1 assert ni == vn2 for ii in range(ni): B = sp((v[ii, :], (i[:, ii], j[:, ii])), shape=(m, n)) A += B return A
def create_cluster_vector_and_gwbowv_sparse(wtv,wordlist,num_cluster,word2vec_dim,train=False): global c # This function computes SDV feature vectors. bag_of_centroids = sp(np.zeros((num_cluster, word2vec_dim) , dtype="float32" )) # print(bag_of_centroids.shape) global min_no global max_no for word in wordlist: try: bag_of_centroids += wtv[word] except: continue # print(word) # print("word = ",wtv[word].shape) # print(bag_of_centroids.shape,wtv[word].shape) # bag_of_centroids = bag_of_centroids + wtv[word] # bag_of_centroids.sum(wtv[word]) # print(len(bag_of_centroids.nonzero()[0])) c+= len(bag_of_centroids.nonzero()[0]) # norm = np.sqrt(np.einsum('...i,...i', bag_of_centroids, bag_of_centroids)) norm = normsp(bag_of_centroids) bag_of_centroids.multiply(1/norm) # if(norm!=0): # bag_of_centroids /= norm # To make feature vector sparse, make note of minimum and maximum values. # if train: # min_no += min(bag_of_centroids) # max_no += max(bag_of_centroids) # bag_of_centroids = bag_of_centroids.toarray() return bag_of_centroids
def IsoMassAssembler(self): rspts, qwgts = GaussPoints(2) #Quadrature Rule M = sp((self.Np, self.Np)).tolil() for i in range(self.Nt): loc2glb = self.T[i] #node numbers x, y = self.P[loc2glb].T #Node x- and y-coordinates MK = sp((3, 3)) #elements stiffness for q in range(len(qwgts)): #quadrature loop r = rspts[q, 0] #quadrature r-coordinate s = rspts[q, 1] #quadrature s-coordinate S, dSdx, dSdy, detJ = Isopmap(x, y, r, s, self.shapes) wxarea = qwgts[q] * detJ / 2 #weight times area MK += (S @ S.T) * wxarea #Element Stiffness M[loc2glb[:, None], loc2glb[None, :]] += MK return M.tocsr()
def get_matrix(articles, base_words): A = scipy.sparse.lil_matrix((len(articles), len(base_words))) stemmer = SnowballStemmer("english", ignore_stopwords=True) all_words = set() stem_table = {} keys = base_words.keys() article_id = 0 for article in articles: for word in article['raw'].split(): if word not in all_words: if only_letters(word): if word not in stem_table.keys(): stem_table[word] = stemmer.stem(word) if stem_table[word] in keys: A[article_id, base_words[stem_table[word]]] += 1 else: all_words.add(word) article_id += 1 return sp(A).transpose()
def IsoStrainYAssembler(self, func): rspts, qwgts = GaussPoints(2) #Quadrature Rule S = sp((self.Np, self.Np)).tolil() for i in range(self.Nt): loc2glb = self.T[i] #node numbers x, y = self.P[loc2glb].T #Node x- and y-coordinates SK = sp((3, 3)) #elements stiffness for q in range(len(qwgts)): #quadrature loop r = rspts[q, 0] #quadrature r-coordinate s = rspts[q, 1] #quadrature s-coordinate Sv, dSdx, dSdy, detJ = Isopmap(x, y, r, s, self.shapes) xc, yc = Sv[:, 0].dot(x), Sv[:, 0].dot(y) wxarea = qwgts[q] * detJ / 2 #weight times area SK += (Sv @ dSdy.T) * wxarea * func(xc, yc) #Element Stiffness S[loc2glb[:, None], loc2glb[None, :]] += SK return S.tocsr()
def mass_matrix_assembly2D(self): M = sp((self.Np, self.Np)).tolil() for i in range(self.Nt): loc2glb = self.T[i] MK = np.array([[2, 1, 1], [1, 2, 1], [1, 1, 2]]) * self.K[i] / 12 M[loc2glb[:, None], loc2glb[None, :]] += MK return M
def __init__(self, N, K, D, SNR): self.N = N self.K = K self.D = D self.A = np.matrix(np.random.randn(K,N)/np.sqrt(N)) self.X = 10*sp(N,1,density=D/N).todense() self.Y = self.A * self.X self.SNR = SNR snr = 10**(-self.SNR/10.0) eng = norm(self.Y)/np.sqrt(K) self.V = np.matrix(eng*snr*np.random.randn(K,1)) self.Y = self.Y + self.V
def RobinMassMatrix2D(self, kappa): R = sp((self.Np, self.Np)).tolil() for i in range(self.Ne): loc2glb = self.E[i] x, y = self.P[loc2glb].T L = np.sqrt((x[0] - x[1])**2 + (y[0] - y[1])**2) xc, yc = np.mean(x), np.mean(y) k = kappa(xc, yc) RE = k * L * np.array([[2, 1], [1, 2]]) / 6 R[loc2glb[:, None], loc2glb[None, :]] += RE return R.tocsr()
def stiffness_matrix_assembly2D(self, func): S = sp((self.Np, self.Np)).tolil() for i in range(self.Nt): loc2glb = self.T[i] x, y = self.P[loc2glb].T b = np.array([y[1] - y[2], y[2] - y[0], y[0] - y[1] ]) / (2 * self.K[i]) c = np.array([x[2] - x[1], x[0] - x[2], x[1] - x[0] ]) / (2 * self.K[i]) xc, yc = np.mean(x), np.mean(y) abar = func(xc, yc) SK = abar * self.K[i] * (b[:, None] * b[None, :] + c[:, None] * c[None, :]) S[loc2glb[:, None], loc2glb[None, :]] += SK return S.tocsr()
def trapezium_matrix(x): """ This function will return the numerical integration matrix for the trapezium rule. Multiplying the return matrix to a functional array will give the numerical integration from x[0] to x[1:]. Parameters ---------- x : numpy array The domain over which to perform the numerical integration Returns ------- scipy.sparse matrix The sparse matrix for the numerical trapezium rule. """ N = len(x) - 1 T = np.tril(np.ones((N, N))) dx = np.diag(np.diff(x)) weights = (np.eye(N + 1, k=1) + np.eye(N + 1))[:-1, :] return sp(T @ dx / 2 @ weights)
def tri2edge(self): from ppp.PDE import sparse_matlab from scipy.sparse import triu, find i, j, k = self.T.T[:, None] A = sparse_matlab(i, k, -1, self.Np, self.Np) #1st edge is between (j, k) A += sparse_matlab(j, k, -1, self.Np, self.Np) #2nd A += sparse_matlab(i, j, -1, self.Np, self.Np) #3rd indices = (A + A.T).todense() < 0 A = sp((self.Np, self.Np)).tolil() A[indices] = -1 A = triu(A) r, c, v = find(A) v = np.linspace(0, len(v) - 1, len(v))[:, None] A = sparse_matlab(r[None, :], c[None, :], v, self.Np, self.Np) A += A.T edges = np.zeros((self.Nt, 3), dtype=int) for k in range(self.Nt): edges[k] = np.array([ A[self.T[k, 1], self.T[k, 2]], A[self.T[k, 0], self.T[k, 2]], A[self.T[k, 0], self.T[k, 1]] ]) return edges
save_npz(os.path.join( folder_name, file_name+'.npz' ), *arrays ) def load_arrays(file_name, wd=None, folder_name=None): from scipy.sparse import load_npz if not wd: wd = os.getcwd() if not folder_name: folder_name = '' loaded = load_npz( os.path.join( wd, os.path.join( folder_name, file_name+'.npz' ) ) ) return loaded if __name__ == '__main__': test_array = sp(np.random.rand(5, 2**10)) test_vector = sp(np.random.rand(2**10)) arrays = test_array, test_vector save_arrays('arrays', arrays) a, b = load_arrays('arrays') print(a) print() print(b)