# Wolfram Function Repository

Instant-use add-on functions for the Wolfram Language

Function Repository Resource:

Fast vectorized evaluation of array inequalities

Contributed by:
Szabolcs Horvát

ResourceFunction["BoolEval"][ replaces elements of | |

ResourceFunction["BoolEval"][ takes a |

ResourceFunction["BoolEval"] is intended for use with numerical arrays.

The arrays appearing in the conditions must have compatible sizes.

ResourceFunction["BoolEval"] has attribute HoldAll, and only processes operators that appear literally in the input.

The primary purpose of ResourceFunction["BoolEval"] is to achieve maximum performance, therefore it does not check its input for correctness. This is left to the programmer.

Internally, ResourceFunction["BoolEval"] converts inequalities to expressions in terms of UnitStep and logical operators to arithmetic ones.

The BoolEval package contains the same function (BoolEval) as well as related utilities and documentation.

Identify elements of an array that are larger than 5:

In[1]:= |

Out[1]= |

Identify elements that are greater than or equal to 3 but less than 7:

In[2]:= |

Out[3]= |

Higher dimensional arrays are supported:

In[4]:= |

Out[4]= |

Compare each row of a matrix with a different constant:

In[5]:= |

Out[1]= |

Compare two arrays:

In[6]:= |

Out[6]= |

BoolEval supports any expression that works with arithmetic and basic numerical operations:

In[7]:= |

Out[7]= |

BoolEval works with images:

In[8]:= |

Out[8]= |

Count the number of array elements that are greater than 0.3:

In[9]:= |

Out[10]= |

Pick non-positive elements of an array:

In[11]:= |

Out[12]= |

Process an array much faster than it would be possible using Select or Cases:

In[13]:= |

Out[4]= |

In[14]:= |

Out[14]= |

In[15]:= |

Out[15]= |

In[16]:= |

Out[16]= |

BoolEval effectively threads comparisons over arrays, then converts truth values to 0 or 1:

In[17]:= |

Out[17]= |

In[18]:= |

Out[18]= |

Literal truth values are also converted to 0s and 1s:

In[19]:= |

Out[19]= |

Passing symbolic input to BoolEval reveals how it translates inequalities to UnitStep and logical operators to arithmetic ones:

In[20]:= |

Out[20]= |

In[21]:= |

Out[21]= |

In[22]:= |

Out[22]= |

Only those operators are processed which appear literally in the argument of BoolEval:

In[23]:= |

Out[24]= |

In[25]:= |

Out[25]= |

Evaluate the argument explicitly using the Evaluate function to allow BoolEval to see the operators:

In[26]:= |

Out[27]= |

BoolEval is optimized for performance, and does not check its arguments for correctness:

In[28]:= |

Out[28]= |

The arrays in the argument must be numeric:

In[29]:= |

Out[29]= |

Estimate the value of * π* using Monte Carlo integration:

In[30]:= |

Out[30]= |

- 1.0.0 – 31 July 2019

This work is licensed under a Creative Commons Attribution 4.0 International License