Modelica 中布尔值数组的 if 条件

发布于 2024-10-31 08:56:02 字数 267 浏览 1 评论 0原文

如果这是一个“阅读手册”问题(我读了但找不到答案),我很抱歉。

我有一个布尔数组,我想测试其中是否有一个为真。

model TestArray

(...)
Boolean[:] booleanArray;
Real y;

equation
y = if [if any element in booleanArray is true] then ... else ...;

end TestArray;

我该怎么做? 谢谢, 罗尔

I'm sorry if this is a 'read the manual' question (I did but can't find an answer).

I have an array of Booleans and I want to test if any of them is true.

model TestArray

(...)
Boolean[:] booleanArray;
Real y;

equation
y = if [if any element in booleanArray is true] then ... else ...;

end TestArray;

How can I do this?
Thanks,
Roel

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

守望孤独 2024-11-07 08:56:02

有类似于您在 Modelica.Math.BooleanVectors 中请求的函数。

在这里您将找到 allTrue(Boolean b[:])、anyTrue(Boolean b[:]) 和 oneTrue(Boolean b[:])。

There are functions like the ones you are requesting in Modelica.Math.BooleanVectors.

Here you'll find allTrue(Boolean b[:]), anyTrue(Boolean b[:]) and oneTrue(Boolean b[:]).

森林散布 2024-11-07 08:56:02

这是一个有趣的问题。坦率地说,我不知道有任何内置功能可以执行此操作(尽管对此类功能的需求肯定是有效的)。

我们过去经常做的是编写名为“any”和“all”的实用函数,它们看起来像这样(未经测试,但你明白了):

function any
  input Boolean vals[:];
  output Boolean result;
algorithm
  result := max({if i==true then 1 else 0 for i in vals})==1;
end any;

function all
  input Boolean vals[:];
  output Boolean result;
algorithm
  result := min({if i==true then 1 else 0 for i in vals})==1;
end all;

这与你所做的类似,但使用数组推导式,然后将其封装在函数中。这允许您编写如下代码:

if any(conditions) then ... else ...;

理想情况下,可以将这些函数添加到内置的“归约运算符”集中(例如 min 和 max),但是语言组在引入此类运算符方面往往有些保守,因为它们会污染命名空间并与现有代码产生潜在的冲突。

请注意,使用when 子句时事情会变得有点棘手。对于when 子句,有一个向量结构,例如

when {cond1, cond2, cond3} then
  ...
end when;

,它具有非常有用的语义,但并不100% 类似于上面写的“any”或“all”。因此,如果您打算在 when 子句中使用条件向量,请阅读如何处理它(在规范中)或提出一个后续问题,我可以详细说明(这有点超出了这个问题)。

This is an interesting question. Frankly, I'm not aware of any built-in capabilities for doing this (although the need for such capabilities is certainly valid).

What we've frequently done in the past is to write utility functions called "any" and "all", that look like this (untested, but you get the idea):

function any
  input Boolean vals[:];
  output Boolean result;
algorithm
  result := max({if i==true then 1 else 0 for i in vals})==1;
end any;

function all
  input Boolean vals[:];
  output Boolean result;
algorithm
  result := min({if i==true then 1 else 0 for i in vals})==1;
end all;

This is similar to what you did but using array comprehensions and then encapsulating that in functions. This allows you to write code like:

if any(conditions) then ... else ...;

Ideally, these functions could be added to the built-in set of "reduction operators" (like min and max), but the language group tends to be somewhat conservative about introducing such operators because they pollute the namespace and create potential collisions with existing code.

Note that things get a bit tricky when using when clauses. With when clauses, there is a vector construction, e.g.

when {cond1, cond2, cond3} then
  ...
end when;

Which has very useful semantics, but is not 100% analogous to either "any" or "all" as written above. So if you intend to use a vector of conditions in a when clause, then read up on how this is handled (in the specification) or ask a follow-up question on that and I can elaborate more (it is somewhat beyond the scope of this question).

九命猫 2024-11-07 08:56:02

Modelica 规范版本 4.3 第 10.3.4 节 允许布尔数组 v 作为 min(v)max(v) 的参数。

如果 v 的所有分量均为 true,则 min(v) 给出 truefalse > 否则。
如果 v 的所有分量均为 false,则 max(v) 给出 falsetrue > 否则。

型号示例:

model Test
    Boolean anyFalseGivesFalse = min( { true, false } );
    Boolean allTrueGivesTrue = min( { true, true } );
    Boolean allFalseGivesFalse = max( { false, false } );
    Boolean anyTrueGivesTrue = max( { false, true } );
end Test;

Section 10.3.4 of Modelica Specification Version 4.3 allows Boolean arrays v as arguments of min(v) and max(v).

If all components of v are true then min(v) gives true, false otherwise.
If all components of v are false then max(v) gives false, true otherwise.

Example model:

model Test
    Boolean anyFalseGivesFalse = min( { true, false } );
    Boolean allTrueGivesTrue = min( { true, true } );
    Boolean allFalseGivesFalse = max( { false, false } );
    Boolean anyTrueGivesTrue = max( { false, true } );
end Test;
把回忆走一遍 2024-11-07 08:56:02

现在我找到了一个解决方法,但一定可以做得更好:

model TestArray

(...)
Boolean[:] booleanArray;
Real y;
Real[:] test;

equation
for i in 1:size(booleanArray):
  test[i] = if booleanArray[i] then 1 else 0;
end for;

y = if sum(test) > 0 then ... else ...;

end TestArray;

Now I found a workaround, but it must be possible to do it much nicer:

model TestArray

(...)
Boolean[:] booleanArray;
Real y;
Real[:] test;

equation
for i in 1:size(booleanArray):
  test[i] = if booleanArray[i] then 1 else 0;
end for;

y = if sum(test) > 0 then ... else ...;

end TestArray;
梦毁影碎の 2024-11-07 08:56:02

您可以使用 Modelica.Blocks.Math.BooleanToInteger 将布尔数组转换为整数数组,您可以用它来计算...

You could use Modelica.Blocks.Math.BooleanToInteger to convert your Boolean-array to an Integer-array with which you can calculate ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文