discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Fwd: Re: Adding vectors of unequal length

SL
Steve Lelievre
Tue, Sep 12, 2023 1:44 AM

On 2023-09-08 10:43 a.m., nop head wrote:

<something>

I've lost track of the specific issue that was to be solved by extending
a shorter vector with elements that are set to zero, but it seems be
about coercing a bunch of 2D coordinates into 3D coordinates by giving
them a z coordinate of 0.

Doing it in user code involves no risk of existing programs breaking, no
workload for the developers, and is minimal effort for anyone who needs
that specific behavior. So, for example, and with apologies if someone
has already pointed this out,  a cast function based on a simple
generator can turn a vector of 2D points into 3D points:

function zIt(v) = [for (i = [0 : len(v) - 1]) [v[i].x, v[i].y, 0]];

2D = [[1, 1], [3, 4], [0, 1], [-1, -4]];

3D = zIt(2D);

echo(3D); // [[1, 1, 0], [3, 4, 0], [0, 1, 0], [-1, -4, 0]]

So my question: what is the specific case for a change to OpenSCAD's
behavior, rather than having the programmer solve it?

On 2023-09-08 10:43 a.m., nop head wrote: > <something> I've lost track of the specific issue that was to be solved by extending a shorter vector with elements that are set to zero, but it seems be about coercing a bunch of 2D coordinates into 3D coordinates by giving them a z coordinate of 0. Doing it in user code involves no risk of existing programs breaking, no workload for the developers, and is minimal effort for anyone who needs that specific behavior. So, for example, and with apologies if someone has already pointed this out,  a cast function based on a simple generator can turn a vector of 2D points into 3D points: function zIt(v) = [for (i = [0 : len(v) - 1]) [v[i].x, v[i].y, 0]]; 2D = [[1, 1], [3, 4], [0, 1], [-1, -4]]; 3D = zIt(2D); echo(3D); // [[1, 1, 0], [3, 4, 0], [0, 1, 0], [-1, -4, 0]] So my question: what is the specific case for a change to OpenSCAD's behavior, rather than having the programmer solve it?
AM
Adrian Mariano
Tue, Sep 12, 2023 2:07 AM

I believe the central concern is captured by this quote from nophead: "I
hate typing because nearly everything I type is a typo, so I prefer terse
and expressive languages."  Basically nophead has a preference for the
language to be terse, so no user space solution will suit because they all
involve more typing.  There is possibly a secondary concern that the
current behavior when one adds vectors of dissimilar length is unexpected
and could therefore be a source of confusion.  (But I feel compelled to
note that the only other language I know of that allows vectors of
dissimilar length to be added does exactly the same thing as OpenSCAD, so
apparently the OpenSCAD behavior makes sense to some people.)

Note also that it is faster to write the vector extension function using a
matrix multiply instead of a loop.

On Mon, Sep 11, 2023 at 9:45 PM Steve Lelievre <
steve.lelievre.canada@gmail.com> wrote:

On 2023-09-08 10:43 a.m., nop head wrote:

<something>

I've lost track of the specific issue that was to be solved by extending a
shorter vector with elements that are set to zero,  but it seems be about
coercing a bunch of 2D coordinates into 3D coordinates by giving them a z
coordinate of 0.

Doing it in user code involves no risk of existing programs breaking, no
workload for the developers, and is minimal effort for anyone who needs
that specific behavior. So, for example, and with apologies if someone has
already pointed this out,  a cast function based on a simple generator can
turn a vector of 2D points into 3D points:

function zIt(v) = [for (i = [0 : len(v) - 1]) [v[i].x, v[i].y, 0]];

2D = [[1, 1], [3, 4], [0, 1], [-1, -4]];

3D = zIt(2D);

echo(3D); // [[1, 1, 0], [3, 4, 0], [0, 1, 0], [-1, -4, 0]]

So my question: what is the specific case for a change to OpenSCAD's
behavior, rather than having the programmer solve it?


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

