double for()

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

double for()

roipoussiere
Hello again.

It's the first time I use declarative language, so there are some thing which I don't know how to do...
I would like to do something like that in Openscad :

n=0;
it = [4,2,0,1,5];
for(i = [0 : 5]) {
        for(j = [0 : it[i]) { // (it[i] is not always the same value)
                n = n+j; // This line is my problem : I can't do this :/
                echo(i, j, n); // I want to use 'n'
                ....
        }
}

I tried with assign, but I have not succeeded.
Maybe I could do this with a concat() function, containing the vector of vector to a big vector, but it's not implemented yet

Thanks,
Roipou
Reply | Threaded
Open this post in threaded view
|

Re: double for()

nophead
You have to write a recursive function to do that rather than iteration.


On 12 November 2013 19:30, roipoussiere <[hidden email]> wrote:
Hello again.

It's the first time I use declarative language, so there are some thing
which I don't know how to do...
I would like to do something like that in Openscad :

n=0;
it = [4,2,0,1,5];
for(i = [0 : 5]) {
        for(j = [0 : it[i]) { // (it[i] is not always the same value)
                n = n+j; // This line is my problem : I can't do this :/
                echo(i, j, n); // I want to use 'n'
                ....
        }
}

I tried with assign, but I have not succeeded.
Maybe I could do this with a concat() function, containing the vector of
vector to a big vector, but  it's not implemented yet
<https://github.com/openscad/openscad/wiki/List-and-Array-operations>

Thanks,
Roipou



--
View this message in context: http://forum.openscad.org/double-for-tp5932.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
_______________________________________________
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: double for()

Peter Falke
You need to write a function
function n(i,j,it)=
+(i<1)?1:0
+(i<2)?1:0
+(i<3)?1:0
+(i<4)?1:0
+(i<5)?1:0

... but much more complicated, also maybe using recursion.



2013/11/12 nop head <[hidden email]>
You have to write a recursive function to do that rather than iteration.


On 12 November 2013 19:30, roipoussiere <[hidden email]> wrote:
Hello again.

It's the first time I use declarative language, so there are some thing
which I don't know how to do...
I would like to do something like that in Openscad :

n=0;
it = [4,2,0,1,5];
for(i = [0 : 5]) {
        for(j = [0 : it[i]) { // (it[i] is not always the same value)
                n = n+j; // This line is my problem : I can't do this :/
                echo(i, j, n); // I want to use 'n'
                ....
        }
}

I tried with assign, but I have not succeeded.
Maybe I could do this with a concat() function, containing the vector of
vector to a big vector, but  it's not implemented yet
<https://github.com/openscad/openscad/wiki/List-and-Array-operations>

Thanks,
Roipou



--
View this message in context: http://forum.openscad.org/double-for-tp5932.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
_______________________________________________
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: double for()

MichaelAtOz
Administrator
In reply to this post by roipoussiere
two variable recursion or if the vector is small just pre-calculate a 2D vector (matrix) and use i,j to retrieve the value.
Admin - email* me if you need anything,
or if I've done something stupid...
* click on my MichaelAtOz label, there is a link to email me.

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!
Reply | Threaded
Open this post in threaded view
|

Re: double for()

devlaam
In reply to this post by roipoussiere
In my hack of openscad (2013.06 + hacks)  
  https://github.com/devlaam/openscad
you can do things like:

  function p(t) = for(i=[0:t],[i]);
  function q(t) = for(i=[0:t],[i,2*i,t]);

so that

  echo( p(5) ); //generates [[0], [1], [2], [3], [4], [5]]
  echo( q(5) ); //generates [[0, 0, 5], [1, 2, 5], [2, 4, 5], [3, 6, 5], [4, 8, 5], [5, 10, 5]]

So what also is possible is:

  it = [4,2,0,1,5];
  function v(n) = for(i=[0:5],[for(j=[0:it[i]],[i,j,n+j])]);

so that

echo (v(0));

gives

ECHO: [[[[0, 0, 0], [0, 1, 1], [0, 2, 2], [0, 3, 3], [0, 4, 4]]], [[[1, 0, 0], [1, 1, 1], [1, 2, 2]]], [[[2, 0, 0]]], [[[3, 0, 0], [3, 1, 1]]], [[[4, 0, 0], [4, 1, 1], [4, 2, 2], [4, 3, 3], [4, 4, 4], [4, 5, 5]]], [undef]]

(and a couple of warnings i still have to fix)

note that the last [undef] results from the
fact your vector it is one value short.

Would that be useful?

Ruud


On 12-11-13 20:30, roipoussiere wrote:

> Hello again.
>
> It's the first time I use declarative language, so there are some thing
> which I don't know how to do...
> I would like to do something like that in Openscad :
>
> n=0;
> it = [4,2,0,1,5];
> for(i = [0 : 5]) {
> for(j = [0 : it[i]) { // (it[i] is not always the same value)
> n = n+j; // This line is my problem : I can't do this :/
> echo(i, j, n); // I want to use 'n'
> ....
> }
> }
>
> I tried with assign, but I have not succeeded.
> Maybe I could do this with a concat() function, containing the vector of
> vector to a big vector, but  it's not implemented yet
> <https://github.com/openscad/openscad/wiki/List-and-Array-operations>
>
> Thanks,
> Roipou
>
>
>
> --
> View this message in context: http://forum.openscad.org/double-for-tp5932.html
> Sent from the OpenSCAD mailing list archive at Nabble.com.
> _______________________________________________
> 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: double for()

Whosawhatsis
I would definitely find that useful.

On Tuesday, November 12, 2013 at 2:21 PM, Ruud Vlaming wrote:

In my hack of openscad (2013.06 + hacks)
you can do things like:

function p(t) = for(i=[0:t],[i]);
function q(t) = for(i=[0:t],[i,2*i,t]);

so that

echo( p(5) ); //generates [[0], [1], [2], [3], [4], [5]]
echo( q(5) ); //generates [[0, 0, 5], [1, 2, 5], [2, 4, 5], [3, 6, 5], [4, 8, 5], [5, 10, 5]]

So what also is possible is:

it = [4,2,0,1,5];
function v(n) = for(i=[0:5],[for(j=[0:it[i]],[i,j,n+j])]);

so that

echo (v(0));

gives

ECHO: [[[[0, 0, 0], [0, 1, 1], [0, 2, 2], [0, 3, 3], [0, 4, 4]]], [[[1, 0, 0], [1, 1, 1], [1, 2, 2]]], [[[2, 0, 0]]], [[[3, 0, 0], [3, 1, 1]]], [[[4, 0, 0], [4, 1, 1], [4, 2, 2], [4, 3, 3], [4, 4, 4], [4, 5, 5]]], [undef]]

(and a couple of warnings i still have to fix)

note that the last [undef] results from the
fact your vector it is one value short.

Would that be useful?

Ruud


On 12-11-13 20:30, roipoussiere wrote:
Hello again.

It's the first time I use declarative language, so there are some thing
which I don't know how to do...
I would like to do something like that in Openscad :

n=0;
it = [4,2,0,1,5];
for(i = [0 : 5]) {
for(j = [0 : it[i]) { // (it[i] is not always the same value)
n = n+j; // This line is my problem : I can't do this :/
echo(i, j, n); // I want to use 'n'
....
}
}

I tried with assign, but I have not succeeded.
Maybe I could do this with a concat() function, containing the vector of
vector to a big vector, but it's not implemented yet

Thanks,
Roipou



--
Sent from the OpenSCAD mailing list archive at Nabble.com.
_______________________________________________
OpenSCAD mailing list

_______________________________________________
OpenSCAD mailing list


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

Re: double for()

tp3
Whosawhatsis wrote
I would definitely find that useful.

On Tuesday, November 12, 2013 at 2:21 PM, Ruud Vlaming wrote:

> In my hack of openscad (2013.06 + hacks)
> https://github.com/devlaam/openscad
> you can do things like:
>
> function p(t) = for(i=[0:t],[i]);
> function q(t) = for(i=[0:t],[i,2*i,t]);
>
The functionality looks quite useful. The syntax does break any expectations though. I guess it should be at least renamed.
-- Torsten
Reply | Threaded
Open this post in threaded view
|

Re: double for()

nophead

This is how to do it without changing the language:


n=0;

it = [4,2,0,1,5];


// sum of arithmentic progression

function ap(first, last) = (last - first + 1) * (first + last) / 2;


// sum of a list of aritmetic progressions

function sumap(i) = i >= 0 ? ap(0, it[i]) + sumap(i - 1) : 0;  


for(i = [0 : len(it) - 1]) {

        for(j = [0 : it[i]]) // (it[i] is not always the same value)

            assign(n = sumap(i - 1) + ap(0, j)) {

                echo(i, j, n); // I want to use 'n'

        }

}


It gives the following:
ECHO: 0, 0, 0
ECHO: 0, 1, 1
ECHO: 0, 2, 3
ECHO: 0, 3, 6
ECHO: 0, 4, 10
ECHO: 1, 0, 10
ECHO: 1, 1, 11
ECHO: 1, 2, 13
ECHO: 2, 0, 13
ECHO: 3, 0, 13
ECHO: 3, 1, 14
ECHO: 4, 0, 14
ECHO: 4, 1, 15
ECHO: 4, 2, 17
ECHO: 4, 3, 20
ECHO: 4, 4, 24
ECHO: 4, 5, 29






On 12 November 2013 22:51, tp3 <[hidden email]> wrote:
Whosawhatsis wrote
> I would definitely find that useful.
>
> On Tuesday, November 12, 2013 at 2:21 PM, Ruud Vlaming wrote:
>
>> In my hack of openscad (2013.06 + hacks)
>> https://github.com/devlaam/openscad
>> you can do things like:
>>
>> function p(t) = for(i=[0:t],[i]);
>> function q(t) = for(i=[0:t],[i,2*i,t]);
>>

The functionality looks quite useful. The syntax does break any expectations
though. I guess it should be at least renamed.




--
View this message in context: http://forum.openscad.org/double-for-tp5932p5938.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
_______________________________________________
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: double for()

nophead
A slightly simpler version that directly replaces iteration with recursion and doesn't rely on knowing what the sum of 1 to n is. This is the general way to do calculations like this in OpenScad avoiding the need for mutable variables.

it = [4,2,0,1,5];

// sum numbers up to n
function sum_n(n) = n > 0 ? n + sum_n(n - 1) : 0;

// sum of a list of aritmetic progressions
function sum_n_list(i) = i >= 0 ? sum_n(it[i]) + sum_n_list(i - 1) : 0;  

for(i = [0 : len(it) - 1]) {
    for(j = [0 : it[i]]) // (it[i] is not always the same value)
        assign(n = sum_n_list(i - 1) + sum_n(j)) {
            echo(i, j, n); // I want to use 'n'
        }
}



On 12 November 2013 23:05, nop head <[hidden email]> wrote:

This is how to do it without changing the language:


n=0;

it = [4,2,0,1,5];


// sum of arithmentic progression

function ap(first, last) = (last - first + 1) * (first + last) / 2;


// sum of a list of aritmetic progressions

function sumap(i) = i >= 0 ? ap(0, it[i]) + sumap(i - 1) : 0;  


for(i = [0 : len(it) - 1]) {

        for(j = [0 : it[i]]) // (it[i] is not always the same value)

            assign(n = sumap(i - 1) + ap(0, j)) {

                echo(i, j, n); // I want to use 'n'

        }

}


It gives the following:
ECHO: 0, 0, 0
ECHO: 0, 1, 1
ECHO: 0, 2, 3
ECHO: 0, 3, 6
ECHO: 0, 4, 10
ECHO: 1, 0, 10
ECHO: 1, 1, 11
ECHO: 1, 2, 13
ECHO: 2, 0, 13
ECHO: 3, 0, 13
ECHO: 3, 1, 14
ECHO: 4, 0, 14
ECHO: 4, 1, 15
ECHO: 4, 2, 17
ECHO: 4, 3, 20
ECHO: 4, 4, 24
ECHO: 4, 5, 29






On 12 November 2013 22:51, tp3 <[hidden email]> wrote:
Whosawhatsis wrote
> I would definitely find that useful.
>
> On Tuesday, November 12, 2013 at 2:21 PM, Ruud Vlaming wrote:
>
>> In my hack of openscad (2013.06 + hacks)
>> https://github.com/devlaam/openscad
>> you can do things like:
>>
>> function p(t) = for(i=[0:t],[i]);
>> function q(t) = for(i=[0:t],[i,2*i,t]);
>>

The functionality looks quite useful. The syntax does break any expectations
though. I guess it should be at least renamed.




--
View this message in context: http://forum.openscad.org/double-for-tp5932p5938.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
_______________________________________________
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: double for()

roipoussiere
Thanks a lot for all your answers ! You are very nice !!

So :
In my hack of openscad (2013.06 + hacks)  
  https://github.com/devlaam/openscad
you can do things like:

  function p(t) = for(i=[0:t],[i]);
  function q(t) = for(i=[0:t],[i,2*i,t]);
[...]


I find this function very very interesting and usefull !
However I would like to publish my creation with Customizer from Thingiverse, which run on the official OpenScad version. So I can't use your hack, but I hope your work will be implemented in next versions of OpenScad.

This is how to do it without changing the language:
[...]


Thanks !!
I also thought than recursive function was the right way !

However it's not exactly what I want, because 'n' must be the global index, not a sum.
I would like something like this :

ECHO: 0, 0, 0
ECHO: 0, 1, 1
ECHO: 0, 2, 2
ECHO: 0, 3, 3
ECHO: 0, 4, 4
ECHO: 1, 0, 5
ECHO: 1, 1, 6
ECHO: 1, 2, 7
ECHO: 2, 0, 8
ECHO: 3, 0, 9
ECHO: 3, 1, 10
ECHO: 4, 0, 11
ECHO: 4, 1, 12
ECHO: 4, 2, 13
ECHO: 4, 3, 14
ECHO: 4, 4, 15
ECHO: 4, 5, 16
Reply | Threaded
Open this post in threaded view
|

Re: double for()

nophead
So your original program should have n = n + 1 after the echo instead of n = n + j before the echo. That is much easier as all you need j + i + the sum of the vector up to i - 1.

it = [4,2,0,1,5];

// sum a vector up to position i
function sum_vec(v, i) = i >= 0 ? v[i] + sum_vec(v, i - 1) : 0;

for(i = [0 : len(it) - 1]) {
    for(j = [0 : it[i]]) // (it[i] is not always the same value)
        assign(n = sum_vec(it, i - 1) + j + i) {

            echo(i, j, n); // I want to use 'n'
        }
}

ECHO: 0, 0, 0

ECHO: 0, 1, 1

ECHO: 0, 2, 2

ECHO: 0, 3, 3

ECHO: 0, 4, 4

ECHO: 1, 0, 5

ECHO: 1, 1, 6

ECHO: 1, 2, 7

ECHO: 2, 0, 8

ECHO: 3, 0, 9

ECHO: 3, 1, 10

ECHO: 4, 0, 11

ECHO: 4, 1, 12

ECHO: 4, 2, 13

ECHO: 4, 3, 14

ECHO: 4, 4, 15

ECHO: 4, 5, 16




On 13 November 2013 23:10, roipoussiere <[hidden email]> wrote:
Thanks a lot for all your answers ! You are very nice !!

So :
/In my hack of openscad (2013.06 + hacks)
  https://github.com/devlaam/openscad
you can do things like:

  function p(t) = for(i=[0:t],[i]);
  function q(t) = for(i=[0:t],[i,2*i,t]);
[...]/

I find this function very very interesting and usefull !
However I would like to publish my creation with Customizer from
Thingiverse, which run on the official OpenScad version. So I can't use your
hack, but I hope your work will be implemented in next versions of OpenScad.

/This is how to do it without changing the language:
[...]/

Thanks !!
I also thought than recursive function was the right way !

However it's not exactly what I want, because 'n' must be the global index,
not a sum.
I would like something like this :

ECHO: 0, 0, 0
ECHO: 0, 1, 1
ECHO: 0, 2, 2
ECHO: 0, 3, 3
ECHO: 0, 4, 4
ECHO: 1, 0, 5
ECHO: 1, 1, 6
ECHO: 1, 2, 7
ECHO: 2, 0, 8
ECHO: 3, 0, 9
ECHO: 3, 1, 10
ECHO: 4, 0, 11
ECHO: 4, 1, 12
ECHO: 4, 2, 13
ECHO: 4, 3, 14
ECHO: 4, 4, 15
ECHO: 4, 5, 16



--
View this message in context: http://forum.openscad.org/double-for-tp5932p5946.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
_______________________________________________
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: double for()

devlaam
In reply to this post by roipoussiere
On 14-11-13 00:10, roipoussiere wrote:

> Thanks a lot for all your answers ! You are very nice !!
>
> So :
> /In my hack of openscad (2013.06 + hacks)
>    https://github.com/devlaam/openscad
> you can do things like:
>
>    function p(t) = for(i=[0:t],[i]);
>    function q(t) = for(i=[0:t],[i,2*i,t]);
> [...]/
>
> I find this function very very interesting and usefull !
> However I would like to publish my creation with Customizer from
> Thingiverse, which run on the official OpenScad version. So I can't use your
> hack, but I hope your work will be implemented in next versions of OpenScad.

That is perfectly understandable. I hope the same, but there is more
to it than just merge my code in. Marius is looking into this.
I made my extensions with the boundary condition that no changes
to existing language were allowed for i did not want to change
the parser. Therefore my syntax is not necessarily the one that
heavy openSCAD users may expect. So ... this might take a while.


Regards,
Ruud

BTW, this 'for' extension was written to quickly generate point
sequences. I found myself making them in a spreadsheet, much to
involved.
_______________________________________________
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: double for()

kintel
Administrator
In reply to this post by nophead
Been away for some days, but here's my simplified version of nophead's last solution:

 it = [4,2,0,1,5];

// sum a vector up to position i
function sum_vec(v, i) = i >= 0 ? v[i] + sum_vec(v, i - 1) : 0;

for(i = [0 : len(it) - 1], j = [0 : it[i]], n = sum_vec(it, i - 1) + j + i) {
  echo(i, j, n); // I want to use 'n'
}

Note that this requires OpenSCAD 2013.06, which isn't yet deployed on Thingiverse.
I'm talking to MakerBot in terms of getting it updated and they say they're working on it - it's not something I control though.

 -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: double for()

roipoussiere
Hello all ! Thanks a lot !!

So your original program should have n = n + 1 after the echo instead of n = n + j before the echo. That is much easier as all you need j + i + the sum of the vector up to i - 1.

Yes, it was an error, sorry.


That is perfectly understandable. I hope the same, but there is more
to it than just merge my code in. Marius is looking into this.
I made my extensions with the boundary condition that no changes
to existing language were allowed for i did not want to change
the parser. Therefore my syntax is not necessarily the one that
heavy openSCAD users may expect. So ... this might take a while.


Okay, so I will wait (and I look forward) ;-)

Been away for some days, but here's my simplified version of nophead's last solution
Hello Marius, thanks for your code !
The version 2013.06 seems very powerful :-) I will put your code in my project when Thingiverse will update openscad.

In fact I'm working on a little nesting library (very basic).

I'm impressed, this community is very nice and helpful !
I flattred the OpenScad page. I would like to give more, I don't know how.
When I will get more experience in OpenScad, I think I will teach some people how to use it. ;-)