[SciPy-Dev] Adding boolean support to the sparse package, design choices

Pauli Virtanen pav@iki...
Tue Apr 23 03:19:58 CDT 2013


Blake Griffith <blake.a.griffith <at> gmail.com> writes:
[clip]
> I've been investigating how I would go about doing this,
> so far there seem to be three ways. (A) By keeping all the
> operations in python and using it's bool dtype.(B) By adding
> bool support to the existing C++ routines we currently have.
> (C) Or by converting back and forth from int8 to bool where
> the python and C++ interface.
[clip]
> (B) Seems like the *right* way but I'm not sure how I would
> add bool support to the routines we have. Should I write
> new functions just for bool operations? Or can support for
> bool types be added to the existing functions?
[clip]

I think this can be done by adding to the sparsetools/*.h files
new routines such as

#define COMPARE_EQ 0
#define COMPARE_GT 1
#define COMPARE_LT 2
...

template <class I, class T, class T2>
void csr_cmp_csr(const I n_row, const I n_col, 
                 const I Ap[], const I Aj[], const T Ax[],
                 const I Bp[], const I Bj[], const T Bx[],
                       I Cp[],       I Cj[],      T2 Cx[],
                 int cmpflag)
{
    if (cmpflag == COMPARE_EQ) {
       csr_binop_csr(n_row,n_col,Ap,Aj,Ax,Bp,Bj,Bx,Cp,Cj,Cx,std::equal_to<T>());
    }
    else if
    ...
}

Note that the generic code doing the heavy lifting for binary
operations is already there, so you can just use those functions.

What needs to be changed is:

- Change the csr_binop_csr C++ templates (and ditto for the other
  routines) so that the return type can be different from the input
  argument types. I.e., add a new template type as above.

- Read on SWIG type maps and try to understand how they work.
  Best place to start is probably the SWIG documentation, and to try
  some simple examples first.

- Add new type maps for the comparison routines, with signatures
  T, T -> bool

- Add type maps for logical operation routines (or, and, xor, ...) with
  signatures bool, bool -> bool

- The numerical type used for booleans internally inside sparsetools
  should be int8/char (the builtin C++ bool type can be bigger than int8).

- SWIG can be told to dispatch differently for NPY_BOOL and NPY_INT8 arrays.
  You probably need to look inside numpy.i how this magic happens.

-- 
Pauli Virtanen



More information about the SciPy-Dev mailing list