I believe the central concern is captured by this quote from nophead: "I hate typing because nearly everything I type is a typo, so I prefer terse and expressive languages." Basically nophead has a preference for the language to be terse, so no user space solution will suit because they all involve more typing. There is possibly a secondary concern that the current behavior when one adds vectors of dissimilar length is unexpected and could therefore be a source of confusion. (But I feel compelled to note that the only other language I know of that allows vectors of dissimilar length to be added does exactly the same thing as OpenSCAD, so apparently the OpenSCAD behavior makes sense to some people.) Note also that it is faster to write the vector extension function using a matrix multiply instead of a loop. On Mon, Sep 11, 2023 at 9:45 PM Steve Lelievre < steve.lelievre.canada@gmail.com> wrote: > > On 2023-09-08 10:43 a.m., nop head wrote: > > <something> > > > I've lost track of the specific issue that was to be solved by extending a > shorter vector with elements that are set to zero, but it seems be about > coercing a bunch of 2D coordinates into 3D coordinates by giving them a z > coordinate of 0. > > Doing it in user code involves no risk of existing programs breaking, no > workload for the developers, and is minimal effort for anyone who needs > that specific behavior. So, for example, and with apologies if someone has > already pointed this out, a cast function based on a simple generator can > turn a vector of 2D points into 3D points: > > function zIt(v) = [for (i = [0 : len(v) - 1]) [v[i].x, v[i].y, 0]]; > > 2D = [[1, 1], [3, 4], [0, 1], [-1, -4]]; > > 3D = zIt(2D); > > echo(3D); // [[1, 1, 0], [3, 4, 0], [0, 1, 0], [-1, -4, 0]] > > > So my question: what is the specific case for a change to OpenSCAD's > behavior, rather than having the programmer solve it? > > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
SL
Steve Lelievre
Tue, Sep 12, 2023 3:19 AM

On 2023-09-11 7:07 p.m., Adrian Mariano wrote:

Note also that it is faster to write the vector extension function
using a matrix multiply instead of a loop.

Oh, I did not know that; thanks for the tip.

After a little reading, I even worked out how to do it! For anyone else
who didn't immediately see how:

2D = [[1, 2], [2, 7], [3, 3], [4, 5], [99, 99]];
m = [[1, 0, 0], [0, 1, 0]];
echo(2D * m);

Steve

On 2023-09-11 7:07 p.m., Adrian Mariano wrote: > > Note also that it is faster to write the vector extension function > using a matrix multiply instead of a loop. Oh, I did not know that; thanks for the tip. After a little reading, I even worked out how to do it! For anyone else who didn't immediately see how: 2D = [[1, 2], [2, 7], [3, 3], [4, 5], [99, 99]]; m = [[1, 0, 0], [0, 1, 0]]; echo(2D * m); Steve
SP
Sanjeev Prabhakar
Wed, Sep 13, 2023 2:01 AM

to convert 1 million 2d points to 3d with list comprehension it takes 1 sec.

On Tue, 12 Sept 2023 at 08:49, Steve Lelievre <
steve.lelievre.canada@gmail.com> wrote:

On 2023-09-11 7:07 p.m., Adrian Mariano wrote:

Note also that it is faster to write the vector extension function using a
matrix multiply instead of a loop.

Oh, I did not know that; thanks for the tip.

After a little reading, I even worked out how to do it! For anyone else
who didn't immediately see how:

2D = [[1, 2], [2, 7], [3, 3], [4, 5], [99, 99]];
m = [[1, 0, 0], [0, 1, 0]];
echo(2D * m);

Steve


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

to convert 1 million 2d points to 3d with list comprehension it takes 1 sec. On Tue, 12 Sept 2023 at 08:49, Steve Lelievre < steve.lelievre.canada@gmail.com> wrote: > On 2023-09-11 7:07 p.m., Adrian Mariano wrote: > > > Note also that it is faster to write the vector extension function using a > matrix multiply instead of a loop. > > Oh, I did not know that; thanks for the tip. > > After a little reading, I even worked out how to do it! For anyone else > who didn't immediately see how: > > 2D = [[1, 2], [2, 7], [3, 3], [4, 5], [99, 99]]; > m = [[1, 0, 0], [0, 1, 0]]; > echo(2D * m); > > Steve > > > > > > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
SP
Sanjeev Prabhakar
Wed, Sep 13, 2023 2:25 AM

