Faster version of norm_fro, and some arg validation

This commit is contained in:
Adrian Mariano 2020-09-02 16:46:58 -04:00
parent 47a1dfaa23
commit 9cd91cc6cc
2 changed files with 5 additions and 1 deletions

View file

@ -1224,6 +1224,7 @@ function block_matrix(M) =
// its diagonal. The off diagonal entries are set to offdiag,
// which is zero by default.
function diagonal_matrix(diag,offdiag=0) =
assert(is_list(diag) && len(diag)>0)
[for(i=[0:1:len(diag)-1]) [for(j=[0:len(diag)-1]) i==j?diag[i] : offdiag]];
@ -1237,6 +1238,8 @@ function diagonal_matrix(diag,offdiag=0) =
function submatrix_set(M,A,m=0,n=0) =
assert(is_list(M))
assert(is_list(A))
assert(is_int(m))
assert(is_int(n))
let( badrows = [for(i=idx(A)) if (!is_list(A[i])) i])
assert(badrows==[], str("Input submatrix malformed rows: ",badrows))
[for(i=[0:1:len(M)-1])

View file

@ -894,7 +894,8 @@ function is_matrix(A,m,n,square=false) =
// squares of all of the entries of the matrix. On vectors it is the same as the usual 2-norm.
// This is an easily computed norm that is convenient for comparing two matrices.
function norm_fro(A) =
sqrt(sum([for(entry=A) sum_of_squares(entry)]));
assert(is_matrix(A) || is_vector(A))
norm(flatten(A));
// Section: Comparisons and Logic