discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

adding vectors function?

JD
Jerry Davis
Mon, Nov 16, 2015 2:09 AM

I am trying to tackle user-defined functions for the first time:
What I want is an add function that does this:

assumption: both vectors are the same length.

add(first_vector, second_vector):

that returns a vector element by element addition, such that:
v1 = [10,2,0]
v2 = [2,2,3]

returns [12,4,3]

does anyone know how I should go about doing this?

jerry

Extra Ham Operator: K7AZJ
Registered Linux User: 275424
Raspberry Pi and Arduino developer

The most exciting phrase to hear in science - the one that heralds new
discoveries - is not "Eureka!" but "That's funny...".
- Isaac. Asimov

I
*f you give someone a program, you will frustrate them for a day; if you
teach them how to program, you will frustrate them for a lifetime. *-
Anonymous

If writing good code requires very little comments, then writing really
excellent code requires no comments at all!
- Ken Thompson

I am trying to tackle user-defined functions for the first time: What I want is an add function that does this: assumption: both vectors are the same length. add(first_vector, second_vector): that returns a vector element by element addition, such that: v1 = [10,2,0] v2 = [2,2,3] returns [12,4,3] does anyone know how I should go about doing this? jerry -- Extra Ham Operator: K7AZJ Registered Linux User: 275424 Raspberry Pi and Arduino developer *The most exciting phrase to hear in science - the one that heralds new discoveries - is not "Eureka!" but "That's funny...".*- Isaac. Asimov *I* *f you give someone a program, you will frustrate them for a day; if you teach them how to program, you will frustrate them for a lifetime. *- Anonymous *If writing good code requires very little comments, then writing really excellent code requires no comments at all!*- Ken Thompson
JD
Jerry Davis
Mon, Nov 16, 2015 2:21 AM

ok, as usual, when I post, I come up with the answer:

using list comprehensions, I think this is the ticket:

function addvec(v, av) = (len(v) == len(av)) ? [ for (i = [ 0 : len(v)-1 ])
v[i] + av[i] ] : [0,0,0];

--
Extra Ham Operator: K7AZJ
Registered Linux User: 275424
Raspberry Pi and Arduino developer

The most exciting phrase to hear in science - the one that heralds new
discoveries - is not "Eureka!" but "That's funny...".
- Isaac. Asimov

I
*f you give someone a program, you will frustrate them for a day; if you
teach them how to program, you will frustrate them for a lifetime. *-
Anonymous

If writing good code requires very little comments, then writing really
excellent code requires no comments at all!
- Ken Thompson

On Sun, Nov 15, 2015 at 7:09 PM, Jerry Davis jdawgaz@gmail.com wrote:

I am trying to tackle user-defined functions for the first time:
What I want is an add function that does this:

assumption: both vectors are the same length.

add(first_vector, second_vector):

that returns a vector element by element addition, such that:
v1 = [10,2,0]
v2 = [2,2,3]

returns [12,4,3]

does anyone know how I should go about doing this?

jerry

Extra Ham Operator: K7AZJ
Registered Linux User: 275424
Raspberry Pi and Arduino developer

The most exciting phrase to hear in science - the one that heralds new
discoveries - is not "Eureka!" but "That's funny...".
- Isaac. Asimov

I
*f you give someone a program, you will frustrate them for a day; if you
teach them how to program, you will frustrate them for a lifetime. *-
Anonymous

If writing good code requires very little comments, then writing really
excellent code requires no comments at all!
- Ken Thompson

