Fixed product() for matrices.

This commit is contained in:
Revar Desmera 2019-05-12 13:41:26 -07:00
parent ba9b7c5b3b
commit 11e5406951
2 changed files with 6 additions and 1 deletions

View File

@ -279,12 +279,13 @@ function deltas(v) = len(v)<2? v : [for (p=pair(v)) p.y-p.x];
// Description:
// Returns the product of all entries in the given list.
// If passed an array of vectors, returns a vector of products of each part.
// If passed an array of matrices, returns a the resulting product matrix.
// Arguments:
// v = The list to get the product of.
// Example:
// product([2,3,4]); // returns 24.
// product([[1,2,3], [3,4,5], [5,6,7]]); // returns [15, 48, 105]
function product(v, i=0, tot=undef) = i>=len(v)? tot : product(v, i+1, ((tot==undef)? v[i] : is_list(v[i])? vmul(tot,v[i]) : tot*v[i]));
function product(v, i=0, tot=undef) = i>=len(v)? tot : product(v, i+1, ((tot==undef)? v[i] : is_vector(v[i])? vmul(tot,v[i]) : tot*v[i]));
// Function: mean()

View File

@ -263,6 +263,10 @@ test_deltas();
module test_product() {
assert(product([2,3,4]) == 24);
assert(product([[1,2,3], [3,4,5], [5,6,7]]) == [15, 48, 105]);
m1 = [[2,3,4],[4,5,6],[6,7,8]];
m2 = [[4,1,2],[3,7,2],[8,7,4]];
m3 = [[3,7,8],[9,2,4],[5,8,3]];
assert(product([m1,m2,m3]) == m1*m2*m3);
}
test_product();