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?
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?
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
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
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
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.