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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
|
"""Base class for sparse matrices"""
__all__ = ['spmatrix', 'isspmatrix', 'issparse',
'SparseWarning','SparseEfficiencyWarning']
from warnings import warn
import numpy as np
from sputils import isdense, isscalarlike, isintlike
class SparseWarning(Warning): pass
class SparseFormatWarning(SparseWarning): pass
class SparseEfficiencyWarning(SparseWarning): pass
# The formats that we might potentially understand.
_formats = {'csc':[0, "Compressed Sparse Column"],
'csr':[1, "Compressed Sparse Row"],
'dok':[2, "Dictionary Of Keys"],
'lil':[3, "LInked List"],
'dod':[4, "Dictionary of Dictionaries"],
'sss':[5, "Symmetric Sparse Skyline"],
'coo':[6, "COOrdinate"],
'lba':[7, "Linpack BAnded"],
'egd':[8, "Ellpack-itpack Generalized Diagonal"],
'dia':[9, "DIAgonal"],
'bsr':[10, "Block Sparse Row"],
'msr':[11, "Modified compressed Sparse Row"],
'bsc':[12, "Block Sparse Column"],
'msc':[13, "Modified compressed Sparse Column"],
'ssk':[14, "Symmetric SKyline"],
'nsk':[15, "Nonsymmetric SKyline"],
'jad':[16, "JAgged Diagonal"],
'uss':[17, "Unsymmetric Sparse Skyline"],
'vbr':[18, "Variable Block Row"],
'und':[19, "Undefined"]
}
MAXPRINT = 50
class spmatrix(object):
""" This class provides a base class for all sparse matrices. It
cannot be instantiated. Most of the work is provided by subclasses.
"""
__array_priority__ = 10.1
ndim = 2
def __init__(self, maxprint=MAXPRINT):
self.format = self.__class__.__name__[:3]
self._shape = None
if self.format == 'spm':
raise ValueError, "This class is not intended" \
" to be instantiated directly."
self.maxprint = maxprint
def set_shape(self,shape):
shape = tuple(shape)
if len(shape) != 2:
raise ValueError("Only two-dimensional sparse arrays "
"are supported.")
try:
shape = int(shape[0]),int(shape[1]) #floats, other weirdness
except:
raise TypeError('invalid shape')
if not (shape[0] >= 1 and shape[1] >= 1):
raise ValueError('invalid shape')
if (self._shape != shape) and (self._shape is not None):
try:
self = self.reshape(shape)
except NotImplementedError:
raise NotImplementedError("Reshaping not implemented for %s." %
self.__class__.__name__)
self._shape = shape
def get_shape(self):
return self._shape
shape = property(fget=get_shape, fset=set_shape)
def reshape(self,shape):
raise NotImplementedError
def astype(self, t):
return self.tocsr().astype(t).asformat(self.format)
def asfptype(self):
"""Upcast matrix to a floating point format (if necessary)"""
fp_types = ['f','d','F','D']
if self.dtype.char in fp_types:
return self
else:
for fp_type in fp_types:
if self.dtype <= np.dtype(fp_type):
return self.astype(fp_type)
raise TypeError,'cannot upcast [%s] to a floating \
point format' % self.dtype.name
def __iter__(self):
for r in xrange(self.shape[0]):
yield self[r,:]
def getmaxprint(self):
try:
maxprint = self.maxprint
except AttributeError:
maxprint = MAXPRINT
return maxprint
#def typecode(self):
# try:
# typ = self.dtype.char
# except AttributeError:
# typ = None
# return typ
def getnnz(self):
try:
return self.nnz
except AttributeError:
raise AttributeError, "nnz not defined"
def getformat(self):
try:
format = self.format
except AttributeError:
format = 'und'
return format
@np.deprecate
def rowcol(self, num):
return (None, None)
@np.deprecate
def getdata(self, num):
return None
@np.deprecate
def listprint(self, start, stop):
"""Provides a way to print over a single index.
"""
return '\n'.join([' %s\t%s' % (self.rowcol(ind), self.getdata(ind))
for ind in xrange(start,stop)]) + '\n'
def __repr__(self):
nnz = self.getnnz()
format = self.getformat()
return "<%dx%d sparse matrix of type '%s'\n" \
"\twith %d stored elements in %s format>" % \
(self.shape + (self.dtype.type, nnz, _formats[format][1]))
def __str__(self):
maxprint = self.getmaxprint()
A = self.tocoo()
nnz = self.getnnz()
# helper function, outputs "(i,j) v"
def tostr(row,col,data):
triples = zip(zip(row,col),data)
return '\n'.join( [ (' %s\t%s' % t) for t in triples] )
if nnz > maxprint:
half = maxprint // 2
out = tostr(A.row[:half], A.col[:half], A.data[:half])
out += "\n :\t:\n"
half = maxprint - maxprint//2
out += tostr(A.row[-half:], A.col[-half:], A.data[-half:])
else:
out = tostr(A.row, A.col, A.data)
return out
def __nonzero__(self): # Simple -- other ideas?
return self.getnnz() > 0
# What should len(sparse) return? For consistency with dense matrices,
# perhaps it should be the number of rows? But for some uses the number of
# non-zeros is more important. For now, raise an exception!
def __len__(self):
# return self.getnnz()
raise TypeError, "sparse matrix length is ambiguous; use getnnz()" \
" or shape[0]"
def asformat(self, format):
"""Return this matrix in a given sparse format
Parameters
----------
format : {string, None}
desired sparse matrix format
- None for no format conversion
- "csr" for csr_matrix format
- "csc" for csc_matrix format
- "lil" for lil_matrix format
- "dok" for dok_matrix format and so on
"""
if format is None or format == self.format:
return self
else:
return getattr(self,'to' + format)()
###################################################################
# NOTE: All arithmetic operations use csr_matrix by default.
# Therefore a new sparse matrix format just needs to define a
# .tocsr() method to provide arithmetic support. Any of these
# methods can be overridden for efficiency.
####################################################################
def multiply(self, other):
"""Point-wise multiplication by another matrix
"""
return self.tocsr().multiply(other)
def __abs__(self):
return abs(self.tocsr())
def __add__(self, other): # self + other
return self.tocsr().__add__(other)
def __radd__(self, other): # other + self
return self.tocsr().__radd__(other)
def __sub__(self, other): # self - other
#note: this can't be replaced by self + (-other) for unsigned types
return self.tocsr().__sub__(other)
def __rsub__(self, other): # other - self
return self.tocsr().__rsub__(other)
# old __mul__ interfaces
@np.deprecate
def matvec(self,other):
return self * other
@np.deprecate
def matmat(self,other):
return self * other
@np.deprecate
def dot(self, other):
return self * other
@np.deprecate
def rmatvec(self, other, conjugate=True):
"""Multiplies the vector 'other' by the sparse matrix, returning a
dense vector as a result.
If 'conjugate' is True:
- returns A.transpose().conj() * other
Otherwise:
- returns A.transpose() * other.
"""
if conjugate:
return self.conj().transpose() * other
else:
return self.transpose() * other
def __mul__(self, other):
"""interpret other and call one of the following
self._mul_scalar()
self._mul_vector()
self._mul_multivector()
self._mul_sparse_matrix()
"""
M,N = self.shape
if isscalarlike(other):
# scalar value
return self._mul_scalar(other)
if issparse(other):
if self.shape[1] != other.shape[0]:
raise ValueError('dimension mismatch')
return self._mul_sparse_matrix(other)
try:
other.shape
except AttributeError:
# If it's a list or whatever, treat it like a matrix
other = np.asanyarray(other)
other = np.asanyarray(other)
if other.ndim == 1 or other.ndim == 2 and other.shape[1] == 1:
# dense row or column vector
if other.shape != (N,) and other.shape != (N,1):
raise ValueError('dimension mismatch')
result = self._mul_vector(np.ravel(other))
if isinstance(other, np.matrix):
result = np.asmatrix(result)
if other.ndim == 2 and other.shape[1] == 1:
# If 'other' was an (nx1) column vector, reshape the result
result = result.reshape(-1,1)
return result
elif other.ndim == 2:
##
# dense 2D array or matrix ("multivector")
if other.shape[0] != self.shape[1]:
raise ValueError('dimension mismatch')
result = self._mul_multivector(np.asarray(other))
if isinstance(other, np.matrix):
result = np.asmatrix(result)
return result
else:
raise ValueError('could not interpret dimensions')
# by default, use CSR for __mul__ handlers
def _mul_scalar(self, other):
return self.tocsr()._mul_scalar(other)
def _mul_vector(self, other):
return self.tocsr()._mul_vector(other)
def _mul_multivector(self, other):
return self.tocsr()._mul_multivector(other)
def _mul_sparse_matrix(self, other):
return self.tocsr()._mul_sparse_matrix(other)
def __rmul__(self, other): # other * self
if isscalarlike(other):
return self.__mul__(other)
else:
# Don't use asarray unless we have to
try:
tr = other.transpose()
except AttributeError:
tr = np.asarray(other).transpose()
return (self.transpose() * tr).transpose()
####################
# Other Arithmetic #
####################
def __truediv__(self, other):
if isscalarlike(other):
return self * (1./other)
else:
return self.tocsr().__truediv__(other)
def __div__(self, other):
# Always do true division
return self.__truediv__(other)
def __neg__(self):
return -self.tocsr()
def __iadd__(self, other):
raise NotImplementedError
def __isub__(self, other):
raise NotImplementedError
def __imul__(self, other):
raise NotImplementedError
def __idiv__(self, other):
return self.__itruediv__(other)
def __itruediv__(self, other):
raise NotImplementedError
def __pow__(self, other):
if self.shape[0] != self.shape[1]:
raise TypeError('matrix is not square')
if isintlike(other):
other = int(other)
if other < 0:
raise ValueError('exponent must be >= 0')
if other == 0:
from construct import identity
return identity( self.shape[0], dtype=self.dtype )
elif other == 1:
return self.copy()
else:
result = self
for i in range(1,other):
result = result*self
return result
elif isscalarlike(other):
raise ValueError('exponent must be an integer')
elif isspmatrix(other):
warn('Using ** for elementwise multiplication is deprecated.'\
'Use .multiply() instead', DeprecationWarning)
return self.multiply(other)
else:
raise NotImplementedError
def __getattr__(self, attr):
if attr == 'A':
return self.toarray()
elif attr == 'T':
return self.transpose()
elif attr == 'H':
return self.getH()
elif attr == 'real':
return self._real()
elif attr == 'imag':
return self._imag()
elif attr == 'size':
return self.getnnz()
else:
raise AttributeError, attr + " not found"
def transpose(self):
return self.tocsr().transpose()
def conj(self):
return self.tocsr().conj()
def conjugate(self):
return self.conj()
# Renamed conjtranspose() -> getH() for compatibility with dense matrices
def getH(self):
return self.transpose().conj()
def _real(self):
return self.tocsr()._real()
def _imag(self):
return self.tocsr()._imag()
def nonzero(self):
"""nonzero indices
Returns a tuple of arrays (row,col) containing the indices
of the non-zero elements of the matrix.
Example
-------
>>> from scipy.sparse import csr_matrix
>>> A = csr_matrix([[1,2,0],[0,0,3],[4,0,5]])
>>> A.nonzero()
(array([0, 0, 1, 2, 2]), array([0, 1, 2, 0, 2]))
"""
# convert to COOrdinate format
A = self.tocoo()
nz_mask = A.data != 0
return (A.row[nz_mask],A.col[nz_mask])
def getcol(self, j):
"""Returns a copy of column j of the matrix, as an (m x 1) sparse
matrix (column vector).
"""
# Spmatrix subclasses should override this method for efficiency.
# Post-multiply by a (n x 1) column vector 'a' containing all zeros
# except for a_j = 1
from csc import csc_matrix
n = self.shape[1]
a = csc_matrix((n, 1), dtype=self.dtype)
a[j, 0] = 1
return self * a
def getrow(self, i):
"""Returns a copy of row i of the matrix, as a (1 x n) sparse
matrix (row vector).
"""
# Spmatrix subclasses should override this method for efficiency.
# Pre-multiply by a (1 x m) row vector 'a' containing all zeros
# except for a_i = 1
from csr import csr_matrix
m = self.shape[0]
a = csr_matrix((1, m), dtype=self.dtype)
a[0, i] = 1
return a * self
#def __array__(self):
# return self.toarray()
def todense(self):
return np.asmatrix(self.toarray())
def toarray(self):
return self.tocoo().toarray()
def todok(self):
return self.tocoo().todok()
def tocoo(self):
return self.tocsr().tocoo()
def tolil(self):
return self.tocsr().tolil()
def todia(self):
return self.tocoo().todia()
def tobsr(self, blocksize=None):
return self.tocsr().tobsr(blocksize=blocksize)
def copy(self):
return self.__class__(self,copy=True)
def sum(self, axis=None):
"""Sum the matrix over the given axis. If the axis is None, sum
over both rows and columns, returning a scalar.
"""
# We use multiplication by an array of ones to achieve this.
# For some sparse matrix formats more efficient methods are
# possible -- these should override this function.
m, n = self.shape
if axis == 0:
# sum over columns
return np.asmatrix(np.ones((1, m), dtype=self.dtype)) * self
elif axis == 1:
# sum over rows
return self * np.asmatrix(np.ones((n, 1), dtype=self.dtype))
elif axis is None:
# sum over rows and columns
return ( self * np.asmatrix(np.ones((n, 1), dtype=self.dtype)) ).sum()
else:
raise ValueError, "axis out of bounds"
def mean(self, axis=None):
"""Average the matrix over the given axis. If the axis is None,
average over both rows and columns, returning a scalar.
"""
if axis == 0:
mean = self.sum(0)
mean *= 1.0 / self.shape[0]
return mean
elif axis == 1:
mean = self.sum(1)
mean *= 1.0 / self.shape[1]
return mean
elif axis is None:
return self.sum(None) * 1.0 / (self.shape[0]*self.shape[1])
else:
raise ValueError, "axis out of bounds"
def diagonal(self):
"""Returns the main diagonal of the matrix
"""
#TODO support k != 0
return self.tocsr().diagonal()
def setdiag(self, values, k=0):
"""Fills the diagonal elements {a_ii} with the values from the
given sequence. If k != 0, fills the off-diagonal elements
{a_{i,i+k}} instead.
values may have any length. If the diagonal is longer than values,
then the remaining diagonal entries will not be set. If values if
longer than the diagonal, then the remaining values are ignored.
"""
M, N = self.shape
if (k > 0 and k >= N) or (k < 0 and -k >= M):
raise ValueError, "k exceedes matrix dimensions"
if k < 0:
max_index = min(M+k, N, len(values))
for i,v in enumerate(values[:max_index]):
self[i - k, i] = v
else:
max_index = min(M, N-k, len(values))
for i,v in enumerate(values[:max_index]):
self[i, i + k] = v
def save(self, file_name, format = '%d %d %f\n'):
#deprecated on Dec 14 2007
#remove after 0.7 release
warn('save() is deprecated, consider using mmwrite() or savemat()' \
' provided by scipy.io instead',
DeprecationWarning)
try:
fd = open(file_name, 'w')
except Exception, e:
raise e, file_name
fd.write('%d %d\n' % self.shape)
fd.write('%d\n' % self.size)
for ii in xrange(self.size):
ir, ic = self.rowcol(ii)
data = self.getdata(ii)
fd.write(format % (ir, ic, data))
fd.close()
from sputils import _isinstance
def isspmatrix(x):
return _isinstance(x, spmatrix)
issparse = isspmatrix
|