While looking at the belisarius's question about generation of non-singular integer matrices with uniform distribution of its elements, I was studying a paper by Dana Randal, "Efficient generation of random non-singular matrices". The algorithm proposed is recursive, and involves generating a matrix of lower dimension and assigning it to a given minor. I used combinations of Insert and Transpose to do it, but there are must be more efficient ways of doing it. How would you do it?
The following is the code:
Clear[Gen]; Gen[p_, 1] := {{{1}}, RandomInteger[{1, p - 1}, {1, 1}]}; Gen[p_, n_] := Module[{v, r, aa, tt, afr, am, tm}, While[True, v = RandomInteger[{0, p - 1}, n]; r = LengthWhile[v, # == 0 &] + 1; If[r <= n, Break[]] ]; afr = UnitVector[n, r]; {am, tm} = Gen[p, n - 1]; {Insert[ Transpose[ Insert[Transpose[am], RandomInteger[{0, p - 1}, n - 1], r]], afr, 1], Insert[ Transpose[Insert[Transpose[tm], ConstantArray[0, n - 1], r]], v, r]} ] NonSingularRandomMatrix[p_?PrimeQ, n_] := Mod[Dot @@ Gen[p, n], p] It does generate a non-singular matrix, and has uniform distribution of matrix elements, but requires p to be prime:

The code is also not every efficient, which is, I suspect due to my inefficient matrix constructors:
In[10]:= Timing[NonSingularRandomMatrix[101, 300];] Out[10]= {0.421, Null} EDIT So let me condense my question. The minor matrix of a given matrix
m can be computed as follows: MinorMatrix[m_?MatrixQ, {i_, j_}] := Drop[Transpose[Drop[Transpose[m], {j}]], {i}] It is the original matrix with i-th row and j-th column deleted.
I now need to create a matrix of size n by n that will have the given minor matrix mm at position {i,j}. What I used in the algorithm was:
ExpandMinor[minmat_, {i_, j_}, v1_, v2_] /; {Length[v1] - 1, Length[v2]} == Dimensions[minmat] := Insert[Transpose[Insert[Transpose[minmat], v2, j]], v1, i] Example:
In[31]:= ExpandMinor[ IdentityMatrix[4], {2, 3}, {1, 2, 3, 4, 5}, {2, 3, 4, 4}] Out[31]= {{1, 0, 2, 0, 0}, {1, 2, 3, 4, 5}, {0, 1, 3, 0, 0}, {0, 0, 4, 1, 0}, {0, 0, 4, 0, 1}} I am hoping this can be done more efficiently, which is what I am soliciting in the question.
Per blisarius's suggestion I looked into implementing ExpandMinor via ArrayFlatten.
Clear[ExpandMinorAlt]; ExpandMinorAlt[m_, {i_ /; i > 1, j_}, v1_, v2_] /; {Length[v1] - 1, Length[v2]} == Dimensions[m] := ArrayFlatten[{ {Part[m, ;; i - 1, ;; j - 1], Transpose@{v2[[;; i - 1]]}, Part[m, ;; i - 1, j ;;]}, {{v1[[;; j - 1]]}, {{v1[[j]]}}, {v1[[j + 1 ;;]]}}, {Part[m, i ;;, ;; j - 1], Transpose@{v2[[i ;;]]}, Part[m, i ;;, j ;;]} }] ExpandMinorAlt[m_, {1, j_}, v1_, v2_] /; {Length[v1] - 1, Length[v2]} == Dimensions[m] := ArrayFlatten[{ {{v1[[;; j - 1]]}, {{v1[[j]]}}, {v1[[j + 1 ;;]]}}, {Part[m, All, ;; j - 1], Transpose@{v2}, Part[m, All, j ;;]} }] In[192]:= dim = 5; mm = RandomInteger[{-5, 5}, {dim, dim}]; v1 = RandomInteger[{-5, 5}, dim + 1]; v2 = RandomInteger[{-5, 5}, dim]; In[196]:= Table[ExpandMinor[mm, {i, j}, v1, v2] == ExpandMinorAlt[mm, {i, j}, v1, v2], {i, dim}, {j, dim}] // Flatten // DeleteDuplicates Out[196]= {True}
Insertetc, which create a copy. For larger matrices especially, it is this copying that makes the code inefficient. Your only friend isPart, used in vectorized fashion to modify a matrix in place. For the case at hand, I posted a solution below. Whether or not some generic functions can be extracted from it to perform a general task that you request, I don't yet know, but it seems quite possible.