ModelicaStandardLibrary
ModelicaStandardLibrary copied to clipboard
Use boolean array reductions
Opening this based on recent comment by @AHaumer.
The point of the change is to take advantage of optimized tool implementations of Boolean array reductions.
@henrikt-ma Hmmm ... tried your solution with Dymola 2021x: Function input had incorrect type in the definition equation result = size(b, 1) > 0 and min(b) Argument number 1 in min(b) is of type Boolean, but it must be a subtype of Real. @HansOlsson Have min(Boolean) and max(Boolean) been implemented in a newer version of Dymola?
Just a less elegant solution but also a one-liner (maybe intuitive to read due to recursion):
output Boolean result = if size(b,1)>1 then b[1] and myAndTrue(b[2:end]) else b[1];
@HansOlsson Should this work in Dymola?
@henrikt-ma Hmmm ... tried your solution with Dymola 2021x: Function input had incorrect type in the definition equation result = size(b, 1) > 0 and min(b) Argument number 1 in min(b) is of type Boolean, but it must be a subtype of Real. @HansOlsson Have min(Boolean) and max(Boolean) been implemented in a newer version of Dymola?
It's valid at least as of Modelica 3.4: https://specification.modelica.org/maint/3.4/Ch10.html#reduction-expressions
Just a less elegant solution but also a one-liner (maybe intuitive to read due to recursion):
output Boolean result = if size(b,1)>1 then b[1] and myAndTrue(b[2:end]) else b[1];@HansOlsson Should this work in Dymola?
I don't think a recursive implementation has any clear advantage over the old algorithm with iteration. The point or the PR is to make use of the simplicity and efficiency of array reductions.
(By the way, if one should make a recursive implementation, I think that the base case should be the empty array.)
Yes, I agree that min(b) would be much nicer. It's a simple implementation and in the specification since 2017. If it's a problem that Dymola doesn't yet support it, wait a little while before merging until it supports it. Or test if min(e for e in b) works better.
Yes, I agree that
min(b)would be much nicer. It's a simple implementation and in the specification since 2017. If it's a problem that Dymola doesn't yet support it, wait a little while before merging until it supports it. Or test ifmin(e for e in b)works better.
Of the two alternatives, I'd prefer waiting for them to get the support in place. I see a clear risk of the reduction with iterator not being implemented as efficiently as the variant that looks like a function call.
@henrikt-ma Hmmm ... tried your solution with Dymola 2021x: Function input had incorrect type in the definition equation result = size(b, 1) > 0 and min(b) Argument number 1 in min(b) is of type Boolean, but it must be a subtype of Real. @HansOlsson Have min(Boolean) and max(Boolean) been implemented in a newer version of Dymola?
It will be fully implemented in Dymola 2023. I don't understand why it was missed earlier.
OK from my side. But I am not sure if it actually is min(Boolean array) is more readable than the explicit algorithm.
The motivation is twofold: Tools may have efficient implementations for the min-reduction, and the functional style definition makes the function easily inlineable.
@HansOlsson, I suppose Dymola 2023 has been released by now, so that we can proceed here?
@HansOlsson @MartinOtter, can this approved PR be labeled with 4.1.0 milestone?
I'd say, if ModelicaTest.Math.BooleanFunctions still succeeds it can be merged (with milestone set accordingly).
@HansOlsson @MartinOtter, can this approved PR be labeled with 4.1.0 milestone?
Yes.
I'd say, if ModelicaTest.Math.BooleanFunctions still succeeds it can be merged (with milestone set accordingly).
yes the test still succeeds , and the reference files are matching