discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

incremental summation through a list

SP
Sanjeev Prabhakar
Tue, Mar 28, 2023 4:50 AM

correction

function cumsum(a,s=a[0],s1=[ ],n=1)=
n==len(a)-1?s1:cumsum(a,s+a[n],concat(s1,s),n+1);

these are always very confusing to me

On Tue, 28 Mar 2023, 10:09 Sanjeev Prabhakar, sprabhakar2006@gmail.com
wrote:

this is definitely quadratic
i did not bother too much about the speed in this case as this was simpler
to write.

the other way to write the same function which is linear is as following:
function cumsum(a,s=0,s1=[ ],n=0)=
n==len(a)-1?s1:cumsum(a,s+a[n],concat(s1,s),n+1);

a=[ for(i=[0:1000])i];
echo(cumsum(a));

On Tue, 28 Mar 2023, 02:08 Adrian Mariano, avm4@cornell.edu wrote:

Summing a vector using the dot product is good---best way to compute sum
of a list of numbers in OpenSCAD.  But doing cumulative sum with a loop of
sum operations is bad---it turns a linear operation into a quadratic
operation.  Despite the fact that matrix operations are super fast in
OpenSCAD, the code posted above that does cumulative sums via a loop over
dot products is slower than the recursive cumulative sum approach once
N>9.

On Mon, Mar 27, 2023 at 2:01 PM David Phillip Oster <
davidphilliposter@gmail.com> wrote:

I was impressed by the idea of summing a vector by doing the dot product
of the vector and an all-ones vector. Very clever, and it avoids the
recursion.

On Mon, Mar 27, 2023 at 7:11 AM Raymond West raywest@raywest.com
wrote:

Thanks for the info.

I'm using

function sumvector(v,i) = i == 0 ? v[i] : v[i] + sumvector(v, i-1);

function incrementalSum(v,wall) = [for(i = [0:len(v)-1])
sumvector(v,i)+wall*i];

which is as David posted earlier. It works fine for the relatively
short lists  I'm using.

On 27/03/2023 09:06, Sanjeev Prabhakar wrote:

cumulative sum calculation for a list
a=[1,2,3,4,5,6,7,8,9];
cs=[ for(i=[0:len(a)-1]) a*[ for (j=[0:len(a)-1]) j<=i?1:0]];
echo(cs);

On Mon, 27 Mar 2023, 12:54 Curt McDowell, maker@fishlet.com wrote:

The general answer is to use a recursive function to implement a loop.
Functional programming is quite painful and is a bane of OpenSCAD. As in,
experienced developers need help to express what should be a simple "for"
loop. Forget about nested ones with interrelated loop boundaries and
variable dependencies. OpenSCAD survives because it has workarounds like
for() inside modules and list comprehensions.

I get why purists like FP, but as the quote goes: it makes difficult
things easy, and easy things very difficult.

walls = function(pockets, wall)
let (loop = function(pockets, wall, ans, i, sum)
i >= len(pockets) ? ans :
let (next = sum + pockets[i] + wall)
loop(pockets, wall, concat(ans, [next]), i +
1, next))
loop(pockets, wall, [], 0, 0);

echo(walls([20, 20, 30, 30], 2));

Regards,
Curt
On 3/24/2023 8:03 AM, Raymond West wrote:

I guess there is a succinct general answer, but i can't discover it.

Given a list (describing the width of pockets within a drawer, say,)
and a wall thickness for each pocket, I want to be able to locate
(translate) the actual position of the walls.

e.g.  list of pocket widths  list[20,20,30,30] and wall =2,  I want
to derive the values 22, 44,7 6, 108 to be generated.


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

