Advertisement
ositos

Untitled

Sep 19th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. class Vector:
  2. def _init_(self,d):
  3. if(isinstace(d,int)== true):
  4. self.coords =[0]*d
  5. else:
  6. self.coords=d
  7. def __len__(self):
  8. return len(self.coords)
  9. def __getitem__(self, j):
  10. return self.coords[j]
  11. def __setitem__(self, j, val):
  12. self.coords[j]=val
  13. def __add__(self, other):
  14. if (len(self) !=len(other)):
  15. raise ValueError("dimension must agree")
  16. result = vector(len(self))
  17. for j in range(len(self)):
  18. result[j]=self[j]+other[j]
  19. return result
  20. def __eq__(self, other):
  21. return self.coords == other.coords
  22. def __ne__(self, other):
  23. return not (self==other)
  24. def __str__(self):
  25. return '<'+str(self.coords)[1:-1]+'>'
  26. def __repr__(self):
  27. return str(self)
  28. def __sub__(self, other):
  29. if (len(self) !=len(other)):
  30. raise ValueError("dimension must agree")
  31. result = Vector(len(self))
  32. for j in range(len(self)):
  33. result[j]=self[j]-other[j]
  34. return result
  35. def __neg__(self):
  36. x=Vector(len(self))
  37. for i in range(len(self)):
  38. x[i]=self[1]*n
  39. return x
  40. def __mul__(self, n):
  41. x=Vector(len(self))
  42. if(isinstance(n,int)== true):
  43. for i in range(len(self)):
  44. x[i]=self[i]
  45. return sum(x)
  46. def __rmul__(self, n):
  47. x=Vector(len(self))
  48. for i in range(len(self)):
  49. x[i]=n*self[i]
  50. return x
  51.  
  52. v1= Vector(5)
  53. v1[1]=10
  54. v1[-1]=10
  55. print(v1)
  56. v2= Vector([2,4,6,8,10])
  57. print(v2)
  58. u1=-v1+v2
  59. print(u1)
  60. u2 = -v2
  61. print(u2)
  62. u3=3*v2
  63. print(u3)
  64. u4=v2*3
  65. print(u4)
  66. u5=v1*v2
  67. print(u5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement