Add is_zero(), is_positive(), is_negative(), is_nonpositive(), is_nonnegative().

This commit is contained in:
Garth Minette 2020-08-26 18:02:16 -07:00
parent 90e02ad7a4
commit b679ea52dc
3 changed files with 220 additions and 3 deletions

119
math.scad
View file

@ -864,6 +864,123 @@ function is_matrix(A,m,n,square=false) =
// Section: Comparisons and Logic
// Function: is_zero()
// Usage:
// is_zero(x);
// Description:
// Returns true if the number passed to it is approximately zero, to within `eps`.
// If passed a list, recursively checks if all items in the list are approximately zero.
// Otherwise, returns false.
// Arguments:
// x = The value to check.
// eps = The maximum allowed variance. Default: `EPSILON` (1e-9)
// Example:
// is_zero(0); // Returns: true.
// is_zero(1e-3); // Returns: false.
// is_zero([0,0,0]); // Returns: true.
// is_zero([0,0,1e-3]); // Returns: false.
function is_zero(x, eps=EPSILON) =
is_list(x)? (x != [] && [for (xx=x) if(!is_zero(xx,eps=eps)) 1] == []) :
is_num(x)? approx(x,eps) :
false;
// Function: is_positive()
// Usage:
// is_positive(x);
// Description:
// Returns true if the number passed to it is greater than zero.
// If passed a list, recursively checks if all items in the list are positive.
// Otherwise, returns false.
// Arguments:
// x = The value to check.
// Example:
// is_positive(-2); // Returns: false.
// is_positive(0); // Returns: false.
// is_positive(2); // Returns: true.
// is_positive([0,0,0]); // Returns: false.
// is_positive([0,1,2]); // Returns: false.
// is_positive([3,1,2]); // Returns: true.
// is_positive([3,-1,2]); // Returns: false.
function is_positive(x) =
is_list(x)? (x != [] && [for (xx=x) if(!is_positive(xx)) 1] == []) :
is_num(x)? x>0 :
false;
// Function: is_negative()
// Usage:
// is_negative(x);
// Description:
// Returns true if the number passed to it is less than zero.
// If passed a list, recursively checks if all items in the list are negative.
// Otherwise, returns false.
// Arguments:
// x = The value to check.
// Example:
// is_negative(-2); // Returns: true.
// is_negative(0); // Returns: false.
// is_negative(2); // Returns: false.
// is_negative([0,0,0]); // Returns: false.
// is_negative([0,1,2]); // Returns: false.
// is_negative([3,1,2]); // Returns: false.
// is_negative([3,-1,2]); // Returns: false.
// is_negative([-3,-1,-2]); // Returns: true.
function is_negative(x) =
is_list(x)? (x != [] && [for (xx=x) if(!is_negative(xx)) 1] == []) :
is_num(x)? x<0 :
false;
// Function: is_nonpositive()
// Usage:
// is_nonpositive(x);
// Description:
// Returns true if the number passed to it is less than or equal to zero.
// If passed a list, recursively checks if all items in the list are nonpositive.
// Otherwise, returns false.
// Arguments:
// x = The value to check.
// Example:
// is_nonpositive(-2); // Returns: true.
// is_nonpositive(0); // Returns: true.
// is_nonpositive(2); // Returns: false.
// is_nonpositive([0,0,0]); // Returns: true.
// is_nonpositive([0,1,2]); // Returns: false.
// is_nonpositive([3,1,2]); // Returns: false.
// is_nonpositive([3,-1,2]); // Returns: false.
// is_nonpositive([-3,-1,-2]); // Returns: true.
function is_nonpositive(x) =
is_list(x)? (x != [] && [for (xx=x) if(!is_nonpositive(xx)) 1] == []) :
is_num(x)? x<=0 :
false;
// Function: is_nonnegative()
// Usage:
// is_nonnegative(x);
// Description:
// Returns true if the number passed to it is greater than or equal to zero.
// If passed a list, recursively checks if all items in the list are nonnegative.
// Otherwise, returns false.
// Arguments:
// x = The value to check.
// Example:
// is_nonnegative(-2); // Returns: false.
// is_nonnegative(0); // Returns: true.
// is_nonnegative(2); // Returns: true.
// is_nonnegative([0,0,0]); // Returns: true.
// is_nonnegative([0,1,2]); // Returns: true.
// is_nonnegative([0,-1,-2]); // Returns: false.
// is_nonnegative([3,1,2]); // Returns: true.
// is_nonnegative([3,-1,2]); // Returns: false.
// is_nonnegative([-3,-1,-2]); // Returns: false.
function is_nonnegative(x) =
is_list(x)? (x != [] && [for (xx=x) if(!is_nonnegative(xx)) 1] == []) :
is_num(x)? x>=0 :
false;
// Function: approx()
// Usage:
// approx(a,b,[eps])
@ -1382,4 +1499,4 @@ function real_roots(p,eps=undef,tol=1e-14) =
? [for(z=roots) if (abs(z.y)/(1+norm(z))<eps) z.x]
: [for(i=idx(roots)) if (abs(roots[i].y)<=err[i]) roots[i].x];
// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap
// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap

View file

@ -100,6 +100,106 @@ module test_is_matrix() {
test_is_matrix();
module test_is_zero() {
assert(is_zero(0));
assert(is_zero([0,0,0]));
assert(is_zero([[0,0,0],[0,0]]));
assert(is_zero([EPSILON/2,EPSILON/2,EPSILON/2]));
assert(!is_zero(1e-3));
assert(!is_zero([0,0,1e-3]));
assert(!is_zero([EPSILON*10,0,0]));
assert(!is_zero([0,EPSILON*10,0]));
assert(!is_zero([0,0,EPSILON*10]));
assert(!is_zero(true));
assert(!is_zero(false));
assert(!is_zero(INF));
assert(!is_zero(-INF));
assert(!is_zero(NAN));
assert(!is_zero("foo"));
assert(!is_zero([]));
assert(!is_zero([0:1:2]));
}
test_is_zero();
module test_is_positive() {
assert(!is_positive(-2));
assert(!is_positive(0));
assert(is_positive(2));
assert(!is_positive([0,0,0]));
assert(!is_positive([0,1,2]));
assert(is_positive([3,1,2]));
assert(!is_positive([3,-1,2]));
assert(!is_positive([]));
assert(!is_positive(true));
assert(!is_positive(false));
assert(!is_positive("foo"));
assert(!is_positive([0:1:2]));
}
test_is_positive();
module test_is_negative() {
assert(is_negative(-2));
assert(!is_negative(0));
assert(!is_negative(2));
assert(!is_negative([0,0,0]));
assert(!is_negative([0,1,2]));
assert(!is_negative([3,1,2]));
assert(!is_negative([3,-1,2]));
assert(is_negative([-3,-1,-2]));
assert(!is_negative([-3,1,-2]));
assert(is_negative([[-5,-7],[-3,-1,-2]]));
assert(!is_negative([[-5,-7],[-3,1,-2]]));
assert(!is_negative([]));
assert(!is_negative(true));
assert(!is_negative(false));
assert(!is_negative("foo"));
assert(!is_negative([0:1:2]));
}
test_is_negative();
module test_is_nonpositive() {
assert(is_nonpositive(-2));
assert(is_nonpositive(0));
assert(!is_nonpositive(2));
assert(is_nonpositive([0,0,0]));
assert(!is_nonpositive([0,1,2]));
assert(is_nonpositive([0,-1,-2]));
assert(!is_nonpositive([3,1,2]));
assert(!is_nonpositive([3,-1,2]));
assert(!is_nonpositive([]));
assert(!is_nonpositive(true));
assert(!is_nonpositive(false));
assert(!is_nonpositive("foo"));
assert(!is_nonpositive([0:1:2]));
}
test_is_nonpositive();
module test_is_nonnegative() {
assert(!is_nonnegative(-2));
assert(is_nonnegative(0));
assert(is_nonnegative(2));
assert(is_nonnegative([0,0,0]));
assert(is_nonnegative([0,1,2]));
assert(is_nonnegative([3,1,2]));
assert(!is_nonnegative([3,-1,2]));
assert(!is_nonnegative([-3,-1,-2]));
assert(!is_nonnegative([[-5,-7],[-3,-1,-2]]));
assert(!is_nonnegative([[-5,-7],[-3,1,-2]]));
assert(!is_nonnegative([[5,7],[3,-1,2]]));
assert(is_nonnegative([[5,7],[3,1,2]]));
assert(!is_nonnegative([]));
assert(!is_nonnegative(true));
assert(!is_nonnegative(false));
assert(!is_nonnegative("foo"));
assert(!is_nonnegative([0:1:2]));
}
test_is_nonnegative();
module test_approx() {
assert_equal(approx(PI, 3.141592653589793236), true);
assert_equal(approx(PI, 3.1415926), false);
@ -924,4 +1024,4 @@ module test_poly_add(){
}
test_poly_add();
// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap
// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap

View file

@ -8,7 +8,7 @@
//////////////////////////////////////////////////////////////////////
BOSL_VERSION = [2,0,408];
BOSL_VERSION = [2,0,409];
// Section: BOSL Library Version Functions