with matrix multiplication it takes 0.03 sec

On Wed, 13 Sept 2023 at 07:31, Sanjeev Prabhakar sprabhakar2006@gmail.com
wrote:

to convert 1 million 2d points to 3d with list comprehension it takes 1
sec.

On Tue, 12 Sept 2023 at 08:49, Steve Lelievre <
steve.lelievre.canada@gmail.com> wrote:

On 2023-09-11 7:07 p.m., Adrian Mariano wrote:

Note also that it is faster to write the vector extension function using
a matrix multiply instead of a loop.

Oh, I did not know that; thanks for the tip.

After a little reading, I even worked out how to do it! For anyone else
who didn't immediately see how:

2D = [[1, 2], [2, 7], [3, 3], [4, 5], [99, 99]];
m = [[1, 0, 0], [0, 1, 0]];
echo(2D * m);

Steve


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

with matrix multiplication it takes 0.03 sec On Wed, 13 Sept 2023 at 07:31, Sanjeev Prabhakar <sprabhakar2006@gmail.com> wrote: > to convert 1 million 2d points to 3d with list comprehension it takes 1 > sec. > > > On Tue, 12 Sept 2023 at 08:49, Steve Lelievre < > steve.lelievre.canada@gmail.com> wrote: > >> On 2023-09-11 7:07 p.m., Adrian Mariano wrote: >> >> >> Note also that it is faster to write the vector extension function using >> a matrix multiply instead of a loop. >> >> Oh, I did not know that; thanks for the tip. >> >> After a little reading, I even worked out how to do it! For anyone else >> who didn't immediately see how: >> >> 2D = [[1, 2], [2, 7], [3, 3], [4, 5], [99, 99]]; >> m = [[1, 0, 0], [0, 1, 0]]; >> echo(2D * m); >> >> Steve >> >> >> >> >> >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to discuss-leave@lists.openscad.org >> >
SL
Steve Lelievre
Wed, Sep 13, 2023 2:50 AM

And it’s much easier to write than my first attempt.

S.

On Tue, Sep 12, 2023 at 19:25, Sanjeev Prabhakar sprabhakar2006@gmail.com
wrote:

with matrix multiplication it takes 0.03 sec

On Wed, 13 Sept 2023 at 07:31, Sanjeev Prabhakar sprabhakar2006@gmail.com
wrote:

to convert 1 million 2d points to 3d with list comprehension it takes 1
sec.

On Tue, 12 Sept 2023 at 08:49, Steve Lelievre <
steve.lelievre.canada@gmail.com> wrote:

On 2023-09-11 7:07 p.m., Adrian Mariano wrote:

Note also that it is faster to write the vector extension function using
a matrix multiply instead of a loop.

Oh, I did not know that; thanks for the tip.

After a little reading, I even worked out how to do it! For anyone else
who didn't immediately see how:

2D = [[1, 2], [2, 7], [3, 3], [4, 5], [99, 99]];
m = [[1, 0, 0], [0, 1, 0]];
echo(2D * m);

Steve


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

