diff options
| -rw-r--r-- | vector_lenghts.py | 34 | 
1 files changed, 34 insertions, 0 deletions
diff --git a/vector_lenghts.py b/vector_lenghts.py new file mode 100644 index 0000000..af0c57b --- /dev/null +++ b/vector_lenghts.py @@ -0,0 +1,34 @@ +""" +> Generate N amount of 3-dimensional vectors with random floating point values and sort them +> Then square each component, sum them and take the square root (aka get the length of the vector), do this in parallel. +> Then zip the lengths together with their corresponding vectors +> Then print out the first vector and its length and ensure that it's same as what you started with + +""" + + +import math +import random +from multiprocessing import Pool + +def get_vectors(n: int, d: int) -> list: +    return [gen_vector(d) for i in range(n)] + +def gen_vector(d: int) -> list: +    return sorted([random.uniform(1, 10) for i in range(d)]) + +def vector_length(vectors: list) -> list: +    with Pool(4) as p: +        return p.map(find_length, vectors) + +def find_length(vector: list) -> float: +    return math.sqrt(sum([(component*component) for component in vector])) + +if __name__ == "__main__": + +    n = 999 +    d = 3 +    vectors = get_vectors(n, d) +    length = vector_length(vectors) +    vl = list(zip(vectors, length)) +    print(vl)  # ToDo
\ No newline at end of file  | 
