move comparison functions out of math.scad and arrays.scad into comparisons.scad

rename arrays.scad to lists.scad
This commit is contained in:
Adrian Mariano
2021-10-26 23:12:51 -04:00
parent 8cfb77cf64
commit 71dab62432
8 changed files with 1492 additions and 1216 deletions

280
tests/test_comparisons.scad Normal file
View File

@@ -0,0 +1,280 @@
module test_sort() {
assert(sort([7,3,9,4,3,1,8]) == [1,3,3,4,7,8,9]);
assert(sort([[4,0],[7],[3,9],20,[4],[3,1],[8]]) == [20,[3,1],[3,9],[4],[4,0],[7],[8]]);
assert(sort([[4,0],[7],[3,9],20,[4],[3,1],[8]],idx=1) == [[7],20,[4],[8],[4,0],[3,1],[3,9]]);
assert(sort([[8,6],[3,1],[9,2],[4,3],[3,4],[1,5],[8,0]]) == [[1,5],[3,1],[3,4],[4,3],[8,0],[8,6],[9,2]]);
assert(sort([[8,0],[3,1],[9,2],[4,3],[3,4],[1,5],[8,6]],idx=1) == [[8,0],[3,1],[9,2],[4,3],[3,4],[1,5],[8,6]]);
assert(sort(["cat", "oat", "sat", "bat", "vat", "rat", "pat", "mat", "fat", "hat", "eat"])
== ["bat", "cat", "eat", "fat", "hat", "mat", "oat", "pat", "rat", "sat", "vat"]);
assert(sort(enumerate([[2,3,4],[1,2,3],[2,4,3]]),idx=1)==[[1,[1,2,3]], [0,[2,3,4]], [2,[2,4,3]]]);
assert(sort([0,"1",[1,0],2,"a",[1]])== [0,2,"1","a",[1],[1,0]]);
assert(sort([["oat",0], ["cat",1], ["bat",3], ["bat",2], ["fat",3]])== [["bat",2],["bat",3],["cat",1],["fat",3],["oat",0]]);
}
test_sort();
module test_sortidx() {
lst1 = ["da","bax","eaw","cav"];
assert(sortidx(lst1) == [1,3,0,2]);
lst5 = [3,5,1,7];
assert(sortidx(lst5) == [2,0,1,3]);
lst2 = [
["foo", 88, [0,0,1], false],
["bar", 90, [0,1,0], true],
["baz", 89, [1,0,0], false],
["qux", 23, [1,1,1], true]
];
assert(sortidx(lst2, idx=1) == [3,0,2,1]);
assert(sortidx(lst2, idx=0) == [1,2,0,3]);
assert(sortidx(lst2, idx=[1,3]) == [3,0,2,1]);
lst3 = [[-4,0,0],[0,0,-4],[0,-4,0],[-4,0,0],[0,-4,0],[0,0,4],
[0,0,-4],[0,4,0],[4,0,0],[0,0,4],[0,4,0],[4,0,0]];
assert(sortidx(lst3)==[0,3,2,4,1,6,5,9,7,10,8,11]);
assert(sortidx([[4,0],[7],[3,9],20,[4],[3,1],[8]]) == [3,5,2,4,0,1,6]);
assert(sortidx([[4,0],[7],[3,9],20,[4],[3,1],[8]],idx=1) == [1,3,4,6,0,5,2]);
lst4=[0,"1",[1,0],2,"a",[1]];
assert(sortidx(lst4)== [0,3,1,4,5,2]);
assert(sortidx(["cat","oat","sat","bat","vat","rat","pat","mat","fat","hat","eat"])
== [3,0,10,8,9,7,1,6,5,2,4]);
assert(sortidx([["oat",0], ["cat",1], ["bat",3], ["bat",2], ["fat",3]])== [3,2,1,4,0]);
assert(sortidx(["Belfry", "OpenScad", "Library", "Documentation"])==[0,3,2,1]);
assert(sortidx(["x",1,[],0,"abc",true])==[5,3,1,4,0,2]);
}
test_sortidx();
module test_group_sort() {
assert_equal(group_sort([]), [[]]);
assert_equal(group_sort([8]), [[8]]);
assert_equal(group_sort([7,3,9,4,3,1,8]), [[1], [3, 3], [4], [7], [8], [9]]);
assert_equal(group_sort([[5,"a"],[2,"b"], [5,"c"], [3,"d"], [2,"e"] ], idx=0), [[[2, "b"], [2, "e"]], [[3, "d"]], [[5, "a"], [5, "c"]]]);
assert_equal(group_sort([["a",5],["b",6], ["c",1], ["d",2], ["e",6] ], idx=1), [[["c", 1]], [["d", 2]], [["a", 5]], [["b", 6], ["e", 6]]] );
}
test_group_sort();
module test_unique() {
assert_equal(unique([]), []);
assert_equal(unique([8]), [8]);
assert_equal(unique([7,3,9,4,3,1,8]), [1,3,4,7,8,9]);
assert_equal(unique(["A","B","R","A","C","A","D","A","B","R","A"]), ["A", "B", "C", "D", "R"]);
}
test_unique();
module test_unique_count() {
assert_equal(
unique_count([3,1,4,1,5,9,2,6,5,3,5,8,9,7,9,3,2,3,6]),
[[1,2,3,4,5,6,7,8,9],[2,2,4,1,3,2,1,1,3]]
);
assert_equal(
unique_count(["A","B","R","A","C","A","D","A","B","R","A"]),
[["A","B","C","D","R"],[5,2,1,1,2]]
);
}
test_unique_count();
module test_is_increasing() {
assert(is_increasing([1,2,3,4]) == true);
assert(is_increasing([1,2,2,2]) == true);
assert(is_increasing([1,3,2,4]) == false);
assert(is_increasing([4,3,2,1]) == false);
assert(is_increasing([1,2,3,4],strict=true) == true);
assert(is_increasing([1,2,2,2],strict=true) == false);
assert(is_increasing([1,3,2,4],strict=true) == false);
assert(is_increasing([4,3,2,1],strict=true) == false);
assert(is_increasing(["AB","BC","DF"]) == true);
assert(is_increasing(["AB","DC","CF"]) == false);
assert(is_increasing([[1,2],[1,4],[2,3],[2,2]])==false);
assert(is_increasing([[1,2],[1,4],[2,3],[2,3]])==true);
assert(is_increasing([[1,2],[1,4],[2,3],[2,3]],strict=true)==false);
assert(is_increasing("ABCFZ")==true);
assert(is_increasing("ZYWRA")==false);
}
test_is_increasing();
module test_is_decreasing() {
assert(is_decreasing([1,2,3,4]) == false);
assert(is_decreasing([4,2,3,1]) == false);
assert(is_decreasing([4,2,2,1]) == true);
assert(is_decreasing([4,3,2,1]) == true);
assert(is_decreasing([1,2,3,4],strict=true) == false);
assert(is_decreasing([4,2,3,1],strict=true) == false);
assert(is_decreasing([4,2,2,1],strict=true) == false);
assert(is_decreasing([4,3,2,1],strict=true) == true);
assert(is_decreasing(reverse(["AB","BC","DF"])) == true);
assert(is_decreasing(reverse(["AB","DC","CF"])) == false);
assert(is_decreasing(reverse([[1,2],[1,4],[2,3],[2,2]]))==false);
assert(is_decreasing(reverse([[1,2],[1,4],[2,3],[2,3]]))==true);
assert(is_decreasing(reverse([[1,2],[1,4],[2,3],[2,3]]),strict=true)==false);
assert(is_decreasing("ABCFZ")==false);
assert(is_decreasing("ZYWRA")==true);
}
test_is_decreasing();
module test_find_approx() {
assert(find_approx(1, [2,3,1.05,4,1,2,.99], eps=.1)==2);
assert(find_approx(1, [2,3,1.05,4,1,2,.99], all=true, eps=.1)==[2,4,6]);
}
test_find_approx();
module test_deduplicate() {
assert_equal(deduplicate([8,3,4,4,4,8,2,3,3,8,8]), [8,3,4,8,2,3,8]);
assert_equal(deduplicate(closed=true, [8,3,4,4,4,8,2,3,3,8,8]), [8,3,4,8,2,3]);
assert_equal(deduplicate("Hello"), "Helo");
assert_equal(deduplicate([[3,4],[7,1.99],[7,2],[1,4]],eps=0.1), [[3,4],[7,2],[1,4]]);
assert_equal(deduplicate([], closed=true), []);
assert_equal(deduplicate([[1,[1,[undef]]],[1,[1,[undef]]],[1,[2]],[1,[2,[0]]]]), [[1, [1,[undef]]],[1,[2]],[1,[2,[0]]]]);
}
test_deduplicate();
module test_deduplicate_indexed() {
assert(deduplicate_indexed([8,6,4,6,3], [1,4,3,1,2,2,0,1]) == [1,4,1,2,0,1]);
assert(deduplicate_indexed([8,6,4,6,3], [1,4,3,1,2,2,0,1], closed=true) == [1,4,1,2,0]);
}
test_deduplicate_indexed();
module test_all_zero() {
assert(all_zero(0));
assert(all_zero([0,0,0]));
assert(all_zero([[0,0,0],[0,0]]));
assert(all_zero([EPSILON/2,EPSILON/2,EPSILON/2]));
assert(!all_zero(1e-3));
assert(!all_zero([0,0,1e-3]));
assert(!all_zero([EPSILON*10,0,0]));
assert(!all_zero([0,EPSILON*10,0]));
assert(!all_zero([0,0,EPSILON*10]));
assert(!all_zero(true));
assert(!all_zero(false));
assert(!all_zero(INF));
assert(!all_zero(-INF));
assert(!all_zero(NAN));
assert(!all_zero("foo"));
assert(!all_zero([]));
assert(!all_zero([0:1:2]));
}
test_all_zero();
module test_all_nonzero() {
assert(!all_nonzero(0));
assert(!all_nonzero([0,0,0]));
assert(!all_nonzero([[0,0,0],[0,0]]));
assert(!all_nonzero([EPSILON/2,EPSILON/2,EPSILON/2]));
assert(all_nonzero(1e-3));
assert(!all_nonzero([0,0,1e-3]));
assert(!all_nonzero([EPSILON*10,0,0]));
assert(!all_nonzero([0,EPSILON*10,0]));
assert(!all_nonzero([0,0,EPSILON*10]));
assert(all_nonzero([1e-3,1e-3,1e-3]));
assert(all_nonzero([EPSILON*10,EPSILON*10,EPSILON*10]));
assert(!all_nonzero(true));
assert(!all_nonzero(false));
assert(!all_nonzero(INF));
assert(!all_nonzero(-INF));
assert(!all_nonzero(NAN));
assert(!all_nonzero("foo"));
assert(!all_nonzero([]));
assert(!all_nonzero([0:1:2]));
}
test_all_nonzero();
module test_all_positive() {
assert(!all_positive(-2));
assert(!all_positive(0));
assert(all_positive(2));
assert(!all_positive([0,0,0]));
assert(!all_positive([0,1,2]));
assert(all_positive([3,1,2]));
assert(!all_positive([3,-1,2]));
assert(!all_positive([]));
assert(!all_positive(true));
assert(!all_positive(false));
assert(!all_positive("foo"));
assert(!all_positive([0:1:2]));
}
test_all_positive();
module test_all_negative() {
assert(all_negative(-2));
assert(!all_negative(0));
assert(!all_negative(2));
assert(!all_negative([0,0,0]));
assert(!all_negative([0,1,2]));
assert(!all_negative([3,1,2]));
assert(!all_negative([3,-1,2]));
assert(all_negative([-3,-1,-2]));
assert(!all_negative([-3,1,-2]));
assert(all_negative([[-5,-7],[-3,-1,-2]]));
assert(!all_negative([[-5,-7],[-3,1,-2]]));
assert(!all_negative([]));
assert(!all_negative(true));
assert(!all_negative(false));
assert(!all_negative("foo"));
assert(!all_negative([0:1:2]));
}
test_all_negative();
module test_all_nonpositive() {
assert(all_nonpositive(-2));
assert(all_nonpositive(0));
assert(!all_nonpositive(2));
assert(all_nonpositive([0,0,0]));
assert(!all_nonpositive([0,1,2]));
assert(all_nonpositive([0,-1,-2]));
assert(!all_nonpositive([3,1,2]));
assert(!all_nonpositive([3,-1,2]));
assert(!all_nonpositive([]));
assert(!all_nonpositive(true));
assert(!all_nonpositive(false));
assert(!all_nonpositive("foo"));
assert(!all_nonpositive([0:1:2]));
}
test_all_nonpositive();
module test_all_nonnegative() {
assert(!all_nonnegative(-2));
assert(all_nonnegative(0));
assert(all_nonnegative(2));
assert(all_nonnegative([0,0,0]));
assert(all_nonnegative([0,1,2]));
assert(all_nonnegative([3,1,2]));
assert(!all_nonnegative([3,-1,2]));
assert(!all_nonnegative([-3,-1,-2]));
assert(!all_nonnegative([[-5,-7],[-3,-1,-2]]));
assert(!all_nonnegative([[-5,-7],[-3,1,-2]]));
assert(!all_nonnegative([[5,7],[3,-1,2]]));
assert(all_nonnegative([[5,7],[3,1,2]]));
assert(!all_nonnegative([]));
assert(!all_nonnegative(true));
assert(!all_nonnegative(false));
assert(!all_nonnegative("foo"));
assert(!all_nonnegative([0:1:2]));
}
test_all_nonnegative();
module test_approx() {
assert_equal(approx(PI, 3.141592653589793236), true);
assert_equal(approx(PI, 3.1415926), false);
assert_equal(approx(PI, 3.1415926, eps=1e-6), true);
assert_equal(approx(-PI, -3.141592653589793236), true);
assert_equal(approx(-PI, -3.1415926), false);
assert_equal(approx(-PI, -3.1415926, eps=1e-6), true);
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();

252
tests/test_linalg.scad Normal file
View File

@@ -0,0 +1,252 @@
include <../std.scad>
module test_qr_factor() {
// Check that R is upper triangular
function is_ut(R) =
let(bad = [for(i=[1:1:len(R)-1], j=[0:min(i-1, len(R[0])-1)]) if (!approx(R[i][j],0)) 1])
bad == [];
// Test the R is upper trianglar, Q is orthogonal and qr=M
function qrok(qr,M) =
is_ut(qr[1]) && approx(qr[0]*transpose(qr[0]), ident(len(qr[0]))) && approx(qr[0]*qr[1],M) && qr[2]==ident(len(qr[2]));
// Test the R is upper trianglar, Q is orthogonal, R diagonal non-increasing and qrp=M
function qrokpiv(qr,M) =
is_ut(qr[1])
&& approx(qr[0]*transpose(qr[0]), ident(len(qr[0])))
&& approx(qr[0]*qr[1]*transpose(qr[2]),M)
&& is_decreasing([for(i=[0:1:min(len(qr[1]),len(qr[1][0]))-1]) abs(qr[1][i][i])]);
M = [[1,2,9,4,5],
[6,7,8,19,10],
[11,12,13,14,15],
[1,17,18,19,20],
[21,22,10,24,25]];
assert(qrok(qr_factor(M),M));
assert(qrok(qr_factor(select(M,0,3)),select(M,0,3)));
assert(qrok(qr_factor(transpose(select(M,0,3))),transpose(select(M,0,3))));
A = [[1,2,9,4,5],
[6,7,8,19,10],
[0,0,0,0,0],
[1,17,18,19,20],
[21,22,10,24,25]];
assert(qrok(qr_factor(A),A));
B = [[1,2,0,4,5],
[6,7,0,19,10],
[0,0,0,0,0],
[1,17,0,19,20],
[21,22,0,24,25]];
assert(qrok(qr_factor(B),B));
assert(qrok(qr_factor([[7]]), [[7]]));
assert(qrok(qr_factor([[1,2,3]]), [[1,2,3]]));
assert(qrok(qr_factor([[1],[2],[3]]), [[1],[2],[3]]));
assert(qrokpiv(qr_factor(M,pivot=true),M));
assert(qrokpiv(qr_factor(select(M,0,3),pivot=true),select(M,0,3)));
assert(qrokpiv(qr_factor(transpose(select(M,0,3)),pivot=true),transpose(select(M,0,3))));
assert(qrokpiv(qr_factor(B,pivot=true),B));
assert(qrokpiv(qr_factor([[7]],pivot=true), [[7]]));
assert(qrokpiv(qr_factor([[1,2,3]],pivot=true), [[1,2,3]]));
assert(qrokpiv(qr_factor([[1],[2],[3]],pivot=true), [[1],[2],[3]]));
}
test_qr_factor();
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]]);
}
test_matrix_inverse();
module test_det2() {
assert_equal(det2([[6,-2], [1,8]]), 50);
assert_equal(det2([[4,7], [3,2]]), -13);
assert_equal(det2([[4,3], [3,4]]), 7);
}
test_det2();
module test_det3() {
M = [ [6,4,-2], [1,-2,8], [1,5,7] ];
assert_equal(det3(M), -334);
}
test_det3();
module test_determinant() {
M = [ [6,4,-2,9], [1,-2,8,3], [1,5,7,6], [4,2,5,1] ];
assert_equal(determinant(M), 2267);
}
test_determinant();
module test_matrix_trace() {
M = [ [6,4,-2,9], [1,-2,8,3], [1,5,7,6], [4,2,5,1] ];
assert_equal(matrix_trace(M), 6-2+7+1);
}
test_matrix_trace();
module test_norm_fro(){
assert_approx(norm_fro([[2,3,4],[4,5,6]]), 10.29563014098700);
} test_norm_fro();
module test_linear_solve(){
M = [[-2,-5,-1,3],
[3,7,6,2],
[6,5,-1,-6],
[-7,1,2,3]];
assert_approx(linear_solve(M, [-3,43,-11,13]), [1,2,3,4]);
assert_approx(linear_solve(M, [[-5,8],[18,-61],[4,7],[-1,-12]]), [[1,-2],[1,-3],[1,-4],[1,-5]]);
assert_approx(linear_solve([[2]],[4]), [2]);
assert_approx(linear_solve([[2]],[[4,8]]), [[2, 4]]);
assert_approx(linear_solve(select(M,0,2), [2,4,4]), [ 2.254871220604705e+00,
-8.378819388897780e-01,
2.330507118860985e-01,
8.511278195488737e-01]);
assert_approx(linear_solve(submatrix(M,idx(M),[0:2]), [2,4,4,4]),
[-2.457142857142859e-01,
5.200000000000000e-01,
7.428571428571396e-02]);
assert_approx(linear_solve([[1,2,3,4]], [2]), [0.066666666666666, 0.13333333333, 0.2, 0.266666666666]);
assert_approx(linear_solve([[1],[2],[3],[4]], [4,3,2,1]), [2/3]);
rd = [[-2,-5,-1,3],
[3,7,6,2],
[3,7,6,2],
[-7,1,2,3]];
assert_equal(linear_solve(rd,[1,2,3,4]),[]);
assert_equal(linear_solve(select(rd,0,2), [2,4,4]), []);
assert_equal(linear_solve(transpose(select(rd,0,2)), [2,4,3,4]), []);
}
test_linear_solve();
module test_null_space(){
assert_equal(null_space([[3,2,1],[3,6,3],[3,9,-3]]),[]);
function nullcheck(A,dim) =
let(v=null_space(A))
len(v)==dim && all_zero(A*transpose(v),eps=1e-12);
A = [[-1, 2, -5, 2],[-3,-1,3,-3],[5,0,5,0],[3,-4,11,-4]];
assert(nullcheck(A,1));
B = [
[ 4, 1, 8, 6, -2, 3],
[ 10, 5, 10, 10, 0, 5],
[ 8, 1, 8, 8, -6, 1],
[ -8, -8, 6, -1, -8, -1],
[ 2, 2, 0, 1, 2, 1],
[ 2, -3, 10, 6, -8, 1],
];
assert(nullcheck(B,3));
}
test_null_space();
module test_column() {
v = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]];
assert(column(v,2) == [3, 7, 11, 15]);
data = [[1,[3,4]], [3, [9,3]], [4, [3,1]]]; // Matrix with non-numeric entries
assert_equal(column(data,0), [1,3,4]);
assert_equal(column(data,1), [[3,4],[9,3],[3,1]]);
}
test_column();
// Need decision about behavior for out of bounds ranges, empty ranges
module test_submatrix(){
M = [[1,2,3,4,5],
[6,7,8,9,10],
[11,12,13,14,15],
[16,17,18,19,20],
[21,22,23,24,25]];
assert_equal(submatrix(M,[1:2], [3:4]), [[9,10],[14,15]]);
assert_equal(submatrix(M,[1], [3,4]), [[9,10]]);
assert_equal(submatrix(M,1, [3,4]), [[9,10]]);
assert_equal(submatrix(M, [3,4],1), [[17],[22]]);
assert_equal(submatrix(M, [1,3],[2,4]), [[8,10],[18,20]]);
assert_equal(submatrix(M, 1,3), [[9]]);
A = [[true, 17, "test"],
[[4,2], 91, false],
[6, [3,4], undef]];
assert_equal(submatrix(A,[0,2],[1,2]),[[17, "test"], [[3, 4], undef]]);
}
test_submatrix();
module test_hstack() {
M = ident(3);
v1 = [2,3,4];
v2 = [5,6,7];
v3 = [8,9,10];
a = hstack(v1,v2);
b = hstack(v1,v2,v3);
c = hstack([M,v1,M]);
d = hstack(column(M,0), submatrix(M,idx(M),[1,2]));
assert_equal(a,[[2, 5], [3, 6], [4, 7]]);
assert_equal(b,[[2, 5, 8], [3, 6, 9], [4, 7, 10]]);
assert_equal(c,[[1, 0, 0, 2, 1, 0, 0], [0, 1, 0, 3, 0, 1, 0], [0, 0, 1, 4, 0, 0, 1]]);
assert_equal(d,M);
strmat = [["three","four"], ["five","six"]];
assert_equal(hstack(strmat,strmat), [["three", "four", "three", "four"], ["five", "six", "five", "six"]]);
strvec = ["one","two"];
assert_equal(hstack(strvec,strmat),[["o", "n", "e", "three", "four"], ["t", "w", "o", "five", "six"]]);
}
test_hstack();
module test_block_matrix() {
A = [[1,2],[3,4]];
B = ident(2);
assert_equal(block_matrix([[A,B],[B,A],[A,B]]), [[1,2,1,0],[3,4,0,1],[1,0,1,2],[0,1,3,4],[1,2,1,0],[3,4,0,1]]);
assert_equal(block_matrix([[A,B],ident(4)]), [[1,2,1,0],[3,4,0,1],[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]);
text = [["aa","bb"],["cc","dd"]];
assert_equal(block_matrix([[text,B]]), [["aa","bb",1,0],["cc","dd",0,1]]);
}
test_block_matrix();
module test_diagonal_matrix() {
assert_equal(diagonal_matrix([1,2,3]), [[1,0,0],[0,2,0],[0,0,3]]);
assert_equal(diagonal_matrix([1,"c",2]), [[1,0,0],[0,"c",0],[0,0,2]]);
assert_equal(diagonal_matrix([1,"c",2],"X"), [[1,"X","X"],["X","c","X"],["X","X",2]]);
assert_equal(diagonal_matrix([[1,1],[2,2],[3,3]], [0,0]), [[ [1,1],[0,0],[0,0]], [[0,0],[2,2],[0,0]], [[0,0],[0,0],[3,3]]]);
}
test_diagonal_matrix();
module test_submatrix_set() {
test = [[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15], [16,17,18,19,20]];
ragged = [[1,2,3,4,5],[6,7,8,9,10],[11,12], [16,17]];
assert_equal(submatrix_set(test,[[9,8],[7,6]]), [[9,8,3,4,5],[7,6,8,9,10],[11,12,13,14,15], [16,17,18,19,20]]);
assert_equal(submatrix_set(test,[[9,7],[8,6]],1),[[1,2,3,4,5],[9,7,8,9,10],[8,6,13,14,15], [16,17,18,19,20]]);
assert_equal(submatrix_set(test,[[9,8],[7,6]],n=1), [[1,9,8,4,5],[6,7,6,9,10],[11,12,13,14,15], [16,17,18,19,20]]);
assert_equal(submatrix_set(test,[[9,8],[7,6]],1,2), [[1,2,3,4,5],[6,7,9,8,10],[11,12,7,6,15], [16,17,18,19,20]]);
assert_equal(submatrix_set(test,[[9,8],[7,6]],-1,-1), [[6,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15], [16,17,18,19,20]]);
assert_equal(submatrix_set(test,[[9,8],[7,6]],n=4), [[1,2,3,4,9],[6,7,8,9,7],[11,12,13,14,15], [16,17,18,19,20]]);
assert_equal(submatrix_set(test,[[9,8],[7,6]],7,7), [[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15], [16,17,18,19,20]]);
assert_equal(submatrix_set(ragged, [["a","b"],["c","d"]], 1, 1), [[1,2,3,4,5],[6,"a","b",9,10],[11,"c"], [16,17]]);
assert_equal(submatrix_set(test, [[]]), test);
}
test_submatrix_set();
module test_transpose() {
assert(transpose([[1,2,3],[4,5,6],[7,8,9]]) == [[1,4,7],[2,5,8],[3,6,9]]);
assert(transpose([[1,2,3],[4,5,6]]) == [[1,4],[2,5],[3,6]]);
assert(transpose([[1,2,3],[4,5,6]],reverse=true) == [[6,3], [5,2], [4,1]]);
assert(transpose([3,4,5]) == [3,4,5]);
}
test_transpose();

View File

@@ -91,52 +91,6 @@ module test_in_list() {
test_in_list();
module test_is_increasing() {
assert(is_increasing([1,2,3,4]) == true);
assert(is_increasing([1,2,2,2]) == true);
assert(is_increasing([1,3,2,4]) == false);
assert(is_increasing([4,3,2,1]) == false);
assert(is_increasing([1,2,3,4],strict=true) == true);
assert(is_increasing([1,2,2,2],strict=true) == false);
assert(is_increasing([1,3,2,4],strict=true) == false);
assert(is_increasing([4,3,2,1],strict=true) == false);
assert(is_increasing(["AB","BC","DF"]) == true);
assert(is_increasing(["AB","DC","CF"]) == false);
assert(is_increasing([[1,2],[1,4],[2,3],[2,2]])==false);
assert(is_increasing([[1,2],[1,4],[2,3],[2,3]])==true);
assert(is_increasing([[1,2],[1,4],[2,3],[2,3]],strict=true)==false);
assert(is_increasing("ABCFZ")==true);
assert(is_increasing("ZYWRA")==false);
}
test_is_increasing();
module test_is_decreasing() {
assert(is_decreasing([1,2,3,4]) == false);
assert(is_decreasing([4,2,3,1]) == false);
assert(is_decreasing([4,2,2,1]) == true);
assert(is_decreasing([4,3,2,1]) == true);
assert(is_decreasing([1,2,3,4],strict=true) == false);
assert(is_decreasing([4,2,3,1],strict=true) == false);
assert(is_decreasing([4,2,2,1],strict=true) == false);
assert(is_decreasing([4,3,2,1],strict=true) == true);
assert(is_decreasing(reverse(["AB","BC","DF"])) == true);
assert(is_decreasing(reverse(["AB","DC","CF"])) == false);
assert(is_decreasing(reverse([[1,2],[1,4],[2,3],[2,2]]))==false);
assert(is_decreasing(reverse([[1,2],[1,4],[2,3],[2,3]]))==true);
assert(is_decreasing(reverse([[1,2],[1,4],[2,3],[2,3]]),strict=true)==false);
assert(is_decreasing("ABCFZ")==false);
assert(is_decreasing("ZYWRA")==true);
}
test_is_decreasing();
module test_find_approx() {
assert(find_approx(1, [2,3,1.05,4,1,2,.99], eps=.1)==2);
assert(find_approx(1, [2,3,1.05,4,1,2,.99], all=true, eps=.1)==[2,4,6]);
}
test_find_approx();
// Section: Basic List Generation
@@ -183,23 +137,6 @@ module test_list_rotate() {
test_list_rotate();
module test_deduplicate() {
assert_equal(deduplicate([8,3,4,4,4,8,2,3,3,8,8]), [8,3,4,8,2,3,8]);
assert_equal(deduplicate(closed=true, [8,3,4,4,4,8,2,3,3,8,8]), [8,3,4,8,2,3]);
assert_equal(deduplicate("Hello"), "Helo");
assert_equal(deduplicate([[3,4],[7,1.99],[7,2],[1,4]],eps=0.1), [[3,4],[7,2],[1,4]]);
assert_equal(deduplicate([], closed=true), []);
assert_equal(deduplicate([[1,[1,[undef]]],[1,[1,[undef]]],[1,[2]],[1,[2,[0]]]]), [[1, [1,[undef]]],[1,[2]],[1,[2,[0]]]]);
}
test_deduplicate();
module test_deduplicate_indexed() {
assert(deduplicate_indexed([8,6,4,6,3], [1,4,3,1,2,2,0,1]) == [1,4,1,2,0,1]);
assert(deduplicate_indexed([8,6,4,6,3], [1,4,3,1,2,2,0,1], closed=true) == [1,4,1,2,0]);
}
test_deduplicate_indexed();
module test_list_set() {
assert_equal(list_set([2,3,4,5], 2, 21), [2,3,21,5]);
@@ -332,82 +269,6 @@ module test_shuffle() {
test_shuffle();
module test_sort() {
assert(sort([7,3,9,4,3,1,8]) == [1,3,3,4,7,8,9]);
assert(sort([[4,0],[7],[3,9],20,[4],[3,1],[8]]) == [20,[3,1],[3,9],[4],[4,0],[7],[8]]);
assert(sort([[4,0],[7],[3,9],20,[4],[3,1],[8]],idx=1) == [[7],20,[4],[8],[4,0],[3,1],[3,9]]);
assert(sort([[8,6],[3,1],[9,2],[4,3],[3,4],[1,5],[8,0]]) == [[1,5],[3,1],[3,4],[4,3],[8,0],[8,6],[9,2]]);
assert(sort([[8,0],[3,1],[9,2],[4,3],[3,4],[1,5],[8,6]],idx=1) == [[8,0],[3,1],[9,2],[4,3],[3,4],[1,5],[8,6]]);
assert(sort(["cat", "oat", "sat", "bat", "vat", "rat", "pat", "mat", "fat", "hat", "eat"])
== ["bat", "cat", "eat", "fat", "hat", "mat", "oat", "pat", "rat", "sat", "vat"]);
assert(sort(enumerate([[2,3,4],[1,2,3],[2,4,3]]),idx=1)==[[1,[1,2,3]], [0,[2,3,4]], [2,[2,4,3]]]);
assert(sort([0,"1",[1,0],2,"a",[1]])== [0,2,"1","a",[1],[1,0]]);
assert(sort([["oat",0], ["cat",1], ["bat",3], ["bat",2], ["fat",3]])== [["bat",2],["bat",3],["cat",1],["fat",3],["oat",0]]);
}
test_sort();
module test_sortidx() {
lst1 = ["da","bax","eaw","cav"];
assert(sortidx(lst1) == [1,3,0,2]);
lst5 = [3,5,1,7];
assert(sortidx(lst5) == [2,0,1,3]);
lst2 = [
["foo", 88, [0,0,1], false],
["bar", 90, [0,1,0], true],
["baz", 89, [1,0,0], false],
["qux", 23, [1,1,1], true]
];
assert(sortidx(lst2, idx=1) == [3,0,2,1]);
assert(sortidx(lst2, idx=0) == [1,2,0,3]);
assert(sortidx(lst2, idx=[1,3]) == [3,0,2,1]);
lst3 = [[-4,0,0],[0,0,-4],[0,-4,0],[-4,0,0],[0,-4,0],[0,0,4],
[0,0,-4],[0,4,0],[4,0,0],[0,0,4],[0,4,0],[4,0,0]];
assert(sortidx(lst3)==[0,3,2,4,1,6,5,9,7,10,8,11]);
assert(sortidx([[4,0],[7],[3,9],20,[4],[3,1],[8]]) == [3,5,2,4,0,1,6]);
assert(sortidx([[4,0],[7],[3,9],20,[4],[3,1],[8]],idx=1) == [1,3,4,6,0,5,2]);
lst4=[0,"1",[1,0],2,"a",[1]];
assert(sortidx(lst4)== [0,3,1,4,5,2]);
assert(sortidx(["cat","oat","sat","bat","vat","rat","pat","mat","fat","hat","eat"])
== [3,0,10,8,9,7,1,6,5,2,4]);
assert(sortidx([["oat",0], ["cat",1], ["bat",3], ["bat",2], ["fat",3]])== [3,2,1,4,0]);
assert(sortidx(["Belfry", "OpenScad", "Library", "Documentation"])==[0,3,2,1]);
assert(sortidx(["x",1,[],0,"abc",true])==[5,3,1,4,0,2]);
}
test_sortidx();
module test_group_sort() {
assert_equal(group_sort([]), [[]]);
assert_equal(group_sort([8]), [[8]]);
assert_equal(group_sort([7,3,9,4,3,1,8]), [[1], [3, 3], [4], [7], [8], [9]]);
assert_equal(group_sort([[5,"a"],[2,"b"], [5,"c"], [3,"d"], [2,"e"] ], idx=0), [[[2, "b"], [2, "e"]], [[3, "d"]], [[5, "a"], [5, "c"]]]);
assert_equal(group_sort([["a",5],["b",6], ["c",1], ["d",2], ["e",6] ], idx=1), [[["c", 1]], [["d", 2]], [["a", 5]], [["b", 6], ["e", 6]]] );
}
test_group_sort();
module test_unique() {
assert_equal(unique([]), []);
assert_equal(unique([8]), [8]);
assert_equal(unique([7,3,9,4,3,1,8]), [1,3,4,7,8,9]);
assert_equal(unique(["A","B","R","A","C","A","D","A","B","R","A"]), ["A", "B", "C", "D", "R"]);
}
test_unique();
module test_unique_count() {
assert_equal(
unique_count([3,1,4,1,5,9,2,6,5,3,5,8,9,7,9,3,2,3,6]),
[[1,2,3,4,5,6,7,8,9],[2,2,4,1,3,2,1,1,3]]
);
assert_equal(
unique_count(["A","B","R","A","C","A","D","A","B","R","A"]),
[["A","B","C","D","R"],[5,2,1,1,2]]
);
}
test_unique_count();
// Sets
@@ -509,13 +370,13 @@ module test_zip() {
test_zip();
module test_array_group() {
module test_list_to_matrix() {
v = [1,2,3,4,5,6];
assert(array_group(v,2) == [[1,2], [3,4], [5,6]]);
assert(array_group(v,3) == [[1,2,3], [4,5,6]]);
assert(array_group(v,4,0) == [[1,2,3,4], [5,6,0,0]]);
assert(list_to_matrix(v,2) == [[1,2], [3,4], [5,6]]);
assert(list_to_matrix(v,3) == [[1,2,3], [4,5,6]]);
assert(list_to_matrix(v,4,0) == [[1,2,3,4], [5,6,0,0]]);
}
test_array_group();
test_list_to_matrix();
module test_group_data() {

View File

@@ -109,128 +109,6 @@ module test_is_matrix() {
test_is_matrix();
module test_all_zero() {
assert(all_zero(0));
assert(all_zero([0,0,0]));
assert(all_zero([[0,0,0],[0,0]]));
assert(all_zero([EPSILON/2,EPSILON/2,EPSILON/2]));
assert(!all_zero(1e-3));
assert(!all_zero([0,0,1e-3]));
assert(!all_zero([EPSILON*10,0,0]));
assert(!all_zero([0,EPSILON*10,0]));
assert(!all_zero([0,0,EPSILON*10]));
assert(!all_zero(true));
assert(!all_zero(false));
assert(!all_zero(INF));
assert(!all_zero(-INF));
assert(!all_zero(NAN));
assert(!all_zero("foo"));
assert(!all_zero([]));
assert(!all_zero([0:1:2]));
}
test_all_zero();
module test_all_nonzero() {
assert(!all_nonzero(0));
assert(!all_nonzero([0,0,0]));
assert(!all_nonzero([[0,0,0],[0,0]]));
assert(!all_nonzero([EPSILON/2,EPSILON/2,EPSILON/2]));
assert(all_nonzero(1e-3));
assert(!all_nonzero([0,0,1e-3]));
assert(!all_nonzero([EPSILON*10,0,0]));
assert(!all_nonzero([0,EPSILON*10,0]));
assert(!all_nonzero([0,0,EPSILON*10]));
assert(all_nonzero([1e-3,1e-3,1e-3]));
assert(all_nonzero([EPSILON*10,EPSILON*10,EPSILON*10]));
assert(!all_nonzero(true));
assert(!all_nonzero(false));
assert(!all_nonzero(INF));
assert(!all_nonzero(-INF));
assert(!all_nonzero(NAN));
assert(!all_nonzero("foo"));
assert(!all_nonzero([]));
assert(!all_nonzero([0:1:2]));
}
test_all_nonzero();
module test_all_positive() {
assert(!all_positive(-2));
assert(!all_positive(0));
assert(all_positive(2));
assert(!all_positive([0,0,0]));
assert(!all_positive([0,1,2]));
assert(all_positive([3,1,2]));
assert(!all_positive([3,-1,2]));
assert(!all_positive([]));
assert(!all_positive(true));
assert(!all_positive(false));
assert(!all_positive("foo"));
assert(!all_positive([0:1:2]));
}
test_all_positive();
module test_all_negative() {
assert(all_negative(-2));
assert(!all_negative(0));
assert(!all_negative(2));
assert(!all_negative([0,0,0]));
assert(!all_negative([0,1,2]));
assert(!all_negative([3,1,2]));
assert(!all_negative([3,-1,2]));
assert(all_negative([-3,-1,-2]));
assert(!all_negative([-3,1,-2]));
assert(all_negative([[-5,-7],[-3,-1,-2]]));
assert(!all_negative([[-5,-7],[-3,1,-2]]));
assert(!all_negative([]));
assert(!all_negative(true));
assert(!all_negative(false));
assert(!all_negative("foo"));
assert(!all_negative([0:1:2]));
}
test_all_negative();
module test_all_nonpositive() {
assert(all_nonpositive(-2));
assert(all_nonpositive(0));
assert(!all_nonpositive(2));
assert(all_nonpositive([0,0,0]));
assert(!all_nonpositive([0,1,2]));
assert(all_nonpositive([0,-1,-2]));
assert(!all_nonpositive([3,1,2]));
assert(!all_nonpositive([3,-1,2]));
assert(!all_nonpositive([]));
assert(!all_nonpositive(true));
assert(!all_nonpositive(false));
assert(!all_nonpositive("foo"));
assert(!all_nonpositive([0:1:2]));
}
test_all_nonpositive();
module test_all_nonnegative() {
assert(!all_nonnegative(-2));
assert(all_nonnegative(0));
assert(all_nonnegative(2));
assert(all_nonnegative([0,0,0]));
assert(all_nonnegative([0,1,2]));
assert(all_nonnegative([3,1,2]));
assert(!all_nonnegative([3,-1,2]));
assert(!all_nonnegative([-3,-1,-2]));
assert(!all_nonnegative([[-5,-7],[-3,-1,-2]]));
assert(!all_nonnegative([[-5,-7],[-3,1,-2]]));
assert(!all_nonnegative([[5,7],[3,-1,2]]));
assert(all_nonnegative([[5,7],[3,1,2]]));
assert(!all_nonnegative([]));
assert(!all_nonnegative(true));
assert(!all_nonnegative(false));
assert(!all_nonnegative("foo"));
assert(!all_nonnegative([0:1:2]));
}
test_all_nonnegative();
module test_all_integer() {
@@ -253,20 +131,6 @@ module test_all_integer() {
test_all_integer();
module test_approx() {
assert_equal(approx(PI, 3.141592653589793236), true);
assert_equal(approx(PI, 3.1415926), false);
assert_equal(approx(PI, 3.1415926, eps=1e-6), true);
assert_equal(approx(-PI, -3.141592653589793236), true);
assert_equal(approx(-PI, -3.1415926), false);
assert_equal(approx(-PI, -3.1415926, eps=1e-6), true);
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();
module test_min_index() {