Modelica 中布尔值数组的 if 条件
如果这是一个“阅读手册”问题(我读了但找不到答案),我很抱歉。
我有一个布尔数组,我想测试其中是否有一个为真。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有类似于您在 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[:]).
这是一个有趣的问题。坦率地说,我不知道有任何内置功能可以执行此操作(尽管对此类功能的需求肯定是有效的)。
我们过去经常做的是编写名为“any”和“all”的实用函数,它们看起来像这样(未经测试,但你明白了):
这与你所做的类似,但使用数组推导式,然后将其封装在函数中。这允许您编写如下代码:
理想情况下,可以将这些函数添加到内置的“归约运算符”集中(例如 min 和 max),但是语言组在引入此类运算符方面往往有些保守,因为它们会污染命名空间并与现有代码产生潜在的冲突。
请注意,使用when 子句时事情会变得有点棘手。对于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):
This is similar to what you did but using array comprehensions and then encapsulating that in functions. This allows you to write code like:
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.
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).
Modelica 规范版本 4.3 第 10.3.4 节 允许布尔数组
v
作为min(v)
和max(v)
的参数。如果
v
的所有分量均为true
,则min(v)
给出true
、false
> 否则。如果
v
的所有分量均为false
,则max(v)
给出false
、true
> 否则。型号示例:
Section 10.3.4 of Modelica Specification Version 4.3 allows Boolean arrays
v
as arguments ofmin(v)
andmax(v)
.If all components of
v
aretrue
thenmin(v)
givestrue
,false
otherwise.If all components of
v
arefalse
thenmax(v)
givesfalse
,true
otherwise.Example model:
现在我找到了一个解决方法,但一定可以做得更好:
Now I found a workaround, but it must be possible to do it much nicer:
您可以使用 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 ...