From 2ec87b2287fcb6f20d7f617cfabd04bc00e218f1 Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Wed, 26 Feb 2020 09:36:55 +0800 Subject: [PATCH] add m_determinant --- .../_impl/_m_determinant_impl.scad | 20 +++++++++++++++++++ src/experimental/m_determinant.scad | 3 +++ 2 files changed, 23 insertions(+) create mode 100644 src/experimental/_impl/_m_determinant_impl.scad create mode 100644 src/experimental/m_determinant.scad diff --git a/src/experimental/_impl/_m_determinant_impl.scad b/src/experimental/_impl/_m_determinant_impl.scad new file mode 100644 index 00000000..f838921b --- /dev/null +++ b/src/experimental/_impl/_m_determinant_impl.scad @@ -0,0 +1,20 @@ +use ; + +function _m_determinant_sum(lt, leng, i = 0) = + i == leng ? 0 : (lt[i] + _m_determinant_sum(lt, leng, i + 1)); + +function _m_determinant_sub(matrix, leng, fc) = + let( + init_sub_m = [for(i = [1:leng - 1]) matrix[i]], + sub_m = [for(i = [0:len(init_sub_m) - 1]) + concat(slice(init_sub_m[i], 0, fc), slice(init_sub_m[i], fc + 1)) + ], + sgn = pow(-1, fc % 2) + ) + sgn * matrix[0][fc] * _m_determinant(sub_m); + +function _m_determinant(matrix) = + let(leng = len(matrix)) + leng == 2 ? matrix[0][0] * matrix[1][1] - matrix[1][0] * matrix[0][1] : + let(indices = [for(i = [0:leng - 1]) i]) + _m_determinant_sum([for(fc = indices) _m_determinant_sub(matrix, leng, fc)], leng); \ No newline at end of file diff --git a/src/experimental/m_determinant.scad b/src/experimental/m_determinant.scad new file mode 100644 index 00000000..b131dc55 --- /dev/null +++ b/src/experimental/m_determinant.scad @@ -0,0 +1,3 @@ +use ; + +function m_determinant(matrix) = _m_determinant(matrix); \ No newline at end of file