summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKyle K <kylek389@gmail.com>2021-08-02 00:17:07 -0500
committerKyle K <kylek389@gmail.com>2021-08-02 00:17:07 -0500
commit5f20ddef75af630d64ee2987d2d8d24d484254aa (patch)
tree0c744c5df9b381af567e274a7e4d7f19c8add7bb
parent3d99158564e8082222138cc1b1e0a7dfcd7e8f66 (diff)
downloadPythonPractice-5f20ddef75af630d64ee2987d2d8d24d484254aa.tar.gz
PythonPractice-5f20ddef75af630d64ee2987d2d8d24d484254aa.tar.bz2
PythonPractice-5f20ddef75af630d64ee2987d2d8d24d484254aa.zip
vector example
-rw-r--r--vector_lenghts.py34
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