Revert "input checks in math and new function definitions"

This reverts commit 19e5a9504a5c1cb9cb58753e4633f263ad8b0014.
This commit is contained in:
RonaldoCMP 2020-07-29 21:50:22 +01:00
parent 19e5a9504a
commit babc10d60d
4 changed files with 229 additions and 462 deletions

View File

@ -99,14 +99,6 @@ function is_finite(v) = is_num(0*v);
function is_range(x) = !is_list(x) && is_finite(x[0]+x[1]+x[2]) ; function is_range(x) = !is_list(x) && is_finite(x[0]+x[1]+x[2]) ;
// Function: valid_range()
// Description:
// Returns true if its argument is a valid range (deprecated range is excluded).
function valid_range(ind) =
is_range(ind)
&& ( ( ind[1]>0 && ind[0]<=ind[2]) || (ind[1]<0 && ind[0]>=ind[2]) );
// Function: is_list_of() // Function: is_list_of()
// Usage: // Usage:
// is_list_of(list, pattern) // is_list_of(list, pattern)
@ -141,15 +133,10 @@ function is_list_of(list,pattern) =
// is_consistent([[3,[3,4,[5]]], [5,[2,9,[9]]]]); // Returns true // is_consistent([[3,[3,4,[5]]], [5,[2,9,[9]]]]); // Returns true
// is_consistent([[3,[3,4,[5]]], [5,[2,9,9]]]); // Returns false // is_consistent([[3,[3,4,[5]]], [5,[2,9,9]]]); // Returns false
function is_consistent(list) = function is_consistent(list) =
is_list_of(list, _list_pattern(list[0])); is_list(list) && is_list_of(list, list[0]);
//Internal function
//Creates a list with the same structure of `list` with each of its elements substituted by 0.
// `list` must be a list
function _list_pattern(list) =
is_list(list)
? [for(entry=list) is_list(entry) ? _list_pattern(entry) : 0]
: 0;
// Function: same_shape() // Function: same_shape()
// Usage: // Usage:
@ -159,7 +146,7 @@ function _list_pattern(list) =
// Example: // Example:
// same_shape([3,[4,5]],[7,[3,4]]); // Returns true // same_shape([3,[4,5]],[7,[3,4]]); // Returns true
// same_shape([3,4,5], [7,[3,4]]); // Returns false // same_shape([3,4,5], [7,[3,4]]); // Returns false
function same_shape(a,b) = _list_pattern(a) == b*0; function same_shape(a,b) = a*0 == b*0;
// Section: Handling `undef`s. // Section: Handling `undef`s.

617
math.scad

File diff suppressed because it is too large Load Diff

View File

