# Generate Non-Singular Matrix of $n*n$ dimension

I want to create a function that will make an $n*n$ matrix which is non-singular that will always retrieve the same matrix (meaning that the matrix will not be random each time).

I tried using the table function with different expressions using $i$ and $j$ but they always ended up singular because they had similar rules.

So please don't give me a random matrix, the matrix needs to have a pattern that if two people used the function on different computers with the same $n$ value, they would get the same matrix

P.S. Keep the matrix in the Real set of numbers (also Integers) and don't make them too high so that its not too big to work with (I want the values to be ideally under 20)

-
I couldn't resist: IdentityMatrix[n] – Kuba Aug 17 '13 at 9:25
hmm yeah thats not gonna work, I'm doing Matrix Cryptography and I am going to pretend that the person I send the encrypted matrix knows the encoding matrix and using the Identity Matrix to encode it, won't be that encrypted. – VikeStep Aug 17 '13 at 9:27
I know it is not what are you looking for but it fits the question :) that's I couldn't resist to post it :) – Kuba Aug 17 '13 at 9:28
true true, well thanks, I'll update it in the OP, but the matrix should be only Integers, so Nassers answer won't work – VikeStep Aug 17 '13 at 9:30
I am confused. You say but the matrix should be only Integers, so Nassers answer won't work but HankelMatrix is all integers. May be you meant should NOT be integers? – Nasser Aug 17 '13 at 9:36

A non-singular matrix is a matrix with full rank. You can use any orthogonal basis, for example:

Table[HermiteH[i, j], {i, 5}, {j, 5}]


For a 5x5 non-singular matrix. There are several more basis generating function in Mathematica.

Since these numbers can be a bit big, here's a way of generating non-singular, non-random matrices with entries between -1 and 1:

orthMatrix[n_] := Orthogonalize[RandomReal[1, {n, n}]]


To make it non-random we use RandomSeed:

SeedRandom[1337]; orthMatrix[10]

-
wow, that has HUGH condition number! MatrixConditionNumber[ Table[HermiteH[i, j], {i, 5}, {j, 5}]] // N gives 431620. en.wikipedia.org/wiki/Condition_number that will cause about 5 decimal point accuracy loss? – Nasser Aug 17 '13 at 9:17
@Nasser We don't really know what the requirements for the matrix is. I'm just pointing out that HermiteH, LaguerreL, BesselJ etc. all solves the problem in a straightforward manner. – C. E. Aug 17 '13 at 9:23
Sure. But high condition number will cause numerical problems when used for Ax=b problems. But there are many ways to generate random matrices that are not singular. just googled it. So, fixing the seed to fixed value before starting the random number generator will cause the same matrix to be generated each time. – Nasser Aug 17 '13 at 9:26
@Nasser Added such a method. – C. E. Aug 17 '13 at 9:41
Using your last method with the orthMatrix, my values are all decimals, is there a way to make them integers under a maximum value? – VikeStep Aug 17 '13 at 9:56

maybe Hankel?

N[LinearAlgebraMatrixConditionNumber[HankelMatrix[#]]] & /@ Range[4, 32]

{4.09288, 5.13808, 6.11956, 7.14252, 8.1322, 9.14612, 10.1395, \
11.1489, 12.1443, 13.151, 14.1477, 15.1527, 16.1502, 17.1541, \
18.1521, 19.1552, 20.1536, 21.1562, 22.1548, 23.157, 24.1559, \
25.1577, 26.1567, 27.1583, 28.1574, 29.1588, 30.1581, 31.1593, \
32.1586}

-
I updated it in the OP, but I forgot to mention that all values are integers :/ thanks anyways – VikeStep Aug 17 '13 at 9:33
but it IS all integers. That what Hankel matrix is. The above is NOT the matrix, these are the conditions numbers. Showing the matrix is stable numerically. – Nasser Aug 17 '13 at 9:37
oh, ok, I tested it out in Mathematica and it seems like the best for my scenario so far, I will keep this option open – VikeStep Aug 17 '13 at 9:44

Just for fun:

n = 5;
Partition[Prime /@ Range[n^2], n]

{{2, 3, 5, 7, 11},
{13, 17, 19, 23, 29},
{31, 37, 41, 43, 47},
{53, 59, 61, 67, 71},
{73, 79, 83, 89, 97}}


a little improvement:

Prime@Array[Plus, {n, n}]

{{3, 5, 7, 11, 13},
{5, 7, 11, 13, 17},
{7, 11, 13, 17, 19},
{11, 13, 17, 19, 23},
{13, 17, 19, 23, 29}}


it will generate lower values because rows will overlap in n-1 posistions. Also I used the fact that Prime is listable.

-
Thanks, this is my favourite, and this will work for all values of n? – VikeStep Aug 17 '13 at 9:18
@user9053 it suppose to but for huge n it may be slow and generate huge values :) I don't have big experience with primes so you have to check if it is good for your purposes :) Please, do not accept so fast, the question is quite new, let's do not discourage others :) you can still upvote if you like it – Kuba Aug 17 '13 at 9:21
High condition number also ! – Nasser Aug 17 '13 at 9:22
I'll unaccept the answer for the moment, and if it's the best answer later I'll accept it – VikeStep Aug 17 '13 at 9:32
@user9053 Ok ;). Also, take a look at my edit, it will generate lower values. – Kuba Aug 17 '13 at 9:34

OK, so I decided on a method similar to Anon's solution

I am just going to use

SeedRandom[value];EncodingMatrix=RandomInteger[1,{dimension,dimension}]


However I wanted to find an optimal seed for my scenario. I generally wanted to have all values of $n$ from 1 - 20 so I made the following program to find it out

GetEncodingMatrix[Dimensions_,Seed_]:=(SeedRandom[Seed];RandomInteger[1,{Dimension,Dimension}])
TestValid[n_]:=(state="True";For[i=1,i<21,i++,If[Det[GetEncodingMatrix[i,n]]==0,state="False"]];state)
n=1;While[n<5000,If[TestValid[n]=="true",Print[n]];n++]
`

1918, 3381, 3385

$\therefore$ My seed value should work for those values.

If anyone refers back to this and needs to make it work for every value of $n$, I recommend any of the other three options as they are all excellent

-