From f67226a6dd61105766f0dc3b6e72329e0d5419b1 Mon Sep 17 00:00:00 2001 From: RonaldoCMP Date: Wed, 29 Jul 2020 03:19:01 +0100 Subject: [PATCH] formating --- arrays.scad | 42 +++--------------------------------------- math.scad | 52 +++++++++++++++++----------------------------------- 2 files changed, 20 insertions(+), 74 deletions(-) diff --git a/arrays.scad b/arrays.scad index 365649a..aa401bd 100644 --- a/arrays.scad +++ b/arrays.scad @@ -73,9 +73,6 @@ function select(list, start, end=undef) = : concat([for (i = [s:1:l-1]) list[i]], [for (i = [0:1:e]) list[i]]) ; - - - // Function: slice() // Description: // Returns a slice of a list. The first item is index 0. @@ -101,8 +98,6 @@ function slice(list,start,end) = ) [for (i=[s:1:e-1]) if (e>s) list[i]]; - - // Function: in_list() // Description: Returns true if value `val` is in list `list`. When `val==NAN` the answer will be false for any list. // Arguments: @@ -118,7 +113,6 @@ function in_list(val,list,idx=undef) = s==[] || s[0]==[] ? false : is_undef(idx) ? val==list[s] : val==list[s][idx]; - // Function: min_index() @@ -209,7 +203,6 @@ function repeat(val, n, i=0) = [for (j=[1:1:n[i]]) repeat(val, n, i+1)]; - // Function: list_range() // Usage: // list_range(n, [s], [e]) @@ -246,8 +239,6 @@ function list_range(n=undef, s=0, e=undef, step=undef) = [for (i=[0:1:n-1]) s+step*i ] : assert( is_vector([s,step,e]), "Start `s`, step `step` and end `e` must be numbers.") [for (v=[s:step:e]) v] ; - - // Section: List Manipulation @@ -315,8 +306,6 @@ function deduplicate(list, closed=false, eps=EPSILON) = : [for (i=[0:1:l-1]) if (i==end || !approx(list[i], list[(i+1)%l], eps)) list[i]]; - - // Function: deduplicate_indexed() // Usage: // new_idxs = deduplicate_indexed(list, indices, [closed], [eps]); @@ -351,8 +340,6 @@ function deduplicate_indexed(list, indices, closed=false, eps=EPSILON) = ]; - - // Function: repeat_entries() // Usage: // newlist = repeat_entries(list, N) @@ -392,8 +379,6 @@ function repeat_entries(list, N, exact = true) = [for(i=[0:length-1]) each repeat(list[i],reps[i])]; - - // Function: list_set() // Usage: // list_set(list, indices, values, [dflt], [minlen]) @@ -433,7 +418,6 @@ function list_set(list=[],indices,values,dflt=0,minlen=0) = ]; - // Function: list_insert() // Usage: // list_insert(list, indices, values); @@ -465,8 +449,6 @@ function list_insert(list, indices, values, _i=0) = ]; - - // Function: list_remove() // Usage: // list_remove(list, indices) @@ -489,8 +471,6 @@ function list_remove(list, indices) = if ( []==search(i,indices,1) ) list[i] ]; - - // Function: list_remove_values() // Usage: // list_remove_values(list,values,all=false) = @@ -560,8 +540,6 @@ function list_bset(indexset, valuelist, dflt=0) = ); - - // Section: List Length Manipulation // Function: list_shortest() @@ -574,7 +552,6 @@ function list_shortest(array) = min([for (v = array) len(v)]); - // Function: list_longest() // Description: // Returns the length of the longest sublist in a list of lists. @@ -624,7 +601,6 @@ function list_fit(array, length, fill) = : list_pad(array,length,fill); - // Section: List Shuffling and Sorting // Function: shuffle() @@ -679,6 +655,7 @@ function _sort_vectors2(arr) = ) concat( _sort_vectors2(lesser), equal, _sort_vectors2(greater) ); + // Sort a vector of vectors based on the first three entries of each vector // Lexicographic order, remaining entries of vector ignored function _sort_vectors3(arr) = @@ -756,6 +733,7 @@ function _sort_general(arr, idx=undef) = ) concat(_sort_general(lesser,idx), equal, _sort_general(greater,idx)); + function _sort_general(arr, idx=undef) = (len(arr)<=1) ? arr : let( @@ -774,9 +752,6 @@ function _sort_general(arr, idx=undef) = concat(_sort_general(lesser,idx), equal, _sort_general(greater,idx)); - - - // Function: sort() // Usage: // sort(list, [idx]) @@ -809,7 +784,6 @@ function sort(list, idx=undef) = : _sort_general(list); - // Function: sortidx() // Description: // Given a list, calculates the sort order of the list, and returns @@ -853,6 +827,7 @@ function sortidx(list, idx=undef) = : // general case subindex(_sort_general(aug, idx=list_range(s=1,n=len(aug)-1)), 0); + function sortidx(list, idx=undef) = list==[] ? [] : let( size = array_dim(list), @@ -891,7 +866,6 @@ function unique(arr) = ]; - // Function: unique_count() // Usage: // unique_count(arr); @@ -908,8 +882,6 @@ function unique_count(arr) = [ select(arr,ind), deltas( concat(ind,[len(arr)]) ) ]; - - // Section: List Iteration Helpers // Function: idx() @@ -1104,8 +1076,6 @@ function set_union(a, b, get_indices=false) = ) [idxs, nset]; - - // Function: set_difference() // Usage: // s = set_difference(a, b); @@ -1125,7 +1095,6 @@ function set_difference(a, b) = [ for (i=idx(a)) if(found[i]==[]) a[i] ]; - // Function: set_intersection() // Usage: // s = set_intersection(a, b); @@ -1145,8 +1114,6 @@ function set_intersection(a, b) = [ for (i=idx(a)) if(found[i]!=[]) a[i] ]; - - // Section: Array Manipulation // Function: add_scalar() @@ -1165,7 +1132,6 @@ function add_scalar(v,s) = is_finite(s) ? [for (x=v) is_list(x)? add_scalar(x,s) : is_finite(x) ? x+s: x] : v; - // Function: subindex() // Description: // For each array item, return the indexed subitem. @@ -1349,6 +1315,4 @@ function transpose(arr) = : arr; - - // vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap diff --git a/math.scad b/math.scad index 23a8174..2e915ee 100644 --- a/math.scad +++ b/math.scad @@ -107,6 +107,7 @@ function binomial(n) = c = c*(n-i)/(i+1), i = i+1 ) c ] ; + // Function: binomial_coefficient() // Usage: // x = binomial_coefficient(n,k); @@ -129,6 +130,7 @@ function binomial_coefficient(n,k) = ) c] ) b[len(b)-1]; + // Function: lerp() // Usage: // x = lerp(a, b, u); @@ -166,7 +168,6 @@ function lerp(a,b,u) = [for (v = u) lerp(a,b,v)]; - // Function: all_numeric() // Usage: // x = all_numeric(list); @@ -184,22 +185,6 @@ function all_numeric(list) = || []==[for(vi=list) if( !all_numeric(vi)) 0] ; -// Function: is_addable() -// Usage: -// x = is_addable(list); -// Description: -// Returns true if `list` is both consistent and numerical. -// Arguments: -// list = The list to check -// Example: -// x = is_addable([[[1],2],[[0],2]])); // Returns: true -// y = is_addable([[[1],2],[[0],[]]])); // Returns: false -function is_addable(list) = // consistent and numerical - is_list_of(list,list[0]) - && ( ( let(v = list*list[0]) is_num(0*(v*v)) ) - || []==[for(vi=list) if( !all_numeric(vi)) 0] ); - - // Section: Hyperbolic Trigonometry @@ -245,7 +230,6 @@ function atanh(x) = ln((1+x)/(1-x))/2; - // Section: Quantization // Function: quant() @@ -273,14 +257,13 @@ function atanh(x) = // quant([9,10,10.4,10.5,11,12],3); // Returns: [9,9,9,12,12,12] // quant([[9,10,10.4],[10.5,11,12]],3); // Returns: [[9,9,9],[12,12,12]] function quant(x,y) = - assert(is_int(y), "The multiple must be an integer.") + assert(is_finite(y), "The multiple must be an integer.") is_list(x) ? [for (v=x) quant(v,y)] : assert( is_finite(x), "The input to quantize must be a number or a list of numbers.") floor(x/y+0.5)*y; - // Function: quantdn() // Description: // Quantize a value `x` to an integer multiple of `y`, rounding down to the previous multiple. @@ -306,7 +289,7 @@ function quant(x,y) = // quantdn([9,10,10.4,10.5,11,12],3); // Returns: [9,9,9,9,9,12] // quantdn([[9,10,10.4],[10.5,11,12]],3); // Returns: [[9,9,9],[9,9,12]] function quantdn(x,y) = - assert(is_int(y), "The multiple must be an integer.") + assert(is_finite(y), "The multiple must be a finite number.") is_list(x) ? [for (v=x) quantdn(v,y)] : assert( is_finite(x), "The input to quantize must be a number or a list of numbers.") @@ -338,7 +321,7 @@ function quantdn(x,y) = // quantup([9,10,10.4,10.5,11,12],3); // Returns: [9,12,12,12,12,12] // quantup([[9,10,10.4],[10.5,11,12]],3); // Returns: [[9,12,12],[12,12,12]] function quantup(x,y) = - assert(is_int(y), "The multiple must be an integer.") + assert(is_finite(y), "The multiple must be a number.") is_list(x) ? [for (v=x) quantup(v,y)] : assert( is_finite(x), "The input to quantize must be a number or a list of numbers.") @@ -384,7 +367,7 @@ function constrain(v, minval, maxval) = // posmod(700,360); // Returns: 340 // posmod(3,2.5); // Returns: 0.5 function posmod(x,m) = - assert( is_finite(x) && is_int(m), "Input must be finite numbers.") + assert( is_finite(x) && is_finite(m), "Input must be finite numbers.") (x%m+m)%m; @@ -421,7 +404,7 @@ function modang(x) = // modrange(90,270,360, step=-45); // Returns: [90,45,0,315,270] // modrange(270,90,360, step=-45); // Returns: [270,225,180,135,90] function modrange(x, y, m, step=1) = - assert( is_finite(x+y+step) && is_int(m), "Input must be finite numbers.") + assert( is_finite(x+y+step+m), "Input must be finite numbers.") let( a = posmod(x, m), b = posmod(y, m), @@ -582,7 +565,6 @@ function cumsum(v, off) = ) S ]; - // Function: sum_of_squares() // Description: // Returns the sum of the square of each element of a vector. @@ -709,7 +691,6 @@ function convolve(p,q) = [for(i=[0:n+m-2], k1 = max(0,i-n+1), k2 = min(i,m-1) ) [for(j=[k1:k2]) p[i-j] ] * [for(j=[k1:k2]) q[j] ] ]; - @@ -899,11 +880,6 @@ function determinant(M) = // m = optional height of matrix // n = optional width of matrix // square = set to true to require a square matrix. Default: false -function is_matrix(A,m,n,square=false) = - is_vector(A[0],n) - && is_vector(A*(0*A[0]),m) - && ( !square || len(A)==len(A[0])); - function is_matrix(A,m,n,square=false) = is_list(A[0])     && ( let(v = A*A[0]) is_num(0*(v*v)) ) // a matrix of finite numbers @@ -1097,7 +1073,7 @@ function count_true(l, nmax) = // h = the parametric sampling of the data. // closed = boolean to indicate if the data set should be wrapped around from the end to the start. function deriv(data, h=1, closed=false) = - assert( is_addable(data) , "Input list is not consistent or not numerical.") + assert( is_consistent(data) , "Input list is not consistent or not numerical.") assert( len(data)>=2, "Input `data` should have at least 2 elements.") assert( is_finite(h) || is_vector(h), "The sampling `h` must be a number or a list of numbers." ) assert( is_num(h) || len(h) == len(data)-(closed?0:1), @@ -1159,7 +1135,7 @@ function _deriv_nonuniform(data, h, closed) = // h = the constant parametric sampling of the data. // closed = boolean to indicate if the data set should be wrapped around from the end to the start. function deriv2(data, h=1, closed=false) = - assert( is_addable(data) , "Input list is not consistent or not numerical.") + assert( is_consistent(data) , "Input list is not consistent or not numerical.") assert( len(data)>=3, "Input list has less than 3 elements.") assert( is_finite(h), "The sampling `h` must be a number." ) let( L = len(data) ) @@ -1195,7 +1171,7 @@ function deriv2(data, h=1, closed=false) = // the estimates are f'''(t) = (-5*f(t)+18*f(t+h)-24*f(t+2*h)+14*f(t+3*h)-3*f(t+4*h)) / 2h^3 and // f'''(t) = (-3*f(t-h)+10*f(t)-12*f(t+h)+6*f(t+2*h)-f(t+3*h)) / 2h^3. function deriv3(data, h=1, closed=false) = - assert( is_addable(data) , "Input list is not consistent or not numerical.") + assert( is_consistent(data) , "Input list is not consistent or not numerical.") assert( len(data)>=5, "Input list has less than 5 elements.") assert( is_finite(h), "The sampling `h` must be a number." ) let( @@ -1273,7 +1249,13 @@ function polynomial(p, z, _k, _zk, _total) = is_num(z) ? _zk*z : C_times(_zk,z), _total+_zk*p[_k]); - +function newpoly(p,z,k,total) = +     is_undef(k) +   ?    assert( is_vector(p) || p==[], "Input polynomial coefficients must be a vector." ) +        assert( is_finite(z) || is_vector(z,2), "The value of `z` must be a real or a complex number." ) +        newpoly(p, z, 0, is_num(z) ? 0 : [0,0]) +   : k==len(p) ? total +   : newpoly(p,z,k+1, is_num(z) ? total*z+p[k] : C_times(total,z)+[p[k],0]); // Function: poly_mult() // Usage