-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmath_util.py
42 lines (31 loc) · 1.04 KB
/
math_util.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
'''
contains functions that convenient math stuff, usually involving operations that are not
defined in normal mathematics but could be useful
@author: Klint
'''
# matrix stuff, collapse matrix in interesting ways
def collapse_matrix_cols(matrix: [list])-> list:
'''flatten a matrix by m by n into a array of size m by adding columns together
ie
| a_00 a_10 ... a_m0 |
| a_01 ............. |
| ................. | = [a_00 + a_01 + .. + a_0n, a_10 + a_11 + ... + a_1n, a_m0 + a_m1 + ... + a_mn]
| a_0n ........ a_mn |
'''
to_return = []
size = len(matrix[0])
for j in range(size):
to_append = 0
for i in range(len(matrix)):
assert(len(matrix[i]) == size)
to_append += matrix[i][j]
to_return.append(to_append)
return to_return
def collapse_matrix_rows(matrix: [list])-> list:
'''flatten a matrix by adding rows
'''
to_return = []
for row in matrix:
to_return = sum(row)
return to_return
#