blob: b216c1e267ea126f8b48f4682f32900f17c6b105 (
plain) (
blame)
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
|
from functools import reduce
def sum_of_ints(values):
return reduce(lambda x,y: x + y if type(y) == int else x, values, 0)
def flatten_list1(vals):
res = []
for val in vals:
if type(val) == list:
res.extend(flatten_list1(val))
else:
res.append(val)
return res
def flatten_list2(vals):
return flatten_list1(vals)
def get_first_column(matrix):
return [row[0] for row in matrix]
def get_nth_column(n, matrix):
return [row[n-1] for row in matrix]
def get_all_columns(matrix):
return [get_nth_column(i+1, matrix) for i in range(len(matrix[0]))]
def scalar_product(vec1, vec2):
return sum(map(lambda pair: pair[0] * pair[1], zip(vec1, vec2)))
def matrix_square(matrix):
new_matrix = []
for row in matrix:
new_row = []
for i in range(len(row)):
new_row.append(scalar_product(row, get_nth_column(i+1, matrix)))
new_matrix.append(new_row)
return new_matrix
|