-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathvector_norm.py
156 lines (128 loc) · 3.9 KB
/
vector_norm.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
''' mbinary
#########################################################################
# File : vector_norm.py
# Author: mbinary
# Mail: [email protected]
# Blog: https://mbinary.xyz
# Github: https://github.com/mbinary
# Created Time: 2018-10-02 21:14
# Description:
#########################################################################
'''
from random import randint, random
import numpy as np
from operator import neg, and_
from functools import reduce
class obj():
def __init__(self, data):
self.data = np.array(data)
def __add__(self, x):
data = x.data if self.__class__ == x.__class__ else x
return self.__class__(self.data + data)
def __radd__(self, x):
data = x.data if self.__class__ == x.__class__ else x
return self.__class__(data + self.data)
def __iadd__(self, x):
data = x.data if self.__class__ == x.__class__ else x
self.data += data
def __mul__(self, x):
data = x.data if self.__class__ == x.__class__ else x
return self.__class__(self.data * data)
def __imul__(self, x):
data = x.data if self.__class__ == x.__class__ else x
self.data *= data
def __rmul__(self, x):
data = x.data if self.__class__ == x.__class__ else x
return self.__class__(data * self.data)
def __neg__(self):
return neg(self)
def __abs__(self):
return abs(self.data)
'''
@property
def data(self):
return self.data
@data.setter
def data(self,s):
self.data = s
'''
def norm(self, n=0):
'''the default is +oo norm'''
absolute = abs(self.data)
if n < 1:
return max(absolute)
return (sum(absolute**n))**(1/n)
def hasNorm(self):
'''check norm's three necessary conditions:
1. not neg
2. homogenious (qici)
3. triangle inequlity
there is much probably wrong
'''
bl = reduce(and_, [self.norm(i) >= 0 for i in range(3)])
if bl:
n = randint(2, 100)
bl = reduce(and_, [n*(self.norm(i)) == (n*self).norm(i)
for i in range(3)])
if bl:
another = self*randint(2, 10)-randint(1, 100)
return reduce(and_, [(another+self).norm(i) <= another.norm(i)+self.norm(i) for i in range(3)])
return False
class vector(obj):
def __init__(self, arr):
''' arr: iterable'''
self.data = np.array(arr)
def innerProduct(self, x):
return sum(self.data*x)
def outerProduct(self, x):
pass
class matrix(obj):
def __init__(self, s):
'''s is a list of lists'''
self.data = np.mat(s)
self.T = None
self. I = None
'''
@property
def T(self):
if self.T==None:self.T = self.data.T
return self.T
@T.setter
def T(self,s):
self.T = s
@property
def I(self):
if self.I == None: self.I = self.data.I
return self.I
@I.setter
def I(self,s):
self.I = s
'''
def E(self, n=None):
if n is None:
n = self.data.shape[0]
return np.eye(n)
def norm(self, n=0):
absolute = abs(self.data)
if n < 1:
# max of one row sum
return max([sum(i) for i in absolute])
if n == 1:
return self.norm1()
elif n == 2:
return self.norm2()
def norm1(self):
''' max of sum of cols'''
absolute = abs(self.data)
return max(absolute.sum(axis=0))
def norm2(self):
''' max of sum of rows'''
absolute = abs(self.data)
return max(absolute.sum(axis=1))
def norm_f(self):
return sum((self.data**2).sum(axis=1))**0.5
if __name__ == '__main__':
v1 = vector([1, -2, 3, 4])
v2 = vector([0, 2, 0, 5])
m1 = matrix([v1, v2, v2, v1])
print([v1.norm(i) for i in range(3)])