mirror of
https://github.com/BelfrySCAD/BOSL2.git
synced 2025-03-04 16:39:37 +00:00
Merge pull request #282 from revarbat/revarbat_dev
sqr() fixes for #262
This commit is contained in:
commit
9ce6d28a1b
3 changed files with 7 additions and 29 deletions
24
math.scad
24
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()
|
||||
|
@ -554,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)
|
||||
|
@ -762,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),
|
||||
|
|
|
@ -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();
|
||||
|
@ -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);
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
BOSL_VERSION = [2,0,432];
|
||||
BOSL_VERSION = [2,0,434];
|
||||
|
||||
|
||||
// Section: BOSL Library Version Functions
|
||||
|
|
Loading…
Reference in a new issue