Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a time dependent matrix $M(t)$ of $L^n \times L^n$ size and want to write differential equations like D[M[i,j][t],t] = H[i,j][t]

Here is my code:

L=3; n=3;
Rho[t_] = Table[M[i,j][t], {i,L^n}, {j,L^n}]; //Timing(*Rho2[t]//MatrixForm*)
(* {0.00100,Null} *)

Hop[t_, i_] =
    Transpose[
      Table[If[i == j, 1, Cos[t]], {i, {1}}, {j, L}]]
    . Table[If[i == j, 1, Sin[t]], {i, {2}}, {j, L}]

I1[i_Integer] := I1[i]=IdentityMatrix[L^(i-1)];
I2[i_Integer] := I2[i]=IdentityMatrix[L^(n-i)];

H[t_] = Sum[FixedPoint[ArrayFlatten, I1[i] \[TensorProduct] Hop[t,i] 
    \[TensorProduct]I2[i]],{i,n}];

Rho1[t_] =
    Table[
        D[Rho[t],t][[i,j]] == H[t][[i,j]],
        {i,L^n}, {j,L^n}]; // Timing(*Rho1[t]//MatrixForm*)
(* {0.33500,Null} *)

I can make it faster by using the Parallel command:

Rho2[t_] =
    ParallelTable[
        D[Rho[t],t][[i,j]] == H[t][[i,j]],
        {i,L^n}, {j,L^n}]; // Timing(*Rho1[t]//MatrixForm*)
(* {0.08100,Null} *)

Is there any other way by using Map or Transpose to make it more efficient?

share|improve this question
1  
In your formulation of Rho1[t_] you should be able to simply use Map[D[#, t] &, Rho[t]]. On my machine using Table gives a timing of 0.277257, while using Map gives a timing of 0.001321. Is this the operation you are trying to speed up? Maybe I misunderstood the question. –  leibs Jan 27 at 18:08
    
There's no need to Table in your Rho1[t_] = .... D is Listable so you can just write Rho1[t_] = D[Rho[t], t]. –  Silvia Jan 27 at 18:14
    
@Silvia right you are. I forgot about the Listable nature of D[…]. Your solution reads better. –  leibs Jan 27 at 18:21
    
@Silvia I made a mistake before, please look at the edited version. I want to make set of differential equations in this form D[M[i,j][t],t]=H[i,j][t]. @leibs Map make it good but still not that efficient because I need to create set of diff. equations. –  santosh Jan 27 at 18:26
    
Do you want your Rho2 a matrix shape or a 1-dimension list with all equations is ok? –  Silvia Jan 27 at 18:36
show 1 more comment

1 Answer 1

up vote 2 down vote accepted

You can use Thread to "thread" the == over the list:

Rho2[t_] = Thread /@ Thread[D[Rho[t], t] == H[t]];

Which is much more efficient than Table or ParallelTable.

The inner Thread map {{..}, {..}, ..., {...}} == {{..}, {..}, ..., {..}} to {{..} == {..}, {..} == {..}, ..., {..} == {..}}; the outer Thread perform on each .. == .. to make it {{.. == .., ...}, {.. == .., ...}, ..., {.. == .., ...}}.

share|improve this answer
    
Another way to look at it is to flatten the arrays first and then use MapThread such as MapThread[Equal, {Flatten[D[Rho[t], t]], Flatten[H[t]]}] The difference being that the above solution will produce an array of the same dimension as the original where as using the solution with Flatten will produce a list of equations. –  leibs Jan 27 at 19:01
    
@leibs Or Thread[Flatten[D[Rho[t], t] - H[t]] == 0]:) But Flatten can be a bit more time-consuming than two Thread. –  Silvia Jan 27 at 19:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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