From 37ae0cbba00fecd27bb78ba4d7975a55019ada4e Mon Sep 17 00:00:00 2001 From: Garth Minette Date: Mon, 28 Sep 2020 16:52:17 -0700 Subject: [PATCH 1/2] Simplify sqr(x) to x*x --- math.scad | 10 ++++------ tests/test_math.scad | 2 +- version.scad | 2 +- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/math.scad b/math.scad index 3569866c..574f098e 100644 --- a/math.scad +++ b/math.scad @@ -27,18 +27,16 @@ NAN = acos(2); // The value `nan`, useful for comparisons. // sqr(x); // Description: // If given a number, returns the square of that number, -// If given a vector, returns a vector of the squares of each element in the input vector. +// If given a vector, returns the sum-of-squares/dot product of the vector elements. // If given a matrix, returns the matrix multiplication of the matrix with itself. // Examples: // sqr(3); // Returns: 9 // sqr(-4); // Returns: 16 -// sqr([3,4]); // Returns: [9,16] +// sqr([2,3,4]); // Returns: 29 // sqr([[1,2],[3,4]]); // Returns [[7,10],[15,22]] function sqr(x) = - is_finite(x) ? x*x : - is_vector(x) ? vmul(x,x) : - is_matrix(x) ? x*x : - assert(is_finite(x) || is_vector(x) || is_matrix(x), "Input is not a number nor a list of numbers."); + assert(is_finite(x) || is_vector(x) || is_matrix(x), "Input is not a number nor a list of numbers.") + x*x; // Function: log2() diff --git a/tests/test_math.scad b/tests/test_math.scad index 69f6fbde..2ba6a1ac 100644 --- a/tests/test_math.scad +++ b/tests/test_math.scad @@ -313,7 +313,7 @@ module test_sqr() { assert_equal(sqr(2.5), 6.25); assert_equal(sqr(3), 9); assert_equal(sqr(16), 256); - assert_equal(sqr([2,3,4]), [4,9,16]); + assert_equal(sqr([2,3,4]), 29); assert_equal(sqr([[2,3,4],[3,5,7],[3,5,1]]), [[25,41,33],[42,69,54],[24,39,48]]); } test_sqr(); diff --git a/version.scad b/version.scad index afcf6630..6da91fae 100644 --- a/version.scad +++ b/version.scad @@ -8,7 +8,7 @@ ////////////////////////////////////////////////////////////////////// -BOSL_VERSION = [2,0,432]; +BOSL_VERSION = [2,0,433]; // Section: BOSL Library Version Functions From 23bcc1b806e27b454422039a74423cd7d53cde9e Mon Sep 17 00:00:00 2001 From: Garth Minette Date: Mon, 28 Sep 2020 16:56:18 -0700 Subject: [PATCH 2/2] Removed sum_of_squares() as it's redundant with sqr(vector) --- math.scad | 14 +------------- tests/test_math.scad | 8 -------- version.scad | 2 +- 3 files changed, 2 insertions(+), 22 deletions(-) diff --git a/math.scad b/math.scad index 574f098e..9a0a26e6 100644 --- a/math.scad +++ b/math.scad @@ -552,18 +552,6 @@ function _cumsum(v,_i=0,_acc=[]) = ); -// Function: sum_of_squares() -// Description: -// Returns the sum of the square of each element of a vector. -// Arguments: -// v = The vector to get the sum of. -// Example: -// sum_of_squares([1,2,3]); // Returns: 14. -// sum_of_squares([1,2,4]); // Returns: 21 -// sum_of_squares([-3,-2,-1]); // Returns: 14 -function sum_of_squares(v) = sum(vmul(v,v)); - - // Function: sum_of_sines() // Usage: // sum_of_sines(a,sines) @@ -760,7 +748,7 @@ function _qr_factor(A,Q,P, pivot, column, m, n) = column >= min(m-1,n) ? [Q,A,P] : let( swap = !pivot ? 1 - : _swap_matrix(n,column,column+max_index([for(i=[column:n-1]) sum_of_squares([for(j=[column:m-1]) A[j][i]])])), + : _swap_matrix(n,column,column+max_index([for(i=[column:n-1]) sqr([for(j=[column:m-1]) A[j][i]])])), A = pivot ? A*swap : A, x = [for(i=[column:1:m-1]) A[i][column]], alpha = (x[0]<=0 ? 1 : -1) * norm(x), diff --git a/tests/test_math.scad b/tests/test_math.scad index 2ba6a1ac..2f1faafd 100644 --- a/tests/test_math.scad +++ b/tests/test_math.scad @@ -472,14 +472,6 @@ module test_cumsum() { test_cumsum(); -module test_sum_of_squares() { - assert_equal(sum_of_squares([1,2,3]), 14); - assert_equal(sum_of_squares([1,2,4]), 21); - assert_equal(sum_of_squares([-3,-2,-1]), 14); -} -test_sum_of_squares(); - - module test_sum_of_sines() { assert_equal(sum_of_sines(0, [[3,4,0],[2,2,0]]), 0); assert_equal(sum_of_sines(45, [[3,4,0],[2,2,0]]), 2); diff --git a/version.scad b/version.scad index 6da91fae..9d80cf47 100644 --- a/version.scad +++ b/version.scad @@ -8,7 +8,7 @@ ////////////////////////////////////////////////////////////////////// -BOSL_VERSION = [2,0,433]; +BOSL_VERSION = [2,0,434]; // Section: BOSL Library Version Functions