Skip to content

Commit

Permalink
Use boolean array reductions (modelica#3919)
Browse files Browse the repository at this point in the history
* Change description of allTrue to show difference compared to andTrue

* Use Boolean array reductions in some BooleanVectors functions

This takes advantages of tools' dedicated implementations of conjunction and disjunction of multiple values expressed as Boolean array reduction with min and max, respectively.

With the result being given by simple expressions, it seemed natural to also set Inline = true.

* Remove empty algorithm

Addressing review comment by @beutlich.

* Remove empty algorithm

* Remove empty algorithm

As suggested by @beutlich.
  • Loading branch information
henrikt-ma committed Jan 18, 2024
1 parent 07b1594 commit ceae187
Showing 1 changed file with 7 additions and 22 deletions.
29 changes: 7 additions & 22 deletions Modelica/Math/BooleanVectors.mo
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@ within Modelica.Math;
package BooleanVectors "Library of functions operating on Boolean vectors"
extends Modelica.Icons.Package;
function allTrue
"Returns true, if all elements of the Boolean input vector are true ('and')"
"Returns true, if Boolean input vector is non-empty and all elements are true ('and')"
extends Modelica.Icons.Function;
input Boolean b[:] "Boolean vector";
output Boolean result "= true, if all elements of b are true";
algorithm
result := size(b,1) > 0;
for i in 1:size(b,1) loop
result := result and b[i];
end for;
annotation (Documentation(info="<html>
output Boolean result = size(b, 1) > 0 and min(b) "= true, if all elements of b are true";
annotation (Inline = true, Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
<strong>allTrue</strong>(b);
Expand Down Expand Up @@ -52,13 +47,8 @@ function andTrue
"Returns true, if all elements of the Boolean input vector are true ('and')"
extends Modelica.Icons.Function;
input Boolean b[:] "Boolean vector";
output Boolean result "= true, if all elements of b are true";
algorithm
result := true;
for i in 1:size(b,1) loop
result := result and b[i];
end for;
annotation (Documentation(info="<html>
output Boolean result = min(b) "= true, if all elements of b are true";
annotation (Inline = true, Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
<strong>andTrue</strong>(b);
Expand Down Expand Up @@ -100,13 +90,8 @@ function anyTrue

extends Modelica.Icons.Function;
input Boolean b[:] "Boolean vector";
output Boolean result "= true, if at least one element of b is true";
algorithm
result := false;
for i in 1:size(b,1) loop
result := result or b[i];
end for;
annotation (Documentation(info="<html>
output Boolean result = max(b) "= true, if at least one element of b is true";
annotation (Inline = true, Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
<strong>anyTrue</strong>(b);
Expand Down

0 comments on commit ceae187

Please sign in to comment.