73 lines
2.0 KiB
Python
73 lines
2.0 KiB
Python
|
class array:
|
||
|
def __init__(self, content):
|
||
|
def get_content(data):
|
||
|
rslt = []
|
||
|
for i in data:
|
||
|
if isinstance(i, (list, tuple)): rslt.append(get_content(i))
|
||
|
else: rslt.append(i)
|
||
|
return rslt
|
||
|
|
||
|
def get_shape(data, shape=[]):
|
||
|
shape.append(len(data))
|
||
|
if type(data[0]) == list:
|
||
|
get_shape(data[0], shape)
|
||
|
return shape
|
||
|
|
||
|
def get_size(shape):
|
||
|
size = 1
|
||
|
for i in shape:
|
||
|
size *= i
|
||
|
return size
|
||
|
|
||
|
if type(content) == int:
|
||
|
self.content = content
|
||
|
self.shape = 0
|
||
|
self.size = 1
|
||
|
|
||
|
else:
|
||
|
self.content = get_content(content)
|
||
|
self.shape= tuple(get_shape(self.content))
|
||
|
self.size = get_size(self.shape)
|
||
|
|
||
|
|
||
|
def __str__(self):
|
||
|
msg = "["
|
||
|
if len(self.shape) > 1:
|
||
|
msg += "\n".join([f"{i}, " for i in self.content])
|
||
|
else:
|
||
|
msg += "".join([f"{i}, " for i in self.content])
|
||
|
msg = msg[:-2] + "]"
|
||
|
return msg
|
||
|
|
||
|
def __repr__(self):
|
||
|
msg = "array(["
|
||
|
if len(self.shape) > 1:
|
||
|
msg += "\n ".join([f"{i}, " for i in self.content])
|
||
|
else:
|
||
|
msg += "".join([f"{i}, " for i in self.content])
|
||
|
msg = msg[:-2] + "])"
|
||
|
return msg
|
||
|
|
||
|
def __getitem__(self, index):
|
||
|
result = self.content[:]
|
||
|
for i in index:
|
||
|
if type(i) == int and type(result[0]) != int:
|
||
|
result = [a[i] for a in result]
|
||
|
else:
|
||
|
result = result[i]
|
||
|
|
||
|
if type(result) == int: break
|
||
|
return array(result)
|
||
|
|
||
|
def __get_column(self, col):
|
||
|
return array([content[col] for i in self.content])
|
||
|
|
||
|
def copy(self):
|
||
|
return array(self.content[:])
|
||
|
|
||
|
|
||
|
|
||
|
def linspace(start, stop, num=50):
|
||
|
step = (stop - start) / (num - 1)
|
||
|
return array([start + i * step for i in range(num)])
|