Logical operators on lists?



I get a list of boolean values (e.g. from a select function, or when I slice a matrix to another matrix with fewer dimensions), and I want to make “and” or “or” of all these values, returning a single boolean.

Unfortunately, the and/or functions do not work as a summary on lists, the would need two lists, and then work on each element, returning a list. What I want is to work them on all elements on the list, returning a scalar.

As a hack, I now use “sum (list of booleans)” for “or”,  and  not ( sum ( not (list of booleans) )) for “and”, which is a bit clumsy?

Is there a better workaround for this problem?

Is this worth a feature request?

Thanks, Gilbert

S A U Answered question June 9, 2024

what if we use to compute Boolean functions over a vector of Boolean values:

min(list_of_booleans) for AND-logic;
max(list_of_booleans) for OR-logic.

Good luck

S A U Answered question June 9, 2024

Gilbert, I don’t use the sum() based formula because its result may not be limited to values from the range [0, 1]. For example, for list_of_booleans = [1, 0, 1] sum(list_of_booleans) = 2. But the formula 1 – product(1 – list_of_booleans) will return: 1 – 0 * 1 * 0 = 1.

If, however, you prefer a sum() based formula for OR-logic, then min(1; sum(list_of_booleans)) will limit the result to values in the range [0, 1].

Good luck

Gilbert Posted new comment June 8, 2024

agreed. “sign ( sum ( list of boolean) )” will also limit to 1.


Thanks S A U!!!

You’re thinking along the same lines as I do … if we fuse our approaches, a nicer one might be:

AND: product (list of boolean)

OR:  sign ( sum (list of boolean) )

This should work in practice, still is less readable than a “AND (list of boolean)” and “OR (list of boolean)” would be … but yes, the reasoning behind might be, that in Quantrix, booleans are basically thought of as being numbers ….

Thanks a lot for your contribution!


Gilbert Edited answer June 8, 2024

Hi Gilbert,

Try these formulaes:
product(list_of_booleans) for AND-logic;
1 – product(1 – list_of_booleans) for OR-logic.

Good luck

S A U Answered question June 4, 2024