max() of more than 2 values

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

max() of more than 2 values

Peter Falke
Hi,

I would like to get the biggest element of a vector.
I tried this:


a=[1,3,5,8,1,2];


function maximum(a,i)=(i<len(a)-1)?max(a[i],maximum(a,i-1):0;


m=maximum(a,0);

echo(a);

echo(m);



But this gives me a syntax error in the function declaration.
Any pointers?

Thanks,

TakeItAndRun

--
[hidden email]

P.S. Falls meine E-Mail kürzer ausfällt als Dir angenehm ist:
Ich probiere gerade aus kurze Antworten statt gar keine Antworten zu schreiben.
Wenn Du gerne mehr lesen möchtest, dann lass es mich bitte wissen.

P.S. In case my e-mail is shorter than you enjoy:
I am currently trying short replies instead of no replies at all.
Please let me know, if you like to read more.

Enjoy!

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: max() of more than 2 values

nophead
You have a missing closing bracket. Should be: function maximum(a,i)=(i<len(a)-1)?max(a[i],maximum(a,i-1)):0;



On 22 April 2013 14:00, Peter Falke <[hidden email]> wrote:
Hi,

I would like to get the biggest element of a vector.
I tried this:


a=[1,3,5,8,1,2];


function maximum(a,i)=(i<len(a)-1)?max(a[i],maximum(a,i-1):0;


m=maximum(a,0);

echo(a);

echo(m);



But this gives me a syntax error in the function declaration.
Any pointers?

Thanks,

TakeItAndRun

--
[hidden email]

P.S. Falls meine E-Mail kürzer ausfällt als Dir angenehm ist:
Ich probiere gerade aus kurze Antworten statt gar keine Antworten zu schreiben.
Wenn Du gerne mehr lesen möchtest, dann lass es mich bitte wissen.

P.S. In case my e-mail is shorter than you enjoy:
I am currently trying short replies instead of no replies at all.
Please let me know, if you like to read more.

Enjoy!

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: max() of more than 2 values

nophead
Also should be i+1 I think, plus the 0 would cause problems with negative numbers and does look at the last element.

 function maximum(a, i = 0) = (i < len(a) - 1) ? max(a[i], maximum(a, i +1)) : a[i];

Works and doesn't require the zero to be passed explicitly.



On 22 April 2013 15:02, nop head <[hidden email]> wrote:
You have a missing closing bracket. Should be: function maximum(a,i)=(i<len(a)-1)?max(a[i],maximum(a,i-1)):0;



On 22 April 2013 14:00, Peter Falke <[hidden email]> wrote:
Hi,

I would like to get the biggest element of a vector.
I tried this:


a=[1,3,5,8,1,2];


function maximum(a,i)=(i<len(a)-1)?max(a[i],maximum(a,i-1):0;


m=maximum(a,0);

echo(a);

echo(m);



But this gives me a syntax error in the function declaration.
Any pointers?

Thanks,

TakeItAndRun

--
[hidden email]

P.S. Falls meine E-Mail kürzer ausfällt als Dir angenehm ist:
Ich probiere gerade aus kurze Antworten statt gar keine Antworten zu schreiben.
Wenn Du gerne mehr lesen möchtest, dann lass es mich bitte wissen.

P.S. In case my e-mail is shorter than you enjoy:
I am currently trying short replies instead of no replies at all.
Please let me know, if you like to read more.

Enjoy!

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566



_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: max() of more than 2 values

Peter Falke
Thanks nop head.

Using function with recursion is so cool.
But thinking about it still ties my head in a knot.

Greetings,

TakeItAndRun


On 22 April 2013 22:12, nop head <[hidden email]> wrote:
Also should be i+1 I think, plus the 0 would cause problems with negative numbers and does look at the last element.

 function maximum(a, i = 0) = (i < len(a) - 1) ? max(a[i], maximum(a, i +1)) : a[i];

Works and doesn't require the zero to be passed explicitly.



On 22 April 2013 15:02, nop head <[hidden email]> wrote:
You have a missing closing bracket. Should be: function maximum(a,i)=(i<len(a)-1)?max(a[i],maximum(a,i-1)):0;



On 22 April 2013 14:00, Peter Falke <[hidden email]> wrote:
Hi,

I would like to get the biggest element of a vector.
I tried this:


a=[1,3,5,8,1,2];


function maximum(a,i)=(i<len(a)-1)?max(a[i],maximum(a,i-1):0;


m=maximum(a,0);

echo(a);

echo(m);



But this gives me a syntax error in the function declaration.
Any pointers?

Thanks,

TakeItAndRun

--
[hidden email]

P.S. Falls meine E-Mail kürzer ausfällt als Dir angenehm ist:
Ich probiere gerade aus kurze Antworten statt gar keine Antworten zu schreiben.
Wenn Du gerne mehr lesen möchtest, dann lass es mich bitte wissen.

P.S. In case my e-mail is shorter than you enjoy:
I am currently trying short replies instead of no replies at all.
Please let me know, if you like to read more.

Enjoy!

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566



_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566



--
[hidden email]

P.S. Falls meine E-Mail kürzer ausfällt als Dir angenehm ist:
Ich probiere gerade aus kurze Antworten statt gar keine Antworten zu schreiben.
Wenn Du gerne mehr lesen möchtest, dann lass es mich bitte wissen.

P.S. In case my e-mail is shorter than you enjoy:
I am currently trying short replies instead of no replies at all.
Please let me know, if you like to read more.

Enjoy!

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: max() of more than 2 values

nophead
It would be a bit easier if OpenScad had list slicing

function maximum(a) =(len(a) > 1) ? max(a[i], maximum(a[1:])) : a[0];




On 22 April 2013 21:51, Peter Falke <[hidden email]> wrote:
Thanks nop head.

Using function with recursion is so cool.
But thinking about it still ties my head in a knot.

Greetings,

TakeItAndRun


On 22 April 2013 22:12, nop head <[hidden email]> wrote:
Also should be i+1 I think, plus the 0 would cause problems with negative numbers and does look at the last element.

 function maximum(a, i = 0) = (i < len(a) - 1) ? max(a[i], maximum(a, i +1)) : a[i];

Works and doesn't require the zero to be passed explicitly.



On 22 April 2013 15:02, nop head <[hidden email]> wrote:
You have a missing closing bracket. Should be: function maximum(a,i)=(i<len(a)-1)?max(a[i],maximum(a,i-1)):0;



On 22 April 2013 14:00, Peter Falke <[hidden email]> wrote:
Hi,

I would like to get the biggest element of a vector.
I tried this:


a=[1,3,5,8,1,2];


function maximum(a,i)=(i<len(a)-1)?max(a[i],maximum(a,i-1):0;


m=maximum(a,0);

echo(a);

echo(m);



But this gives me a syntax error in the function declaration.
Any pointers?

Thanks,

TakeItAndRun

--
[hidden email]

P.S. Falls meine E-Mail kürzer ausfällt als Dir angenehm ist:
Ich probiere gerade aus kurze Antworten statt gar keine Antworten zu schreiben.
Wenn Du gerne mehr lesen möchtest, dann lass es mich bitte wissen.

P.S. In case my e-mail is shorter than you enjoy:
I am currently trying short replies instead of no replies at all.
Please let me know, if you like to read more.

Enjoy!

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566



_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566



--
[hidden email]

P.S. Falls meine E-Mail kürzer ausfällt als Dir angenehm ist:
Ich probiere gerade aus kurze Antworten statt gar keine Antworten zu schreiben.
Wenn Du gerne mehr lesen möchtest, dann lass es mich bitte wissen.

P.S. In case my e-mail is shorter than you enjoy:
I am currently trying short replies instead of no replies at all.
Please let me know, if you like to read more.

Enjoy!

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: max() of more than 2 values

kintel
Administrator
On 2013-04-22, at 17:03 , nop head wrote:

> It would be a bit easier if OpenScad had list slicing
>
> function maximum(a) =(len(a) > 1) ? max(a[i], maximum(a[1:])) : a[0];
>
I think this list slicing syntax fits well into the OpenSCAD language. Perhaps we should clarify the whole concept of ranges and generalize it to have meaning for e.g. lists and strings?
Any opinions on that?

See this page for ideas: https://github.com/openscad/openscad/wiki/List-and-Array-operations 

 -Marius

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: max() of more than 2 values

nophead
I would do the slicing exactly the same as Python, including negative indices to denote indexing from the end. 

I don't think lists should be mutable in keeping with all variables, so list = [0,1,2,3]; list[1] = 4; should not be allowed.

list2 = list[:1] ~ [4] ~ list[2:];




On 22 April 2013 22:16, Marius Kintel <[hidden email]> wrote:
On 2013-04-22, at 17:03 , nop head wrote:

> It would be a bit easier if OpenScad had list slicing
>
> function maximum(a) =(len(a) > 1) ? max(a[i], maximum(a[1:])) : a[0];
>
I think this list slicing syntax fits well into the OpenSCAD language. Perhaps we should clarify the whole concept of ranges and generalize it to have meaning for e.g. lists and strings?
Any opinions on that?

See this page for ideas: https://github.com/openscad/openscad/wiki/List-and-Array-operations

 -Marius

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: max() of more than 2 values

Alan Cox
On Mon, 22 Apr 2013 22:39:42 +0100
nop head <[hidden email]> wrote:

> I would do the slicing exactly the same as Python, including negative
> indices to denote indexing from the end.
>
> I don't think lists should be mutable in keeping with all variables, so list
> = [0,1,2,3]; list[1] = 4; should not be allowed.
>
> list2 = list[:1] ~ [4] ~ list[2:];

I do wonder if for non programmers having the built in functions do
sensible things with vectors where it makes sense would also be helpful ?

ie max([1,2,3,4,5,6]);  [1,2,3,4,5,6] * 1.5  etc

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: max() of more than 2 values

nophead
Yes extending max and min would make things easier. The second example already works.


On 22 April 2013 22:52, Alan Cox <[hidden email]> wrote:
On Mon, 22 Apr 2013 22:39:42 +0100
nop head <[hidden email]> wrote:

> I would do the slicing exactly the same as Python, including negative
> indices to denote indexing from the end.
>
> I don't think lists should be mutable in keeping with all variables, so list
> = [0,1,2,3]; list[1] = 4; should not be allowed.
>
> list2 = list[:1] ~ [4] ~ list[2:];

I do wonder if for non programmers having the built in functions do
sensible things with vectors where it makes sense would also be helpful ?

ie max([1,2,3,4,5,6]);  [1,2,3,4,5,6] * 1.5  etc

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: max() of more than 2 values

donbright
OpenSCAD is linked with https://www.bountysource.com if you want to start a bounty. The bounties are linked to issues on github.


On Mon, Apr 22, 2013 at 4:59 PM, nop head <[hidden email]> wrote:
Yes extending max and min would make things easier. The second example already works.


On 22 April 2013 22:52, Alan Cox <[hidden email]> wrote:
On Mon, 22 Apr 2013 22:39:42 +0100
nop head <[hidden email]> wrote:

> I would do the slicing exactly the same as Python, including negative
> indices to denote indexing from the end.
>
> I don't think lists should be mutable in keeping with all variables, so list
> = [0,1,2,3]; list[1] = 4; should not be allowed.
>
> list2 = list[:1] ~ [4] ~ list[2:];

I do wonder if for non programmers having the built in functions do
sensible things with vectors where it makes sense would also be helpful ?

ie max([1,2,3,4,5,6]);  [1,2,3,4,5,6] * 1.5  etc

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566