I am trying to solve a simple optimization problem on the sphere, and for some reason, I got some memory leak that I am not able to solve. To decompose the problem in step, what I wan to do is

  • Take an integer n and randomly distribute points around the sphere. To do so, I create a vector with n components $\theta$ that takes random values. I do the same for $\phi$. Then I use pairwise ($\theta,\phi$) to construct n 3x3 matrices of the form \begin{equation} M = 1 + a2(\begin{pmatrix}\sin(\theta)\cos(\phi)\\ \sin(\theta)\sin(\phi)\\ cos(\theta)\end{pmatrix}^T\begin{pmatrix}\sin(\theta)\cos(\phi)\\ \sin(\theta)\sin(\phi)\\ cos(\theta)\end{pmatrix}-\frac{1}{3} 1) \end{equation}

  • Let us call the $i^{th}$ of this matrices $M_i$. Then, what I want to do is to sum over all the permutations of this. Say I have $n=3$, then I have 3 matrices $(M_1,M_2,M_3)$ and I want to construct a another matrix that is \begin{equation} M_{tot} = \sum_{perm} M_{\sigma(i)} = M_{1}M_{2}M_{3} + M_{1}M_{3}M_{2} + M_{2}M_1M_3 + M_2M_3M_1+M_3M_1M_2 + M_3M_2M_1 \end{equation}

  • Then, I want to get the three eigenvalues of $M_{tot}$ and have some procedure to choose whether I want to keep them or not. I run this 20 times to try to get a minimal and a maximal value. For some reason, it works fine up to say $n=6$, but requires really high Ram which makes it impossible to run for bigger $n$. Here is my code (sorry for the for loops but I managed to make it work like that). The output I am interested in is the tables min and max. I am sure my solution is not particularly elegant but I was not able to find a good alternative at that point.

nmax = 4;
kmax = 20;
max = Table[3, {i, 1, 1}];
min = Table[-3/2, {i, 1, 1}];
For[k = 1, k < kmax + 1, k++,
 θ = Table[RandomReal[] π, {i, 1, nmax}]; 
 ϕ = Table[RandomReal[] 2 π, {i, 1, nmax}];
 Ms = Table[
   IdentityMatrix[3] + 
    a2 (Transpose[{{Sin[θ[[i]]] Cos[ϕ[[i]]], 
           Sin[θ[[i]]] Sin[ϕ[[i]]], 
           Cos[θ[[i]]]}}].{{Sin[θ[[i]]] Cos[ϕ[[
             i]]], Sin[θ[[i]]] Sin[ϕ[[i]]], 
          Cos[θ[[i]]]}} - 1/3 IdentityMatrix[3]), {i, 1, nmax}];
 perm = Permutations[Table[i, {i, 1, nmax}]];
 Mtot = Sum[
   Apply[Dot, Table[Ms[[perm[[i, j]]]], {j, 1, nmax}]], {i, 1, nmax!}];
 eigen = Eigenvalues[Mtot];
 a21 = a2 /. NSolve[eigen[[1]] == 0, a2];
 a22 = a2 /. NSolve[eigen[[2]] == 0, a2];
 a23 = a2 /. NSolve[eigen[[3]] == 0, a2];
 a2tot = Join[a21, a22, a23];
 For[i = 1, i < Length[a2tot] + 1, i++,
  If[a2tot[[i]] ∈ Reals && a2tot[[i]] < 0 && 
    a2tot[[i]] > min[[1]],
   min = ReplacePart[min, {1} -> a2tot[[i]]]]];
 For[j = 1, j < Length[a2tot] + 1, j++,
  If[a2tot[[j]] ∈ Reals && a2tot[[j]] > 0 && 
    a2tot[[j]] < max[[1]],
   max = ReplacePart[max, {1} -> a2tot[[j]]];]];
  • 2
    $\begingroup$ How many matrices are you going to multiply and sum? Try this: Length[Permutations[Range[n]]] For n=3 its small, for n=6 it's 720, for n=7 its over 5000. Is this a "memory leak" or is it just that you are asking for too many matrix multiplies? $\endgroup$ – bill s Apr 10 at 3:22
  • $\begingroup$ I know that I am going to mutliply n! matrices and that it is a lot, but I am sure that Mathemica can handle mutliplying 3x3 matrices no? Maybe there is a more clever way of doing that $\endgroup$ – Ezareth Apr 10 at 7:26

Your Answer

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

Browse other questions tagged or ask your own question.