correction function cumsum(a,s=a[0],s1=[ ],n=1)= n==len(a)-1?s1:cumsum(a,s+a[n],concat(s1,s),n+1); these are always very confusing to me On Tue, 28 Mar 2023, 10:09 Sanjeev Prabhakar, <sprabhakar2006@gmail.com> wrote: > this is definitely quadratic > i did not bother too much about the speed in this case as this was simpler > to write. > > the other way to write the same function which is linear is as following: > function cumsum(a,s=0,s1=[ ],n=0)= > n==len(a)-1?s1:cumsum(a,s+a[n],concat(s1,s),n+1); > > a=[ for(i=[0:1000])i]; > echo(cumsum(a)); > > On Tue, 28 Mar 2023, 02:08 Adrian Mariano, <avm4@cornell.edu> wrote: > >> Summing a vector using the dot product is good---best way to compute sum >> of a list of numbers in OpenSCAD. But doing cumulative sum with a loop of >> sum operations is bad---it turns a linear operation into a quadratic >> operation. Despite the fact that matrix operations are super fast in >> OpenSCAD, the code posted above that does cumulative sums via a loop over >> dot products is slower than the recursive cumulative sum approach once >> N>9. >> >> On Mon, Mar 27, 2023 at 2:01 PM David Phillip Oster < >> davidphilliposter@gmail.com> wrote: >> >>> I was impressed by the idea of summing a vector by doing the dot product >>> of the vector and an all-ones vector. Very clever, and it avoids the >>> recursion. >>> >>> On Mon, Mar 27, 2023 at 7:11 AM Raymond West <raywest@raywest.com> >>> wrote: >>> >>>> Thanks for the info. >>>> >>>> I'm using >>>> >>>> function sumvector(v,i) = i == 0 ? v[i] : v[i] + sumvector(v, i-1); >>>> >>>> function incrementalSum(v,wall) = [for(i = [0:len(v)-1]) >>>> sumvector(v,i)+wall*i]; >>>> >>>> which is as David posted earlier. It works fine for the relatively >>>> short lists I'm using. >>>> >>>> >>>> On 27/03/2023 09:06, Sanjeev Prabhakar wrote: >>>> >>>> cumulative sum calculation for a list >>>> a=[1,2,3,4,5,6,7,8,9]; >>>> cs=[ for(i=[0:len(a)-1]) a*[ for (j=[0:len(a)-1]) j<=i?1:0]]; >>>> echo(cs); >>>> >>>> >>>> On Mon, 27 Mar 2023, 12:54 Curt McDowell, <maker@fishlet.com> wrote: >>>> >>>>> The general answer is to use a recursive function to implement a loop. >>>>> Functional programming is quite painful and is a bane of OpenSCAD. As in, >>>>> experienced developers need help to express what should be a simple "for" >>>>> loop. Forget about nested ones with interrelated loop boundaries and >>>>> variable dependencies. OpenSCAD survives because it has workarounds like >>>>> for() inside modules and list comprehensions. >>>>> >>>>> I get why purists like FP, but as the quote goes: it makes difficult >>>>> things easy, and easy things very difficult. >>>>> >>>>> walls = function(pockets, wall) >>>>> let (loop = function(pockets, wall, ans, i, sum) >>>>> i >= len(pockets) ? ans : >>>>> let (next = sum + pockets[i] + wall) >>>>> loop(pockets, wall, concat(ans, [next]), i + >>>>> 1, next)) >>>>> loop(pockets, wall, [], 0, 0); >>>>> >>>>> echo(walls([20, 20, 30, 30], 2)); >>>>> >>>>> Regards, >>>>> Curt >>>>> On 3/24/2023 8:03 AM, Raymond West wrote: >>>>> >>>>> I guess there is a succinct general answer, but i can't discover it. >>>>> >>>>> Given a list (describing the width of pockets within a drawer, say,) >>>>> and a wall thickness for each pocket, I want to be able to locate >>>>> (translate) the actual position of the walls. >>>>> >>>>> e.g. list of pocket widths list[20,20,30,30] and wall =2, I want >>>>> to derive the values 22, 44,7 6, 108 to be generated. >>>>> _______________________________________________ >>>>> OpenSCAD mailing list >>>>> To unsubscribe send an email to discuss-leave@lists.openscad.org >>>>> >>>>> _______________________________________________ >>>>> OpenSCAD mailing list >>>>> To unsubscribe send an email to discuss-leave@lists.openscad.org >>>>> >>>> _______________________________________________ >>>> OpenSCAD mailing list >>>> To unsubscribe send an email to discuss-leave@lists.openscad.org >>>> >>> _______________________________________________ >>> OpenSCAD mailing list >>> To unsubscribe send an email to discuss-leave@lists.openscad.org >>> >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to discuss-leave@lists.openscad.org >> >
SP
Sanjeev Prabhakar
Tue, Mar 28, 2023 3:11 PM

