discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

bug report: max(), min() and norm()

P
Parkinbot
Thu, Dec 17, 2015 2:40 PM

While trying to implement some min()/max()/norm() related stuff I stumbled
over the following buggy semantics.
I implemented the expected semantics for primitive functions:

A = [
[2, 5, 3, 4],
[1, 2, 3, 4],
[2, 4, 5, 7],
];

// Max
echo(max(A));        // [2, 5, 3, 4] ???
echo(max(max(A)));  // 5 ???
echo(Max(A));        // 7 expected
echo(Max(A, false)); // [5, 4,7] expected

echo(min(A));        // [2, 5, 3, 4] ???
echo(min(min(A)));  // 2 ???
echo(Min(A));        // 1 expected
echo(Min(A, false)); // [2, 1, 2] expected

echo(norm(A));      // undef + warning ???
echo(Norm(A));      // [7.34847, 5.47723, 9.69536] expected

function Max(A, flat=true) =
let (m =
len(A[0])>1?
[for(i=[0:len(A)-1]) Max(A[i])]:
max(A) )
flat?max(m):m;

function Min(A, flat=true) =
let (m =
len(A[0])>1?
[for(i=[0:len(A)-1]) Min(A[i])]:
min(A) )
flat?min(m):m;

function Norm(A) =
let (m =
len(A[0])>1?
[for(i=[0:len(A)-1]) Norm(A[i])]:
norm(A) )
m;

--
View this message in context: http://forum.openscad.org/bug-report-max-min-and-norm-tp15195.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

While trying to implement some min()/max()/norm() related stuff I stumbled over the following buggy semantics. I implemented the *expected* semantics for primitive functions: > A = [ > [2, 5, 3, 4], > [1, 2, 3, 4], > [2, 4, 5, 7], > ]; > > // Max > echo(max(A)); // [2, 5, 3, 4] *???* > echo(max(max(A))); // 5 *???* > echo(Max(A)); // 7 *expected* > echo(Max(A, false)); // [5, 4,7] *expected* > > echo(min(A)); // [2, 5, 3, 4] *???* > echo(min(min(A))); // 2 *???* > echo(Min(A)); // 1 *expected* > echo(Min(A, false)); // [2, 1, 2] *expected* > > echo(norm(A)); // undef + warning *???* > echo(Norm(A)); // [7.34847, 5.47723, 9.69536] *expected* > > function Max(A, flat=true) = > let (m = > len(A[0])>1? > [for(i=[0:len(A)-1]) Max(A[i])]: > max(A) ) > flat?max(m):m; > > function Min(A, flat=true) = > let (m = > len(A[0])>1? > [for(i=[0:len(A)-1]) Min(A[i])]: > min(A) ) > flat?min(m):m; > > function Norm(A) = > let (m = > len(A[0])>1? > [for(i=[0:len(A)-1]) Norm(A[i])]: > norm(A) ) > m; -- View this message in context: http://forum.openscad.org/bug-report-max-min-and-norm-tp15195.html Sent from the OpenSCAD mailing list archive at Nabble.com.
M
MichaelAtOz
Thu, Dec 17, 2015 9:59 PM

From the wiki, it is not designed for matrices.

max

Returns the maximum of the parameters. If a single vector is given as
parameter, returns the maximum element of that vector.

Parameters

max(n,n{,n}...)
max(vector)

<n> Two or more decimals <vector> Single vector of decimals (requires OpenSCAD version 2014.06 or later).

Usage Example:

max(3.0,5.0)
max(8.0,3.0,4.0,5.0)
max([8,3,4,5])

Results:

5
8
8

min

Returns the minimum of the parameters. If a single vector is given as
parameter, returns the minimum element of that vector.

Parameters

min(n,n{,n}...)
min(vector)

<n> Two or more decimals <vector> Single vector of decimals (requires OpenSCAD version 2014.06 or later).

Newly minted Admin - PM me if you need anything, or if I've done something stupid...

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above.

The TPP is no simple “trade agreement.”  Fight it! http://www.ourfairdeal.org/  time is running out!

View this message in context: http://forum.openscad.org/bug-report-max-min-and-norm-tp15195p15201.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

