mirror of
https://github.com/BelfrySCAD/BOSL2.git
synced 2024-12-28 15:59:45 +00:00
parent
62629b6eb6
commit
014eea601d
2 changed files with 74 additions and 20 deletions
42
arrays.scad
42
arrays.scad
|
@ -73,6 +73,9 @@ 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.
|
||||
|
@ -98,6 +101,8 @@ 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:
|
||||
|
@ -113,6 +118,7 @@ function in_list(val,list,idx=undef) =
|
|||
s==[] || s[0]==[] ? false
|
||||
: is_undef(idx) ? val==list[s]
|
||||
: val==list[s][idx];
|
||||
|
||||
|
||||
|
||||
// Function: min_index()
|
||||
|
@ -203,6 +209,7 @@ 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])
|
||||
|
@ -239,6 +246,8 @@ 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
|
||||
|
@ -306,6 +315,8 @@ 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]);
|
||||
|
@ -340,6 +351,8 @@ function deduplicate_indexed(list, indices, closed=false, eps=EPSILON) =
|
|||
];
|
||||
|
||||
|
||||
|
||||
|
||||
// Function: repeat_entries()
|
||||
// Usage:
|
||||
// newlist = repeat_entries(list, N)
|
||||
|
@ -379,6 +392,8 @@ 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])
|
||||
|
@ -418,6 +433,7 @@ function list_set(list=[],indices,values,dflt=0,minlen=0) =
|
|||
];
|
||||
|
||||
|
||||
|
||||
// Function: list_insert()
|
||||
// Usage:
|
||||
// list_insert(list, indices, values);
|
||||
|
@ -449,6 +465,8 @@ function list_insert(list, indices, values, _i=0) =
|
|||
];
|
||||
|
||||
|
||||
|
||||
|
||||
// Function: list_remove()
|
||||
// Usage:
|
||||
// list_remove(list, indices)
|
||||
|
@ -471,6 +489,8 @@ function list_remove(list, indices) =
|
|||
if ( []==search(i,indices,1) ) list[i] ];
|
||||
|
||||
|
||||
|
||||
|
||||
// Function: list_remove_values()
|
||||
// Usage:
|
||||
// list_remove_values(list,values,all=false) =
|
||||
|
@ -540,6 +560,8 @@ function list_bset(indexset, valuelist, dflt=0) =
|
|||
);
|
||||
|
||||
|
||||
|
||||
|
||||
// Section: List Length Manipulation
|
||||
|
||||
// Function: list_shortest()
|
||||
|
@ -552,6 +574,7 @@ 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.
|
||||
|
@ -601,6 +624,7 @@ function list_fit(array, length, fill) =
|
|||
: list_pad(array,length,fill);
|
||||
|
||||
|
||||
|
||||
// Section: List Shuffling and Sorting
|
||||
|
||||
// Function: shuffle()
|
||||
|
@ -655,7 +679,6 @@ 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) =
|
||||
|
@ -733,7 +756,6 @@ 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(
|
||||
|
@ -752,6 +774,9 @@ function _sort_general(arr, idx=undef) =
|
|||
concat(_sort_general(lesser,idx), equal, _sort_general(greater,idx));
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// Function: sort()
|
||||
// Usage:
|
||||
// sort(list, [idx])
|
||||
|
@ -784,6 +809,7 @@ function sort(list, idx=undef) =
|
|||
: _sort_general(list);
|
||||
|
||||
|
||||
|
||||
// Function: sortidx()
|
||||
// Description:
|
||||
// Given a list, calculates the sort order of the list, and returns
|
||||
|
@ -827,7 +853,6 @@ 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),
|
||||
|
@ -866,6 +891,7 @@ function unique(arr) =
|
|||
];
|
||||
|
||||
|
||||
|
||||
// Function: unique_count()
|
||||
// Usage:
|
||||
// unique_count(arr);
|
||||
|
@ -882,6 +908,8 @@ function unique_count(arr) =
|
|||
[ select(arr,ind), deltas( concat(ind,[len(arr)]) ) ];
|
||||
|
||||
|
||||
|
||||
|
||||
// Section: List Iteration Helpers
|
||||
|
||||
// Function: idx()
|
||||
|
@ -1076,6 +1104,8 @@ function set_union(a, b, get_indices=false) =
|
|||
) [idxs, nset];
|
||||
|
||||
|
||||
|
||||
|
||||
// Function: set_difference()
|
||||
// Usage:
|
||||
// s = set_difference(a, b);
|
||||
|
@ -1095,6 +1125,7 @@ function set_difference(a, b) =
|
|||
[ for (i=idx(a)) if(found[i]==[]) a[i] ];
|
||||
|
||||
|
||||
|
||||
// Function: set_intersection()
|
||||
// Usage:
|
||||
// s = set_intersection(a, b);
|
||||
|
@ -1114,6 +1145,8 @@ function set_intersection(a, b) =
|
|||
[ for (i=idx(a)) if(found[i]!=[]) a[i] ];
|
||||
|
||||
|
||||
|
||||
|
||||
// Section: Array Manipulation
|
||||
|
||||
// Function: add_scalar()
|
||||
|
@ -1132,6 +1165,7 @@ 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.
|
||||
|
@ -1315,4 +1349,6 @@ function transpose(arr) =
|
|||
: arr;
|
||||
|
||||
|
||||
|
||||
|
||||
// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap
|
||||
|
|
52
math.scad
52
math.scad
|
@ -107,7 +107,6 @@ function binomial(n) =
|
|||
c = c*(n-i)/(i+1), i = i+1
|
||||
) c ] ;
|
||||
|
||||
|
||||
// Function: binomial_coefficient()
|
||||
// Usage:
|
||||
// x = binomial_coefficient(n,k);
|
||||
|
@ -130,7 +129,6 @@ function binomial_coefficient(n,k) =
|
|||
) c] )
|
||||
b[len(b)-1];
|
||||
|
||||
|
||||
// Function: lerp()
|
||||
// Usage:
|
||||
// x = lerp(a, b, u);
|
||||
|
@ -168,6 +166,7 @@ function lerp(a,b,u) =
|
|||
[for (v = u) lerp(a,b,v)];
|
||||
|
||||
|
||||
|
||||
// Function: all_numeric()
|
||||
// Usage:
|
||||
// x = all_numeric(list);
|
||||
|
@ -185,6 +184,22 @@ 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
|
||||
|
||||
|
@ -230,6 +245,7 @@ function atanh(x) =
|
|||
ln((1+x)/(1-x))/2;
|
||||
|
||||
|
||||
|
||||
// Section: Quantization
|
||||
|
||||
// Function: quant()
|
||||
|
@ -257,13 +273,14 @@ 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_finite(y), "The multiple must be an integer.")
|
||||
assert(is_int(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.
|
||||
|
@ -289,7 +306,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_finite(y), "The multiple must be a finite number.")
|
||||
assert(is_int(y), "The multiple must be an integer.")
|
||||
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.")
|
||||
|
@ -321,7 +338,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_finite(y), "The multiple must be a number.")
|
||||
assert(is_int(y), "The multiple must be an integer.")
|
||||
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.")
|
||||
|
@ -367,7 +384,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_finite(m), "Input must be finite numbers.")
|
||||
assert( is_finite(x) && is_int(m), "Input must be finite numbers.")
|
||||
(x%m+m)%m;
|
||||
|
||||
|
||||
|
@ -404,7 +421,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+m), "Input must be finite numbers.")
|
||||
assert( is_finite(x+y+step) && is_int(m), "Input must be finite numbers.")
|
||||
let(
|
||||
a = posmod(x, m),
|
||||
b = posmod(y, m),
|
||||
|
@ -565,6 +582,7 @@ function cumsum(v, off) =
|
|||
) S ];
|
||||
|
||||
|
||||
|
||||
// Function: sum_of_squares()
|
||||
// Description:
|
||||
// Returns the sum of the square of each element of a vector.
|
||||
|
@ -691,6 +709,7 @@ 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] ]
|
||||
];
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -880,6 +899,11 @@ 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
|
||||
|
@ -1073,7 +1097,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_consistent(data) , "Input list is not consistent or not numerical.")
|
||||
assert( is_addable(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),
|
||||
|
@ -1135,7 +1159,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_consistent(data) , "Input list is not consistent or not numerical.")
|
||||
assert( is_addable(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) )
|
||||
|
@ -1171,7 +1195,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_consistent(data) , "Input list is not consistent or not numerical.")
|
||||
assert( is_addable(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(
|
||||
|
@ -1249,13 +1273,7 @@ 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
|
||||
|
|
Loading…
Reference in a new issue