forked from jaymody/picoGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
np_replace.py
83 lines (49 loc) · 3.04 KB
/
np_replace.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
"""
NumPy Replacement Functions, written in pure Python
Please don't use these for anything remotely serious
"""
import math
def matmul(inputarray1, inputarray2):
return([[sum(a * b for a, b in zip(i, f)) for f in zip(*inputarray2)] for i in inputarray1])
def matmul_elem(inputarray1, inputarray2):
if not isinstance(inputarray1[0], list):
return([[inputarray1[f] * inputarray2[i][f] for f in range(len(inputarray1))] for i in range(len(inputarray2))])
elif not isinstance(inputarray2[0], list):
return([[inputarray2[f] * inputarray1[i][f] for f in range(len(inputarray2))] for i in range(len(inputarray1))])
else:
return([[inputarray1[i][f] * inputarray2[i][f] for f in range(len(inputarray2[0]))] for i in range(len(inputarray1))])
def matadd(inputarray1, inputarray2):
if not isinstance(inputarray1[0], list):
return([[inputarray1[f] + inputarray2[i][f] for f in range(len(inputarray1))] for i in range(len(inputarray2))])
elif not isinstance(inputarray2[0], list):
return([[inputarray2[f] + inputarray1[i][f] for f in range(len(inputarray2))] for i in range(len(inputarray1))])
else:
return([[inputarray1[i][f] + inputarray2[i][f] for f in range(len(inputarray2[0]))] for i in range(len(inputarray1))])
def matsub(inputarray1, inputarray2):
return([[inputarray1[i][f] - inputarray2[i][0] for f in range(len(inputarray1[i]))] for i in range(len(inputarray1))])
def matdiv(inputarray1, inputarray2):
return([[inputarray1[i][f] / inputarray2[i][0] for f in range(len(inputarray1[i]))] for i in range(len(inputarray1))] if len(inputarray2) > 1 else [[inputarray1[i][f] / inputarray2[0][0] for f in range(len(inputarray1[i]))] for i in range(len(inputarray1))])
def matmax(inputarray):
return([[max(i)] for i in inputarray])
def matsum(inputarray):
return([[sum(i)] for i in inputarray])
def matexp(inputarray):
return([matexp(i) for i in inputarray] if isinstance(inputarray, list) else math.exp(inputarray))
def matmean(inputarray):
return([[sum(i) / len(i)] for i in inputarray])
def matvar(inputarray):
return([[sum((f - (sum(i) / len(i))) ** 2 for f in i) / len(i)] for i in inputarray])
def matsplit(inputarray, indices):
return([[[f[q:q + (len(f) // indices)] for q in range(0, len(f), (len(f) // indices))][i] for f in inputarray] for i in range(indices)])
def mattranspose(inputarray):
return([list(i) for i in zip(*inputarray)] if len(inputarray) > 1 else [[f] for f in inputarray[0]])
def mattri(size):
return([[-0.e+00 if j <= i - 0 else -1.e+10 for j in range(size)] for i in range(size)]) # I know I'm cheesing this one by combining multiple operations - thing is, it's only used once. Lemme have this one.
def mathstack(arrays):
return([[elem for subarr in arrays for elem in subarr[i]] for i in range(len(arrays[0]))] if len(arrays[0]) > 1 else [[elem for arr in arrays for elem in arr[0]]])
def matsqueeze(arrays):
return([i for i in arrays[0]] if len(arrays) == 1 else arrays)
def main():
pass
if __name__ == "__main__":
main()