### Matrix generator for a specific determinant

closed account (zv05oG1T)
I tried to generate a matrix with the code below. It works, but it has some isues. One of them is that for a matrix NxN, the code generates random elements, but it generates the same matrix, for the same n.
For example: n=3, it generates the matrix:
-1 0 2
-2 2 2
1 1 0
I thought next time i generate a new 3x3 matrix, it will be different, but it's always the same. Still, that's not the problem. Even if it wasn't this problem, i need an algorithm which generates an invertible matrix. Additionally, i need the determinant=1. I have to use the inverse matrix. So, if the determinant is not equal 1, it will be more difficult to finish the program.

 ``123`` `````` for (i=0;i
> I thought next time i generate a new 3x3 matrix, it will be different, but it's always the same
¿are you sedding the random number generator?

> i need an algorithm which generates an invertible matrix.
create a triangular matrix, then operate with the rows.

> Additionally, i need the determinant=1.
do M/det(M)
closed account (zv05oG1T)
that's the code for invertible matrix
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108`` ``````ofstream g ("inv.txt"); float determinant(float matrix[30][30],float size) { float s=1,det=0,m_minor[30][30]; int i,j,m,n,c; if (size==1) { return (matrix[0][0]); } else { det=0; for (c=0;c
closed account (zv05oG1T)
I have to generate matrix NxN with the determinant=1, each time a different matrix
Do you have any conditions that the matrices must meet beyond their being invertible? For example, it seems like you could pick a single non-zero random number x0 and then generate the matrix
x0 0 0 ...
0 x0 0 ...
0 0 x0 ...
... ... ... ...
closed account (zv05oG1T)
That won't work. I can have some zeroes in matrix, but i can't have too many. My program encodes a message, assigning one number to each character. Then, i have to multiply those numbers with the matrix. For example: I want to encode the message: "That's my program!"; it will display: 20 727 506 454 149 481 321 314 77 657 495 406 162 727 509 430 147 79 78 1 0. Too many zeroes means losing the message...
Last edited on
 Too many zeroes means losing the message...
Well, no. You will only lose information if the matrix is non-invertible. That's what it means for a matrix to be invertible.

Any invertible matrix can be reduced to an equivalent diagonal matrix
x0 0 0 ... 0
0 x1 0 ... 0
0 0 x2 ... 0
... ... ... ... 0
0 0 0 0 xn
where x0, x1, ..., xn are non-zero reals. Therefore, for a message of length n, all you need to do is generate n non-zero reals; then treating the plaintext as a vector and multiplying it by that diagonal matrix is equivalent to doing cyphertext[i] = plaintext[i] * M[i][i]

I will advice that this is way, way more inefficient a one-time pad with XOR.
closed account (zv05oG1T)
Random diagonal matrix means invertible matrix, but it also means random determinant. If the determinant is not equal 1 or -1, there will be an inverse matrix, type float. I need random matrix with the determinant=1 or -1. Still, you gave me a great idea. I'll try to generate diagonal matrices which contain only the elements 1 and -1.
closed account (zv05oG1T)
That should do the trick.
 ``12`` ``````for (i=0;i
 Random diagonal matrix means invertible matrix, but it also means random determinant. If the determinant is not equal 1 or -1, there will be an inverse matrix, type float. I need random matrix with the determinant=1 or -1.
As a counter-example, the matrix
2 0
0 1
has a determinant equal to 2, but also has an inverse:
0.5 0
0 1
The condition for invertibility is det(M) != 0.

If you still want to force det(M) = k for whatever reason, simply pick non-zero reals x0, x1, ..., xn-1, then xn = k/(x0 * x1 * ... * xn-1).

 I'll try to generate diagonal matrices which contain only the elements 1 and -1.
This is really bad. Do not do this. Half of your cyphertext will be just be the same as the plaintext.
closed account (zv05oG1T)
Yeah, not a good idea... I really need det(M)=1 or -1.
 If you still want to force det(M) = k for whatever reason, simply pick non-zero reals x0, x1, ..., xn-1, then xn = k/(x0 * x1 * ... * xn-1).
I'll try this. Thank you!
Topic archived. No new replies allowed.