unable to get over it!
This is the final one. I worked on openscad after a long time.

function cumsum(a,s=a[0],s1=[ ],n=1)=
n==len(a)?concat(s1,s):cumsum(a,s+a[n],concat(s1,s),n+1);

function sum(a,s=a[0],n=1)= n==len(a)?s:sum(a,s+a[n],n+1);

a=[for(i=[0:10])i];
echo(cumsum(a),sum(a));

On Tue, 28 Mar 2023 at 10:20, Sanjeev Prabhakar sprabhakar2006@gmail.com
wrote:

correction

function cumsum(a,s=a[0],s1=[ ],n=1)=
n==len(a)-1?s1:cumsum(a,s+a[n],concat(s1,s),n+1);

these are always very confusing to me

On Tue, 28 Mar 2023, 10:09 Sanjeev Prabhakar, sprabhakar2006@gmail.com
wrote:

this is definitely quadratic
i did not bother too much about the speed in this case as this was
simpler to write.

the other way to write the same function which is linear is as following:
function cumsum(a,s=0,s1=[ ],n=0)=
n==len(a)-1?s1:cumsum(a,s+a[n],concat(s1,s),n+1);

a=[ for(i=[0:1000])i];
echo(cumsum(a));

On Tue, 28 Mar 2023, 02:08 Adrian Mariano, avm4@cornell.edu wrote:

Summing a vector using the dot product is good---best way to compute sum
of a list of numbers in OpenSCAD.  But doing cumulative sum with a loop of
sum operations is bad---it turns a linear operation into a quadratic
operation.  Despite the fact that matrix operations are super fast in
OpenSCAD, the code posted above that does cumulative sums via a loop over
dot products is slower than the recursive cumulative sum approach once
N>9.

On Mon, Mar 27, 2023 at 2:01 PM David Phillip Oster <
davidphilliposter@gmail.com> wrote:

I was impressed by the idea of summing a vector by doing the dot
product of the vector and an all-ones vector. Very clever, and it avoids
the recursion.

On Mon, Mar 27, 2023 at 7:11 AM Raymond West raywest@raywest.com
wrote:

Thanks for the info.

I'm using

function sumvector(v,i) = i == 0 ? v[i] : v[i] + sumvector(v, i-1);

function incrementalSum(v,wall) = [for(i = [0:len(v)-1])
sumvector(v,i)+wall*i];

which is as David posted earlier. It works fine for the relatively
short lists  I'm using.

On 27/03/2023 09:06, Sanjeev Prabhakar wrote:

cumulative sum calculation for a list
a=[1,2,3,4,5,6,7,8,9];
cs=[ for(i=[0:len(a)-1]) a*[ for (j=[0:len(a)-1]) j<=i?1:0]];
echo(cs);

On Mon, 27 Mar 2023, 12:54 Curt McDowell, maker@fishlet.com wrote:

The general answer is to use a recursive function to implement a
loop. Functional programming is quite painful and is a bane of OpenSCAD. As
in, experienced developers need help to express what should be a simple
"for" loop. Forget about nested ones with interrelated loop boundaries and
variable dependencies. OpenSCAD survives because it has workarounds like
for() inside modules and list comprehensions.

I get why purists like FP, but as the quote goes: it makes difficult
things easy, and easy things very difficult.

walls = function(pockets, wall)
let (loop = function(pockets, wall, ans, i, sum)
i >= len(pockets) ? ans :
let (next = sum + pockets[i] + wall)
loop(pockets, wall, concat(ans, [next]), i +
1, next))
loop(pockets, wall, [], 0, 0);

echo(walls([20, 20, 30, 30], 2));

Regards,
Curt
On 3/24/2023 8:03 AM, Raymond West wrote:

I guess there is a succinct general answer, but i can't discover it.

Given a list (describing the width of pockets within a drawer, say,)
and a wall thickness for each pocket, I want to be able to locate
(translate) the actual position of the walls.

e.g.  list of pocket widths  list[20,20,30,30] and wall =2,  I want
to derive the values 22, 44,7 6, 108 to be generated.


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

