mirror of
https://github.com/BelfrySCAD/BOSL2.git
synced 2025-01-01 09:49:45 +00:00
Added is_matrix() and error handling for linear_solve() and qr()
This commit is contained in:
parent
d26a1ab3d4
commit
4722cc0d01
1 changed files with 16 additions and 0 deletions
16
math.scad
16
math.scad
|
@ -538,6 +538,8 @@ function mean(v) = sum(v)/len(v);
|
||||||
// the least squares solution is returned. If A is underdetermined, the minimal norm solution is returned.
|
// the least squares solution is returned. If A is underdetermined, the minimal norm solution is returned.
|
||||||
// If A is rank deficient or singular then linear_solve returns `undef`.
|
// If A is rank deficient or singular then linear_solve returns `undef`.
|
||||||
function linear_solve(A,b) =
|
function linear_solve(A,b) =
|
||||||
|
assert(is_matrix(A))
|
||||||
|
assert(is_vector(b))
|
||||||
let(
|
let(
|
||||||
dim = array_dim(A),
|
dim = array_dim(A),
|
||||||
m=dim[0], n=dim[1]
|
m=dim[0], n=dim[1]
|
||||||
|
@ -569,6 +571,7 @@ function submatrix(M,ind1,ind2) = [for(i=ind1) [for(j=ind2) M[i][j] ] ];
|
||||||
// Calculates the QR factorization of the input matrix A and returns it as the list [Q,R]. This factorization can be
|
// Calculates the QR factorization of the input matrix A and returns it as the list [Q,R]. This factorization can be
|
||||||
// used to solve linear systems of equations.
|
// used to solve linear systems of equations.
|
||||||
function qr_factor(A) =
|
function qr_factor(A) =
|
||||||
|
assert(is_matrix(A))
|
||||||
let(
|
let(
|
||||||
dim = array_dim(A),
|
dim = array_dim(A),
|
||||||
m = dim[0],
|
m = dim[0],
|
||||||
|
@ -673,6 +676,19 @@ function determinant(M) =
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
// Function: is_matrix()
|
||||||
|
// Usage:
|
||||||
|
// is_matrix(A,[m],[n])
|
||||||
|
// Description:
|
||||||
|
// Returns true if A is a numeric matrix of height m and width n. If m or n
|
||||||
|
// are omitted or set to undef then true is returned for any positive dimension.
|
||||||
|
function is_matrix(A,m,n) =
|
||||||
|
is_list(A) && len(A)>0 &&
|
||||||
|
(is_undef(m) || len(A)==m) &&
|
||||||
|
is_vector(A[0]) &&
|
||||||
|
(is_undef(n) || len(A[0])==n) &&
|
||||||
|
is_consistent(A);
|
||||||
|
|
||||||
|
|
||||||
// Section: Comparisons and Logic
|
// Section: Comparisons and Logic
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue