使用布尔数组进行数组索引

Array indexing with boolean array

给定一个实数数组(例如myArray)和一个布尔数组(例如myMask),我想要:

确实有效

model BooleanIndexing
  parameter Boolean myMask[3] = {false, true, true};
  parameter Boolean myMask_negated[3] = {true, false, false};
  Real myArray[3];
  parameter Real myValueTrue = 3.0;
  parameter Real myValueFalse = 5.0;
equation
  myArray[Modelica.Math.BooleanVectors.index(myMask)] = fill(myValueTrue, Modelica.Math.BooleanVectors.countTrue(myMask));
  myArray[Modelica.Math.BooleanVectors.index(myMask_negated)] = fill(myValueFalse, Modelica.Math.BooleanVectors.countTrue(myMask_negated));
end BooleanIndexing;

但这不会

model BooleanIndexing
  parameter Boolean myMask[3] = {false, true, true};
  parameter Boolean myMask_negated[3] = not myMask;
  Real myArray[3];
  parameter Real myValueTrue = 3.0;
  parameter Real myValueFalse = 5.0;
equation
  myArray[Modelica.Math.BooleanVectors.index(myMask)] = fill(myValueTrue, Modelica.Math.BooleanVectors.countTrue(myMask));
  myArray[Modelica.Math.BooleanVectors.index(myMask_negated)] = fill(myValueFalse, Modelica.Math.BooleanVectors.countTrue(myMask_negated));
end BooleanIndexing;

唯一不同的是我如何初始化 myMask_negated

OpenModelica 中的错误是:

[BooleanIndexing: 9:3-9:139]: Illegal subscript Modelica.Math.BooleanVectors.index({myMask_negated[1], myMask_negated[2], myMask_negated[3]}) for dimensions 3 in component myArray[Modelica.Math.BooleanVectors.index(myMask_negated)].

[BooleanIndexing: 9:3-9:139]: Variable myArray[Modelica.Math.BooleanVectors.index(myMask_negated)] not found in scope BooleanIndexing.

Error occurred while flattening model BooleanIndexing

并在 Dymola2018

Translation of BooleanIndexing:

Failed to expand myArray[Modelica.Math.BooleanVectors.index(myMask)].

Errors or failure to expand the equation:
myArray[Modelica.Math.BooleanVectors.index(myMask)] = fill(myValueTrue, Modelica.Math.BooleanVectors.countTrue(myMask));
Found in class BooleanIndexing, C:/workspace/modelica_vehicle/modelica_test/BooleanIndexing.mo at line 8.

Errors or failure to expand vector or matrix expressions.

Translation aborted.

直接布尔索引 myArray[myMask] 似乎不是这里的解决方案。 我不明白为什么他们会失败,以及是否有更优雅的解决方案。

你的两个版本都不能保证生成正确数量的方程。 myValueTruemyValueTrue_negated 都是参数,因此用户可以将向量的值更改为不互补的​​值。

因此,我建议设置

final parameter Boolean myMask_negated[3] = not myMask;

但这在 Open Modelica 和 Dymola 中也不起作用。

因此,我建议删除 myMask_negated 并改用 for 循环。 有两个单独的:

model BooleanIndexing
  parameter Boolean myMask[3] = {false, true, true};
  Real myArray[3];
  parameter Real myValueTrue = 3.0;
  parameter Real myValueFalse = 5.0;
equation 
  for i in Modelica.Math.BooleanVectors.index(myMask) loop
    myArray[i] = myValueTrue;
  end for;

  for i in Modelica.Math.BooleanVectors.index(not myMask) loop
    myArray[i] = myValueFalse;
  end for;

end BooleanIndexing;

或使用数组构造函数的单个 for 循环,如下所示:

model BooleanIndexing2
  parameter Boolean myMask[3] = {false, true, true};
  Real myArray[3];
  parameter Real myValueTrue = 3.0;
  parameter Real myValueFalse = 5.0;
equation 

  myArray = {if value then myValueTrue else myValueFalse for value in myMask};

end BooleanIndexing2;