-
Notifications
You must be signed in to change notification settings - Fork 1
/
Random_matrix.py
executable file
·55 lines (40 loc) · 933 Bytes
/
Random_matrix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import random
import sys
## Generates a random dense matrix
## Use: python randomMatrix.py M N
if (len(sys.argv) > 1):
M = int(sys.argv[1])
N = M
ans = [ 0 for i in range(M) ]
b = [ 0 for i in range(M) ]
table2 = [ [ 0 for i in range(N) ] for j in range(M) ]
for i in range(M):
ans[i] = random.random()
b[i] = 0
for i in range(M):
for j in range(N):
if i <= j:
table2[i][j] = random.random()
if i < j:
if random.random() < 0.95 :
table2[i][j] = 0
table2[j][i] = table2[i][j]
f = open('matrix','w');
for i in range(M):
for j in range(N):
##table2[i][j] = random.random()
f.write(str(table2[i][j]))
f.write('\t')
b[i] += table2[i][j] * ans[j]
f.write('\n')
for i in range(M):
f.write(str(b[i]))
f.write('\n')
f.close()
f = open('ans', 'w')
for i in range(M):
f.write(str(ans[i]))
f.write('\n')
f.close()
else:
print "please input a number"