Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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}};

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}$$

share|improve this question

closed as too localized by Dr. belisarius, m_goldberg, Artes, chris, R. M. 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 – Dr. 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
up vote 3 down vote accepted

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

A={{{0, 1}, {1, 1}},{{-1, 0}, {0, 0}}};

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).


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

A={{{0, 1}, {1, 1}},{{-1, 0}, {0, 0}}};
share|improve this answer
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

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