unable to get over it! This is the final one. I worked on openscad after a long time. function cumsum(a,s=a[0],s1=[ ],n=1)= n==len(a)?concat(s1,s):cumsum(a,s+a[n],concat(s1,s),n+1); function sum(a,s=a[0],n=1)= n==len(a)?s:sum(a,s+a[n],n+1); a=[for(i=[0:10])i]; echo(cumsum(a),sum(a)); On Tue, 28 Mar 2023 at 10:20, Sanjeev Prabhakar <sprabhakar2006@gmail.com> wrote: > correction > > function cumsum(a,s=a[0],s1=[ ],n=1)= > n==len(a)-1?s1:cumsum(a,s+a[n],concat(s1,s),n+1); > > these are always very confusing to me > > On Tue, 28 Mar 2023, 10:09 Sanjeev Prabhakar, <sprabhakar2006@gmail.com> > wrote: > >> this is definitely quadratic >> i did not bother too much about the speed in this case as this was >> simpler to write. >> >> the other way to write the same function which is linear is as following: >> function cumsum(a,s=0,s1=[ ],n=0)= >> n==len(a)-1?s1:cumsum(a,s+a[n],concat(s1,s),n+1); >> >> a=[ for(i=[0:1000])i]; >> echo(cumsum(a)); >> >> On Tue, 28 Mar 2023, 02:08 Adrian Mariano, <avm4@cornell.edu> wrote: >> >>> Summing a vector using the dot product is good---best way to compute sum >>> of a list of numbers in OpenSCAD. But doing cumulative sum with a loop of >>> sum operations is bad---it turns a linear operation into a quadratic >>> operation. Despite the fact that matrix operations are super fast in >>> OpenSCAD, the code posted above that does cumulative sums via a loop over >>> dot products is slower than the recursive cumulative sum approach once >>> N>9. >>> >>> On Mon, Mar 27, 2023 at 2:01 PM David Phillip Oster < >>> davidphilliposter@gmail.com> wrote: >>> >>>> I was impressed by the idea of summing a vector by doing the dot >>>> product of the vector and an all-ones vector. Very clever, and it avoids >>>> the recursion. >>>> >>>> On Mon, Mar 27, 2023 at 7:11 AM Raymond West <raywest@raywest.com> >>>> wrote: >>>> >>>>> Thanks for the info. >>>>> >>>>> I'm using >>>>> >>>>> function sumvector(v,i) = i == 0 ? v[i] : v[i] + sumvector(v, i-1); >>>>> >>>>> function incrementalSum(v,wall) = [for(i = [0:len(v)-1]) >>>>> sumvector(v,i)+wall*i]; >>>>> >>>>> which is as David posted earlier. It works fine for the relatively >>>>> short lists I'm using. >>>>> >>>>> >>>>> On 27/03/2023 09:06, Sanjeev Prabhakar wrote: >>>>> >>>>> cumulative sum calculation for a list >>>>> a=[1,2,3,4,5,6,7,8,9]; >>>>> cs=[ for(i=[0:len(a)-1]) a*[ for (j=[0:len(a)-1]) j<=i?1:0]]; >>>>> echo(cs); >>>>> >>>>> >>>>> On Mon, 27 Mar 2023, 12:54 Curt McDowell, <maker@fishlet.com> wrote: >>>>> >>>>>> The general answer is to use a recursive function to implement a >>>>>> loop. Functional programming is quite painful and is a bane of OpenSCAD. As >>>>>> in, experienced developers need help to express what should be a simple >>>>>> "for" loop. Forget about nested ones with interrelated loop boundaries and >>>>>> variable dependencies. OpenSCAD survives because it has workarounds like >>>>>> for() inside modules and list comprehensions. >>>>>> >>>>>> I get why purists like FP, but as the quote goes: it makes difficult >>>>>> things easy, and easy things very difficult. >>>>>> >>>>>> walls = function(pockets, wall) >>>>>> let (loop = function(pockets, wall, ans, i, sum) >>>>>> i >= len(pockets) ? ans : >>>>>> let (next = sum + pockets[i] + wall) >>>>>> loop(pockets, wall, concat(ans, [next]), i + >>>>>> 1, next)) >>>>>> loop(pockets, wall, [], 0, 0); >>>>>> >>>>>> echo(walls([20, 20, 30, 30], 2)); >>>>>> >>>>>> Regards, >>>>>> Curt >>>>>> On 3/24/2023 8:03 AM, Raymond West wrote: >>>>>> >>>>>> I guess there is a succinct general answer, but i can't discover it. >>>>>> >>>>>> Given a list (describing the width of pockets within a drawer, say,) >>>>>> and a wall thickness for each pocket, I want to be able to locate >>>>>> (translate) the actual position of the walls. >>>>>> >>>>>> e.g. list of pocket widths list[20,20,30,30] and wall =2, I want >>>>>> to derive the values 22, 44,7 6, 108 to be generated. >>>>>> _______________________________________________ >>>>>> OpenSCAD mailing list >>>>>> To unsubscribe send an email to discuss-leave@lists.openscad.org >>>>>> >>>>>> _______________________________________________ >>>>>> OpenSCAD mailing list >>>>>> To unsubscribe send an email to discuss-leave@lists.openscad.org >>>>>> >>>>> _______________________________________________ >>>>> OpenSCAD mailing list >>>>> To unsubscribe send an email to discuss-leave@lists.openscad.org >>>>> >>>> _______________________________________________ >>>> OpenSCAD mailing list >>>> To unsubscribe send an email to discuss-leave@lists.openscad.org >>>> >>> _______________________________________________ >>> OpenSCAD mailing list >>> To unsubscribe send an email to discuss-leave@lists.openscad.org >>> >>
SP
Sanjeev Prabhakar
Tue, Mar 28, 2023 3:32 PM

