forked from tdamdouni/Pythonista
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.py
More file actions
166 lines (114 loc) · 3.46 KB
/
Copy patharray.py
File metadata and controls
166 lines (114 loc) · 3.46 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
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
157
158
159
160
161
162
163
164
165
# coding: utf-8
# https://gist.github.com/roger-/4192561
from __future__ import division
from __future__ import print_function
import numbers
import operator
import math
class array(list):
def __getitem__(self, y):
if isinstance(y, (numbers.Integral, slice)):
return list.__getitem__(self, y)
if isinstance(y[0], bool):
inds = (i for i, yi in enumerate(y) if yi)
return array(list.__getitem__(self, i) for i in inds)
if isinstance(y[0], numbers.Integral):
return array(list.__getitem__(self, yi) for yi in y)
raise TypeError
def __setitem__(self, keys, y):
if isinstance(keys, (numbers.Integral, slice)):
return list.__setitem__(self, key, y)
if isinstance(keys[0], bool):
inds = (i for i, ki in enumerate(keys) if ki)
if isinstance(y, numbers.Integral):
for i in inds:
list.__setitem__(self, i, y)
else:
for i, yi in zip(inds, y):
list.__setitem__(self, i, yi)
return
if isinstance(keys[0], numbers.Integral):
if isinstance(y, numbers.Integral):
for i in keys:
list.__setitem__(self, i, y)
else:
for i, yi in zip(keys, y):
list.__setitem__(self, i, yi)
return
raise TypeError
def __abs__(self):
return array(abs(xi) for xi in self)
def elementwise_op(op_name, right=False):
## get standard operator
op = getattr(operator, '__' + op_name + '__')
## element-wise replacement operator
# left operators
def new_l_op(self_, y):
#print 'LOP'
if isinstance(y, numbers.Number):
return array(op(xi, y) for xi in self_)
else:
return array(op(xi, yi) for xi, yi in zip(self_, y))
# right operators
def new_r_op(self_, y):
#print 'ROP'
if isinstance(y, numbers.Number):
return array(op(y, xi) for xi in self_)
else:
return array(op(yi, xi) for xi, yi in zip(self_, y))
## replace standard operator
if right:
setattr(array, '__r' + op_name + '__', new_r_op)
else:
setattr(array, '__' + op_name + '__', new_l_op)
def patch_array():
REPLACED_OPS = ['add', 'sub', 'mul', 'div', 'gt', 'ge', 'lt', 'le', 'pow', \
'or', 'and', 'xor', 'truediv', 'mod']
REPLACED_OPS2 = ['iadd', 'isub', 'imul', 'idiv', 'ipow', 'ior', 'iand', \
'ixor', 'itruediv', 'imod']
for op_name in REPLACED_OPS:
elementwise_op(op_name)
elementwise_op(op_name, right=True)
for op_name in REPLACED_OPS2:
elementwise_op(op_name)
patch_array()
def arange(*args, **kwargs):
return array(xrange(*args, **kwargs))
def linspace(start, stop, num=50, endpoint=True, retstep=False):
if num == 1:
return array([start])
elif num == 0:
return array([])
end_factor = 1 if endpoint else 0
step = (stop - start)/(num - end_factor)
space = arange(num)*step + start
if retstep:
return space, step
return space
def zeros(num):
return array([0]*num)
def ones(num):
return array([1]*num)
def dot(x, y):
return sum(xi*yi for xi, yi in zip(x, y))
def vectorize(func):
if not hasattr(func, '__call__'):
return func
def new_func(x, *args, **kwargs):
y = [func(xi, *args, **kwargs) for xi in x]
return array(y)
return new_func
def vectorize_all(namespace):
for func in namespace.__dict__.keys():
namespace.__dict__[func] = vectorize(namespace.__dict__[func])
vectorize_all(math)
from math import *
def main():
x = arange(20)
print(x[(x >= 5) & (x <= 15)])
x[x > 5] += 4
print(x[[6,7,11]])
print(sin(linspace(0, 2*pi, 5)))
print(linspace(0, 7, 5, retstep=True))
if __name__ == '__main__':
main()