Guest User

Untitled

a guest
Jan 21st, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. from scipy import sparse
  2. from numpy.linalg import norm
  3.  
  4. vector1 = sparse.csr_matrix([ 0 for i in xrange(4000000) ], dtype = float64)
  5.  
  6. #just to test I set a few points to a value higher than 0
  7.  
  8. vector1[ (0, 10) ] = 5
  9. vector1[ (0, 1500) ] = 80
  10. vector1[ (0, 2000000) ] = 6
  11.  
  12. n = norm(t1)
  13.  
  14. ValueError: dimension mismatch
  15.  
  16. norm(asarray(vector1.todense()))
  17.  
  18. (vector1.data ** 2).sum()
  19.  
  20. # sparseLib.pyx
  21. #cython: boundscheck=False
  22. from cython.parallel cimport prange
  23. from cython.view cimport array as cvarray
  24.  
  25. import numpy as np
  26.  
  27. from libc.math cimport sqrt
  28.  
  29. cpdef double sparseNorm2(double [:] data) nogil:
  30. cdef long i
  31. cdef double value = 0.0
  32. for i in xrange(data.shape[0]):
  33. value += data[i]*data[i]
  34. return sqrt(value)
Add Comment
Please, Sign In to add comment