From e94d21ee3a2d7d053899d490b115ba763d0a1bc2 Mon Sep 17 00:00:00 2001 From: rodri Date: Wed, 20 Dec 2023 11:27:36 +0000 Subject: [PATCH] libgeometry: add matrix minor and cofactor functions --- sys/include/geometry.h | 4 ++++ sys/man/2/geometry | 26 ++++++++++++++++++++++++- sys/src/libgeometry/matrix.c | 37 ++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/sys/include/geometry.h b/sys/include/geometry.h index 401ed2ae7..31c2ebde6 100644 --- a/sys/include/geometry.h +++ b/sys/include/geometry.h @@ -84,6 +84,8 @@ void smulm(Matrix, double); void transposem(Matrix); double detm(Matrix); double tracem(Matrix); +double minorm(Matrix, int, int); +double cofactorm(Matrix, int, int); void adjm(Matrix); void invm(Matrix); Point2 xform(Point2, Matrix); @@ -97,6 +99,8 @@ void smulm3(Matrix3, double); void transposem3(Matrix3); double detm3(Matrix3); double tracem3(Matrix3); +double minorm3(Matrix3, int, int); +double cofactorm3(Matrix3, int, int); void adjm3(Matrix3); void invm3(Matrix3); Point3 xform3(Point3, Matrix3); diff --git a/sys/man/2/geometry b/sys/man/2/geometry index 9eeb57c53..8f8de54aa 100644 --- a/sys/man/2/geometry +++ b/sys/man/2/geometry @@ -1,6 +1,6 @@ .TH GEOMETRY 2 .SH NAME -Flerp, fclamp, Pt2, Vec2, addpt2, subpt2, mulpt2, divpt2, lerp2, dotvec2, vec2len, normvec2, edgeptcmp, ptinpoly, Pt3, Vec3, addpt3, subpt3, mulpt3, divpt3, lerp3, dotvec3, crossvec3, vec3len, normvec3, identity, addm, subm, mulm, smulm, transposem, detm, tracem, adjm, invm, xform, identity3, addm3, subm3, mulm3, smulm3, transposem3, detm3, tracem3, adjm3, invm3, xform3, Quat, Quatvec, addq, subq, mulq, smulq, sdivq, dotq, invq, qlen, normq, slerp, qrotate, rframexform, rframexform3, invrframexform, invrframexform3, centroid, barycoords, centroid3, vfmt, Vfmt, GEOMfmtinstall \- computational geometry library +Flerp, fclamp, Pt2, Vec2, addpt2, subpt2, mulpt2, divpt2, lerp2, dotvec2, vec2len, normvec2, edgeptcmp, ptinpoly, Pt3, Vec3, addpt3, subpt3, mulpt3, divpt3, lerp3, dotvec3, crossvec3, vec3len, normvec3, identity, addm, subm, mulm, smulm, transposem, detm, tracem, minorm, cofactorm, adjm, invm, xform, identity3, addm3, subm3, mulm3, smulm3, transposem3, detm3, tracem3, minorm3, cofactorm3, adjm3, invm3, xform3, Quat, Quatvec, addq, subq, mulq, smulq, sdivq, dotq, invq, qlen, normq, slerp, qrotate, rframexform, rframexform3, invrframexform, invrframexform3, centroid, barycoords, centroid3, vfmt, Vfmt, GEOMfmtinstall \- computational geometry library .SH SYNOPSIS .de PB .PP @@ -95,6 +95,8 @@ void smulm(Matrix m, double s); void transposem(Matrix m); double detm(Matrix m); double tracem(Matrix m); +double minorm(Matrix m, int row, int col); +double cofactorm(Matrix m, int row, int col); void adjm(Matrix m); void invm(Matrix m); Point2 xform(Point2 p, Matrix m); @@ -108,6 +110,8 @@ void smulm3(Matrix3 m, double s); void transposem3(Matrix3 m); double detm3(Matrix3 m); double tracem3(Matrix3 m); +double minorm3(Matrix3 m, int row, int col); +double cofactorm3(Matrix3 m, int row, int col); void adjm3(Matrix3 m); void invm3(Matrix3 m); Point3 xform3(Point3 p, Matrix3 m); @@ -386,6 +390,16 @@ Computes the trace of .I m and returns the result. .TP +.B minorm(\fIm\fP,\fIrow\fP,\fIcol\fP) +Computes the minor of +.I m +and returns the result. +.TP +.B cofactorm(\fIm\fP,\fIrow\fP,\fIcol\fP) +Computes the cofactor of +.I m +and returns the result. +.TP .B adjm(\fIm\fP) Transforms the matrix .I m @@ -454,6 +468,16 @@ Computes the trace of .I m and returns the result. .TP +.B minorm3(\fIm\fP,\fIrow\fP,\fIcol\fP) +Computes the minor of +.I m +and returns the result. +.TP +.B cofactorm3(\fIm\fP,\fIrow\fP,\fIcol\fP) +Computes the cofactor of +.I m +and returns the result. +.TP .B adjm3(\fIm\fP) Transforms the matrix .I m diff --git a/sys/src/libgeometry/matrix.c b/sys/src/libgeometry/matrix.c index d640403a7..5eebbb18a 100644 --- a/sys/src/libgeometry/matrix.c +++ b/sys/src/libgeometry/matrix.c @@ -84,6 +84,24 @@ tracem(Matrix m) return m[0][0] + m[1][1] + m[2][2]; } +double +minorm(Matrix m, int row, int col) +{ + int i, j; + double subm[2][2]; + + for(i = 0; i < 3-1; i++) + for(j = 0; j < 3-1; j++) + subm[i][j] = m[i < row? i: i+1][j < col? j: j+1]; + return subm[0][0]*subm[1][1] - subm[0][1]*subm[1][0]; +} + +double +cofactorm(Matrix m, int row, int col) +{ + return minorm(m, row, col)*((row+col)&1 == 0? 1: -1); +} + void adjm(Matrix m) { @@ -238,6 +256,25 @@ tracem3(Matrix3 m) return m[0][0] + m[1][1] + m[2][2] + m[3][3]; } +double +minorm3(Matrix3 m, int row, int col) +{ + int i, j; + Matrix subm; + + memset(subm, 0, sizeof subm); + for(i = 0; i < 4-1; i++) + for(j = 0; j < 4-1; j++) + subm[i][j] = m[i < row? i: i+1][j < col? j: j+1]; + return detm(subm); +} + +double +cofactorm3(Matrix3 m, int row, int col) +{ + return minorm3(m, row, col)*((row+col)&1 == 0? 1: -1); +} + void adjm3(Matrix3 m) {