scipy dealing with matrices expressed as sum of rank one matrices
up vote
0
down vote
favorite
I have the need to write an algorithm that deals with some very low rank (compared to the dimension) square matrices. I'd like to write such matrices as sum of "product" of a (d, 1)-matrix with a (1, d)-matrix by saving only a list of the vectors.
Also I'd like to have left and right matrix multiplication done with application of the matrix to the vectors: i.e. call $M = sum_i v_i * w_i^T$ then I'd like that $TM = sum_i (T v_i) * w_i^T$ and the like.
I've not seen any such thing in scipy but this would be really useful since matrix multiplication now becomes some matrix-vector multiplication.
Please note that the rank of my matrices is about 20, while their dimension is about 400.000, so this would save my computations a lot of time.
Please also note that such matrices are not sparse, they are just low rank and already decomposed into a sum of (d, 1)-matrix with a (1, d)-matrix.
How do you advise to do such a thing? Where can i found references to add a matrix type to scipy?
python numpy matrix scipy
|
show 2 more comments
up vote
0
down vote
favorite
I have the need to write an algorithm that deals with some very low rank (compared to the dimension) square matrices. I'd like to write such matrices as sum of "product" of a (d, 1)-matrix with a (1, d)-matrix by saving only a list of the vectors.
Also I'd like to have left and right matrix multiplication done with application of the matrix to the vectors: i.e. call $M = sum_i v_i * w_i^T$ then I'd like that $TM = sum_i (T v_i) * w_i^T$ and the like.
I've not seen any such thing in scipy but this would be really useful since matrix multiplication now becomes some matrix-vector multiplication.
Please note that the rank of my matrices is about 20, while their dimension is about 400.000, so this would save my computations a lot of time.
Please also note that such matrices are not sparse, they are just low rank and already decomposed into a sum of (d, 1)-matrix with a (1, d)-matrix.
How do you advise to do such a thing? Where can i found references to add a matrix type to scipy?
python numpy matrix scipy
have you checkedscipy.outer?
– Mstaino
2 days ago
Matrix algebra in many programming languages is outsourced by wrapping the LAPACK/BLAS libraries (written in FORTRAN). So you would need to look at those, ultimately. Fair warning: many, many have tried to do something smarter than the gods that wrote LAPACK/BLAS (including a SO poster that wishes to remain unnamed). I have yet to hear a success story (stuff that boils down to a graph problem doesn't count).
– Paul Brodersen
2 days ago
Ok, so I wrote a little snippet to test the two ways of doing it: my way (v, w, T) --> outer(T * v, w) and godoflapackway (v, w, T) --> T @ outer(v, w). Turns out that godoflapackway is far too fast that my implementation. With matrix of dimension=100 and rank=10 tested over 100 cycles we have: godoflapack 11ms vs myway 225ms per cycle... I guess that using outer is doing some optimizations (while opposed to simply do v @ w.traspose()).
– Dario Balboni
2 days ago
After trying also (v, w, T) --> T @ (v @ w.transpose()), that I thought it would give far longer execution times, it turned out to be only slightly less efficient that the godoflapack of about 1ms, so that we have a clear winner: just try to use the more concise way to express what do you want to do with available operations and do not try to implement them on your own.
– Dario Balboni
2 days ago
Turns out I was wrong: I mistype (v, w, T) --> outer(T * v, w) instead that the correct outer(T @ v, w) and the code written like that only takes 1.5 ms to execute, for the same dimension... so I'm definitely in need of something to treat small rank matrices, be it from god of lapack or from someone else @PaulBrodersen
– Dario Balboni
2 days ago
|
show 2 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have the need to write an algorithm that deals with some very low rank (compared to the dimension) square matrices. I'd like to write such matrices as sum of "product" of a (d, 1)-matrix with a (1, d)-matrix by saving only a list of the vectors.
Also I'd like to have left and right matrix multiplication done with application of the matrix to the vectors: i.e. call $M = sum_i v_i * w_i^T$ then I'd like that $TM = sum_i (T v_i) * w_i^T$ and the like.
I've not seen any such thing in scipy but this would be really useful since matrix multiplication now becomes some matrix-vector multiplication.
Please note that the rank of my matrices is about 20, while their dimension is about 400.000, so this would save my computations a lot of time.
Please also note that such matrices are not sparse, they are just low rank and already decomposed into a sum of (d, 1)-matrix with a (1, d)-matrix.
How do you advise to do such a thing? Where can i found references to add a matrix type to scipy?
python numpy matrix scipy
I have the need to write an algorithm that deals with some very low rank (compared to the dimension) square matrices. I'd like to write such matrices as sum of "product" of a (d, 1)-matrix with a (1, d)-matrix by saving only a list of the vectors.
Also I'd like to have left and right matrix multiplication done with application of the matrix to the vectors: i.e. call $M = sum_i v_i * w_i^T$ then I'd like that $TM = sum_i (T v_i) * w_i^T$ and the like.
I've not seen any such thing in scipy but this would be really useful since matrix multiplication now becomes some matrix-vector multiplication.
Please note that the rank of my matrices is about 20, while their dimension is about 400.000, so this would save my computations a lot of time.
Please also note that such matrices are not sparse, they are just low rank and already decomposed into a sum of (d, 1)-matrix with a (1, d)-matrix.
How do you advise to do such a thing? Where can i found references to add a matrix type to scipy?
python numpy matrix scipy
python numpy matrix scipy
asked 2 days ago
Dario Balboni
51
51
have you checkedscipy.outer?
– Mstaino
2 days ago
Matrix algebra in many programming languages is outsourced by wrapping the LAPACK/BLAS libraries (written in FORTRAN). So you would need to look at those, ultimately. Fair warning: many, many have tried to do something smarter than the gods that wrote LAPACK/BLAS (including a SO poster that wishes to remain unnamed). I have yet to hear a success story (stuff that boils down to a graph problem doesn't count).
– Paul Brodersen
2 days ago
Ok, so I wrote a little snippet to test the two ways of doing it: my way (v, w, T) --> outer(T * v, w) and godoflapackway (v, w, T) --> T @ outer(v, w). Turns out that godoflapackway is far too fast that my implementation. With matrix of dimension=100 and rank=10 tested over 100 cycles we have: godoflapack 11ms vs myway 225ms per cycle... I guess that using outer is doing some optimizations (while opposed to simply do v @ w.traspose()).
– Dario Balboni
2 days ago
After trying also (v, w, T) --> T @ (v @ w.transpose()), that I thought it would give far longer execution times, it turned out to be only slightly less efficient that the godoflapack of about 1ms, so that we have a clear winner: just try to use the more concise way to express what do you want to do with available operations and do not try to implement them on your own.
– Dario Balboni
2 days ago
Turns out I was wrong: I mistype (v, w, T) --> outer(T * v, w) instead that the correct outer(T @ v, w) and the code written like that only takes 1.5 ms to execute, for the same dimension... so I'm definitely in need of something to treat small rank matrices, be it from god of lapack or from someone else @PaulBrodersen
– Dario Balboni
2 days ago
|
show 2 more comments
have you checkedscipy.outer?
– Mstaino
2 days ago
Matrix algebra in many programming languages is outsourced by wrapping the LAPACK/BLAS libraries (written in FORTRAN). So you would need to look at those, ultimately. Fair warning: many, many have tried to do something smarter than the gods that wrote LAPACK/BLAS (including a SO poster that wishes to remain unnamed). I have yet to hear a success story (stuff that boils down to a graph problem doesn't count).
– Paul Brodersen
2 days ago
Ok, so I wrote a little snippet to test the two ways of doing it: my way (v, w, T) --> outer(T * v, w) and godoflapackway (v, w, T) --> T @ outer(v, w). Turns out that godoflapackway is far too fast that my implementation. With matrix of dimension=100 and rank=10 tested over 100 cycles we have: godoflapack 11ms vs myway 225ms per cycle... I guess that using outer is doing some optimizations (while opposed to simply do v @ w.traspose()).
– Dario Balboni
2 days ago
After trying also (v, w, T) --> T @ (v @ w.transpose()), that I thought it would give far longer execution times, it turned out to be only slightly less efficient that the godoflapack of about 1ms, so that we have a clear winner: just try to use the more concise way to express what do you want to do with available operations and do not try to implement them on your own.
– Dario Balboni
2 days ago
Turns out I was wrong: I mistype (v, w, T) --> outer(T * v, w) instead that the correct outer(T @ v, w) and the code written like that only takes 1.5 ms to execute, for the same dimension... so I'm definitely in need of something to treat small rank matrices, be it from god of lapack or from someone else @PaulBrodersen
– Dario Balboni
2 days ago
have you checked
scipy.outer ?– Mstaino
2 days ago
have you checked
scipy.outer ?– Mstaino
2 days ago
Matrix algebra in many programming languages is outsourced by wrapping the LAPACK/BLAS libraries (written in FORTRAN). So you would need to look at those, ultimately. Fair warning: many, many have tried to do something smarter than the gods that wrote LAPACK/BLAS (including a SO poster that wishes to remain unnamed). I have yet to hear a success story (stuff that boils down to a graph problem doesn't count).
– Paul Brodersen
2 days ago
Matrix algebra in many programming languages is outsourced by wrapping the LAPACK/BLAS libraries (written in FORTRAN). So you would need to look at those, ultimately. Fair warning: many, many have tried to do something smarter than the gods that wrote LAPACK/BLAS (including a SO poster that wishes to remain unnamed). I have yet to hear a success story (stuff that boils down to a graph problem doesn't count).
– Paul Brodersen
2 days ago
Ok, so I wrote a little snippet to test the two ways of doing it: my way (v, w, T) --> outer(T * v, w) and godoflapackway (v, w, T) --> T @ outer(v, w). Turns out that godoflapackway is far too fast that my implementation. With matrix of dimension=100 and rank=10 tested over 100 cycles we have: godoflapack 11ms vs myway 225ms per cycle... I guess that using outer is doing some optimizations (while opposed to simply do v @ w.traspose()).
– Dario Balboni
2 days ago
Ok, so I wrote a little snippet to test the two ways of doing it: my way (v, w, T) --> outer(T * v, w) and godoflapackway (v, w, T) --> T @ outer(v, w). Turns out that godoflapackway is far too fast that my implementation. With matrix of dimension=100 and rank=10 tested over 100 cycles we have: godoflapack 11ms vs myway 225ms per cycle... I guess that using outer is doing some optimizations (while opposed to simply do v @ w.traspose()).
– Dario Balboni
2 days ago
After trying also (v, w, T) --> T @ (v @ w.transpose()), that I thought it would give far longer execution times, it turned out to be only slightly less efficient that the godoflapack of about 1ms, so that we have a clear winner: just try to use the more concise way to express what do you want to do with available operations and do not try to implement them on your own.
– Dario Balboni
2 days ago
After trying also (v, w, T) --> T @ (v @ w.transpose()), that I thought it would give far longer execution times, it turned out to be only slightly less efficient that the godoflapack of about 1ms, so that we have a clear winner: just try to use the more concise way to express what do you want to do with available operations and do not try to implement them on your own.
– Dario Balboni
2 days ago
Turns out I was wrong: I mistype (v, w, T) --> outer(T * v, w) instead that the correct outer(T @ v, w) and the code written like that only takes 1.5 ms to execute, for the same dimension... so I'm definitely in need of something to treat small rank matrices, be it from god of lapack or from someone else @PaulBrodersen
– Dario Balboni
2 days ago
Turns out I was wrong: I mistype (v, w, T) --> outer(T * v, w) instead that the correct outer(T @ v, w) and the code written like that only takes 1.5 ms to execute, for the same dimension... so I'm definitely in need of something to treat small rank matrices, be it from god of lapack or from someone else @PaulBrodersen
– Dario Balboni
2 days ago
|
show 2 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53225218%2fscipy-dealing-with-matrices-expressed-as-sum-of-rank-one-matrices%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
have you checked
scipy.outer?– Mstaino
2 days ago
Matrix algebra in many programming languages is outsourced by wrapping the LAPACK/BLAS libraries (written in FORTRAN). So you would need to look at those, ultimately. Fair warning: many, many have tried to do something smarter than the gods that wrote LAPACK/BLAS (including a SO poster that wishes to remain unnamed). I have yet to hear a success story (stuff that boils down to a graph problem doesn't count).
– Paul Brodersen
2 days ago
Ok, so I wrote a little snippet to test the two ways of doing it: my way (v, w, T) --> outer(T * v, w) and godoflapackway (v, w, T) --> T @ outer(v, w). Turns out that godoflapackway is far too fast that my implementation. With matrix of dimension=100 and rank=10 tested over 100 cycles we have: godoflapack 11ms vs myway 225ms per cycle... I guess that using outer is doing some optimizations (while opposed to simply do v @ w.traspose()).
– Dario Balboni
2 days ago
After trying also (v, w, T) --> T @ (v @ w.transpose()), that I thought it would give far longer execution times, it turned out to be only slightly less efficient that the godoflapack of about 1ms, so that we have a clear winner: just try to use the more concise way to express what do you want to do with available operations and do not try to implement them on your own.
– Dario Balboni
2 days ago
Turns out I was wrong: I mistype (v, w, T) --> outer(T * v, w) instead that the correct outer(T @ v, w) and the code written like that only takes 1.5 ms to execute, for the same dimension... so I'm definitely in need of something to treat small rank matrices, be it from god of lapack or from someone else @PaulBrodersen
– Dario Balboni
2 days ago