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