Overall review, input data check, format, new function definitions

New function definitions in commom.scad:
1. valid_range;
2. _list_pattern

New function definitions in math.scad:
1. binomial;
2. binomial_coefficient;
3. convolve;

Code review in math:
1. sum;
2. median;
3. is_matrix;
4. approx;
5. count_true;
6. doc of deriv2;
7. polynomial;
8. poly_mult;
9; poly_div;
10. _poly_trim

Code change in test_common:
1. new test_valid_range;
2. test_is_consistent

Code change in test_math:
1. test_approx;
2. new test_convolve;
3. new test_binomial;
4. new test_binomial_coefficient;
5. test_outer_product;
6. test_polynomial;
7. test_poly_mult;
8. test_poly_div;
9. test_poly_add;
This commit is contained in:
RonaldoCMP 2020-07-29 22:41:02 +01:00
parent babc10d60d
commit 8a25764744
4 changed files with 497 additions and 225 deletions

View file

@ -99,6 +99,16 @@ function is_finite(v) = is_num(0*v);
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 ranges excluded).
function valid_range(x) =
is_range(x)
&& ( x[1]>0
? x[0]<=x[2]
: ( x[1]<0 && x[0]>=x[2] ) );
// Function: is_list_of()
// Usage:
// is_list_of(list, pattern)
@ -133,9 +143,15 @@ 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 false
function is_consistent(list) =
is_list(list) && is_list_of(list, list[0]);
is_list(list) && is_list_of(list, _list_pattern(list[0]));
//Internal function
//Creates a list with the same structure of `list` with each of its elements substituted by 0.
function _list_pattern(list) =
is_list(list)
? [for(entry=list) is_list(entry) ? _list_pattern(entry) : 0]
: 0;
// Function: same_shape()
@ -146,7 +162,7 @@ function is_consistent(list) =
// Example:
// same_shape([3,[4,5]],[7,[3,4]]); // Returns true
// same_shape([3,4,5], [7,[3,4]]); // Returns false
function same_shape(a,b) = a*0 == b*0;
function same_shape(a,b) = _list_pattern(a) == b*0;
// Section: Handling `undef`s.

623
math.scad

File diff suppressed because it is too large Load diff

View file

@ -169,7 +169,6 @@ module test_is_range() {
assert(!is_range(5));
assert(!is_range(INF));
assert(!is_range(-INF));
assert(!is_nan(NAN));
assert(!is_range(""));
assert(!is_range("foo"));
assert(!is_range([]));
@ -179,9 +178,24 @@ module test_is_range() {
assert(!is_range([3:4:"a"]));
assert(is_range([3:1:5]));
}
test_is_nan();
test_is_range();
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() {
assert(is_list_of([3,4,5], 0));
assert(!is_list_of([3,4,undef], 0));
@ -192,10 +206,14 @@ module test_is_list_of() {
}
test_is_list_of();
module test_is_consistent() {
assert(is_consistent([]));
assert(is_consistent([[],[]]));
assert(is_consistent([3,4,5]));
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,[3,4,[5]]], [5,[2,9,[9]]]]));
assert(!is_consistent([[3,[3,4,[5]]], [5,[2,9,9]]]));

View file

@ -110,6 +110,8 @@ module test_approx() {
assert_equal(approx(1/3, 0.3333333333), true);
assert_equal(approx(-1/3, -0.3333333333), true);
assert_equal(approx(10*[cos(30),sin(30)], 10*[sqrt(3)/2, 1/2]), true);
assert_equal(approx([1,[1,undef]], [1+1e-12,[1,true]]), false);
assert_equal(approx([1,[1,undef]], [1+1e-12,[1,undef]]), true);
}
test_approx();
@ -389,7 +391,6 @@ module test_mean() {
}
test_mean();
module test_median() {
assert_equal(median([2,3,7]), 4.5);
assert_equal(median([[1,2,3], [3,4,5], [8,9,10]]), [4.5,5.5,6.5]);
@ -397,6 +398,16 @@ module test_median() {
test_median();
module test_convolve() {
assert_equal(convolve([],[1,2,1]), []);
assert_equal(convolve([1,1],[]), []);
assert_equal(convolve([1,1],[1,2,1]), [1,3,3,1]);
assert_equal(convolve([1,2,3],[1,2,1]), [1,4,8,8,3]);
}
test_convolve();
module test_matrix_inverse() {
assert_approx(matrix_inverse(rot([20,30,40])), [[0.663413948169,0.556670399226,-0.5,0],[-0.47302145844,0.829769465589,0.296198132726,0],[0.579769465589,0.0400087565481,0.813797681349,0],[0,0,0,1]]);
}
@ -583,6 +594,24 @@ module test_factorial() {
}
test_factorial();
module test_binomial() {
assert_equal(binomial(1), [1,1]);
assert_equal(binomial(2), [1,2,1]);
assert_equal(binomial(3), [1,3,3,1]);
assert_equal(binomial(5), [1,5,10,10,5,1]);
}
test_binomial();
module test_binomial_coefficient() {
assert_equal(binomial_coefficient(2,1), 2);
assert_equal(binomial_coefficient(3,2), 3);
assert_equal(binomial_coefficient(4,2), 6);
assert_equal(binomial_coefficient(10,7), 120);
assert_equal(binomial_coefficient(10,7), binomial(10)[7]);
assert_equal(binomial_coefficient(15,4), binomial(15)[4]);
}
test_binomial_coefficient();
module test_gcd() {
assert_equal(gcd(15,25), 5);
@ -682,6 +711,7 @@ test_linear_solve();
module test_outer_product(){
assert_equal(outer_product([1,2,3],[4,5,6]), [[4,5,6],[8,10,12],[12,15,18]]);
assert_equal(outer_product([1,2],[4,5,6]), [[4,5,6],[8,10,12]]);
assert_equal(outer_product([9],[7]), [[63]]);
}
test_outer_product();
@ -782,8 +812,10 @@ test_deriv3();
module test_polynomial(){
assert_equal(polynomial([],12),0);
assert_equal(polynomial([],[12,4]),[0,0]);
assert_equal(polynomial([0],12),0);
assert_equal(polynomial([0],[12,4]),[0,0]);
// assert_equal(polynomial([],12),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,-1]),[47,-41]);
assert_equal(polynomial([0,0,2],4),2);
@ -879,16 +911,20 @@ test_qr_factor();
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],[]),[]);
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],[],[5,6]]), []);
assert_equal(poly_mult([[3,4,5],[0,0,0]]),[]);
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();
module test_poly_div(){
assert_equal(poly_div(poly_mult([4,3,3,2],[2,1,3]), [2,1,3]),[[4,3,3,2],[]]);
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]), [[], [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([1,2,3,4], [1,-3]), [[1,5,18],[58]]);
@ -899,7 +935,8 @@ test_poly_div();
module test_poly_add(){
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],-[1,2,3]),[]);
assert_equal(poly_add([1,2,3],-[1,2,3]),[0]);
// assert_equal(poly_add([1,2,3],-[1,2,3]),[]);
}
test_poly_add();