MCPcopy Index your code
hub / github.com/geekcomputers/Python / Vector

Class Vector

linear-algebra-python/src/lib.py:27–195  ·  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__() : toString method component(i : int):

Source from the content-addressed store, hash-verified

25
26
27class 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.

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
test_norm_vectorMethod · 0.85

Calls

no outgoing calls

Tested by 13

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
test_norm_vectorMethod · 0.68