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?










share|improve this question





















  • 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















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?










share|improve this question





















  • 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













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?










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 days ago









Dario Balboni

51




51











  • 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

















  • 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
















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


















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














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














































































Popular posts from this blog

Kleinkühnau

Makov (Slowakei)

Deutsches Schauspielhaus