Skip to main content

matrix - While loop (or alternative) to generate list of random submatrices satisfying condition


I'm trying to generate a list of random submatrices with matrix norm 0.1, however I'm getting stuck; currently my code looks like this, where I run over a while loop.


Randnum:= RandomReal[NormalDistribution[0, 1]];                             
Randz := Randnum + I*Randnum;
Randmat[n_] := Table[Randz, {n}, {n}];
Randuni[n_] := Orthogonalize[Randmat[n]];

fbfmatricestest={};

tbtmatricestest={};
epsilontest={};
normepsilontest={};
While[Length[normepsilontest]<=1,
(
(*Generate a random matrix*)
a=Randuni[4];
(*Take the 3x3 submatrix out*)
b=Take[a,{1,3},{1,3}];
(*generate the epsilon matrix*)

\[Epsilon]=Simplify[ConjugateTranspose[b].b -{{1,0,0},{0,1,0},{0,0,1}}];
(*n\[Epsilon] measures gives the norm of each submatrix*)
n\[Epsilon]=Norm[\[Epsilon]];
If[
Abs[n\[Epsilon]-0.1]<0.01,
(
AppendTo[fbfmatricestest,a];
AppendTo[tbtmatricestest,b];
AppendTo[epsilontest,\[Epsilon]];
AppendTo[normepsilontest, n\[Epsilon]]

)
]
)
]

How can I efficiently generate this random list? I read that Reap and Sow might be able to generate a bunch and then collect those which satisfy a condition, but I feel this may end up taking longer than the procedure above I am trying to get working. Many thanks!



Answer



Explanations


I will present a method which relies on p-form calculation of matrix norms, a method that is already implemented in Mathematica.


We generate a random $n_1\times n_2$ complex matrix as follows:



ClearAll[randomMatrix];
randomMatrix[n1_, n2_] := RandomComplex[{-1 - I, 1 + I}, {n1, n2}];
randomMatrix[n_] := RandomComplex[{-1 - I, 1 + I}, {n, n}];

We calculate all possible rectangular submatrices by considering all possible positions for upperleft corner of submatrix and all possible row and column lengths:


ClearAll[possibleSubMatrices];
possibleSubMatrices[n1_, n2_] := possibleSubMatrices[n1, n2] = Flatten[Table[{leftTop1, leftTop2, rowLength, columnLength}, {leftTop1, n1 - 1}, {leftTop2, n2 - 1}, {columnLength, 1, n1 - leftTop1}, {rowLength, 1, n2 - leftTop2}], 3];

Then, given any matrix, following code gives all possible submatrices:


ClearAll[listSubMatrices];

