@@ -45,7 +45,7 @@ class Vector(object):
45
45
changeComponent(pos,value) : changes the specified component.
46
46
TODO: compare-operator
47
47
"""
48
- def __init__ (self ,components ):
48
+ def __init__ (self ,components = [] ):
49
49
"""
50
50
input: components or nothing
51
51
simple constructor for init the vector
@@ -95,36 +95,31 @@ def __add__(self,other):
95
95
returns a new vector that represents the sum.
96
96
"""
97
97
size = len (self )
98
- result = []
99
98
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 )
102
101
else :
103
102
raise Exception ("must have the same size" )
104
- return Vector (result )
105
103
def __sub__ (self ,other ):
106
104
"""
107
105
input: other vector
108
106
assumes: other vector has the same size
109
107
returns a new vector that represents the differenz.
110
108
"""
111
109
size = len (self )
112
- result = []
113
110
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
116
113
else : # error case
117
114
raise Exception ("must have the same size" )
118
- return Vector (result )
119
115
def __mul__ (self ,other ):
120
116
"""
121
117
mul implements the scalar multiplication
122
118
and the dot-product
123
119
"""
124
- ans = []
125
120
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
128
123
elif (isinstance (other ,Vector ) and (len (self ) == len (other ))):
129
124
size = len (self )
130
125
summe = 0
@@ -133,7 +128,6 @@ def __mul__(self,other):
133
128
return summe
134
129
else : # error case
135
130
raise Exception ("invalide operand!" )
136
- return Vector (ans )
137
131
def copy (self ):
138
132
"""
139
133
copies this vector and returns it.
0 commit comments