function cumsum(a,s=0,s1=[ ],n=0)=
n==len(a)?concat(s1,s):cumsum(a,s+a[n],n>0?concat(s1,s):[],n+1);

function sum(a,s=0,n=0)= n==len(a)?s:sum(a,s+a[n],n+1);

a=[for(i=[0:10])i];
echo(cumsum(a),sum(a));
b=[1,3,2,5,7];
echo(cumsum(b));

On Tue, 28 Mar 2023 at 20:41, Sanjeev Prabhakar sprabhakar2006@gmail.com
wrote:

unable to get over it!
This is the final one. I worked on openscad after a long time.

function cumsum(a,s=a[0],s1=[ ],n=1)=
n==len(a)?concat(s1,s):cumsum(a,s+a[n],concat(s1,s),n+1);

function sum(a,s=a[0],n=1)= n==len(a)?s:sum(a,s+a[n],n+1);

a=[for(i=[0:10])i];
echo(cumsum(a),sum(a));

On Tue, 28 Mar 2023 at 10:20, Sanjeev Prabhakar sprabhakar2006@gmail.com
wrote:

correction

function cumsum(a,s=a[0],s1=[ ],n=1)=
n==len(a)-1?s1:cumsum(a,s+a[n],concat(s1,s),n+1);

these are always very confusing to me

On Tue, 28 Mar 2023, 10:09 Sanjeev Prabhakar, sprabhakar2006@gmail.com
wrote:

this is definitely quadratic
i did not bother too much about the speed in this case as this was
simpler to write.

the other way to write the same function which is linear is as following:
function cumsum(a,s=0,s1=[ ],n=0)=
n==len(a)-1?s1:cumsum(a,s+a[n],concat(s1,s),n+1);

a=[ for(i=[0:1000])i];
echo(cumsum(a));

On Tue, 28 Mar 2023, 02:08 Adrian Mariano, avm4@cornell.edu wrote:

Summing a vector using the dot product is good---best way to compute
sum of a list of numbers in OpenSCAD.  But doing cumulative sum with a loop
of sum operations is bad---it turns a linear operation into a quadratic
operation.  Despite the fact that matrix operations are super fast in
OpenSCAD, the code posted above that does cumulative sums via a loop over
dot products is slower than the recursive cumulative sum approach once
N>9.

On Mon, Mar 27, 2023 at 2:01 PM David Phillip Oster <
davidphilliposter@gmail.com> wrote:

I was impressed by the idea of summing a vector by doing the dot
product of the vector and an all-ones vector. Very clever, and it avoids
the recursion.

On Mon, Mar 27, 2023 at 7:11 AM Raymond West raywest@raywest.com
wrote:

Thanks for the info.

I'm using

function sumvector(v,i) = i == 0 ? v[i] : v[i] + sumvector(v, i-1);