ok, as usual, when I post, I come up with the answer: using list comprehensions, I think this is the ticket: function addvec(v, av) = (len(v) == len(av)) ? [ for (i = [ 0 : len(v)-1 ]) v[i] + av[i] ] : [0,0,0]; -- Extra Ham Operator: K7AZJ Registered Linux User: 275424 Raspberry Pi and Arduino developer *The most exciting phrase to hear in science - the one that heralds new discoveries - is not "Eureka!" but "That's funny...".*- Isaac. Asimov *I* *f you give someone a program, you will frustrate them for a day; if you teach them how to program, you will frustrate them for a lifetime. *- Anonymous *If writing good code requires very little comments, then writing really excellent code requires no comments at all!*- Ken Thompson On Sun, Nov 15, 2015 at 7:09 PM, Jerry Davis <jdawgaz@gmail.com> wrote: > I am trying to tackle user-defined functions for the first time: > What I want is an add function that does this: > > assumption: both vectors are the same length. > > add(first_vector, second_vector): > > that returns a vector element by element addition, such that: > v1 = [10,2,0] > v2 = [2,2,3] > > returns [12,4,3] > > does anyone know how I should go about doing this? > > jerry > -- > Extra Ham Operator: K7AZJ > Registered Linux User: 275424 > Raspberry Pi and Arduino developer > > > *The most exciting phrase to hear in science - the one that heralds new > discoveries - is not "Eureka!" but "That's funny...".*- Isaac. Asimov > > *I* > *f you give someone a program, you will frustrate them for a day; if you > teach them how to program, you will frustrate them for a lifetime. *- > Anonymous > > > *If writing good code requires very little comments, then writing really > excellent code requires no comments at all!*- Ken Thompson >
MK
Marius Kintel
Mon, Nov 16, 2015 3:09 AM

that returns a vector element by element addition, such that:
v1 = [10,2,0]
v2 = [2,2,3]

returns [12,4,3]

does anyone know how I should go about doing this?

v1+v2 should work.

-Marius

> that returns a vector element by element addition, such that: > v1 = [10,2,0] > v2 = [2,2,3] > > returns [12,4,3] > > does anyone know how I should go about doing this? > v1+v2 should work. -Marius
JD
Jerry Davis
Mon, Nov 16, 2015 3:26 AM

cool. thanks. at least it looks like I have a handle on simple functions,
though.

--
Extra Ham Operator: K7AZJ
Registered Linux User: 275424
Raspberry Pi and Arduino developer

The most exciting phrase to hear in science - the one that heralds new
discoveries - is not "Eureka!" but "That's funny...".
- Isaac. Asimov

I
*f you give someone a program, you will frustrate them for a day; if you
teach them how to program, you will frustrate them for a lifetime. *-
Anonymous

If writing good code requires very little comments, then writing really
excellent code requires no comments at all!
- Ken Thompson

On Sun, Nov 15, 2015 at 8:09 PM, Marius Kintel marius@kintel.net wrote:

that returns a vector element by element addition, such that:
v1 = [10,2,0]
v2 = [2,2,3]

returns [12,4,3]

does anyone know how I should go about doing this?

cool. thanks. at least it looks like I have a handle on simple functions, though. -- Extra Ham Operator: K7AZJ Registered Linux User: 275424 Raspberry Pi and Arduino developer *The most exciting phrase to hear in science - the one that heralds new discoveries - is not "Eureka!" but "That's funny...".*- Isaac. Asimov *I* *f you give someone a program, you will frustrate them for a day; if you teach them how to program, you will frustrate them for a lifetime. *- Anonymous *If writing good code requires very little comments, then writing really excellent code requires no comments at all!*- Ken Thompson On Sun, Nov 15, 2015 at 8:09 PM, Marius Kintel <marius@kintel.net> wrote: > > that returns a vector element by element addition, such that: > > v1 = [10,2,0] > > v2 = [2,2,3] > > > > returns [12,4,3] > > > > does anyone know how I should go about doing this? > > > v1+v2 should work. > > -Marius > > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org >
C
ctchin
Mon, Nov 16, 2015 5:43 AM

Vector sum and difference work directly, your solution is needed for
element-wise
multiplication and division, except for a minor issue of unequal lengths.
My version is this:

undef should be returned for invalid inputs, this is the same behavior as
OpenSCAD for .

--
View this message in context: http://forum.openscad.org/adding-vectors-function-tp14524p14529.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

Vector sum and difference work directly, your solution is needed for element-wise multiplication and division, except for a minor issue of unequal lengths. My version is this: undef should be returned for invalid inputs, this is the same behavior as OpenSCAD for . -- View this message in context: http://forum.openscad.org/adding-vectors-function-tp14524p14529.html Sent from the OpenSCAD mailing list archive at Nabble.com.