MCPcopy Index your code
hub / github.com/subbarayudu-j/TheAlgorithms-Python / Vector

Class Vector

linear_algebra_python/src/lib.py:28–144  ·  view source on GitHub ↗

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__()

Source from the content-addressed store, hash-verified

26
27
28class 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

Callers 15

test_componentMethod · 0.85
test_strMethod · 0.85
test_sizeMethod · 0.85
test_euclidLengthMethod · 0.85
test_addMethod · 0.85
test_subMethod · 0.85
test_mulMethod · 0.85
test_axpyMethod · 0.85
test_copyMethod · 0.85
test_changeComponentMethod · 0.85
test__mul__matrixMethod · 0.85
__add__Method · 0.85

Calls

no outgoing calls

Tested by 11

test_componentMethod · 0.68
test_strMethod · 0.68
test_sizeMethod · 0.68
test_euclidLengthMethod · 0.68
test_addMethod · 0.68
test_subMethod · 0.68
test_mulMethod · 0.68
test_axpyMethod · 0.68
test_copyMethod · 0.68
test_changeComponentMethod · 0.68
test__mul__matrixMethod · 0.68