Code cleanup
This commit is contained in:
parent
71f6c19f51
commit
c837b75604
@ -1,6 +1,6 @@
|
|||||||
# Project for PWR
|
# Project for PWR
|
||||||
|
|
||||||
Project in python for module "Praktikum Wissenschaftliche Rechnen" in "Applied Mathematics" at *TU Bergakademie Freiberg*.
|
Project in python for module "Praktikum Wissenschaftliches Rechnen" in "Applied Mathematics" at *TU Bergakademie Freiberg*.
|
||||||
|
|
||||||
# Task
|
# Task
|
||||||
Implement MPI parallel Matrix and Vector classes in Python and apply them to a numerical problem / algorithm.
|
Implement MPI parallel Matrix and Vector classes in Python and apply them to a numerical problem / algorithm.
|
@ -1,5 +1,3 @@
|
|||||||
import numpy
|
|
||||||
|
|
||||||
from matrix import Matrix
|
from matrix import Matrix
|
||||||
|
|
||||||
|
|
||||||
|
@ -65,18 +65,18 @@ class Vector:
|
|||||||
"ERROR_1: Die übergebene Variable ist kein Numpy-Array, Keine Initialisierung der Vector-Klasse möglich.")
|
"ERROR_1: Die übergebene Variable ist kein Numpy-Array, Keine Initialisierung der Vector-Klasse möglich.")
|
||||||
|
|
||||||
def __add__(self, other): # Überschreibung der Addition
|
def __add__(self, other): # Überschreibung der Addition
|
||||||
if (isinstance(self, Vector) and isinstance(other, Vector)):
|
if isinstance(self, Vector) and isinstance(other, Vector):
|
||||||
Add_Vec = Vector(np.arange(self.dim))
|
Add_Vec = Vector(np.arange(self.dim))
|
||||||
if (self.vshape[0] == other.vshape[0] and self.vshape[1] == other.vshape[1]):
|
if self.vshape[0] == other.vshape[0] and self.vshape[1] == other.vshape[1]:
|
||||||
for i in range(0, self.rank_size):
|
for i in range(0, self.rank_size):
|
||||||
Add_Vec.vec[i] = self.vec[i] + other.vec[i]
|
Add_Vec.vec[i] = self.vec[i] + other.vec[i]
|
||||||
else:
|
else:
|
||||||
raise ValueError("Die Dimensionen der Vektoren stimmen nicht überein, Addition nicht möglich.")
|
raise ValueError("Die Dimensionen der Vektoren stimmen nicht überein, Addition nicht möglich.")
|
||||||
elif (isinstance(self, Vector) and isinstance(other, (int, float, complex))):
|
elif isinstance(self, Vector) and isinstance(other, (int, float, complex)):
|
||||||
Add_Vec = Vector(np.arange(self.dim))
|
Add_Vec = Vector(np.arange(self.dim))
|
||||||
for i in range(0, self.rank_size):
|
for i in range(0, self.rank_size):
|
||||||
Add_Vec.vec[i] = self.vec[i] + other
|
Add_Vec.vec[i] = self.vec[i] + other
|
||||||
elif (isinstance(self, (int, float, complex)) and isinstance(other, Vector)):
|
elif isinstance(self, (int, float, complex)) and isinstance(other, Vector):
|
||||||
Add_Vec = Vector(np.arange(other.dim))
|
Add_Vec = Vector(np.arange(other.dim))
|
||||||
for i in range(0, other.rank_size):
|
for i in range(0, other.rank_size):
|
||||||
Add_Vec.vec[i] = other.vec[i] + self
|
Add_Vec.vec[i] = other.vec[i] + self
|
||||||
@ -85,8 +85,8 @@ class Vector:
|
|||||||
return Add_Vec
|
return Add_Vec
|
||||||
|
|
||||||
def __radd__(self, other): # Überschreibung der Addition eines Vektors von rechts
|
def __radd__(self, other): # Überschreibung der Addition eines Vektors von rechts
|
||||||
Add_Vec = Vector(np.arange(self.dim))
|
Vector(np.arange(self.dim))
|
||||||
if (isinstance(self, Vector) and isinstance(other, (int, float, complex))):
|
if isinstance(self, Vector) and isinstance(other, (int, float, complex)):
|
||||||
Add_Vec = Vector(np.arange(self.dim))
|
Add_Vec = Vector(np.arange(self.dim))
|
||||||
for i in range(0, self.rank_size):
|
for i in range(0, self.rank_size):
|
||||||
Add_Vec.vec[i] = self.vec[i] + other
|
Add_Vec.vec[i] = self.vec[i] + other
|
||||||
@ -95,18 +95,18 @@ class Vector:
|
|||||||
return Add_Vec
|
return Add_Vec
|
||||||
|
|
||||||
def __sub__(self, other): # Überschreibung der Subtraktion
|
def __sub__(self, other): # Überschreibung der Subtraktion
|
||||||
if (isinstance(self, Vector) and isinstance(other, Vector)):
|
if isinstance(self, Vector) and isinstance(other, Vector):
|
||||||
Sub_Vec = Vector(np.arange(self.dim))
|
Sub_Vec = Vector(np.arange(self.dim))
|
||||||
if (self.vshape[0] == other.vshape[0] and self.vshape[1] == other.vshape[1]):
|
if self.vshape[0] == other.vshape[0] and self.vshape[1] == other.vshape[1]:
|
||||||
for i in range(0, self.rank_size):
|
for i in range(0, self.rank_size):
|
||||||
Sub_Vec.vec[i] = self.vec[i] - other.vec[i]
|
Sub_Vec.vec[i] = self.vec[i] - other.vec[i]
|
||||||
else:
|
else:
|
||||||
raise ValueError("Die Dimension der Vektoren stimmen nicht überein, Subtraktion nicht möglich.")
|
raise ValueError("Die Dimension der Vektoren stimmen nicht überein, Subtraktion nicht möglich.")
|
||||||
elif (isinstance(self, Vector) and isinstance(other, (int, float, complex))):
|
elif isinstance(self, Vector) and isinstance(other, (int, float, complex)):
|
||||||
Sub_Vec = Vector(np.arange(self.dim))
|
Sub_Vec = Vector(np.arange(self.dim))
|
||||||
for i in range(0, self.rank_size):
|
for i in range(0, self.rank_size):
|
||||||
Sub_Vec.vec[i] = self.vec[i] - other
|
Sub_Vec.vec[i] = self.vec[i] - other
|
||||||
elif (isinstance(self, (int, float, complex)) and isinstance(other, Vector)):
|
elif isinstance(self, (int, float, complex)) and isinstance(other, Vector):
|
||||||
Sub_Vec = Vector(np.arange(self.dim))
|
Sub_Vec = Vector(np.arange(self.dim))
|
||||||
for i in range(0, other.rank_size):
|
for i in range(0, other.rank_size):
|
||||||
Sub_Vec.vec[i] = other.vec[i] - self
|
Sub_Vec.vec[i] = other.vec[i] - self
|
||||||
@ -116,7 +116,7 @@ class Vector:
|
|||||||
|
|
||||||
def __rsub__(self, other): # Subtraktion einer Zahl von einem Vektor
|
def __rsub__(self, other): # Subtraktion einer Zahl von einem Vektor
|
||||||
Sub_Vec = Vector(np.arange(self.dim))
|
Sub_Vec = Vector(np.arange(self.dim))
|
||||||
if (isinstance(self, Vector) and isinstance(other, (float, int, complex))):
|
if isinstance(self, Vector) and isinstance(other, (float, int, complex)):
|
||||||
for i in range(0, self.rank_size):
|
for i in range(0, self.rank_size):
|
||||||
Sub_Vec.vec[i] = self.vec[i] - other
|
Sub_Vec.vec[i] = self.vec[i] - other
|
||||||
else:
|
else:
|
||||||
@ -124,26 +124,26 @@ class Vector:
|
|||||||
return Sub_Vec
|
return Sub_Vec
|
||||||
|
|
||||||
def __mul__(self, other): # Überschreibung der Multiplikation
|
def __mul__(self, other): # Überschreibung der Multiplikation
|
||||||
if (isinstance(self, Vector) and isinstance(other, Vector)):
|
if isinstance(self, Vector) and isinstance(other, Vector):
|
||||||
Mult_Vec = Vector(np.arange(self.dim))
|
Mult_Vec = Vector(np.arange(self.dim))
|
||||||
if (self.vshape[0] == other.vshape[0] and self.vshape[1] == other.vshape[
|
if (self.vshape[0] == other.vshape[0] and self.vshape[1] == other.vshape[
|
||||||
1]): # Elementweise Multiplikation
|
1]): # Elementweise Multiplikation
|
||||||
for i in range(0, self.rank_size):
|
for i in range(0, self.rank_size):
|
||||||
Mult_Vec.vec[i] = self.vec[i] * other.vec[i]
|
Mult_Vec.vec[i] = self.vec[i] * other.vec[i]
|
||||||
elif (self.vshape[1] == other.vshape[0] and self.vshape[0] == 1): # Inneres Produkt (Skalarprodukt)
|
elif self.vshape[1] == other.vshape[0] and self.vshape[0] == 1: # Inneres Produkt (Skalarprodukt)
|
||||||
skal_prod = 0
|
skal_prod = 0
|
||||||
for i in range(0, self.rank_size):
|
for i in range(0, self.rank_size):
|
||||||
skal_prod = skal_prod + self.vec[i] * other.vec[i]
|
skal_prod = skal_prod + self.vec[i] * other.vec[i]
|
||||||
return skal_prod
|
return skal_prod
|
||||||
elif (self.vshape[0] == other.vshape[1] and self.vshape[1] == 1):
|
elif self.vshape[0] == other.vshape[1] and self.vshape[1] == 1:
|
||||||
raise ValueError("Kann erst implementiert werden, wenn Matrix-Klasse existiert.")
|
raise ValueError("Kann erst implementiert werden, wenn Matrix-Klasse existiert.")
|
||||||
else:
|
else:
|
||||||
raise ValueError("Die Dimensionen der Vektoren stimmen nicht überein, Multiplikation nicht möglich.")
|
raise ValueError("Die Dimensionen der Vektoren stimmen nicht überein, Multiplikation nicht möglich.")
|
||||||
elif (isinstance(self, Vector) and isinstance(other, (int, float, complex))):
|
elif isinstance(self, Vector) and isinstance(other, (int, float, complex)):
|
||||||
Mult_Vec = Vector(np.arange(self.dim))
|
Mult_Vec = Vector(np.arange(self.dim))
|
||||||
for i in range(0, self.rank_size):
|
for i in range(0, self.rank_size):
|
||||||
Mult_Vec.vec[i] = self.vec[i] * other
|
Mult_Vec.vec[i] = self.vec[i] * other
|
||||||
elif (isinstance(self, (int, float, complex)) and isinstance(other, Vector)):
|
elif isinstance(self, (int, float, complex)) and isinstance(other, Vector):
|
||||||
Mult_Vec = Vector(np.arange(self.dim))
|
Mult_Vec = Vector(np.arange(self.dim))
|
||||||
for i in range(0, other.rank_size):
|
for i in range(0, other.rank_size):
|
||||||
Mult_Vec.vec[i] = other.vec[i] * self
|
Mult_Vec.vec[i] = other.vec[i] * self
|
||||||
@ -153,7 +153,7 @@ class Vector:
|
|||||||
|
|
||||||
def __rmul__(self, other): # Rechtsseitige Multiplikation von einer Zahl an einen Vektor
|
def __rmul__(self, other): # Rechtsseitige Multiplikation von einer Zahl an einen Vektor
|
||||||
Mult_Vec = Vector(np.arange(self.dim))
|
Mult_Vec = Vector(np.arange(self.dim))
|
||||||
if (isinstance(self, Vector) and isinstance(other, (int, float, complex))):
|
if isinstance(self, Vector) and isinstance(other, (int, float, complex)):
|
||||||
for i in range(0, self.rank_size):
|
for i in range(0, self.rank_size):
|
||||||
Mult_Vec.vec[i] = self.vec[i] * other
|
Mult_Vec.vec[i] = self.vec[i] * other
|
||||||
else:
|
else:
|
||||||
@ -162,21 +162,21 @@ class Vector:
|
|||||||
|
|
||||||
def __truediv__(self, other):
|
def __truediv__(self, other):
|
||||||
Div_Vec = Vector(np.arange(self.dim, dtype=np.double))
|
Div_Vec = Vector(np.arange(self.dim, dtype=np.double))
|
||||||
if (isinstance(self, Vector) and isinstance(other, Vector)):
|
if isinstance(self, Vector) and isinstance(other, Vector):
|
||||||
if (self.vshape[0] == other.vshape[0] and self.vshape[1] == other.vshape[1]):
|
if self.vshape[0] == other.vshape[0] and self.vshape[1] == other.vshape[1]:
|
||||||
for i in range(0, self.rank_size):
|
for i in range(0, self.rank_size):
|
||||||
if (other.vec[i] == 0):
|
if other.vec[i] == 0:
|
||||||
raise ValueError("Ein Eintrag des Divisor-Vektors ist 0, Divion nicht möglich.")
|
raise ValueError("Ein Eintrag des Divisor-Vektors ist 0, Divion nicht möglich.")
|
||||||
Div_Vec.vec[i] = self.vec[i] / other.vec[i]
|
Div_Vec.vec[i] = self.vec[i] / other.vec[i]
|
||||||
else:
|
else:
|
||||||
raise ValueError("Die Dimensionen der Vektoren stimmen nicht überein, Division nicht möglich.")
|
raise ValueError("Die Dimensionen der Vektoren stimmen nicht überein, Division nicht möglich.")
|
||||||
elif (isinstance(self, (int, float, complex)) and isinstance(other, Vector)):
|
elif isinstance(self, (int, float, complex)) and isinstance(other, Vector):
|
||||||
for i in range(0, other.rank_size):
|
for i in range(0, other.rank_size):
|
||||||
if (other.vec[i] == 0):
|
if other.vec[i] == 0:
|
||||||
raise ValueError("Ein Eintrag des Divisor-Vektors ist 0, Divion nicht möglich.")
|
raise ValueError("Ein Eintrag des Divisor-Vektors ist 0, Divion nicht möglich.")
|
||||||
Div_Vec.vec[i] = self / other.vec[i]
|
Div_Vec.vec[i] = self / other.vec[i]
|
||||||
elif (isinstance(self, Vector) and isinstance(other, (float, int, complex))):
|
elif isinstance(self, Vector) and isinstance(other, (float, int, complex)):
|
||||||
if (other == 0):
|
if other == 0:
|
||||||
raise ValueError("Division durch Null ist nicht möglich.")
|
raise ValueError("Division durch Null ist nicht möglich.")
|
||||||
else:
|
else:
|
||||||
for i in range(0, self.rank_size):
|
for i in range(0, self.rank_size):
|
||||||
@ -187,8 +187,8 @@ class Vector:
|
|||||||
|
|
||||||
def __rtruediv__(self, other):
|
def __rtruediv__(self, other):
|
||||||
Div_Vec = Vector(np.arange(self.dim, dtype=np.double))
|
Div_Vec = Vector(np.arange(self.dim, dtype=np.double))
|
||||||
if (isinstance(self, Vector) and isinstance(other, (float, int, complex))):
|
if isinstance(self, Vector) and isinstance(other, (float, int, complex)):
|
||||||
if (other == 0):
|
if other == 0:
|
||||||
raise ValueError("Division durch Null ist nicht möglich.")
|
raise ValueError("Division durch Null ist nicht möglich.")
|
||||||
else:
|
else:
|
||||||
for i in range(0, self.rank_size):
|
for i in range(0, self.rank_size):
|
||||||
@ -202,12 +202,12 @@ class Vector:
|
|||||||
return Neg_Vec
|
return Neg_Vec
|
||||||
|
|
||||||
def shape(self):
|
def shape(self):
|
||||||
if (self.rank == 0):
|
if self.rank == 0:
|
||||||
return self.vshape
|
return self.vshape
|
||||||
|
|
||||||
def T(self):
|
def T(self):
|
||||||
Transpose = self
|
Transpose = self
|
||||||
if (self.kind == 'row'):
|
if self.kind == 'row':
|
||||||
Transpose.kind = 'column'
|
Transpose.kind = 'column'
|
||||||
else:
|
else:
|
||||||
Transpose.kind = 'row'
|
Transpose.kind = 'row'
|
||||||
@ -221,101 +221,101 @@ class Vector:
|
|||||||
def str(self): # Rückgabe des gesamten Vektors als string
|
def str(self): # Rückgabe des gesamten Vektors als string
|
||||||
str_rep = ''
|
str_rep = ''
|
||||||
|
|
||||||
if (self.rank == 0):
|
if self.rank == 0:
|
||||||
if (self.kind == 'row'):
|
if self.kind == 'row':
|
||||||
str_rep = '[' + ','.join(map(str, self.vec))
|
str_rep = '[' + ','.join(map(str, self.vec))
|
||||||
if (self.kind == 'column'):
|
if self.kind == 'column':
|
||||||
str_rep = '[' + '\n'.join(map(str, self.vec))
|
str_rep = '[' + '\n'.join(map(str, self.vec))
|
||||||
if (self.size > 1):
|
if self.size > 1:
|
||||||
self.comm.send(str_rep, dest=self.rank + 1)
|
self.comm.send(str_rep, dest=self.rank + 1)
|
||||||
|
|
||||||
elif (self.rank == self.size - 1):
|
elif self.rank == self.size - 1:
|
||||||
if (self.kind == 'row'):
|
if self.kind == 'row':
|
||||||
str_rep = self.comm.recv(source=self.rank - 1) + ',' + ','.join(map(str, self.vec))
|
str_rep = self.comm.recv(source=self.rank - 1) + ',' + ','.join(map(str, self.vec))
|
||||||
if (self.kind == 'column'):
|
if self.kind == 'column':
|
||||||
str_rep = self.comm.recv(source=self.rank - 1) + '\n' + '\n'.join(map(str, self.vec))
|
str_rep = self.comm.recv(source=self.rank - 1) + '\n' + '\n'.join(map(str, self.vec))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
if (self.kind == 'row'):
|
if self.kind == 'row':
|
||||||
str_rep = self.comm.recv(source=self.rank - 1) + ',' + ','.join(map(str, self.vec))
|
str_rep = self.comm.recv(source=self.rank - 1) + ',' + ','.join(map(str, self.vec))
|
||||||
if (self.kind == 'column'):
|
if self.kind == 'column':
|
||||||
str_rep = self.comm.recv(source=self.rank - 1) + '\n' + '\n'.join(map(str, self.vec))
|
str_rep = self.comm.recv(source=self.rank - 1) + '\n' + '\n'.join(map(str, self.vec))
|
||||||
self.comm.send(str_rep, dest=self.rank + 1)
|
self.comm.send(str_rep, dest=self.rank + 1)
|
||||||
|
|
||||||
str_rep = self.comm.bcast(str_rep, root=self.size - 1)
|
str_rep = self.comm.bcast(str_rep, root=self.size - 1)
|
||||||
if (self.rank == 0):
|
if self.rank == 0:
|
||||||
return str_rep + ']'
|
return str_rep + ']'
|
||||||
|
|
||||||
def string(self, limit_entry): # Gibt den Vektor als String zurück bis zum Eintrag limit_entry
|
def string(self, limit_entry): # Gibt den Vektor als String zurück bis zum Eintrag limit_entry
|
||||||
str_rep = ''
|
str_rep = ''
|
||||||
|
|
||||||
if (limit_entry > self.vec_size):
|
if limit_entry > self.vec_size:
|
||||||
raise ValueError("ERROR_4: Die eingegebene Zahl ist größer, als der größte Index des Vectors.")
|
raise ValueError("ERROR_4: Die eingegebene Zahl ist größer, als der größte Index des Vectors.")
|
||||||
|
|
||||||
# Rank 0
|
# Rank 0
|
||||||
if (self.rank == 0 and limit_entry <= self.end_idx): # Limit_entry befindet sich im Rang 0
|
if self.rank == 0 and limit_entry <= self.end_idx: # Limit_entry befindet sich im Rang 0
|
||||||
if (self.kind == 'row'):
|
if self.kind == 'row':
|
||||||
str_rep = '[' + ','.join(map(str, self.vec[:limit_entry]))
|
str_rep = '[' + ','.join(map(str, self.vec[:limit_entry]))
|
||||||
if (self.kind == 'column'):
|
if self.kind == 'column':
|
||||||
str_rep = '[' + '\n'.join(map(str, self.vec[:limit_entry]))
|
str_rep = '[' + '\n'.join(map(str, self.vec[:limit_entry]))
|
||||||
if (self.size > 1):
|
if self.size > 1:
|
||||||
self.comm.send(str_rep, dest=self.rank + 1)
|
self.comm.send(str_rep, dest=self.rank + 1)
|
||||||
if (self.rank == 0 and limit_entry > self.end_idx): # Limit_entry befindet sich nicht im Rang 0
|
if self.rank == 0 and limit_entry > self.end_idx: # Limit_entry befindet sich nicht im Rang 0
|
||||||
if (self.kind == 'row'):
|
if self.kind == 'row':
|
||||||
str_rep = '[' + ','.join(map(str, self.vec))
|
str_rep = '[' + ','.join(map(str, self.vec))
|
||||||
if (self.kind == 'column'):
|
if self.kind == 'column':
|
||||||
str_rep = '[' + '\n'.join(map(str, self.vec))
|
str_rep = '[' + '\n'.join(map(str, self.vec))
|
||||||
if (self.size > 1):
|
if self.size > 1:
|
||||||
self.comm.send(str_rep, dest=self.rank + 1)
|
self.comm.send(str_rep, dest=self.rank + 1)
|
||||||
|
|
||||||
# Rank im Intervall [1,size-1]
|
# Rank im Intervall [1,size-1]
|
||||||
if (
|
if (
|
||||||
self.rank > 0 and self.rank < self.size - 1 and limit_entry <= self.start_idx): # wenn lim_ent == start_idx, dann wurden bereits alle relevanten Indizes im String gespeichert, da Vector nullinitialisiert ist
|
0 < self.rank < self.size - 1 and limit_entry <= self.start_idx): # wenn lim_ent == start_idx, dann wurden bereits alle relevanten Indizes im String gespeichert, da Vector nullinitialisiert ist
|
||||||
str_rep = self.comm.recv(source=self.rank - 1)
|
str_rep = self.comm.recv(source=self.rank - 1)
|
||||||
self.comm.send(str_rep, dest=self.rank + 1)
|
self.comm.send(str_rep, dest=self.rank + 1)
|
||||||
if (
|
if (
|
||||||
self.rank > 0 and self.rank < self.size - 1 and limit_entry > self.start_idx and limit_entry <= self.end_idx):
|
0 < self.rank < self.size - 1 and self.start_idx < limit_entry <= self.end_idx):
|
||||||
if (self.kind == 'row'):
|
if self.kind == 'row':
|
||||||
str_rep = self.comm.recv(source=self.rank - 1) + ',' + ','.join(
|
str_rep = self.comm.recv(source=self.rank - 1) + ',' + ','.join(
|
||||||
map(str, self.vec[:(limit_entry - self.start_idx)]))
|
map(str, self.vec[:(limit_entry - self.start_idx)]))
|
||||||
if (self.kind == 'column'):
|
if self.kind == 'column':
|
||||||
str_rep = self.comm.recv(source=self.rank - 1) + '\n' + '\n'.join(
|
str_rep = self.comm.recv(source=self.rank - 1) + '\n' + '\n'.join(
|
||||||
map(str, self.vec[:(limit_entry - self.start_idx)])) + ']'
|
map(str, self.vec[:(limit_entry - self.start_idx)])) + ']'
|
||||||
self.comm.send(str_rep, dest=self.rank + 1)
|
self.comm.send(str_rep, dest=self.rank + 1)
|
||||||
if (self.rank > 0 and self.rank < self.size - 1 and limit_entry > self.end_idx):
|
if 0 < self.rank < self.size - 1 and limit_entry > self.end_idx:
|
||||||
if (self.kind == 'row'):
|
if self.kind == 'row':
|
||||||
str_rep = self.comm.recv(source=self.rank - 1) + ',' + ','.join(map(str, self.vec))
|
str_rep = self.comm.recv(source=self.rank - 1) + ',' + ','.join(map(str, self.vec))
|
||||||
if (self.kind == 'column'):
|
if self.kind == 'column':
|
||||||
str_rep = self.comm.recv(source=self.rank - 1) + '\n' + '\n'.join(map(str, self.vec))
|
str_rep = self.comm.recv(source=self.rank - 1) + '\n' + '\n'.join(map(str, self.vec))
|
||||||
self.comm.send(str_rep, dest=self.rank + 1)
|
self.comm.send(str_rep, dest=self.rank + 1)
|
||||||
|
|
||||||
# Rank size-1
|
# Rank size-1
|
||||||
if (self.rank == self.size - 1 and limit_entry <= self.start_idx and self.rank > 1):
|
if self.rank == self.size - 1 and limit_entry <= self.start_idx and self.rank > 1:
|
||||||
str_rep = self.comm.recv(source=self.rank - 1)
|
str_rep = self.comm.recv(source=self.rank - 1)
|
||||||
|
|
||||||
if (self.rank == self.size - 1 and limit_entry >= self.start_idx and self.rank > 1):
|
if self.rank == self.size - 1 and limit_entry >= self.start_idx and self.rank > 1:
|
||||||
if (self.kind == 'row'):
|
if self.kind == 'row':
|
||||||
str_rep = self.comm.recv(source=self.rank - 1) + ',' + ','.join(
|
str_rep = self.comm.recv(source=self.rank - 1) + ',' + ','.join(
|
||||||
map(str, self.vec[:(limit_entry - self.start_idx)]))
|
map(str, self.vec[:(limit_entry - self.start_idx)]))
|
||||||
if (self.kind == 'column'):
|
if self.kind == 'column':
|
||||||
str_rep = self.comm.recv(source=self.rank - 1) + '\n' + '\n'.join(
|
str_rep = self.comm.recv(source=self.rank - 1) + '\n' + '\n'.join(
|
||||||
map(str, self.vec[:(limit_entry - self.start_idx)]))
|
map(str, self.vec[:(limit_entry - self.start_idx)]))
|
||||||
|
|
||||||
str_rep = self.comm.bcast(str_rep, root=self.size - 1)
|
str_rep = self.comm.bcast(str_rep, root=self.size - 1)
|
||||||
if (self.rank == 0):
|
if self.rank == 0:
|
||||||
return str_rep + ']'
|
return str_rep + ']'
|
||||||
|
|
||||||
def norm(self): # Berechnung der 2-Norm / euklidischen Norm
|
def norm(self): # Berechnung der 2-Norm / euklidischen Norm
|
||||||
norm = 0
|
0
|
||||||
sum_of_squares = 0
|
sum_of_squares = 0
|
||||||
if (self.rank == 0):
|
if self.rank == 0:
|
||||||
for i in range(0, self.rank_size):
|
for i in range(0, self.rank_size):
|
||||||
sum_of_squares = sum_of_squares + self.vec[i] ** 2
|
sum_of_squares = sum_of_squares + self.vec[i] ** 2
|
||||||
|
|
||||||
if (self.size > 1):
|
if self.size > 1:
|
||||||
self.comm.send(sum_of_squares, dest=self.rank + 1)
|
self.comm.send(sum_of_squares, dest=self.rank + 1)
|
||||||
|
|
||||||
elif (self.rank == self.size - 1):
|
elif self.rank == self.size - 1:
|
||||||
sum_of_squares = self.comm.recv(source=self.rank - 1)
|
sum_of_squares = self.comm.recv(source=self.rank - 1)
|
||||||
for i in range(0, self.rank_size):
|
for i in range(0, self.rank_size):
|
||||||
sum_of_squares = sum_of_squares + self.vec[i] ** 2
|
sum_of_squares = sum_of_squares + self.vec[i] ** 2
|
||||||
@ -333,7 +333,7 @@ class Vector:
|
|||||||
|
|
||||||
def normalize(self): # Normalisierung eines Vectors
|
def normalize(self): # Normalisierung eines Vectors
|
||||||
norm = self.norm()
|
norm = self.norm()
|
||||||
if (norm == 0):
|
if norm == 0:
|
||||||
return self
|
return self
|
||||||
normalized_vec = self / norm
|
normalized_vec = self / norm
|
||||||
return normalized_vec
|
return normalized_vec
|
||||||
|
Loading…
x
Reference in New Issue
Block a user