Correction of is_vector and doc text

This commit is contained in:
RonaldoCMP 2020-07-28 19:02:35 +01:00
parent c10a8b919d
commit ab57790c27
4 changed files with 51 additions and 13 deletions

View file

@ -18,6 +18,20 @@
// Section: List Query Operations
// Function: is_simple_list()
// Description:
// Returns true just when all elements of `list` are simple values.
// Usage:
// is_simple_list(list)
// Arguments:
// list = The list to check.
// Example:
// a = is_simple_list([3,4,5,6,7,8,9]); Returns: true
// b = is_simple_list([3,4,5,[6],7,8]); Returns: false
function is_simple_list(list) =
is_list(list)
&& []==[for(e=list) if(is_list(e)) 0];
// Function: select()
// Description:
@ -1289,6 +1303,17 @@ function array_group(v, cnt=2, dflt=0) = [for (i = [0:cnt:len(v)-1]) [for (j = [
function flatten(l) = [for (a = l) each a];
// Function: full_flatten()
// Description:
// Collects in a list all elements recursively found in any level of the given list.
// The output list is ordered in depth first order.
// Arguments:
// l = List to flatten.
// Example:
// full_flatten([[1,2,3], [4,5,[6,7,8]]]) returns [1,2,3,4,5,6,7,8]
function full_flatten(l) = [for(a=l) if(is_list(a)) (each full_flatten(a)) else a ];
// Internal. Not exposed.
function _array_dim_recurse(v) =
!is_list(v[0])

View file

@ -137,6 +137,12 @@ function is_consistent(list) =
is_list(list) && is_list_of(list, list[0]);
//**
// is_consistent doesn't ensure the list contains just numbers!
// for instance, is_consistent([ [1,undef], [2,"a"] ]) is true
// is_consistent ensures that if we substitute each number in the list by true and any other value by false,
// all list items will be equal. The same happens with same_shape().
// Function: same_shape()
// Usage:
// same_shape(a,b)

View file

@ -3,6 +3,14 @@ include <../std.scad>
// Section: List Query Operations
module test_is_simple_list() {
assert(is_simple_list([1,2,3,4]));
assert(is_simple_list([]));
assert(!is_simple_list([1,2,[3,4]]));
}
test_is_simple_list();
module test_select() {
l = [3,4,5,6,7,8,9];
assert(select(l, 5, 6) == [8,9]);
@ -434,10 +442,18 @@ test_array_group();
module test_flatten() {
assert(flatten([[1,2,3], [4,5,[6,7,8]]]) == [1,2,3,4,5,[6,7,8]]);
assert(flatten([]) == []);
}
test_flatten();
module test_full_flatten() {
assert(full_flatten([[1,2,3], [4,5,[6,[7],8]]]) == [1,2,3,4,5,6,7,8]);
assert(full_flatten([]) == []);
}
test_full_flatten();
module test_array_dim() {
assert(array_dim([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]]) == [2,2,3]);
assert(array_dim([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]], 0) == 2);

View file

@ -37,8 +37,6 @@ function is_vector(v,length,zero,eps=EPSILON) =
&& (is_undef(length) || len(v)==length)
&& (is_undef(zero) || ((norm(v) >= eps) == !zero));
//***
// add_scalar() is an array operation: moved to array.scad
// Function: vang()
// Usage:
@ -56,21 +54,19 @@ function vang(v) =
// Function: vmul()
// Description:
// Element-wise vector multiplication. Multiplies each element of vector `v1` by
// the corresponding element of vector `v2`. Returns a vector of the products.
// the corresponding element of vector `v2`. The vectors should have the same dimension.
// Returns a vector of the products.
// Arguments:
// v1 = The first vector.
// v2 = The second vector.
// Example:
// vmul([3,4,5], [8,7,6]); // Returns [24, 28, 30]
function vmul(v1, v2) =
// this thighter check can be done yet because it would break other codes in the library
// assert( is_vector(v1) && is_vector(v2,len(v1)), "Incompatible vectors")
assert( is_vector(v1) && is_vector(v2), "Invalid vector(s)")
[for (i = [0:1:len(v1)-1]) v1[i]*v2[i]];
//***
// some other functions seem to rely on the multiplication of vectors with different lengths
// so, vmul assert cannot check lengths for now
// when len(v1)>len(v2), undef will be in the output list.
// Function: vdiv()
@ -177,8 +173,6 @@ function vector_angle(v1,v2,v3) =
// NOTE: constrain() corrects crazy FP rounding errors that exceed acos()'s domain.
acos(constrain((vecs[0]*vecs[1])/(norm0*norm1), -1, 1));
//***
// completing input data check
// Function: vector_axis()
// Usage:
@ -224,8 +218,5 @@ function vector_axis(v1,v2=undef,v3=undef) =
) unit(cross(w1,w3));
//***
// completing input data check and refactoring
// Note: vector_angle and vector_axis have the same kind of inputs and two code strategy alternatives
// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap