RW
Raymond West
Fri, Mar 24, 2023 3:03 PM
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.
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.
DP
David Phillip Oster
Fri, Mar 24, 2023 3:22 PM
// sum a vector
function sum(v,i) = i == 0 ? v[i] : v[i] + sum(v, i-1);
// return an array of successively larger items, adding wall to each
function incrementalSum(v,wall) = [for(i = [0:len(v)-1]) sum(v,i)+wall*i];
echo(incrementalSum([20,20,30,30], 2));
returns:
ECHO: [20, 42, 74, 106]
On Fri, Mar 24, 2023 at 8:03 AM Raymond West raywest@raywest.com 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
// sum a vector
function sum(v,i) = i == 0 ? v[i] : v[i] + sum(v, i-1);
// return an array of successively larger items, adding wall to each
function incrementalSum(v,wall) = [for(i = [0:len(v)-1]) sum(v,i)+wall*i];
echo(incrementalSum([20,20,30,30], 2));
returns:
ECHO: [20, 42, 74, 106]
On Fri, Mar 24, 2023 at 8:03 AM Raymond West <raywest@raywest.com> 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
>
SP
Sanjeev Prabhakar
Fri, Mar 24, 2023 5:49 PM
I think you are talking about cumsum function
Cumulative sum
I remember I wrote a function for the same in my library called
dependencies.scad
You can check there, if you want.
It's a simple function as I remember
On Fri, 24 Mar, 2023, 8:34 pm Raymond West, raywest@raywest.com 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
I think you are talking about cumsum function
Cumulative sum
I remember I wrote a function for the same in my library called
dependencies.scad
You can check there, if you want.
It's a simple function as I remember
On Fri, 24 Mar, 2023, 8:34 pm Raymond West, <raywest@raywest.com> 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
>
AM
Adrian Mariano
Fri, Mar 24, 2023 9:35 PM
I suggest writing cumsum() directly to compute a cumulative sum. The
version shown above is maybe OK, but it does call sum in a loop, which
means it's an O(N^2) algorithm for a linear task. You just need to save
the partial results as you go and you'll have the cumulative sum. Also
note that the full sum can be computed as a matrix product with a vector of
ones, which is non-recursive, and hence faster. You can of course also
use the cumsum() function from BOSL2.
On Fri, Mar 24, 2023 at 11:23 AM David Phillip Oster <
davidphilliposter@gmail.com> wrote:
// sum a vector
function sum(v,i) = i == 0 ? v[i] : v[i] + sum(v, i-1);
// return an array of successively larger items, adding wall to each
function incrementalSum(v,wall) = [for(i = [0:len(v)-1]) sum(v,i)+wall*i];
echo(incrementalSum([20,20,30,30], 2));
returns:
ECHO: [20, 42, 74, 106]
On Fri, Mar 24, 2023 at 8:03 AM Raymond West raywest@raywest.com 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
I suggest writing cumsum() directly to compute a cumulative sum. The
version shown above is maybe OK, but it does call sum in a loop, which
means it's an O(N^2) algorithm for a linear task. You just need to save
the partial results as you go and you'll have the cumulative sum. Also
note that the full sum can be computed as a matrix product with a vector of
ones, which is non-recursive, and hence faster. You can of course also
use the cumsum() function from BOSL2.
On Fri, Mar 24, 2023 at 11:23 AM David Phillip Oster <
davidphilliposter@gmail.com> wrote:
> // sum a vector
>
> function sum(v,i) = i == 0 ? v[i] : v[i] + sum(v, i-1);
>
>
> // return an array of successively larger items, adding wall to each
>
> function incrementalSum(v,wall) = [for(i = [0:len(v)-1]) sum(v,i)+wall*i];
>
>
> echo(incrementalSum([20,20,30,30], 2));
>
>
> returns:
>
> ECHO: [20, 42, 74, 106]
>
> On Fri, Mar 24, 2023 at 8:03 AM Raymond West <raywest@raywest.com> 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
>
CM
Curt McDowell
Mon, Mar 27, 2023 7:23 AM
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
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
SP
Sanjeev Prabhakar
Mon, Mar 27, 2023 8:06 AM
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
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
>
RW
Raymond West
Mon, Mar 27, 2023 2:11 PM
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
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
>
DP
David Phillip Oster
Mon, Mar 27, 2023 6:01 PM
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
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
>
AM
Adrian Mariano
Mon, Mar 27, 2023 8:37 PM
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
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
>
SP
Sanjeev Prabhakar
Tue, Mar 28, 2023 4:39 AM
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
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
>