-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix issue with origin, add additional tests with different magnitudes…
… and origins
- Loading branch information
Showing
3 changed files
with
103 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,66 @@ | ||
import numpy as np | ||
import pyMBE | ||
from itertools import combinations | ||
pmb = pyMBE.pymbe_library() | ||
|
||
#Defining variables for test | ||
origin = [1, 2, 3] | ||
magnitude = 1 | ||
n = 10 | ||
tolerance = 1e-3 | ||
original_vector = pmb.generate_trialvectors(center=origin, | ||
radius=magnitude, | ||
n_samples=1, | ||
seed=None, | ||
on_surface=True)[0] | ||
perpendicular_vectors = [] | ||
for _ in range(n): | ||
perpendicular_vector = pmb.generate_trial_perpendicular_vector(vector=original_vector, | ||
origin=origin, | ||
magnitude=magnitude) | ||
perpendicular_vectors.append(perpendicular_vector) | ||
def check_if_different_perpendicular_vectors_are_generated(vector,magnitude,n=50): | ||
""" | ||
Checks if pmb.generate_trial_perpendicular_vector generates perpendicular vectors to `vector`. | ||
print(f"*** generate_trial_perpendicular_vector unit tests ***") | ||
print(f"*** Unit test: Check that the function creates {n} perpendicular vectors to an input vector. ***") | ||
for vector in perpendicular_vectors: | ||
np.testing.assert_almost_equal(actual = np.dot(original_vector, vector), | ||
desired = 0, | ||
decimal = 5, | ||
verbose = True) | ||
Args: | ||
vector(`array`,`float`): vector to which new perpendicular vectors will be generated. | ||
magnitude(`float`): magnitude of the perpendicular vectors. | ||
n(`int`): number of perpendicular vectors to be generated. | ||
""" | ||
perpendicular_vectors = [] | ||
for _ in range(n): | ||
perpendicular_vector = pmb.generate_trial_perpendicular_vector(vector=vector, | ||
magnitude=magnitude) | ||
perpendicular_vectors.append(perpendicular_vector) | ||
# Check that the function creates {n} perpendicular vectors to an input vector | ||
for pvector in perpendicular_vectors: | ||
np.testing.assert_almost_equal(actual = np.dot(pvector, vector), | ||
desired = 0, | ||
decimal = 5, | ||
verbose = True) | ||
# Check that the {n} perpendicular vectors are different | ||
for vector_pair in combinations(perpendicular_vectors, 2): | ||
if np.array_equal(vector_pair[0],vector_pair[1]): | ||
raise Exception(f"Error: pmb.generate_trial_perpendicular_vector two equal perpendicular vectors v1 = {vector_pair[0]} v2 = {vector_pair[1]}") | ||
# Check that the perpendicular vectors have the same magnitude as the input magnitude | ||
for pvector in perpendicular_vectors: | ||
np.testing.assert_almost_equal(actual = np.linalg.norm(pvector), | ||
desired = magnitude, | ||
decimal = 5, | ||
verbose = True) | ||
return | ||
|
||
print(f"***Unit test passed***") | ||
print(f"***Unit test: Check that the {n} perpendicular vectors are different.***") | ||
unique_set = set() | ||
for vector in perpendicular_vectors: | ||
vector_tuple = tuple(round(coord, 2) for coord in vector) | ||
unique_set.add(vector_tuple) | ||
np.testing.assert_equal(actual = len(unique_set), | ||
desired = n, | ||
verbose = True) | ||
print(f"***Unit test passed***") | ||
|
||
print(f"***Unit test: Check that the perpendicular vectors have the same magnitude as the input magnitude.***") | ||
for vector in perpendicular_vectors: | ||
np.testing.assert_almost_equal(actual = np.linalg.norm(vector), | ||
desired = magnitude, | ||
decimal = 5, | ||
verbose = True) | ||
print(f"***Unit test passed") | ||
print(f"*** generate_trial_perpendicular_vector unit tests ***") | ||
print(f"*** Unit test: Check that the function creates perpendicular vectors to an arbitrary vector of the same magnitude ***") | ||
vector = pmb.generate_random_points_in_a_sphere(center=[0,0,0], | ||
radius=1, | ||
n_samples=1, | ||
seed=None, | ||
on_surface=True)[0] | ||
check_if_different_perpendicular_vectors_are_generated(vector=vector, | ||
magnitude=1) | ||
print(f"*** Unit test passed ***") | ||
print(f"*** Unit test: Check that the function creates perpendicular vectors to a vector with an arbitrary origin ***") | ||
vector = pmb.generate_random_points_in_a_sphere(center=[1,2,3], | ||
radius=1, | ||
n_samples=1, | ||
seed=None, | ||
on_surface=True)[0] | ||
check_if_different_perpendicular_vectors_are_generated(vector=vector, | ||
magnitude=1) | ||
print(f"*** Unit test passed ***") | ||
print(f"*** Unit test: Check that the function creates perpendicular vectors with a different magnitude ***") | ||
vector = pmb.generate_random_points_in_a_sphere(center=[1,2,3], | ||
radius=2, | ||
n_samples=1, | ||
seed=None, | ||
on_surface=True)[0] | ||
check_if_different_perpendicular_vectors_are_generated(vector=vector, | ||
magnitude=2) | ||
print(f"*** Unit test passed ***") | ||
print(f"*** All unit tests passed ***") |