>From the wiki, it is not designed for matrices. max Returns the maximum of the parameters. If a single vector is given as parameter, returns the maximum element of that vector. Parameters max(n,n{,n}...) max(vector) <n> Two or more decimals <vector> Single vector of decimals (requires OpenSCAD version 2014.06 or later). Usage Example: max(3.0,5.0) max(8.0,3.0,4.0,5.0) max([8,3,4,5]) Results: 5 8 8 min Returns the minimum of the parameters. If a single vector is given as parameter, returns the minimum element of that vector. Parameters min(n,n{,n}...) min(vector) <n> Two or more decimals <vector> Single vector of decimals (requires OpenSCAD version 2014.06 or later). ----- Newly minted Admin - PM me if you need anything, or if I've done something stupid... Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above. The TPP is no simple “trade agreement.” Fight it! http://www.ourfairdeal.org/ time is running out! -- View this message in context: http://forum.openscad.org/bug-report-max-min-and-norm-tp15195p15201.html Sent from the OpenSCAD mailing list archive at Nabble.com.
N
Neon22
Fri, Dec 18, 2015 2:51 AM

I would interpret it slightly differently.
I.e. I woud lexpect the max of A to be one of A's entries.
I.e. echo(max(A));        // [2, 4, 5, 7] because its the one whose sum is
the max.
I.e. a max of each entry in the array A determines which entry is the max.

Likewise for max(max(A)) I would expect to see 7

So min(A) would be [1,2,3,4]
min(min(A)) would be 1

Its just convention and as nophead points out - its not defined in the
language - yet :)

--
View this message in context: http://forum.openscad.org/bug-report-max-min-and-norm-tp15195p15204.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

I would interpret it slightly differently. I.e. I woud lexpect the max of A to be one of A's entries. I.e. echo(max(A)); // [2, 4, 5, 7] because its the one whose sum is the max. I.e. a max of each entry in the array A determines which entry is the max. Likewise for max(max(A)) I would expect to see 7 So min(A) would be [1,2,3,4] min(min(A)) would be 1 Its just convention and as nophead points out - its not defined in the language - yet :) -- View this message in context: http://forum.openscad.org/bug-report-max-min-and-norm-tp15195p15204.html Sent from the OpenSCAD mailing list archive at Nabble.com.
P
Parkinbot
Fri, Dec 18, 2015 12:57 PM

@MichaelAtOz

Well, at least I would not expect it to give a false result, but a warning
and an undef.

@Neon22
actually this is, what I had expected. But in this case you run into these
problems:

  1. the client code using min/max has to know (and parse) the structure,
    which is not always granted or desireable.
  2. what will max/min do with nested Arrays of the form [.., [..., ],
    ...]?

So better give it a second argument with a default value.

I came over it, because I wanted to use it for bounding box calculations of
interpolated extrusion data, where the size of an object is not known in
advance.

--
View this message in context: http://forum.openscad.org/bug-report-max-min-and-norm-tp15195p15208.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

@MichaelAtOz Well, at least I would not expect it to give a false result, but a warning and an undef. @Neon22 actually this is, what I had expected. But in this case you run into these problems: 1. the client code using min/max has to know (and parse) the structure, which is not always granted or desireable. 2. what will max/min do with nested Arrays of the form [.., [..., ], ...]? So better give it a second argument with a default value. I came over it, because I wanted to use it for bounding box calculations of *interpolated* extrusion data, where the size of an object is not known in advance. -- View this message in context: http://forum.openscad.org/bug-report-max-min-and-norm-tp15195p15208.html Sent from the OpenSCAD mailing list archive at Nabble.com.
M
MichaelAtOz
Fri, Dec 18, 2015 8:03 PM

Parkinbot wrote

@MichaelAtOz
Well, at least I would not expect it to give a false result, but a warning
and an undef.

I agree, silent failure, which seems to be inherent in much of OpenSCAD, is
not one of its shining features.
It drove me nuts on occasion when I was starting to learn it.


Newly minted Admin - PM me if you need anything, or if I've done something stupid...

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above.

The TPP is no simple “trade agreement.”  Fight it! http://www.ourfairdeal.org/  time is running out!

View this message in context: http://forum.openscad.org/bug-report-max-min-and-norm-tp15195p15216.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

Parkinbot wrote > @MichaelAtOz > Well, at least I would not expect it to give a false result, but a warning > and an undef. I agree, silent failure, which seems to be inherent in much of OpenSCAD, is not one of its shining features. It drove me nuts on occasion when I was starting to learn it. ----- Newly minted Admin - PM me if you need anything, or if I've done something stupid... Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above. The TPP is no simple “trade agreement.” Fight it! http://www.ourfairdeal.org/ time is running out! -- View this message in context: http://forum.openscad.org/bug-report-max-min-and-norm-tp15195p15216.html Sent from the OpenSCAD mailing list archive at Nabble.com.