discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Re: [OpenSCAD] A = A + 1;

AG
Adrian Grajdeanu
Tue, Feb 3, 2015 8:14 PM

This was precisely my latest surprise too: no sum vector over array. Can be done with recursion, but yuck! Can't write like so
Sum(v,i=0) = {(i=len(v))? 0: v[i]+sum(v,i+1)}
Because that only works for vector of scalars. If you have vector of vectors you need a new function like above, but replace 0 with [0,0,0], or whatever the equivalent appropiate neutral element for addition. Or you can write it the ugly way:
Sum(v,i=0) = {(i=len(v)-1)? v[i]: v[i]+sum(v,i+1)}

(Syntax may be slightly wrong!)

-------- Original message --------
From: Joymaker kb@sparklight.com
Date:03/02/2015 14:06 (GMT-05:00)
To: discuss@lists.openscad.org
Cc:
Subject: Re: [OpenSCAD] A = A + 1;

My question falls right into this theme.  A few days ago I set out to write a
little function to compute the average of a vector. Thought everything would
be straightforward.

// average a vector
function avg(vec) {
sum = 0;
for (n=vec) sum = sum + n;
return sum / len(vec);
}

Surprise! This language doesn't have functions with curly braces, it has
functions with equal signs. Given that it has modules with curly braces,
this took me greatly by surprise. Now, as I read this thread, I see that the
issue goes much deeper than I realized.

So, please suggest to me how to write the avg function. PLEASE don't tell me
that recursion is the best approach; this feels about as pretty as using
pliers to do a screwdriver's job.

Suggestion on the side: I was surprised not to find sum() and avg() on the
list of built-in functions operating on vectors. I recommend adding them.
Surely they are trivial to implement!

Cheers,
Ken

This was precisely my latest surprise too: no sum vector over array. Can be done with recursion, but yuck! Can't write like so Sum(v,i=0) = {(i=len(v))? 0: v[i]+sum(v,i+1)} Because that only works for vector of scalars. If you have vector of vectors you need a new function like above, but replace 0 with [0,0,0], or whatever the equivalent appropiate neutral element for addition. Or you can write it the ugly way: Sum(v,i=0) = {(i=len(v)-1)? v[i]: v[i]+sum(v,i+1)} (Syntax may be slightly wrong!) -------- Original message -------- From: Joymaker <kb@sparklight.com> Date:03/02/2015 14:06 (GMT-05:00) To: discuss@lists.openscad.org Cc: Subject: Re: [OpenSCAD] A = A + 1; My question falls right into this theme. A few days ago I set out to write a little function to compute the average of a vector. Thought everything would be straightforward. // average a vector function avg(vec) { sum = 0; for (n=vec) sum = sum + n; return sum / len(vec); } Surprise! This language doesn't have functions with curly braces, it has functions with equal signs. Given that it has modules with curly braces, this took me greatly by surprise. Now, as I read this thread, I see that the issue goes much deeper than I realized. So, please suggest to me how to write the avg function. PLEASE don't tell me that recursion is the best approach; this feels about as pretty as using pliers to do a screwdriver's job. Suggestion on the side: I was surprised not to find sum() and avg() on the list of built-in functions operating on vectors. I recommend adding them. Surely they are trivial to implement! Cheers, Ken
M
MichaelAtOz
Tue, Feb 3, 2015 9:27 PM

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. This work is published globally via the internet. :) 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/

View this message in context: http://forum.openscad.org/A-A-1-tp11385p11419.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

----- 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. This work is published globally via the internet. :) 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/ -- View this message in context: http://forum.openscad.org/A-A-1-tp11385p11419.html Sent from the OpenSCAD mailing list archive at Nabble.com.