@ -169,6 +169,7 @@ module test_is_range() {
assert(!is_range(5)); assert(!is_range(5));
assert(!is_range(INF)); assert(!is_range(INF));
assert(!is_range(-INF)); assert(!is_range(-INF));
assert(!is_nan(NAN));
assert(!is_range("")); assert(!is_range(""));
assert(!is_range("foo")); assert(!is_range("foo"));
assert(!is_range([])); assert(!is_range([]));
@ -178,23 +179,7 @@ module test_is_range() {
assert(!is_range([3:4:"a"])); assert(!is_range([3:4:"a"]));
assert(is_range([3:1:5])); assert(is_range([3:1:5]));
} }
test_is_range(); test_is_nan();
module test_valid_range() {
assert(valid_range([0:0]));
assert(valid_range([0:1:0]));
assert(valid_range([0:1:10]));
assert(valid_range([0.1:1.1:2.1]));
assert(valid_range([0:-1:0]));
assert(valid_range([10:-1:0]));
assert(valid_range([2.1:-1.1:0.1]));
assert(!valid_range([10:1:0]));
assert(!valid_range([2.1:1.1:0.1]));
assert(!valid_range([0:-1:10]));
assert(!valid_range([0.1:-1.1:2.1]));
}
test_valid_range();
module test_is_list_of() { module test_is_list_of() {
@ -207,14 +192,10 @@ module test_is_list_of() {
} }
test_is_list_of(); test_is_list_of();
module test_is_consistent() { module test_is_consistent() {
assert(is_consistent([]));
assert(is_consistent([[],[]]));
assert(is_consistent([3,4,5])); assert(is_consistent([3,4,5]));
assert(is_consistent([[3,4],[4,5],[6,7]])); assert(is_consistent([[3,4],[4,5],[6,7]]));
assert(is_consistent([[[3],4],[[4],5]]));
assert(!is_consistent(5));
assert(!is_consistent(undef));
assert(!is_consistent([[3,4,5],[3,4]])); assert(!is_consistent([[3,4,5],[3,4]]));
assert(is_consistent([[3,[3,4,[5]]], [5,[2,9,[9]]]])); assert(is_consistent([[3,[3,4,[5]]], [5,[2,9,[9]]]]));
assert(!is_consistent([[3,[3,4,[5]]], [5,[2,9,9]]])); assert(!is_consistent([[3,[3,4,[5]]], [5,[2,9,9]]]));

View File

@ -782,8 +782,8 @@ test_deriv3();
module test_polynomial(){ module test_polynomial(){
assert_equal(polynomial([0],12),0); assert_equal(polynomial([],12),0);
assert_equal(polynomial([0],[12,4]),[0,0]); assert_equal(polynomial([],[12,4]),[0,0]);
assert_equal(polynomial([1,2,3,4],3),58); assert_equal(polynomial([1,2,3,4],3),58);
assert_equal(polynomial([1,2,3,4],[3,-1]),[47,-41]); assert_equal(polynomial([1,2,3,4],[3,-1]),[47,-41]);
assert_equal(polynomial([0,0,2],4),2); assert_equal(polynomial([0,0,2],4),2);
@ -879,17 +879,17 @@ test_qr_factor();
module test_poly_mult(){ module test_poly_mult(){
assert_equal(poly_mult([3,2,1],[4,5,6,7]),[12,23,32,38,20,7]); assert_equal(poly_mult([3,2,1],[4,5,6,7]),[12,23,32,38,20,7]);
assert_equal(poly_mult([3,2,1],[0]),[0]); assert_equal(poly_mult([3,2,1],[]),[]);
assert_equal(poly_mult([[1,2],[3,4],[5,6]]), [15,68,100,48]); assert_equal(poly_mult([[1,2],[3,4],[5,6]]), [15,68,100,48]);
assert_equal(poly_mult([[1,2],[0],[5,6]]), [0]); assert_equal(poly_mult([[1,2],[],[5,6]]), []);
assert_equal(poly_mult([[3,4,5],[0,0,0]]),[0]); assert_equal(poly_mult([[3,4,5],[0,0,0]]),[]);
} }
test_poly_mult(); test_poly_mult();
module test_poly_div(){ module test_poly_div(){
assert_equal(poly_div(poly_mult([4,3,3,2],[2,1,3]), [2,1,3]),[[4,3,3,2],[0]]); assert_equal(poly_div(poly_mult([4,3,3,2],[2,1,3]), [2,1,3]),[[4,3,3,2],[]]);
assert_equal(poly_div([1,2,3,4],[1,2,3,4,5]), [[0], [1,2,3,4]]); assert_equal(poly_div([1,2,3,4],[1,2,3,4,5]), [[], [1,2,3,4]]);
assert_equal(poly_div(poly_add(poly_mult([1,2,3,4],[2,0,2]), [1,1,2]), [1,2,3,4]), [[2,0,2],[1,1,2]]); assert_equal(poly_div(poly_add(poly_mult([1,2,3,4],[2,0,2]), [1,1,2]), [1,2,3,4]), [[2,0,2],[1,1,2]]);
assert_equal(poly_div([1,2,3,4], [1,-3]), [[1,5,18],[58]]); assert_equal(poly_div([1,2,3,4], [1,-3]), [[1,5,18],[58]]);
} }
@ -899,7 +899,7 @@ test_poly_div();
module test_poly_add(){ module test_poly_add(){
assert_equal(poly_add([2,3,4],[3,4,5,6]),[3,6,8,10]); assert_equal(poly_add([2,3,4],[3,4,5,6]),[3,6,8,10]);
assert_equal(poly_add([1,2,3,4],[-1,-2,3,4]), [6,8]); assert_equal(poly_add([1,2,3,4],[-1,-2,3,4]), [6,8]);
assert_equal(poly_add([1,2,3],-[1,2,3]),[0]); assert_equal(poly_add([1,2,3],-[1,2,3]),[]);
} }
test_poly_add(); test_poly_add();