Closed dmdunla closed 1 year ago
We named our arguments to mirror numpy's naming convention https://numpy.org/doc/stable/reference/generated/numpy.logical_and.html I don't know offhand if we match their behavior
OK, now I see the numpy
examples. The &
operator is not the same, as it is bitwise AND, and it should be used with boolean (and maybe int) arrays. Here's is how the numpy.logical_and
works (note that it is a function of two (array-like) inputs, not a class method (e.g., for sptensor
).
>>> A = np.zeros((2,2))
>>> A[0,0] = 1
>>> np.logical_and(A,A)
array([[ True, False],
[False, False]])
>>> np.logical_and(A,1)
array([[ True, False],
[False, False]])
>>> np.logical_and(A,2)
array([[ True, False],
[False, False]])
I do not see anything equivalent in scipy
but other math libraries (e.g., tensorflow, cupy) do have logical_and
that behave in this way.
Unfortunately, we do not support boolean tensors in pyttb
, only float values at this point.
Ok. So for consistency we want the result to be a sptensor of the same dtype but with explicit values only equal to 1.0 (for logical_or
same dtype argument but might end up a dense tensor). Or did you have something else in mind?
I was trying to say that we do not currently support dytpe
throughout our classes. Some of the support comes from the fact that we use numpy
for a lot of the data members in our classes. So, we can change some of the data to boolean values after construction (as shown in the example above).
So, my suggestion is that we follow MATLAB TTB for now and make the outputs of the logical_*
methods be indicator tensors (with 1.0 for True
and 0 or nothing [i.e., for sptensor
] for False
).
Great. We're saying the same thing in different ways so looks like we're on the same page.
sptensor.logical_and
behavior is different than in MATLAB, and I am not sure that what the MATLAB version is doing is useful/correct.Questions: Do we want to match the MATLAB behavior? Do we want to follow
numpy
/scipy.sparse
? If so, what are those behaviors.Here is the current
sptensor.logical_and
:The output when computing with a tensor is an indicator sparse tensor, whereas it is a boolean sparse tensor for the scalar case. Also, the scalar case is computing the AND as an equality comparison it appears, not just a check for non-zero.
Here is the output from MATLAB:
The scalar AND is odd in MATLAB, as it checks for either zero or non-zero depending on the scalar input:
The documentation in
pyttb
should document this behavior (once it matches with MATLAB).Here are some experiments with
numpy
andscipy
, where I am unable to compute__and__
at all, so I am unsure what the default behavior for those should be: