linear combinations in python/numpy

Posted by nmaxwell on Stack Overflow See other posts from Stack Overflow or by nmaxwell
Published on 2010-05-30T05:33:07Z Indexed on 2010/05/30 5:42 UTC
Read the original article Hit count: 230

Filed under:
|
|
|

greetings,

I'm not sure if this is a dumb question or not.

Lets say I have 3 numpy arrays, A1,A2,A3, and 3 floats, c1,c2,c3

and I'd like to evaluate B = A1*c1+ A2*c2+ A3*c3

will numpy compute this as for example,

E1 = A1*c1 E2 = A2*c2 E3 = A3*c3 D1 = E1+E2 B = D1+E3

or is it more clever than that? In c++ I had a neat way to abstract this kind of operation.

I defined series of general 'LC' template functions, LC for linear combination like:

template<class T,class D>
void LC( T & R,
    T & L0,D C0,
    T & L1,D C1,
    T & L2,D C2)
{
    R = L0*C0
        +L1*C1
        +L2*C2;        
}

and then specialized this for various types,

so for instance, for an array the code looked like

for (int i=0; i<L0.length; i++)
    R.array[i] =
    L0.array[i]*C0 +
    L1.array[i]*C1 +
    L2.array[i]*C2;

thus avoiding having to create new intermediate arrays.

This may look messy but it worked really well.

I could do something similar in python, but I'm not sure if its nescesary.

Thanks in advance for any insight. -nick

© Stack Overflow or respective owner

Related posts about python

Related posts about arrays