ModelicaStandardLibrary icon indicating copy to clipboard operation
ModelicaStandardLibrary copied to clipboard

Use boolean array reductions

Open henrikt-ma opened this issue 3 years ago • 8 comments

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 avatar Dec 14 '21 23:12 henrikt-ma

@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?

AHaumer avatar Dec 15 '21 18:12 AHaumer

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?

AHaumer avatar Dec 15 '21 18:12 AHaumer

@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

henrikt-ma avatar Dec 15 '21 19:12 henrikt-ma

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.)

henrikt-ma avatar Dec 15 '21 19:12 henrikt-ma

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.

sjoelund avatar Dec 15 '21 19:12 sjoelund

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.

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 avatar Dec 15 '21 20:12 henrikt-ma

@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.

HansOlsson avatar Dec 16 '21 08:12 HansOlsson

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.

henrikt-ma avatar Jan 10 '22 19:01 henrikt-ma

@HansOlsson, I suppose Dymola 2023 has been released by now, so that we can proceed here?

henrikt-ma avatar Dec 12 '23 20:12 henrikt-ma

@HansOlsson @MartinOtter, can this approved PR be labeled with 4.1.0 milestone?

arunkumar-narasimhan avatar Jan 18 '24 05:01 arunkumar-narasimhan

I'd say, if ModelicaTest.Math.BooleanFunctions still succeeds it can be merged (with milestone set accordingly).

beutlich avatar Jan 18 '24 05:01 beutlich

@HansOlsson @MartinOtter, can this approved PR be labeled with 4.1.0 milestone?

Yes.

HansOlsson avatar Jan 18 '24 08:01 HansOlsson

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