# Optimizing a simple calculus using a list of matrices - Repeated dot product between a vector and a list of matrices [closed]

I want to make the following operation :

$$\begin{bmatrix} \dot q_1\\\dot q_2 \end{bmatrix} + \begin{bmatrix} \begin{bmatrix} q_1 & q_2 \end{bmatrix} \mathbf A_1 \begin{bmatrix} q_1\\ q_2 \end{bmatrix} \\ \begin{bmatrix} q_1 & q_2 \end{bmatrix} \mathbf A_2 \begin{bmatrix} q_1\\ q_2 \end{bmatrix} \end{bmatrix} = \begin{bmatrix} \dot q_1\\\dot q_2 \end{bmatrix} + \begin{bmatrix} \begin{bmatrix} q_1 & q_2 \end{bmatrix} \begin{bmatrix} 0&1\\ 1&1 \end{bmatrix} \begin{bmatrix} q_1\\ q_2 \end{bmatrix} \\ \begin{bmatrix} q_1 & q_2 \end{bmatrix} \begin{bmatrix} -1&0\\ 0&0 \end{bmatrix} \begin{bmatrix} q_1\\ q_2 \end{bmatrix} \end{bmatrix} = \begin{bmatrix} \dot q_1\\\dot q_2 \end{bmatrix} + \begin{bmatrix} {q_1}^2 + q_2(q_1+q_2)\\ -{q_1}^2 \end{bmatrix}$$

I also want to put the two $\mathbf A_1$ and $\mathbf A_2$ matrices inside a list of matrices A in mathematica.

So I started creating my list of matrices A and did the following :

A={{{0, 1}, {1, 1}},{{-1, 0}, {0, 0}}};
res={{q1, q2}.A[[1]].{q1, q2},{q1, q2}.A[[2]].{q1, q2}};
final={q1p,q2p}+res;


Now, my question : is there a way to optimize the calculation of res? The goal is to avoid calling A[[1]] and A[[2]] independently each time. The perspective is to extend the A list to a list of dimension $n \gt 2$ (something like this) :

$$\begin{bmatrix} \dot q_1\\\dot q_2 \\ \vdots \\ \dot q_n \end{bmatrix} + \begin{bmatrix} \begin{bmatrix} q_1 & q_2 & \cdots & q_n\end{bmatrix} \mathbf A_1 \begin{bmatrix} q_1\\ q_2 \\ \vdots \\ \dot q_n \end{bmatrix} \\ \begin{bmatrix} q_1 & q_2 & \cdots & q_n \end{bmatrix} \mathbf A_2 \begin{bmatrix} q_1\\ q_2 \\ \vdots \\ \dot q_n \end{bmatrix} \\ \vdots \\ \begin{bmatrix} q_1 & q_2 & \cdots & q_n \end{bmatrix} \mathbf A_n \begin{bmatrix} q_1\\ q_2 \\ \vdots \\ q_n \end{bmatrix} \end{bmatrix}$$

-

## closed as too localized by belisarius, m_goldberg, Artes, chris, The Toad♦May 11 '13 at 0:40

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. If this question can be reworded to fit the rules in the help center, please edit the question.

Use /@ like {q1,q2}.#.{q1,q2}&/@A. –  Spawn1701D May 10 '13 at 14:23
@Spawn1701D Ok I'm embarrassed... Thanks! Exactly what I was looking for! –  jrojasqu May 10 '13 at 14:25
Voted to close, but you can now post a detailed answer to your own question –  belisarius May 10 '13 at 14:30
Per belisarius, please do post a detailed answer, it may help others who come looking for a similar problem. –  rcollyer May 10 '13 at 14:48
This also works: A.q.q, where q = {q1, q2, ..., qn}. (Assuming the dot on righthandmost $q_n$ is a typo.) –  Michael E2 May 10 '13 at 14:55

Ok, so, thanks to Spawn1701D's comment here's the simplest way to proceed :

A={{{0, 1}, {1, 1}},{{-1, 0}, {0, 0}}};
res={q1,q2}.#.{q1,q2}&/@A;
final={q1p,q2p}+res;


To state it simple, Spawn1701D prescribes the usage of pure functions to make it extremely terse :

• # function creates a slot between the two versions of the $\mathbf q$ vector
• /@ is a shorthand of the Map function

Basically what this code does is : map the Dot operations to every element on the A list (which in this particular case is a list of matrices).

UPDATE

Another way to proceed (the simplest one in fact, thanks Michael E2) is to simply use the Dot product twice between the list A and vector q

q={q1,q2}
A={{{0, 1}, {1, 1}},{{-1, 0}, {0, 0}}};
res=A.q.q

-
Note that /@ is just shorthand for Map, not a "pure function version". You can use /@ with named functions as in f /@ {1,2,3} and you can use Map with pure functions as in Map[2# &, {1,2,3}] –  Simon Woods May 10 '13 at 15:39