Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Fast vectorized evaluation of array inequalities
ResourceFunction["BoolEval"][array>value] replaces elements of array which are greater than value with 1 and the rest with 0. | |
ResourceFunction["BoolEval"][condition] takes a condition expressed in terms of >,≥,<,≤,⩵,≠ and logical operators, and evaluates it for each element of the arrays appearing in the condition, returning a Boolean array of 1s and 0s. |
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]= | ![]() |
This work is licensed under a Creative Commons Attribution 4.0 International License