I want to randomly generate a square matrix of dimension n with entries in the list StartingEntries, and also satisfying that for any such matrix M, the matrix Inverse[IdentityMatrix[n] - M] has entries coming from the list EndingEntries.
Below is my (not-working) code for the case dimension 3. It's the last line that keeps generating error messages. I'd like to know why. (Also if anyone can think of a better way of solving this problem, I'm all ears!)
(*generate 10,000 possible matrices.*)
mat = {{#1, #2, #3}, {#4, #5, #6}, {#7, #8, #9}} &
candidates = Apply[mat, RandomChoice[StartingEntries, {10000, 9}], 1]
(*get all matrices M so that I-M is invertible*)
invertibleCandidates = Select[candidates, Det[IdentityMatrix[3] - #] != 0 &]
So far so good. Now we want to make sure that the inverted matrix has the correct entries. I couldn't find a boolean Subset function in Mathematica, so I made my own:
SubsetQ[x_, y_] := (Length[Intersection[x, y]] == Length[x])
Now test to see if the inverse has entries lying inside EndingEntries. This is the line that keeps failing on me and I don't know why.
newerCan = Select[invertibleCandidates, SubsetQ[Flatten[Inverse[IdentityMatrix[3] - #]], EndingEntries] &]
Edit 1: SubSetQ modified to be
SubsetQ[x_, y_] := (Length[Intersection[DeleteDuplicates[x], DeleteDuplicates[y]]] == Length[DeleteDuplicates[x]])
Edit 2: The error messages I'm getting are
DeleteDuplicates::normal: Nonatomic expression expected at position 1 in DeleteDuplicates[nums].
Intersection::heads: Heads DeleteDuplicates and List at positions 2 and 1 are expected to be the same.
Inverse::luc: "Result for Inverse of badly conditioned matrix {{0.6,-0.7,-0.2},{-0.4,0.6,-0.1},{-0.2,-0.3,1.}} may contain significant numerical errors."
Inverse::sing: "Matrix {{0.7,-0.8,0.},{0.,0.9,-0.9},{-0.7,0.,0.8}} is singular."
I'm assuming that the first three error messages are because I'm not formatting the input to SubSetQ in the appropriate list format, but I don't know how to fix it, and I don't know why I'd be getting the "singular" error message.
Flatten[Inverse[IdentityMatrix[3] - #]]
aren't unique? In that caseSubsetQ
returns false sinceIntersection[{1,1,2},{1,2}]
is not{1,1,2}
but{1,2}
. The length of the list is two, not three. $\endgroup$