ref: e94d21ee3a2d7d053899d490b115ba763d0a1bc2
parent: 34e7b54c139826b55713702910a1280e6651df60
author: rodri <rgl@antares-labs.eu>
date: Wed Dec 20 06:27:36 EST 2023
libgeometry: add matrix minor and cofactor functions
--- a/sys/include/geometry.h
+++ b/sys/include/geometry.h
@@ -84,6 +84,8 @@
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 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);
--- 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 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 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 @@
.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
@@ -451,6 +465,16 @@
.TP
.B tracem3(\fIm\fP)
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
--- a/sys/src/libgeometry/matrix.c
+++ b/sys/src/libgeometry/matrix.c
@@ -84,6 +84,24 @@
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)
{
@@ -236,6 +254,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