Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'm trying to build a function that gives me matrices such that each:

  1. Has integer coefficients.
  2. Is non singular.
  3. Has inverse which also has integer coefficients.

I have some code that one of my teachers gave me, but it is code written in Derive. I have reproduced the code, but I'm not getting what a really need.

The code for Derive requires a function that takes a column, multiplies it by a constant and adds it to another one, lest call this function ColSum[M, i, j, α], which takes the i-th column multiplied by α and adds it to the j-th column.

The code for the matrix in Derive is:

Matrix(n) ≔ 
  \prod_{h=1}^{n-1}  \prod_{k=h+1}^{n} ColSum(IDENTITYMATRIX(n), h, k, (1 + RANDOM(2))·(-1)^h)*
  \prod_{k=1}^{n-1}  \prod_{h=k+1}^{n} ColSum(IDENTITYMATRIX(n), k, h, (1 + RANDOM(2))·(-1)^k)*
  VECTOR(VECTOR(IF(i_ < j_, 0, IF(i_ = j_, (-1), RANDOM(2) + 1)), i_, 1, n_), j_, 1, n_) 

I think the problem is either in the ColSum function because it is where I've seen that things go wrong, or in the fact that Product does not perform an iterated Dot.

I want to translate this function into Mathematica.

My code so far is:

RowSum[v_,i_,j_,a_] := Table[If[m ==i , Part[v,i] + a*Part[v,j],Part[v,m]],{m,Dimensions[v][[1]]}]

ColSum[v_,i_,j_,a_]:=Transpose[RowSum[Transpose[v],i,j,a]]]


Matrix[n_] := Product[ColSum[IdentityMatrix[n], h, k, (1 + RandomInteger[2])*(-1)^h ], {h,1, n - 1}, {k, h + 1,n}].Product[ColSum[IdentityMatrix[n], k, h, (1 + RandomInteger[2])*(-1)^k ], {k, 1, n - 1}, {h, k + 1, n}].Table[Table[If[i < j, 0, If[i == j, (-1)^i,RandomInteger[2] + 1]], {i, n}], {j, n}]

I think the problem is either in the ColSum function because it is where I've seen that things go wrong, or in the fact that Product does not perform an iterated Dot.

Thanks for helping.

share|improve this question
    
Are you asking how this can be done in Mathematica? –  bobthechemist Oct 24 '13 at 0:27
    
@bobthechemist yes the thing is that I have almost all the code but the thing is that when I do de products It all fails. –  DavidBecharaSenior Oct 24 '13 at 0:28
    
Include your Mathematica code in your question, then. –  bobthechemist Oct 24 '13 at 0:29
    
@bobthechemist Ok I think I have include it –  DavidBecharaSenior Oct 24 '13 at 0:48
    
Your Mathematica code is syntactically incorrect. Please correct the syntax errors and resubmit the corrected code as an edit to your question. Further, for the purposes this site, you should avoid using fancy product formatting. Just enter your products in the form Product[...] –  m_goldberg Oct 24 '13 at 1:12

2 Answers 2

up vote 5 down vote accepted

Can make a product of a random upper triangular with a random lower triangular, with the caveat that diagonal entries in both are all +-1.

rmatrix[n_] := Module[{
   rmat = RandomInteger[{-100, 100}, {n, n}], lower, upper},
  lower = 
   rmat*SparseArray[{i_, j_} /; j < i -> 1, {n, n}] + 
    DiagonalMatrix[RandomChoice[{-1, 1}, n]];
  upper = 
   rmat*SparseArray[{i_, j_} /; j > i -> 1, {n, n}] + 
    DiagonalMatrix[RandomChoice[{-1, 1}, n]];
  lower.upper
  ]

Example:

rmatrix[7]

Out[170]= {{1, -39, 11, -52, 56, -51, -41}, {86, -3353, 910, -4492, 
  4896, -4344, -3513}, {-15, 569, 410, 1037, -2051, 48, 
  388}, {40, -1491, -2096, -6737, 11354, -1411, -1829}, {-52, 
  2120, -3974, -4778, 10489, 422, 4357}, {91, -3528, 228, -6139, 
  7523, -6119, 4759}, {98, -3760, -1217, -10309, 14834, -4149, -1117}}

In[171]:= Det[%]

Out[171]= -1
share|improve this answer

Here's an alternative approach. A matrix with integer values that has an inverse that's also integer-valued is called a unimodular matrix. I don't know how to generate unimodular matrices directly, but an indirect way is to use the Hermite decomposition, which decomposes any matrix into a unimodular matrix and an upper triangular matrix. For instance:

m = RandomInteger[{0, 10}, {5, 5}];
{a, b} = HermiteDecomposition[m];

This generates a random (integer) matrix m and then decomposes it into a and b. The matrix a is unimodular (Det[a]=+/-1) and it's easy to verify that Inverse[a] is also integer-valued and has Det[Inverse[a]] of +/-1.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.