0
$\begingroup$

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?

$\endgroup$
6
  • 1
    $\begingroup$ 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. $\endgroup$
    – leibs
    Jan 27, 2014 at 18:08
  • $\begingroup$ There's no need to Table in your Rho1[t_] = .... D is Listable so you can just write Rho1[t_] = D[Rho[t], t]. $\endgroup$
    – Silvia
    Jan 27, 2014 at 18:14
  • $\begingroup$ @Silvia right you are. I forgot about the Listable nature of D[…]. Your solution reads better. $\endgroup$
    – leibs
    Jan 27, 2014 at 18:21
  • $\begingroup$ @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. $\endgroup$
    – santosh
    Jan 27, 2014 at 18:26
  • $\begingroup$ Do you want your Rho2 a matrix shape or a 1-dimension list with all equations is ok? $\endgroup$
    – Silvia
    Jan 27, 2014 at 18:36

1 Answer 1

2
$\begingroup$

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 {{.. == .., ...}, {.. == .., ...}, ..., {.. == .., ...}}.

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – leibs
    Jan 27, 2014 at 19:01
  • $\begingroup$ @leibs Or Thread[Flatten[D[Rho[t], t] - H[t]] == 0]:) But Flatten can be a bit more time-consuming than two Thread. $\endgroup$
    – Silvia
    Jan 27, 2014 at 19:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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