diff --git a/readme.md b/readme.md index 8298a6b..aba64a5 100644 --- a/readme.md +++ b/readme.md @@ -6750,7 +6750,6 @@ Maths utilities for manipulating vectors and matrices. | `argcoth(x)` | inverse hyperbolic cotangent | | `argsinh(x)` | inverse hyperbolic sine | | `argtanh(x)` | inverse hyperbolic tangent | -| `augment(m)` | Augment a matrix by adding an identity matrix to the right | | `circle_intersect(c1, r1, c2, r2)` | Calculate one point where two circles in the X-Z plane intersect, clockwise around c1 | | `cosh(x)` | hyperbolic cosine | | `coth(x)` | hyperbolic cotangent | @@ -6770,11 +6769,8 @@ Maths utilities for manipulating vectors and matrices. | `rot2_z(a)` | Generate a 2x2 matrix to rotate around z | | `rot3_z(a)` | Generate a 3x3 matrix to rotate around z | | `rotate(a, v)` | Generate a 4x4 rotation matrix, `a` can be a vector of three angles or a single angle around `z`, or around axis `v` | -| `rowswap(m, i, j)` | Swap two rows of a matrix | | `scale(v)` | Generate a 4x4 matrix that scales by `v`, which can be a vector of xyz factors or a scalar to scale all axes equally | | `sinh(x)` | hyperbolic sine | -| `solve(m, i = 0, j = 0)` | Solve each row ensuring diagonal is not zero | -| `solve_row(m, i)` | Make diagonal one by dividing the row by it and subtract from other rows to make column zero | | `sqr(x)` | Square x | | `sumv(v)` | sum a vector of values that can be added with "+" | | `tanh(x)` | hyperbolic tangent | diff --git a/utils/maths.scad b/utils/maths.scad index 6a51cd5..c4d6b64 100644 --- a/utils/maths.scad +++ b/utils/maths.scad @@ -118,24 +118,24 @@ module position_children(list, t) //! Position children if they are on the Z = 0 // Matrix inversion: https://www.mathsisfun.com/algebra/matrix-inverse-row-operations-gauss-jordan.html -function augment(m) = let(l = len(m), n = identity(l)) [ //! Augment a matrix by adding an identity matrix to the right +function augment(m) = let(l = len(m), n = identity(l)) [ // Augment a matrix by adding an identity matrix to the right for(i = [0 : l - 1]) concat(m[i], n[i]) ]; -function rowswap(m, i, j) = [ //! Swap two rows of a matrix +function rowswap(m, i, j) = [ // Swap two rows of a matrix for(k = [0 : len(m) - 1]) k == i ? m[j] : k == j ? m[i] : m[k] ]; -function solve_row(m, i) = let(diag = m[i][i]) [ //! Make diagonal one by dividing the row by it and subtract from other rows to make column zero +function solve_row(m, i) = let(diag = m[i][i]) [ // Make diagonal one by dividing the row by it and subtract from other rows to make column zero for(j = [0 : len(m) - 1]) i == j ? m[j] / diag : m[j] - m[i] * m[j][i] / diag ]; function nearly_zero(x) = abs(x) < 1e-5; //! True if x is close to zero -function solve(m, i = 0, j = 0) = //! Solve each row ensuring diagonal is not zero +function solve(m, i = 0, j = 0) = // Solve each row ensuring diagonal is not zero i < len(m) ? assert(i + j < len(m), "matrix is singular") solve(!nearly_zero(m[i + j][i]) ? solve_row(j ? rowswap(m, i, i + j) : m, i) : solve(m, i, j + 1), i + 1)