listSubMatrices = Function[input, With[{n1 = Dimensions[input][[1]], n2 = Dimensions[input][[2]]},
Function[input[[#[[1]] ;; #[[1]] + #[[4]], #[[2]] ;; #[[2]] + #[[3]]]]] /@ possibleSubMatrices[n1, n2] ]];

For example,


With[{matrix = Array[a, {4, 3}]},
{MatrixForm[matrix], MatrixForm /@ listSubMatrices[matrix]}
] // TeXForm


$$ \left\{\left( \begin{array}{ccc} a(1,1) & a(1,2) & a(1,3) \\ a(2,1) & a(2,2) & a(2,3) \\ a(3,1) & a(3,2) & a(3,3) \\ a(4,1) & a(4,2) & a(4,3) \\ \end{array} \right),\left\{\left( \begin{array}{cc} a(1,1) & a(1,2) \\ a(2,1) & a(2,2) \\ \end{array} \right),\left( \begin{array}{ccc} a(1,1) & a(1,2) & a(1,3) \\ a(2,1) & a(2,2) & a(2,3) \\ \end{array} \right),\left( \begin{array}{cc} a(1,1) & a(1,2) \\ a(2,1) & a(2,2) \\ a(3,1) & a(3,2) \\ \end{array} \right),\left( \begin{array}{ccc} a(1,1) & a(1,2) & a(1,3) \\ a(2,1) & a(2,2) & a(2,3) \\ a(3,1) & a(3,2) & a(3,3) \\ \end{array} \right),\left( \begin{array}{cc} a(1,1) & a(1,2) \\ a(2,1) & a(2,2) \\ a(3,1) & a(3,2) \\ a(4,1) & a(4,2) \\ \end{array} \right),\left( \begin{array}{ccc} a(1,1) & a(1,2) & a(1,3) \\ a(2,1) & a(2,2) & a(2,3) \\ a(3,1) & a(3,2) & a(3,3) \\ a(4,1) & a(4,2) & a(4,3) \\ \end{array} \right),\left( \begin{array}{cc} a(1,2) & a(1,3) \\ a(2,2) & a(2,3) \\ \end{array} \right),\left( \begin{array}{cc} a(1,2) & a(1,3) \\ a(2,2) & a(2,3) \\ a(3,2) & a(3,3) \\ \end{array} \right),\left( \begin{array}{cc} a(1,2) & a(1,3) \\ a(2,2) & a(2,3) \\ a(3,2) & a(3,3) \\ a(4,2) & a(4,3) \\ \end{array} \right),\left( \begin{array}{cc} a(2,1) & a(2,2) \\ a(3,1) & a(3,2) \\ \end{array} \right),\left( \begin{array}{ccc} a(2,1) & a(2,2) & a(2,3) \\ a(3,1) & a(3,2) & a(3,3) \\ \end{array} \right),\left( \begin{array}{cc} a(2,1) & a(2,2) \\ a(3,1) & a(3,2) \\ a(4,1) & a(4,2) \\ \end{array} \right),\left( \begin{array}{ccc} a(2,1) & a(2,2) & a(2,3) \\ a(3,1) & a(3,2) & a(3,3) \\ a(4,1) & a(4,2) & a(4,3) \\ \end{array} \right),\left( \begin{array}{cc} a(2,2) & a(2,3) \\ a(3,2) & a(3,3) \\ \end{array} \right),\left( \begin{array}{cc} a(2,2) & a(2,3) \\ a(3,2) & a(3,3) \\ a(4,2) & a(4,3) \\ \end{array} \right),\left( \begin{array}{cc} a(3,1) & a(3,2) \\ a(4,1) & a(4,2) \\ \end{array} \right),\left( \begin{array}{ccc} a(3,1) & a(3,2) & a(3,3) \\ a(4,1) & a(4,2) & a(4,3) \\ \end{array} \right),\left( \begin{array}{cc} a(3,2) & a(3,3) \\ a(4,2) & a(4,3) \\ \end{array} \right)\right\}\right\} $$




Note that we ignore $1x1$ submatrices.


As the next step, we define


ClearAll[chooseCorrectNorms];
chooseCorrectNorms[list_List, normAverage_, error_] := With[{norms = Norm[#, 1] & /@ list}, Pick[list,
UnitStep[(norms - normAverage - error) (normAverage - error - norms)], 1]];

which eliminates matrices of undesired norms. See that we are using Norm[list,p] command to calculate matrix norm. Also, note that we are using vectorized commands Pick and UnitStep instead of conditional If and friends for better performance.


Now, our last command is this:


ClearAll[try];

try[normAverage_, error_] := With[{matrix = randomMatrix[4]}, {matrix, chooseCorrectNorms[listSubMatrices[matrix], normAverage, error]}];

It generates a random $4x4$ matrix, find all submatrices, and eliminate those with undesired norms.


Now, we simply need to run try command again and again until we hit a nonempty list.


Example


DeleteCases[Table[try[.1,.5],10^3],{_,{}}]
(* {{{{0.670769 - 0.179352 I,
0.583875 - 0.0771007 I, -0.0528683 + 0.221524 I,
0.312784 - 0.899022 I}, {-0.201839 + 0.0572823 I,
0.0601396 + 0.102979 I,

0.572871 + 0.221715 I, -0.352286 + 0.510435 I}, {0.150507 -
0.124843 I, -0.379535 - 0.142305 I,
0.0565353 - 0.22504 I, -0.390374 + 0.823681 I}, {0.664221 +
0.375659 I, 0.913889 - 0.853969 I, -0.923382 + 0.855335 I,
0.110999 + 0.149893 I}}, {{{-0.201839 + 0.0572823 I,
0.0601396 + 0.102979 I}, {0.150507 - 0.124843 I, -0.379535 -
0.142305 I}}}}} *)

which means the random matrix




$$\left( \begin{array}{cccc} 0.670769\, -0.179352 i & 0.583875\, -0.0771007 i & -0.0528683+0.221524 i & 0.312784\, -0.899022 i \\ -0.201839+0.0572823 i & 0.0601396\, +0.102979 i & 0.572871\, +0.221715 i & -0.352286+0.510435 i \\ 0.150507\, -0.124843 i & -0.379535-0.142305 i & 0.0565353\, -0.22504 i & -0.390374+0.823681 i \\ 0.664221\, +0.375659 i & 0.913889\, -0.853969 i & -0.923382+0.855335 i & 0.110999\, +0.149893 i \\ \end{array} \right)$$



has the submatrix



$$\left( \begin{array}{cc} -0.201839+0.0572823 i & 0.0601396\, +0.102979 i \\ 0.150507\, -0.124843 i & -0.379535-0.142305 i \\ \end{array} \right)$$



which has the matrix norm $0.52459$.


Performance considerations


I wrote above example to show that the code works and finds solutions for high enough error margins. However, it needs more trials for lower error margins.


The code works relatively fast in the sense that it generates $10,000$ $4\times 4$ random complex matrices and checks all their submatrices under 3 seconds on may laptop. However, the low error margin means we need a lot more trials and the required time increases as we can see:



DeleteCases[Table[try[.1,.01],10^3],{_,{}}]//Timing
(* {0.274255,{}} *)

DeleteCases[Table[try[.1,.01],10^4],{_,{}}]//Timing
(* {2.70209,{}} *)

DeleteCases[Table[try[.1,.01],10^5],{_,{}}]//Timing
(* {27.9623,{}} *)

One immediate thing that we can do is to parallelize the computations. The simplest thing to do is to use ParallelTable instead of Table. On my laptop which has 2 cores, this seems to be a nice performance improvement:



DeleteCases[ParallelTable[try[.1,.01],10^5],{_,{}}]//Timing
(* {2.21016,{}} *)

DeleteCases[ParallelTable[try[.1,.01],10^6],{_,{}}]//Timing
(* {30.9441,{}} *)

With higher number of cores, it will surely be more manageable.


Final Comments


The way we are doing the calculation here is quite ugly: We brute force generate random matrices and see if they satisfy a very selective condition. As the probability that they satisfy that condition is low, we need to generate lots of matrices to find one, which is simply a waste of resources.


A more proper way to do this is to generate $2\times 2$, $2\times 3$, or $3\times 3$ matries with required form and then embed it into a random $4\times 4$ matrix. For example,



Timing[chooseCorrectNorms[ParallelTable[randomMatrix[2],10^3],.1,0.01]]
(*{0.02439,{}}*)

generates random $2\times 2$ matrices and check their norm directly. This approach is clearly faster, though I still fail to find a solution:


Timing[chooseCorrectNorms[ParallelTable[randomMatrix[2], 10^4], .1, 0.01]]

(* {0.119413, {}} *)

Timing[chooseCorrectNorms[ParallelTable[randomMatrix[2], 10^5], .1, 0.01]]


(*{1.0971, {}}*)

Comments

Popular posts from this blog

plotting - Filling between two spheres in SphericalPlot3D

Manipulate[ SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, Mesh -> None, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], {n, 0, 1}] I cant' seem to be able to make a filling between two spheres. I've already tried the obvious Filling -> {1 -> {2}} but Mathematica doesn't seem to like that option. Is there any easy way around this or ... Answer There is no built-in filling in SphericalPlot3D . One option is to use ParametricPlot3D to draw the surfaces between the two shells: Manipulate[ Show[SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], ParametricPlot3D[{ r {Sin[t] Cos[1.5 Pi], Sin[t] Sin[1.5 Pi], Cos[t]}, r {Sin[t] Cos[0 Pi], Sin[t] Sin[0 Pi], Cos[t]}}, {r, 1, 2 - n}, {t, 0, Pi}, PlotStyle -> Yellow, Mesh -> {2, 15}]], {n, 0, 1}]

plotting - Plot 4D data with color as 4th dimension

I have a list of 4D data (x position, y position, amplitude, wavelength). I want to plot x, y, and amplitude on a 3D plot and have the color of the points correspond to the wavelength. I have seen many examples using functions to define color but my wavelength cannot be expressed by an analytic function. Is there a simple way to do this? Answer Here a another possible way to visualize 4D data: data = Flatten[Table[{x, y, x^2 + y^2, Sin[x - y]}, {x, -Pi, Pi,Pi/10}, {y,-Pi,Pi, Pi/10}], 1]; You can use the function Point along with VertexColors . Now the points are places using the first three elements and the color is determined by the fourth. In this case I used Hue, but you can use whatever you prefer. Graphics3D[ Point[data[[All, 1 ;; 3]], VertexColors -> Hue /@ data[[All, 4]]], Axes -> True, BoxRatios -> {1, 1, 1/GoldenRatio}]

plotting - Mathematica: 3D plot based on combined 2D graphs

I have several sigmoidal fits to 3 different datasets, with mean fit predictions plus the 95% confidence limits (not symmetrical around the mean) and the actual data. I would now like to show these different 2D plots projected in 3D as in but then using proper perspective. In the link here they give some solutions to combine the plots using isometric perspective, but I would like to use proper 3 point perspective. Any thoughts? Also any way to show the mean points per time point for each series plus or minus the standard error on the mean would be cool too, either using points+vertical bars, or using spheres plus tubes. Below are some test data and the fit function I am using. Note that I am working on a logit(proportion) scale and that the final vertical scale is Log10(percentage). (* some test data *) data = Table[Null, {i, 4}]; data[[1]] = {{1, -5.8}, {2, -5.4}, {3, -0.8}, {4, -0.2}, {5, 4.6}, {1, -6.4}, {2, -5.6}, {3, -0.7}, {4, 0.04}, {5, 1.0}, {1, -6.8}, {2, -4.7}, {3, -1.