From ba9b7c5b3bda8e3bd215528683a589d40fd8d49e Mon Sep 17 00:00:00 2001 From: Revar Desmera Date: Sun, 12 May 2019 13:32:34 -0700 Subject: [PATCH] Added product() --- math.scad | 12 ++++++++++++ tests/test_math.scad | 14 ++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/math.scad b/math.scad index 8755b15..31a9ab3 100644 --- a/math.scad +++ b/math.scad @@ -275,6 +275,18 @@ function sum_of_sines(a, sines) = function deltas(v) = len(v)<2? v : [for (p=pair(v)) p.y-p.x]; +// Function: product() +// Description: +// Returns the product of all entries in the given list. +// If passed an array of vectors, returns a vector of products of each part. +// Arguments: +// v = The list to get the product of. +// Example: +// product([2,3,4]); // returns 24. +// product([[1,2,3], [3,4,5], [5,6,7]]); // returns [15, 48, 105] +function product(v, i=0, tot=undef) = i>=len(v)? tot : product(v, i+1, ((tot==undef)? v[i] : is_list(v[i])? vmul(tot,v[i]) : tot*v[i])); + + // Function: mean() // Description: // Returns the mean of all entries in the given array. diff --git a/tests/test_math.scad b/tests/test_math.scad index 466a564..b8d60a6 100644 --- a/tests/test_math.scad +++ b/tests/test_math.scad @@ -253,6 +253,20 @@ module test_sum_of_sines() { test_sum_of_sines(); +module test_deltas() { + assert(deltas([2,5,9,17]) == [3,4,8]); + assert(deltas([[1,2,3], [3,6,8], [4,8,11]]) == [[2,4,5], [1,2,3]]); +} +test_deltas(); + + +module test_product() { + assert(product([2,3,4]) == 24); + assert(product([[1,2,3], [3,4,5], [5,6,7]]) == [15, 48, 105]); +} +test_product(); + + module test_mean() { assert(mean([2,3,4]) == 3); assert(mean([[1,2,3], [3,4,5], [5,6,7]]) == [3,4,5]);