function incrementalSum(v,wall) = [for(i = [0:len(v)-1])
sumvector(v,i)+wall*i];

which is as David posted earlier. It works fine for the relatively
short lists  I'm using.

On 27/03/2023 09:06, Sanjeev Prabhakar wrote:

cumulative sum calculation for a list
a=[1,2,3,4,5,6,7,8,9];
cs=[ for(i=[0:len(a)-1]) a*[ for (j=[0:len(a)-1]) j<=i?1:0]];
echo(cs);

On Mon, 27 Mar 2023, 12:54 Curt McDowell, maker@fishlet.com wrote:

The general answer is to use a recursive function to implement a
loop. Functional programming is quite painful and is a bane of OpenSCAD. As
in, experienced developers need help to express what should be a simple
"for" loop. Forget about nested ones with interrelated loop boundaries and
variable dependencies. OpenSCAD survives because it has workarounds like
for() inside modules and list comprehensions.

I get why purists like FP, but as the quote goes: it makes difficult
things easy, and easy things very difficult.

walls = function(pockets, wall)
let (loop = function(pockets, wall, ans, i, sum)
i >= len(pockets) ? ans :
let (next = sum + pockets[i] + wall)
loop(pockets, wall, concat(ans, [next]), i +
1, next))
loop(pockets, wall, [], 0, 0);

echo(walls([20, 20, 30, 30], 2));

Regards,
Curt
On 3/24/2023 8:03 AM, Raymond West wrote:

I guess there is a succinct general answer, but i can't discover it.

Given a list (describing the width of pockets within a drawer, say,)
and a wall thickness for each pocket, I want to be able to locate
(translate) the actual position of the walls.

e.g.  list of pocket widths  list[20,20,30,30] and wall =2,  I want
to derive the values 22, 44,7 6, 108 to be generated.


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

