-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsparse_block_vector.pyx
More file actions
66 lines (57 loc) · 1.92 KB
/
sparse_block_vector.pyx
File metadata and controls
66 lines (57 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# -*- coding: utf-8 -*-
"""
Created on Sat Oct 12 17:54:15 2013
@author: xm
"""
from __future__ import division
import numpy as np
cimport numpy as np
cimport cython
cdef class SparseBlockArray(object):
cpdef public dict data
cpdef public int blockSize
cpdef public int numBlocks
cpdef public set keys
cpdef public int size
cpdef public object dtype
def __init__(self, blockSize = 1000, dtype = float):
self.data = dict()
self.blockSize = blockSize
self.numBlocks = 0
self.keys = set()
self.size = 0
self.dtype = dtype
def __setitem__(self, key, value):
cdef int blockIndex = key / self.blockSize
cdef int offset = key - blockIndex * self.blockSize
if (not self.data.has_key(blockIndex)):
self.data[blockIndex] = np.zeros(self.blockSize, dtype=self.dtype)
self.numBlocks += 1
self.size += self.blockSize
self.keys.add(blockIndex)
self.data[blockIndex][offset] = value
def __getitem__(self, key):
cdef int blockIndex = key / self.blockSize
cdef int offset = key - blockIndex * self.blockSize
if self.data.has_key(key):
return self.data[blockIndex][offset]
else:
return 0
def memorySize(self):
if (self.numBlocks == 0):
return 0
else:
return self.numBlocks * self.data.values()[0].nbytes
def add(self, other, w):
for x in self.keys & other.keys:
self.data[x] += w * other.data[x]
cpdef float dot(self, SparseBlockArray other):
cdef int index
cdef float result = 0.0
cdef list keys = list(self.keys & other.keys)
cdef int key
cdef int numKey = len(keys)
for index in range(numKey):
key = keys[index]
result += np.dot(self.data[key], other.data[key])
return result