#!/usr/bin/python
#-*- coding:utf8
import vException
import numpy as np
class vectors:
def __init__(self,v):
if isinstance(v,(list,np.ndarray)):
if len(v)==0:
raise vException.InvalidVector, 'ne pustoy'
else:
try:
self.v=np.array(v,dtype=float)
except ValueError:
raise vException.InvalidVector, 'tolko chisla'
else: raise vException.InvalidVector, 'vvod spiska'
def __str__(self):
s=""
for i in range(len(self.v)):
s1=str(float(self.v[i]))
s=s+", "+s1
s=s[1:]
s="("+s+")"
return s
def __eq__(self,other):
if len(self.v)!=len(other.v):
return False
else:
res=True
for i in range(len(self.v)):
if self.v[i]!=other.v[i]:
res=False
return res
def __ne__(self,other):
res= (self == other)
return not (res)
def __add__(self,other):
try :
m=self.v+other.v
except: raise vException.WrongVectorDims
return vectors(m)
def __sub__(self,other):
try:
m=self.v-other.v
except: raise vException.WrongvectorDims
return vectors(m)
def __mul__(self,other):
if (isinstance(other,vectors)):
if len(self.v)!=len(other.v):raise vException.WrongVectorDims
else:
return (self.v*other.v).sum()
elif(isinstance(other,int))or(isinstance(other,float)):
return vectors(self.v*other)
def __rmul__(self,other):
if (isinstance(other,vectors)):
if len(self.v)!=len(other.v):raise vException.WrongVectorDims
else:
return (self.v*other.v).sum()
elif(isinstance(other,int))or(isinstance(other,float)):
return vectors(other*self.v)
def __abs__(self):
d=0
for i in range(len(self.v)):
a=pow(self.v[i],2)
d=a+d
return pow(d,0.5)
def __div__(self,other):
if isinstance(other,(int,float)):
if other==0: raise vException.WrongVectorDims, 'Na 0 delitb nelbzya'
else:
return vectors(self.v/other)
else:
raise vException.WrongVectorDims, 'Delenie na vector'