Multiplication of lists [Thyme]

If you have problems installing or running Algodoo, have found a bug or need in-game help - this is it!

Multiplication of lists [Thyme]

Postby KarateBrot » Thu Dec 01, 2011 6:05 am

I don't know if it's on purpose or not but lists multiplicate like this in the console:
[a, b, c, d]*[w, x, y, z] = [a*w, b*x, c*y, d*z]

But let's say [a,b,c,d] and [w,x,y,z] are vectors.
So the scalar product should be [a,b,c,d]*[w,x,y,z] = a*w + b*x + c*y + d*z.
But in Thyme the result is (like I already said): [a*w, b*x, c*y, d*z]

- - - -

Just wanted you to know. And to kill two birds with one stone I will add a suggestion:

Just in case it's too complicated to implement it for lists in general I suggest to implement a new math function i.e. "math.scalar" which can calculate the scalar product of two vectors with the same dimensions.

To make it one could use the "bug" from above to add the components of i.e. [a*w, b*x, ...] together.
Code: Select all
math.scalar := (v1, v2) =>{
    temp := 0;
    v := v1 * v2;
    for(string.length(v), (i)=>{temp = temp + v(i)})
}
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Multiplication of lists [Thyme]

Postby emilk » Thu Dec 01, 2011 10:45 am

When one say "vector multiplication" one normally refer to one of: inner/scalar/dot product, outer product, and component wise product (or cross product, if the vectors are of length 3). I choose the component wise, because:

A) thyme is not a linear algebra library
B) lists aren't vectors
C) inner and outer products requires vectors to be either columns or rows (one of each), and Thyme has no way to dfferentiate between the two.
D) by choosing the component wise interpretation, we get the same behavior as for +,-,/ etc.

As you point out, one can easily implement this in Thyme anyway, but I'll make it a bit easier by adding the following function to thyme.cfg:

math.sum := (v)=>{
ret = 0;
for(string.length(v), (i)=>{
ret = ret + v(i)
});
ret
};


One can now implement the dot product as

inner = (a,b)=>{math.sum(a*b)};
Emil Ernerfeldt, lead developer
emilk
 
Posts: 616
Joined: Mon Aug 31, 2009 11:01 am
Location: Umeå, Sweden

Re: Multiplication of lists [Thyme]

Postby Kilinich » Thu Dec 01, 2011 12:44 pm

how about adding list(i) = x function?
if it's hard to implement maybe just add infix?

for example:

string.setValue := (list, item, value) => {r := []; for(string.length(list), (i) => {i == item ? {r = r ++ [value]} : {r = r ++ [list(i)]}})};

infix 3 left: _@_ #= _ => string.setValue

in result we will have
a := [1,1,1,1]
a@3 #= 0
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia


Return to Help / Bug reports

Who is online

Users browsing this forum: No registered users and 1 guest