-
Notifications
You must be signed in to change notification settings - Fork 0
/
sparsevector.py
113 lines (92 loc) · 3.22 KB
/
sparsevector.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/python
#
# sparsevector.py
#
# Implementation of a sparse vector class.
#
# Copyright (c) 2008, 2012 Marco Colombo
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# See http://www.gnu.org/licenses/gpl.txt for a copy of the license.
#
from bisect import bisect_left
from numpy import array, delete
class Sparsevector:
def __init__(self, size, values, indices):
'''Constructor.'''
self.dim = size
self.val = array(values)
self.idx = indices[:]
def __getitem__(self, index):
return self.val[index]
def __setitem__(self, index, value):
self.val[index] = value
def __delitem__(self, index):
self.val = delete(self.val, index)
del self.idx[index]
def __len__(self):
return len(self.idx)
def __eq__(self, other):
ret = (self.idx == other.idx and
self.dim == other.dim and
(self.val == other.val).all())
return ret
def __abs__(self):
return Sparsevector(self.dim, abs(self.val), self.idx)
def __add__(self, other):
if hasattr(other, "val"):
return Sparsevector(self.dim, self.val + other.val, self.idx)
else:
return Sparsevector(self.dim, self.val + other, self.idx)
def __sub__(self, other):
if hasattr(other, "val"):
return Sparsevector(self.dim, self.val - other.val, self.idx)
else:
return Sparsevector(self.dim, self.val - other, self.idx)
def __mul__(self, other):
if hasattr(other, "val"):
return Sparsevector(self.dim, self.val * other.val, self.idx)
else:
return Sparsevector(self.dim, self.val * other, self.idx)
def __rmul__(self, other):
return self * other
def __div__(self, other):
if hasattr(other, "val"):
return Sparsevector(self.dim, self.val / other.val, self.idx)
else:
return Sparsevector(self.dim, self.val / other, self.idx)
def __neg__(self):
return Sparsevector(self.dim, -self.val, self.idx)
def __str__(self):
fmt = "(%f, %d) "
str = "[ "
for i in range(len(self)):
str += fmt % (self.val[i], self.idx[i])
str += "]"
return str
def copy(self):
'''Copy the sparse vector.'''
other = Sparsevector(self.dim, self.val, self.idx)
return other
def remove(self, j):
'''Remove the selected index changing the dimension of the vector.'''
if j < 0 or j > len(self.idx):
raise IndexError("Element index out of bounds.")
pos = bisect_left(self.idx, self.idx[j])
del self[j]
for i in xrange(pos, len(self.idx)):
self.idx[i] -= 1
self.dim -= 1
def todense(self):
'''Return a dense version of the vector.'''
v = [0.0] * self.dim
for i in range(len(self)):
v[self.idx[i]] = self.val[i]
return array(v)
def data(self):
'''Return the array of sparse values.'''
return self.val