Skip to content

Commit b2b34e2

Browse files
authored
Update lib.py
providing default value to components parameter of __init__()
1 parent 63b2c4e commit b2b34e2

File tree

1 file changed

+7
-13
lines changed
  • linear_algebra_python/src

1 file changed

+7
-13
lines changed

linear_algebra_python/src/lib.py

+7-13
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Vector(object):
4545
changeComponent(pos,value) : changes the specified component.
4646
TODO: compare-operator
4747
"""
48-
def __init__(self,components):
48+
def __init__(self,components=[]):
4949
"""
5050
input: components or nothing
5151
simple constructor for init the vector
@@ -95,36 +95,31 @@ def __add__(self,other):
9595
returns a new vector that represents the sum.
9696
"""
9797
size = len(self)
98-
result = []
9998
if size == len(other):
100-
for i in range(size):
101-
result.append(self.__components[i] + other.component(i))
99+
result = [self.__components[i] + other.component(i) for i in range(size)]
100+
return Vector(result)
102101
else:
103102
raise Exception("must have the same size")
104-
return Vector(result)
105103
def __sub__(self,other):
106104
"""
107105
input: other vector
108106
assumes: other vector has the same size
109107
returns a new vector that represents the differenz.
110108
"""
111109
size = len(self)
112-
result = []
113110
if size == len(other):
114-
for i in range(size):
115-
result.append(self.__components[i] - other.component(i))
111+
result = [self.__components[i] - other.component(i) for i in range(size)]
112+
return result
116113
else: # error case
117114
raise Exception("must have the same size")
118-
return Vector(result)
119115
def __mul__(self,other):
120116
"""
121117
mul implements the scalar multiplication
122118
and the dot-product
123119
"""
124-
ans = []
125120
if isinstance(other,float) or isinstance(other,int):
126-
for c in self.__components:
127-
ans.append(c*other)
121+
ans = [c*other for c in self.__components]
122+
return ans
128123
elif (isinstance(other,Vector) and (len(self) == len(other))):
129124
size = len(self)
130125
summe = 0
@@ -133,7 +128,6 @@ def __mul__(self,other):
133128
return summe
134129
else: # error case
135130
raise Exception("invalide operand!")
136-
return Vector(ans)
137131
def copy(self):
138132
"""
139133
copies this vector and returns it.

0 commit comments

Comments
 (0)