0
$\begingroup$

I have a list of 2 vectors: x={{a,b},{x,y}}, and a matrix 2 by 2 A.

I want the result to be {A.{a,b},A.{x,y}}.

I thought we could do A.x, but it doesn't work. Also, I've tried something with apply or applythread but no sucess...

Any help would be appreciated.

$\endgroup$
  • 1
    $\begingroup$ How about Transpose[A . Transpose[x]] $\endgroup$ – Carl Woll Aug 4 '17 at 23:47
  • $\begingroup$ Change x={{a,b},{x,y}} to xx={{a,b},{x,y}} and A.#&/@xx works. $\endgroup$ – kglr Aug 4 '17 at 23:50
  • 1
    $\begingroup$ A more general perspective can be seen comparing a suggestion by @CarlWoll with this answer Add a vector to a list of vectors. One can just change Dot or Plus to any two-argument action. $\endgroup$ – Artes Aug 5 '17 at 0:02
  • $\begingroup$ @Artes Thanks for the extra info which is very useful right now! $\endgroup$ – An old man in the sea. Aug 5 '17 at 7:49
5
$\begingroup$

If you are working with large matrices, then matrix dot products are almost certainly the best approach if you are interested in speed. For example, suppose you have a 200 x 200 matrix, and 100 vectors:

A = RandomReal[1, {200, 200}];
v = RandomReal[1, {100, 200}];

Let's compare a few methods:

r1 = Transpose[A . Transpose[v]]; //AbsoluteTiming
r2 = A . #& /@ v; //AbsoluteTiming
r3 = Inner[Times, A, #]& /@ v; //AbsoluteTiming

r1 == r2 == r3

{0.000335, Null}

{0.007227, Null}

{29.0056, Null}

True

Clearly, using Inner instead of Dot for large matrices is much slower. Now, for even larger matrices:

A = RandomReal[1, {2000, 2000}];
v = RandomReal[1, {100, 2000}];

r1 = Transpose[A . Transpose[v]]; //AbsoluteTiming
r2 = A . #& /@ v; //AbsoluteTiming

r1 == r2

{0.007186, Null}

{0.356008, Null}

True

Matrix multiplication using Dot is heavily optimized.

$\endgroup$
1
$\begingroup$
  x = {{a, b}, {c, d}} ;
mat = RandomInteger[{1, 5}, {2, 2}];


 Inner[Times, mat, #] & /@ x // Transpose
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.