summaryrefslogtreecommitdiffstats
path: root/vector_lenghts.py
blob: af0c57b1a2f50deda85c496559ce58a913faaa33 (plain)
1
2
3
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
"""
> 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