And it’s much easier to write than my first attempt. S. On Tue, Sep 12, 2023 at 19:25, Sanjeev Prabhakar <sprabhakar2006@gmail.com> wrote: > with matrix multiplication it takes 0.03 sec > > On Wed, 13 Sept 2023 at 07:31, Sanjeev Prabhakar <sprabhakar2006@gmail.com> > wrote: > >> to convert 1 million 2d points to 3d with list comprehension it takes 1 >> sec. >> >> >> On Tue, 12 Sept 2023 at 08:49, Steve Lelievre < >> steve.lelievre.canada@gmail.com> wrote: >> >>> On 2023-09-11 7:07 p.m., Adrian Mariano wrote: >>> >>> >>> Note also that it is faster to write the vector extension function using >>> a matrix multiply instead of a loop. >>> >>> Oh, I did not know that; thanks for the tip. >>> >>> After a little reading, I even worked out how to do it! For anyone else >>> who didn't immediately see how: >>> >>> 2D = [[1, 2], [2, 7], [3, 3], [4, 5], [99, 99]]; >>> m = [[1, 0, 0], [0, 1, 0]]; >>> echo(2D * m); >>> >>> Steve >>> >>> >>> >>> >>> >>> _______________________________________________ >>> 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
Wed, Sep 13, 2023 2:55 AM

Any time you can do an operation with matrix multiply in OpenSCAD it will
be the fastest approach.  I made calculation of beziers 20x faster by
using a matrix multiply approach, for example, instead of the de casteljau
algorithm that is more typical.  And that can matter if you're computing a
lot of points on a bezier surface.

On Tue, Sep 12, 2023 at 10:51 PM Steve Lelievre <
steve.lelievre.canada@gmail.com> wrote:

And it’s much easier to write than my first attempt.

S.

On Tue, Sep 12, 2023 at 19:25, Sanjeev Prabhakar sprabhakar2006@gmail.com
wrote:

with matrix multiplication it takes 0.03 sec

On Wed, 13 Sept 2023 at 07:31, Sanjeev Prabhakar <
sprabhakar2006@gmail.com> wrote:

to convert 1 million 2d points to 3d with list comprehension it takes 1
sec.

On Tue, 12 Sept 2023 at 08:49, Steve Lelievre <
steve.lelievre.canada@gmail.com> wrote:

On 2023-09-11 7:07 p.m., Adrian Mariano wrote:

Note also that it is faster to write the vector extension function
using a matrix multiply instead of a loop.

Oh, I did not know that; thanks for the tip.

After a little reading, I even worked out how to do it! For anyone else
who didn't immediately see how:

2D = [[1, 2], [2, 7], [3, 3], [4, 5], [99, 99]];
m = [[1, 0, 0], [0, 1, 0]];
echo(2D * m);

Steve


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

Any time you can do an operation with matrix multiply in OpenSCAD it will be the fastest approach. I made calculation of beziers 20x faster by using a matrix multiply approach, for example, instead of the de casteljau algorithm that is more typical. And that can matter if you're computing a lot of points on a bezier surface. On Tue, Sep 12, 2023 at 10:51 PM Steve Lelievre < steve.lelievre.canada@gmail.com> wrote: > > And it’s much easier to write than my first attempt. > > S. > > > > On Tue, Sep 12, 2023 at 19:25, Sanjeev Prabhakar <sprabhakar2006@gmail.com> > wrote: > >> with matrix multiplication it takes 0.03 sec >> >> On Wed, 13 Sept 2023 at 07:31, Sanjeev Prabhakar < >> sprabhakar2006@gmail.com> wrote: >> >>> to convert 1 million 2d points to 3d with list comprehension it takes 1 >>> sec. >>> >>> >>> On Tue, 12 Sept 2023 at 08:49, Steve Lelievre < >>> steve.lelievre.canada@gmail.com> wrote: >>> >>>> On 2023-09-11 7:07 p.m., Adrian Mariano wrote: >>>> >>>> >>>> Note also that it is faster to write the vector extension function >>>> using a matrix multiply instead of a loop. >>>> >>>> Oh, I did not know that; thanks for the tip. >>>> >>>> After a little reading, I even worked out how to do it! For anyone else >>>> who didn't immediately see how: >>>> >>>> 2D = [[1, 2], [2, 7], [3, 3], [4, 5], [99, 99]]; >>>> m = [[1, 0, 0], [0, 1, 0]]; >>>> echo(2D * m); >>>> >>>> Steve >>>> >>>> >>>> >>>> >>>> >>>> _______________________________________________ >>>> 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 >