-
Notifications
You must be signed in to change notification settings - Fork 0
/
randommatrix.py
83 lines (67 loc) · 1.73 KB
/
randommatrix.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import random
import sys
from scipy.sparse import rand
import numpy
from sys import argv
# Input Parameters
dimension = int(argv[1])
sparsity = float(argv[2])
x0 = []
# Initial Matrix and Vectors
A = rand(dimension, dimension, density=sparsity, format="csr")
A = A + A.T
x = numpy.zeros(dimension)
x[0] = 1
b = numpy.random.rand(dimension)
# Iterate
r = b - A.dot(x)
p = r.copy()
for i in range(0, 100):
Ap = A.dot(p)
top = numpy.dot(r.T, r)
bottom = numpy.dot(p.T, Ap)
alpha = top / bottom
x = x + alpha * p
r = r - alpha * Ap
norm_value = numpy.linalg.norm(r)
if norm_value < 1e-8:
x0.append(x)
break
new_top = numpy.dot(r.T, r)
beta = new_top / top
p = r + beta * p
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')
f.close()
f = open('ans', 'w')
f.write("Done in: " + str(i) + " iteration. Norm Value: " + str(norm_value))
f.write('\n')
out = x0[-1]
f.write(str(out))
f.write('\n')
f.close()
else:
print("please input a number")