From feba482396e00818d944466fbd1542c27c913e4b Mon Sep 17 00:00:00 2001 From: Garth Minette Date: Mon, 6 Jul 2020 18:28:35 -0700 Subject: [PATCH] Optimizations for is_vector(), is_matrix(). Added is_finite(). --- common.scad | 8 ++++++++ math.scad | 11 +++-------- paths.scad | 2 +- vectors.scad | 9 +++------ version.scad | 2 +- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/common.scad b/common.scad index a1b6605..8d0655a 100644 --- a/common.scad +++ b/common.scad @@ -82,6 +82,14 @@ function is_integer(n) = is_num(n) && n == round(n); function is_nan(x) = (x!=x); +// Function: is_finite() +// Usage: +// is_finite(x); +// Description: +// Returns true if a given value `x` is a finite number. +function is_finite(v) = is_num(0*v); + + // Function: is_range() // Description: // Returns true if its argument is a range diff --git a/math.scad b/math.scad index 3fb9841..552228f 100644 --- a/math.scad +++ b/math.scad @@ -733,14 +733,9 @@ 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_list(A) && len(A)>0 && - (is_undef(m) || len(A)==m) && - is_vector(A[0]) && - (is_undef(n) || len(A[0])==n) && - (!square || n==m) && - is_consistent(A); - +function is_matrix(A,n,m,square=false) = + is_vector(A[0],n) && is_vector(A*(0*A[0]),m) && + (!square || len(A)==len(A[0])); // Section: Comparisons and Logic diff --git a/paths.scad b/paths.scad index e0075a7..aad26cd 100644 --- a/paths.scad +++ b/paths.scad @@ -43,7 +43,7 @@ include // dim = list of allowed dimensions of the vectors in the path. Default: [2,3] // fast = set to true for fast check that only looks at first entry. Default: false function is_path(list, dim=[2,3], fast=false) = - fast? is_list(list) && is_vector(list[0],fast=true) : + fast? is_list(list) && is_vector(list[0]) : is_list(list) && is_list(list[0]) && len(list)>1 && (is_undef(dim) || in_list(len(list[0]), force_list(dim))) && is_list_of(list, repeat(0,len(list[0]))); diff --git a/vectors.scad b/vectors.scad index 8a96590..1ee1f5d 100644 --- a/vectors.scad +++ b/vectors.scad @@ -13,13 +13,12 @@ // Function: is_vector() // Usage: -// is_vector(v, [length], [fast]); +// is_vector(v, [length]); // Description: // Returns true if v is a list of finite numbers. // Arguments: // v = The value to test to see if it is a vector. // length = If given, make sure the vector is `length` items long. -// fast = If true, do a shallow test that is faster. // Example: // is_vector(4); // Returns false // is_vector([4,true,false]); // Returns false @@ -29,10 +28,8 @@ // is_vector([3,4,5],3); // Returns true // is_vector([3,4,5],4); // Returns true // is_vector([]); // Returns false -// is_vector([3,undef,undef,true], fast=true); // Returns true -function is_vector(v,length,fast=false) = - (fast? (is_list(v) && is_num(v[0])) : is_list_of(v,0)) && - len(v) && (is_undef(length) || length==len(v)); +function is_vector(v,length) = + is_list(v) && is_num(0*(v*v)) && (is_undef(length)||len(v)==length); // Function: add_scalar() diff --git a/version.scad b/version.scad index c88a963..6977ba5 100644 --- a/version.scad +++ b/version.scad @@ -8,7 +8,7 @@ ////////////////////////////////////////////////////////////////////// -BOSL_VERSION = [2,0,384]; +BOSL_VERSION = [2,0,385]; // Section: BOSL Library Version Functions