-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13.16.py
More file actions
56 lines (43 loc) · 1.78 KB
/
13.16.py
File metadata and controls
56 lines (43 loc) · 1.78 KB
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
from collections import defaultdict
from random import randint
from math import ceil
def create():
matrix = [[0 for _ in range(5)] for _ in range(5)]
sparce_matrix = defaultdict(int)
vector = [0 for _ in range(5)]
sparce_vector = defaultdict(int)
nonzero_elements = 0
while nonzero_elements < ceil(0.1*len(matrix)**2):
i = randint(1, len(matrix)-1)
j = randint(1, len(matrix)-1)
if (i+1, j+1) not in sparce_matrix.keys():
sparce_matrix[(i+1, j+1)] = randint(1, 10)
nonzero_elements += 1
nonzero_elements = 0
while nonzero_elements < ceil(0.1*len(vector)):
i = randint(1, len(vector)-1)
if (i+1, 1) not in sparce_vector.keys():
sparce_vector[(i+1, 1)] = randint(1, 10)
nonzero_elements += 1
return sparce_matrix, sparce_vector
def find_max(sparce_matrix):
return f'Максимальний елемент розрідженої матриці: {max(sparce_matrix.values())}'
def multiply(sparce_matrix, sparce_vector):
new_vector = defaultdict(int)
for x in sparce_matrix.keys():
for y in sparce_vector.keys():
if x[1] == y[0]:
new_vector[(x[0], y[1])] += sparce_matrix[x]*sparce_vector[y]
return new_vector
def is_lower(sparce_matrix):
for pos in sparce_matrix.keys():
if pos[0] < pos[1]:
return 'Розріджена матриця не є нижньою трикутною'
return 'Розріджена матриця є нижньою трикутною'
if __name__ == '__main__':
matrix, vector = create()
print(matrix)
print(vector)
print(find_max(matrix))
print(multiply(matrix, vector))
print(is_lower(matrix))