Simplify sqr(x) to x*x

This commit is contained in:
Garth Minette 2020-09-28 16:52:17 -07:00
parent 8ce63f83c6
commit 37ae0cbba0
3 changed files with 6 additions and 8 deletions

View file

@ -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()

View file

@ -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();

View file

@ -8,7 +8,7 @@
//////////////////////////////////////////////////////////////////////
BOSL_VERSION = [2,0,432];
BOSL_VERSION = [2,0,433];
// Section: BOSL Library Version Functions