This week I worked on adding support for inequalities to sparse matrices. The pull request is here (still pending). I also modified, the c++ routines used to produce a boolean output, pull request here. This is so these inequalities don't have to be cast to bool in python. Once both of these are accepted the inequalities will be using the c++ routines that produce boolean output.
These are separate pull requests but they are related and one will need to be rebased once the other is accepted. Or I may combine them.
Each of the four basic inequalities has its own particular quirks which
make it more or less efficient with sparse matrices. Here we consider
A to be a sparse matrix. The cases where
B is a scalar, or sparse
are considered. (By scalar we effectively mean a matrix the same size as
A with every element as the scalar value
A < B
For scalar B, this operation is only efficient if
B < 0 otherwise the
resulting matrix will be dense.
B this is also efficient.
A > B
B the efficiency is the opposite of the less than operator.
B > 0 has efficient output. Similarly to
< this operation is
efficient with other sparse matrices.
A <= B
This operation is pretty much useless. The only case which make this
efficient is when
B < 0. But this case is already covered by
< can be used efficiently with sparse matrices. So why use
discuss the pros and cons of having these non-strict inequalities in a
Another disadvantage of non strict inequalities like these is that they
NotImplementedError when comparing with 0. This is because of
the nature of the c++ routines. When comparing every pair of elements
they don't consider cases where both are zero.
A >= B
<= this operation is only efficient for one case, when
B > 0.
And also like
<=, it is not very useful.
Why have non strict inequalities?
I can't really think of uses for
<= but they might exist, I
added them because they were easy to implement once I had done
<. In practice, they don't slow the usage down. But removing non -
strict inequalities removes 24,990 lines of code.
The inequality operations are implemented as c++ routines, these are wrapped to provide a python interface using SWIG, then the various inequalities are added appropriately.
The binop routines had to altered, another class was added to their
T2, for the type of data out. In theory this could be
something other than bool.
To handle boolean output data a new swig
was added to create typemaps that have
npy_bool_wrapper as this output
class. I then use these to instantiate the boolean operations.
Here the associated python special methods corresponding to the
inequalities were added in
These operations can be used with a scalar, dense, or sparse matrix.
Numpy style broadcasting is not implemented.
To produce a bool output by default for boolean comparisons, I altered
function to pass the c++ routines a matrix to use for output with a bool
When testing the inequalities with dense matrices, I could not get the proper
behavior with a dense matrix on the left hand side. Previously, with
== I modified the
__bool__ method but that did not work in this case. This
is because of the same problem I will be dealing with in the next stage of my
proposal, interactions with numpy ufuncs.