Newer
Older
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""Classe vecteur de nombres de dimension arbitraire."""
def __init__(self, *coord):
"""Initialise un vecteur de coordonnees "coord" """
self._coord = [c for c in coord]
def get_vec(self):
"""Retourne un tuple contenant les coordonnees du vecteur"""
return tuple(self._coord)
def set(self,*coord):
"""Donne les coordonnees "coord" au vecteur"""
self._coord = [c for c in coord]
def set_component(self, index, value):
"""Donne la valeur 'value' a la composante 'index' du vecteur"""
self._coord[index] = value
def norme(self):
"""Retourne la norme du vecteur"""
nn = 0
for x in self._coord:
nn += x*x
return math.sqrt(nn)
def prod_scal(self, vec):
"""Retourne le produit scalaire du vecteur avec vec"""
if not len(vec._coord) == len(self._coord):
print("Error, vectors of different dimensions...")
return 0
ps = 0
for i in range(len(self._coord)):
ps+=self._coord[i] * vec._coord[i]
return ps
def prod_vec(self,vec):
"""Retourne le produit vectoriel du vecteur avec vec"""
if not len(vec._coord) == 3 or not len(self._coord)==3:
print("Error, only possible for dim 3 vectors.")
return Vecteur(0,0,0)
return Vecteur(
self._coord[1]*vec._coord[2] - self._coord[2]*vec._coord[1],
-self._coord[0]*vec._coord[2] + self._coord[2]*vec._coord[0],
self._coord[0]*vec._coord[1] - self._coord[1]*vec._coord[0])
if __name__ == '__main__':
print("Debugging...")
u.set(1,2,3)
print("vecteur u = {} et |u| = {}".format(u.get_vec(),u.norme()))
v.set_component(1,0)
print("vecteur v = {} et |v| = {}".format(v.get_vec(),v.norme()))
print("u.v = {}".format(u.prod_scal(v)))
print("v.u = {}".format(v.prod_scal(u)))
w = u.prod_vec(v)
print("w = u x v = {}".format(w.get_vec()))
print(" v x u = {}".format(v.prod_vec(u).get_vec()))
print("w.u = {} ; u.w = {}".format(w.prod_scal(u),u.prod_scal(w)))
print("w.v = {} ; v.w = {}".format(w.prod_scal(v),v.prod_scal(w)))