/usr/lib/python2.7/dist-packages/pysparse/sparray.py is in python-sparse 1.1.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 114 115 | # -*- coding: iso-8859-1 -*-
from types import IntType,SliceType
import operator
import spmatrix
class sparray:
"""
d-dimensionnal sparse array emulation by a long sparse vector.
supports syntax like:
a[2,1,6,5]=2
a[:,5,6,6]=list of appropriate length (one slice at a time only)
b=a[:,5,6,6] (b is a numeric array)
b=a[n], a[n]=6 if n in range
a=sparray((2,6,9,8),dicto,shifts)
where, optionnally, dicto is a dictionnary whose keys are tuple
in range of (2,6,9,8), and shifts is a tuple
to shift origins in case the smallest coordinate in dicto
is not (0,...,0)
"""
def __init__(self,dim,dicto=None,shifts=None):
"""
attributes: shifts, dims, data, is1D, length
methods : dump
"""
self.shifts=shifts
if type(dim)==type(()) or type(dim)==type([]):
self.data = spmatrix.ll_mat(reduce(operator.mul, dim), 1)
self.dims = dim
if dicto:
for k, v in dicto.iteritems():
shk = map(operator.__sub__, k, shifts)
self.data[self.comp(shk), 0]=v
elif type(dim)==IntType:
self.data = spmatrix.ll_mat(dim,1)
self.dims = dim
if dicto:
for k, v in dicto.iteritems():
shk = k - shifts
self.data[shk,0] = v
self.is1D = type(self.dims)==IntType
def __get_shape0(self):return self.data.shape[0]
length = property(__get_shape0, doc="sparray linear length")
def decomp(self,ind):
"from linear to multi indice"
a = ind
l = len(self.dims)
res = [0]*l
for i in range(l - 1, -1, -1):
a, b = divmod(a,self.dims[i])
res[i] = b
return tuple(res)
def comp(self,indice):
"from multi indice to linear"
l = len(self.dims)
a = 0
for i in range(l-1):
a += reduce(operator.mul, self.dims[i+1:]) * indice[i]
a += indice[l-1]
return a
def __setitem__(self, k, value):
if type(k) is IntType:
self.data[k, 0] = value
return
vec = map(lambda x: type(x) is SliceType, k)
if True in vec: # suppose only one slice
ii = vec.index(True)
indices = []
k = list(k)
comp = self.comp
for i in range(self.dims[ii]):
k[ii] = i
self.data[comp(k), 0] = value[i]
else:
self.data[self.comp(k),0]=value
def __getitem__(self,k):
"""
output a Numeric vector if slice in coordinates
(one slice present only)
"""
if type(k) is IntType: return self.data[k, 0]
vec = map(lambda x: type(x) is SliceType, k)
if True in vec: #suppose only one slice
ii=vec.index(True)
indices=[]
k = list(k)
import numpy
rep = numpy.zeros((self.dims[ii],), 'd')
for i in range(self.dims[ii]):
k[ii] = i
rep[i] = self.data[self.comp(k), 0]
return rep
else:
return self.data[self.comp(k), 0]
def dump(self):
print self.data
|