From 37ca6efebb6fd144b72a51c39d9f13c212b58e7c Mon Sep 17 00:00:00 2001 From: Adrian Mariano Date: Sun, 2 Aug 2020 10:35:32 -0400 Subject: [PATCH 1/3] Modified subindex to be faster. Note also changed behavior for idx=singleton list. --- arrays.scad | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/arrays.scad b/arrays.scad index 36841fe..1a10b9f 100644 --- a/arrays.scad +++ b/arrays.scad @@ -1172,25 +1172,25 @@ function add_scalar(v,s) = // Function: subindex() +// Usage: +// subindex(M, idx) // Description: -// For each array item, return the indexed subitem. -// Returns a list of the values of each vector at the specfied -// index list or range. If the index list or range has -// only one entry the output list is flattened. +// Extracts the entries listed in idx from each entry in M. For a matrix this means +// selecting a specified set of columsn. If idx is a number the return is a vector, otherwise +// it is a list of lists (the submatrix). // Arguments: -// v = The given list of lists. +// M = The given list of lists. // idx = The index, list of indices, or range of indices to fetch. // Example: -// v = [[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]; -// subindex(v,2); // Returns [3, 7, 11, 15] -// subindex(v,[2,1]); // Returns [[3, 2], [7, 6], [11, 10], [15, 14]] -// subindex(v,[1:3]); // Returns [[2, 3, 4], [6, 7, 8], [10, 11, 12], [14, 15, 16]] -function subindex(v, idx) = - [ for(val=v) - let( value=[for(i=idx) val[i]] ) - len(value)==1 ? value[0] : value - ]; - +// M = [[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]; +// subindex(M,2); // Returns [3, 7, 11, 15] +// subindex(M,[2]); // Returns [[3], [7], [11], [15]] +// subindex(M,[2,1]); // Returns [[3, 2], [7, 6], [11, 10], [15, 14]] +// subindex(M,[1:3]); // Returns [[2, 3, 4], [6, 7, 8], [10, 11, 12], [14, 15, 16]] +function subindex(v, idx) = + is_num(idx) + ? [for(row=M) row[idx]] + : [for(row=M) [for(i=idx) row[i]]]; // Function: zip() // Usage: From 06b69475cb8d1bef5794e3dcff2d0e2945af3ec9 Mon Sep 17 00:00:00 2001 From: Adrian Mariano Date: Sun, 2 Aug 2020 10:38:11 -0400 Subject: [PATCH 2/3] Fix bug. Add test for subindex. --- arrays.scad | 2 +- tests/test_arrays.scad | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/arrays.scad b/arrays.scad index 1a10b9f..0b7b924 100644 --- a/arrays.scad +++ b/arrays.scad @@ -1187,7 +1187,7 @@ function add_scalar(v,s) = // subindex(M,[2]); // Returns [[3], [7], [11], [15]] // subindex(M,[2,1]); // Returns [[3, 2], [7, 6], [11, 10], [15, 14]] // subindex(M,[1:3]); // Returns [[2, 3, 4], [6, 7, 8], [10, 11, 12], [14, 15, 16]] -function subindex(v, idx) = +function subindex(M, idx) = is_num(idx) ? [for(row=M) row[idx]] : [for(row=M) [for(i=idx) row[i]]]; diff --git a/tests/test_arrays.scad b/tests/test_arrays.scad index f621cd0..4fffb7d 100644 --- a/tests/test_arrays.scad +++ b/tests/test_arrays.scad @@ -358,6 +358,7 @@ test_add_scalar(); module test_subindex() { v = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]; assert(subindex(v,2) == [3, 7, 11, 15]); + assert(subindex(v,[2]) == [[3], [7], [11], [15]]); assert(subindex(v,[2,1]) == [[3, 2], [7, 6], [11, 10], [15, 14]]); assert(subindex(v,[1:3]) == [[2, 3, 4], [6, 7, 8], [10, 11, 12], [14, 15, 16]]); } From 8f1162e7bf0cb1a30b9ab8758161d2f0c98ede0a Mon Sep 17 00:00:00 2001 From: Adrian Mariano Date: Sun, 2 Aug 2020 10:38:33 -0400 Subject: [PATCH 3/3] doc typo fix --- arrays.scad | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arrays.scad b/arrays.scad index 0b7b924..4f1c317 100644 --- a/arrays.scad +++ b/arrays.scad @@ -1182,7 +1182,7 @@ function add_scalar(v,s) = // M = The given list of lists. // idx = The index, list of indices, or range of indices to fetch. // Example: -// M = [[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]; +// M = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]; // subindex(M,2); // Returns [3, 7, 11, 15] // subindex(M,[2]); // Returns [[3], [7], [11], [15]] // subindex(M,[2,1]); // Returns [[3, 2], [7, 6], [11, 10], [15, 14]]