This class represents a vector of arbitray size. You need to give the vector components. Overview about the methods: constructor(components : list) : init the vector set(components : list) : changes the vector components. __str__() : toString method component(i : int):
| 25 | |
| 26 | |
| 27 | class Vector(object): |
| 28 | """ |
| 29 | This class represents a vector of arbitray size. |
| 30 | You need to give the vector components. |
| 31 | |
| 32 | Overview about the methods: |
| 33 | |
| 34 | constructor(components : list) : init the vector |
| 35 | set(components : list) : changes the vector components. |
| 36 | __str__() : toString method |
| 37 | component(i : int): gets the i-th component (start by 0) |
| 38 | size() : gets the size of the vector (number of components) |
| 39 | euclidLength() : returns the eulidean length of the vector. |
| 40 | operator + : vector addition |
| 41 | operator - : vector subtraction |
| 42 | operator * : scalar multiplication and dot product |
| 43 | copy() : copies this vector and returns it. |
| 44 | changeComponent(pos,value) : changes the specified component. |
| 45 | TODO: compare-operator |
| 46 | """ |
| 47 | |
| 48 | def __init__(self, components): |
| 49 | """ |
| 50 | input: components or nothing |
| 51 | simple constructor for init the vector |
| 52 | """ |
| 53 | self.__components = components |
| 54 | |
| 55 | def set(self, components): |
| 56 | """ |
| 57 | input: new components |
| 58 | changes the components of the vector. |
| 59 | replace the components with newer one. |
| 60 | """ |
| 61 | if len(components) > 0: |
| 62 | self.__components = components |
| 63 | else: |
| 64 | raise Exception("please give any vector") |
| 65 | |
| 66 | def __str__(self): |
| 67 | """ |
| 68 | returns a string representation of the vector |
| 69 | """ |
| 70 | ans = "(" |
| 71 | length = len(self.__components) |
| 72 | for i in range(length): |
| 73 | if i != length - 1: |
| 74 | ans += str(self.__components[i]) + "," |
| 75 | else: |
| 76 | ans += str(self.__components[i]) + ")" |
| 77 | if len(ans) == 1: |
| 78 | ans += ")" |
| 79 | return ans |
| 80 | |
| 81 | def component(self, i): |
| 82 | """ |
| 83 | input: index (start at 0) |
| 84 | output: the i-th component of the vector. |
no outgoing calls