Skip to content

Commit 4d90e9e

Browse files
author
counterclocker
committed
clean up: removed delete_row and masks version from LLSparseMatrix
1 parent 4458c65 commit 4d90e9e

8 files changed

Lines changed: 4642 additions & 5884 deletions

File tree

cysparse/sparse/ll_mat_matrices/ll_mat.cpx

Lines changed: 7 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -362,14 +362,13 @@ cdef class LLSparseMatrix_@index@_@type@(MutableSparseMatrix_@index@_@type@):
362362

363363
k = self.link[k]
364364

365-
def delete_rows(self, B):
365+
def delete_rows(self, Obj):
366366
"""
367367
Delete rows.
368368

369369
Args:
370-
B: List or :program:`NumPy` array with indices of the rows to be deleted.
370+
Obj: List or :program:`NumPy` array with indices of the rows to be deleted.
371371
"""
372-
# TODO: this code is duplicated from 'delete_rows_with_mask': maybe we should merge both codes?
373372
# TODO: this code is very slow... maybe optimize one day? In particular the main loop combined with linear search
374373
# for non existing elements is particularly poor design.
375374

@@ -380,7 +379,7 @@ cdef class LLSparseMatrix_@index@_@type@(MutableSparseMatrix_@index@_@type@):
380379
@index@ nrow
381380
@index@ * row_indices
382381

383-
row_indices = create_c_array_indices_from_python_object_@index@(self.__nrow, <PyObject *> B, &nrow)
382+
row_indices = create_c_array_indices_from_python_object_@index@(self.__nrow, <PyObject *> Obj, &nrow)
384383

385384
# Delete the rows to be cancelled by rearranging the row
386385
# array. After having done so, newdim is the new matrix dim.
@@ -412,109 +411,14 @@ cdef class LLSparseMatrix_@index@_@type@(MutableSparseMatrix_@index@_@type@):
412411

413412
PyMem_Free(row_indices)
414413

415-
def delete_row(self, @index@ row):
416-
"""
417-
Delete one row in place.
418-
419-
Args:
420-
row: Number of the row to delete.
421-
422-
Note:
423-
**Don't** call this method repeatly to delete several rows. See ...
424-
"""
425-
# Deleting one or several rows takes the same amount of time.
426-
427-
if not (0<= row < self.__nrow):
428-
raise IndexError('Wrong row number (%d)' % row)
429-
430-
if self.__is_symmetric:
431-
raise NotImplementedError('This method is not allowed for symmetric matrices')
432-
433-
cdef:
434-
@index@ act, i
435-
@index@ newnnz = self.__nnz
436-
437-
# deal with the row to be deleted
438-
act = self.root[row]
439-
440-
if act != -1:
441-
newnnz -= 1
442-
while self.link[act] != -1: # Walk to the end of the list
443-
act = self.link[act]
444-
newnnz -= 1
445-
446-
self.link[act] = self.free # Attach end of row to free list
447-
self.free = self.root[row] # Start free list where row began
448-
449-
# Shift remaining rows to the left
450-
for i from row <= i < self.__nrow - 1:
451-
self.root[i] = self.root[i+1]
452-
453-
# Set the new values
454-
self.__nrow = self.__nrow - 1
455-
self.__nnz = newnnz
456-
457-
def delete_rows_with_mask(self, cnp.ndarray[dtype=cnp.npy_int8, ndim=1] maskArray):
458-
"""
459-
Delete rows according to a mask ``maskArray``.
460-
461-
If ``maskArray[i] == False``, we delete row ``i``.
462-
463-
Args:
464-
maskArray: :program:`NumPy` array with elements corresponding to the rows, i.e. the array must be at least as long as there are
465-
rows in the matrix.
466-
467-
Warning:
468-
We use ``int8`` as element type for the mask.
469-
"""
470-
# Warning: we use int8 instead of bool. There are too many problems with the bool type...
471-
472-
if self.__is_symmetric:
473-
raise NotImplementedError('This method is not allowed for symmetric matrices')
474-
475-
if maskArray.size < self.__nrow:
476-
raise IndexError('Mask array must be at least as long as the number of rows in this matrix')
477-
478-
# Delete the rows to be cancelled by rearranging the row
479-
# array. After having done so, newdim is the new matrix dim.
480-
cdef:
481-
@index@ row, act
482-
@index@ newm = 0
483-
@index@ newnnz = self.__nnz
484-
485-
cdef:
486-
signed char * maskArray_data = <signed char *> cnp.PyArray_DATA(maskArray)
487-
@index@ maskArray_stride = <@index@> maskArray.strides[0]
488-
489-
for row from 0<= row < self.__nrow:
490-
491-
if maskArray_data[row * maskArray_stride]: # This row has to be kept
492-
self.root[newm] = self.root[row] # Shift row to the left
493-
newm += 1
494-
else: # row left out; update free list
495-
act = self.root[row]
496-
if act != -1:
497-
newnnz -= 1
498-
while self.link[act] != -1: # Walk to the end of the list
499-
act = self.link[act]
500-
newnnz -= 1
501-
502-
self.link[act] = self.free # Attach end of row to free list
503-
self.free = self.root[row] # Start free list where row began
504-
505-
506-
# Set the new values
507-
self.__nrow = newm
508-
self.__nnz = newnnz
509-
510-
def delete_cols(self, B):
414+
def delete_cols(self, Obj):
511415
"""
512416
Delete columns.
513417

514418
Args:
515-
B: List or :program:`NumPy` array with indices of the rows to be deleted.
419+
Obj: List or :program:`NumPy` array with indices of the rows to be deleted.
516420
"""
517-
# TODO: this code is very slow...
421+
# TODO: this code is very slow...
518422
if self.__is_symmetric:
519423
raise NotImplementedError('This method is not allowed for symmetric matrices')
520424

@@ -524,7 +428,7 @@ cdef class LLSparseMatrix_@index@_@type@(MutableSparseMatrix_@index@_@type@):
524428
@index@ * shift
525429
@index@ newn
526430

527-
col_indices = create_c_array_indices_from_python_object_@index@(self.__ncol, <PyObject *> B, &ncol)
431+
col_indices = create_c_array_indices_from_python_object_@index@(self.__ncol, <PyObject *> Obj, &ncol)
528432

529433
# find shift in kept column indices
530434
shift = <@index@ *> malloc(self.__ncol*sizeof(@index@))
@@ -579,7 +483,6 @@ cdef class LLSparseMatrix_@index@_@type@(MutableSparseMatrix_@index@_@type@):
579483
self.__ncol = newn
580484
self.__nnz = newnnz
581485

582-
583486
PyMem_Free(col_indices)
584487
free(shift)
585488

0 commit comments

Comments
 (0)