Libraries & objects
About Archicad and BIMcloud libraries, their management and migration, objects and other library parts, etc.
SOLVED!

Select smallest value from an array

Durval
Enthusiast
Is it possible to use the MIN () command with an array?
I am trying to do something like the script in the image attached, but I get a strange error message (something like "incompatible arrow dimensions on line 8...", in Portuguese).
How could I get the smallest value in that array?
--- www.dtabach.com.br ---
AC 24 BR – MacBook Pro 2,9 GHz Intel Core i7 16GB RAM Mac OS 10.14
1 ACCEPTED SOLUTION

Accepted Solutions
Solution
Ralph Wessel
Mentor
min_value = somevalue[1]
FOR i = 2 TO VARDIM1(somevalue)
      min_value = MIN(min_value, somevalue)
NEXT i
Ralph Wessel BArch
Software Engineer Speckle Systems

View solution in original post

9 REPLIES 9
Jochen Suehlo
Moderator
I think you must write something like this:
min_value = 1000
FOR i = 1 TO VARDIM1(somevalue)
      min_value = MIN(min_value, somevalue)
NEXT i
Jochen Suehlo . AC12-27 . MAC OSX 14.4 . WIN11
GDL object creation: b-prisma.de
Barry Kelly
Moderator
I think I would try this.
check_min = 1000000	!!some value larger than your expected minimum

FOR i = 1 TO VARDIM1(somevalue)
      min_value = MIN(somevalue[1], somevalue)	!!always check against first array value

if min_value < check_min then
      check_min = min_value		!!reset check_min to min_value
endif
NEXT i

Barry.
One of the forum moderators.
Versions 6.5 to 27
i7-10700 @ 2.9Ghz, 32GB ram, GeForce RTX 2060 (6GB), Windows 10
Lenovo Thinkpad - i7-1270P 2.20 GHz, 32GB RAM, Nvidia T550, Windows 11
Solution
Ralph Wessel
Mentor
min_value = somevalue[1]
FOR i = 2 TO VARDIM1(somevalue)
      min_value = MIN(min_value, somevalue)
NEXT i
Ralph Wessel BArch
Software Engineer Speckle Systems
Durval
Enthusiast
I was trying to compare all the array values at once. But the idea of comparing just two values at a time within a loop is great and does what I need. Thanks!
--- www.dtabach.com.br ---
AC 24 BR – MacBook Pro 2,9 GHz Intel Core i7 16GB RAM Mac OS 10.14
Nader Belal
Mentor
@Durval

If you want to compareall array value at once, you will have to apply this approach, best if you can put it in a subroutine or even a macro


for __i = 1 to vardim1 (somevalues) step 1        !<-- step 1 is a redundancy to prevent errors in your code 
    put somevalues[__i]                    !<-- puts all you values' array in the memory stack
next __i

min_value = min  (get (nsp))         !<-- 1. Empties values from the stack to the min function
                                                            !<-- 2. funtion export lowest value to parameter

don't forget to check GDL code style
A good friend of mine have once told me that I´m so brute that I´m capable of creating a GDL script capable of creating GDLs.
Nader Belal
Mentor
To @Ralph , @Barry, & @Joachim ... question

1. In Python, your proposed methods are considered to be similar to "lazy function", while mine is considered "eager" ... what is the impact of each method on the performance ? (considering that this method is applied with in the same GSM file)
A good friend of mine have once told me that I´m so brute that I´m capable of creating a GDL script capable of creating GDLs.
Ralph Wessel
Mentor
Moonlight wrote:
1. In Python, your proposed methods are considered to be similar to "lazy function", while mine is considered "eager" ... what is the impact of each method on the performance ? (considering that this method is applied with in the same GSM file)
Comparing techniques between languages is not necessarily very helpful because they often vary too greatly in functionality and optimisation.

On a purely algorithmic basis, the method you propose is certainly far more resource hungry and likely to be slower. Both iterate through an array of values using a loop, so will be identical from that standpoint. The primary difference is that former makes a less-than comparison whereas the latter is pushing a copy onto the stack. I think it's highly likely that making a copy will be slower. And ultimately the min function at the end of the method you propose will also have to repeat the same action, iterating through all the values to seek the lowest. So it could take twice as long and additionally consume as much memory as the original array. I can't really see an upside to that.

But in most languages, you would expect a 'min' algorithm that would operate directly on the original values (which is what the OP was hoping for).
Ralph Wessel BArch
Software Engineer Speckle Systems
Nader Belal
Mentor
I understand your point of view about resources, which is true, but since the iteration will always be of a reduced number, I don't think that this will be of an issue.

What I'm asking, if there is a way to measure the performance of both methods?
A good friend of mine have once told me that I´m so brute that I´m capable of creating a GDL script capable of creating GDLs.
Ralph Wessel
Mentor
Moonlight wrote:
since the iteration will always be of a reduced number, I don't think that this will be of an issue.
How would it be able to iterate over fewer values?
Moonlight wrote:
What I'm asking, if there is a way to measure the performance of both methods?
Just make a function in an object to iterate over the method thousands of time when a parameter is set/changed, time numerous runs and then average.
Ralph Wessel BArch
Software Engineer Speckle Systems