function cumsum(a,s=0,s1=[ ],n=0)= n==len(a)?concat(s1,s):cumsum(a,s+a[n],n>0?concat(s1,s):[],n+1); function sum(a,s=0,n=0)= n==len(a)?s:sum(a,s+a[n],n+1); a=[for(i=[0:10])i]; echo(cumsum(a),sum(a)); b=[1,3,2,5,7]; echo(cumsum(b)); On Tue, 28 Mar 2023 at 20:41, Sanjeev Prabhakar <sprabhakar2006@gmail.com> wrote: > unable to get over it! > This is the final one. I worked on openscad after a long time. > > function cumsum(a,s=a[0],s1=[ ],n=1)= > n==len(a)?concat(s1,s):cumsum(a,s+a[n],concat(s1,s),n+1); > > function sum(a,s=a[0],n=1)= n==len(a)?s:sum(a,s+a[n],n+1); > > a=[for(i=[0:10])i]; > echo(cumsum(a),sum(a)); > > > > On Tue, 28 Mar 2023 at 10:20, Sanjeev Prabhakar <sprabhakar2006@gmail.com> > wrote: > >> correction >> >> function cumsum(a,s=a[0],s1=[ ],n=1)= >> n==len(a)-1?s1:cumsum(a,s+a[n],concat(s1,s),n+1); >> >> these are always very confusing to me >> >> On Tue, 28 Mar 2023, 10:09 Sanjeev Prabhakar, <sprabhakar2006@gmail.com> >> wrote: >> >>> this is definitely quadratic >>> i did not bother too much about the speed in this case as this was >>> simpler to write. >>> >>> the other way to write the same function which is linear is as following: >>> function cumsum(a,s=0,s1=[ ],n=0)= >>> n==len(a)-1?s1:cumsum(a,s+a[n],concat(s1,s),n+1); >>> >>> a=[ for(i=[0:1000])i]; >>> echo(cumsum(a)); >>> >>> On Tue, 28 Mar 2023, 02:08 Adrian Mariano, <avm4@cornell.edu> wrote: >>> >>>> Summing a vector using the dot product is good---best way to compute >>>> sum of a list of numbers in OpenSCAD. But doing cumulative sum with a loop >>>> of sum operations is bad---it turns a linear operation into a quadratic >>>> operation. Despite the fact that matrix operations are super fast in >>>> OpenSCAD, the code posted above that does cumulative sums via a loop over >>>> dot products is slower than the recursive cumulative sum approach once >>>> N>9. >>>> >>>> On Mon, Mar 27, 2023 at 2:01 PM David Phillip Oster < >>>> davidphilliposter@gmail.com> wrote: >>>> >>>>> I was impressed by the idea of summing a vector by doing the dot >>>>> product of the vector and an all-ones vector. Very clever, and it avoids >>>>> the recursion. >>>>> >>>>> On Mon, Mar 27, 2023 at 7:11 AM Raymond West <raywest@raywest.com> >>>>> wrote: >>>>> >>>>>> Thanks for the info. >>>>>> >>>>>> I'm using >>>>>> >>>>>> function sumvector(v,i) = i == 0 ? v[i] : v[i] + sumvector(v, i-1); >>>>>> >>>>>> function incrementalSum(v,wall) = [for(i = [0:len(v)-1]) >>>>>> sumvector(v,i)+wall*i]; >>>>>> >>>>>> which is as David posted earlier. It works fine for the relatively >>>>>> short lists I'm using. >>>>>> >>>>>> >>>>>> On 27/03/2023 09:06, Sanjeev Prabhakar wrote: >>>>>> >>>>>> cumulative sum calculation for a list >>>>>> a=[1,2,3,4,5,6,7,8,9]; >>>>>> cs=[ for(i=[0:len(a)-1]) a*[ for (j=[0:len(a)-1]) j<=i?1:0]]; >>>>>> echo(cs); >>>>>> >>>>>> >>>>>> On Mon, 27 Mar 2023, 12:54 Curt McDowell, <maker@fishlet.com> wrote: >>>>>> >>>>>>> The general answer is to use a recursive function to implement a >>>>>>> loop. Functional programming is quite painful and is a bane of OpenSCAD. As >>>>>>> in, experienced developers need help to express what should be a simple >>>>>>> "for" loop. Forget about nested ones with interrelated loop boundaries and >>>>>>> variable dependencies. OpenSCAD survives because it has workarounds like >>>>>>> for() inside modules and list comprehensions. >>>>>>> >>>>>>> I get why purists like FP, but as the quote goes: it makes difficult >>>>>>> things easy, and easy things very difficult. >>>>>>> >>>>>>> walls = function(pockets, wall) >>>>>>> let (loop = function(pockets, wall, ans, i, sum) >>>>>>> i >= len(pockets) ? ans : >>>>>>> let (next = sum + pockets[i] + wall) >>>>>>> loop(pockets, wall, concat(ans, [next]), i + >>>>>>> 1, next)) >>>>>>> loop(pockets, wall, [], 0, 0); >>>>>>> >>>>>>> echo(walls([20, 20, 30, 30], 2)); >>>>>>> >>>>>>> Regards, >>>>>>> Curt >>>>>>> On 3/24/2023 8:03 AM, Raymond West wrote: >>>>>>> >>>>>>> I guess there is a succinct general answer, but i can't discover it. >>>>>>> >>>>>>> Given a list (describing the width of pockets within a drawer, say,) >>>>>>> and a wall thickness for each pocket, I want to be able to locate >>>>>>> (translate) the actual position of the walls. >>>>>>> >>>>>>> e.g. list of pocket widths list[20,20,30,30] and wall =2, I want >>>>>>> to derive the values 22, 44,7 6, 108 to be generated. >>>>>>> _______________________________________________ >>>>>>> OpenSCAD mailing list >>>>>>> To unsubscribe send an email to discuss-leave@lists.openscad.org >>>>>>> >>>>>>> _______________________________________________ >>>>>>> OpenSCAD mailing list >>>>>>> To unsubscribe send an email to discuss-leave@lists.openscad.org >>>>>>> >>>>>> _______________________________________________ >>>>>> OpenSCAD mailing list >>>>>> To unsubscribe send an email to discuss-leave@lists.openscad.org >>>>>> >>>>> _______________________________________________ >>>>> OpenSCAD mailing list >>>>> To unsubscribe send an email to discuss-leave@lists.openscad.org >>>>> >>>> _______________________________________________ >>>> OpenSCAD mailing list >>>> To unsubscribe send an email to discuss-leave@lists.openscad.org >>>> >>>