From 1435100e37d18bdd9a932055cd91a64435686d01 Mon Sep 17 00:00:00 2001 From: Garth Minette Date: Fri, 25 Sep 2020 00:31:42 -0700 Subject: [PATCH] Implemented Issue #262 --- math.scad | 12 +++++++----- tests/test_math.scad | 3 +-- version.scad | 2 +- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/math.scad b/math.scad index 935c44a4..3569866c 100644 --- a/math.scad +++ b/math.scad @@ -26,17 +26,19 @@ NAN = acos(2); // The value `nan`, useful for comparisons. // Usage: // sqr(x); // Description: -// Returns the square of the given number or entries in list +// 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 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([[1,2],[3,4]]); // Returns [[1,4],[9,16]] -// sqr([[1,2],3]); // Returns [[1,4],9] +// sqr([[1,2],[3,4]]); // Returns [[7,10],[15,22]] function sqr(x) = - is_list(x) ? [for(val=x) sqr(val)] : is_finite(x) ? x*x : - assert(is_finite(x) || is_vector(x), "Input is not a number nor a list of numbers."); + 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."); // Function: log2() diff --git a/tests/test_math.scad b/tests/test_math.scad index 5641416a..69f6fbde 100644 --- a/tests/test_math.scad +++ b/tests/test_math.scad @@ -314,8 +314,7 @@ module test_sqr() { 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],[3,5,7]]), [[4,9,16],[9,25,49]]); - assert_equal(sqr([]),[]); + 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 33f39d81..834cfc60 100644 --- a/version.scad +++ b/version.scad @@ -8,7 +8,7 @@ ////////////////////////////////////////////////////////////////////// -BOSL_VERSION = [2,0,430]; +BOSL_VERSION = [2,0,431]; // Section: BOSL Library Version Functions