From 054052144ed8c8d376eb3d72f5057c93b8dcb317 Mon Sep 17 00:00:00 2001 From: Adrian Mariano Date: Tue, 25 Jun 2024 19:24:48 -0400 Subject: [PATCH] sum/product optimizations --- math.scad | 108 ++++++++++++++++++++++--------------------- tests/test_math.scad | 12 +++++ 2 files changed, 68 insertions(+), 52 deletions(-) diff --git a/math.scad b/math.scad index bb5be7a..0058d02 100644 --- a/math.scad +++ b/math.scad @@ -721,16 +721,17 @@ function deltas(v, wrap=false) = // cumsum([1,2,3]); // returns [1,3,6] // cumsum([[1,2,3], [3,4,5], [5,6,7]]); // returns [[1,2,3], [4,6,8], [9,12,15]] function cumsum(v) = + v==[] ? [] : assert(is_consistent(v), "The input is not consistent." ) - len(v)<=1 ? v : - _cumsum(v,_i=1,_acc=[v[0]]); - -function _cumsum(v,_i=0,_acc=[]) = - _i>=len(v) ? _acc : - _cumsum( v, _i+1, [ each _acc, _acc[len(_acc)-1] + v[_i] ] ); - - - + [for (a = v[0], + i = 1 + ; + i <= len(v) + ; + a = i=len(v) ? _tot : - _product( v, - i+1, - ( is_vector(v[i])? v_mul(_tot,v[i]) : _tot*v[i] ) ); - +function product(list,right=true) = + list==[] ? [] : + is_matrix(list) ? + [for (a = list[0], + i = 1 + ; + i <= len(list) + ; + a = i