From 11e540695139d2b1f8726b7110518d6d536502e8 Mon Sep 17 00:00:00 2001
From: Revar Desmera <revarbat@gmail.com>
Date: Sun, 12 May 2019 13:41:26 -0700
Subject: [PATCH] Fixed product() for matrices.

---
 math.scad            | 3 ++-
 tests/test_math.scad | 4 ++++
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/math.scad b/math.scad
index 31a9ab3..075597c 100644
--- a/math.scad
+++ b/math.scad
@@ -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()
diff --git a/tests/test_math.scad b/tests/test_math.scad
index b8d60a6..a3f897e 100644
--- a/tests/test_math.scad
+++ b/tests/test_math.scad
@@ -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();