summaryrefslogtreecommitdiff
path: root/det.awk
diff options
context:
space:
mode:
authorwukong <wukong@longaeva>2023-12-18 12:37:31 -0800
committerwukong <wukong@longaeva>2023-12-18 12:37:31 -0800
commit309c5d8f7ff8c36235222d079955cd3783bb7ad0 (patch)
tree22fa0994657cee27292ef1531211a47293afab5f /det.awk
parent0c1d68ee8cc2a631d6029285e771ebbfe119995d (diff)
Added a partially working function performing LU decomposition on a square
matrix, ludcmp.awk. This is providing correct answers on _some_ test data, further testing debugging required here.
Diffstat (limited to 'det.awk')
-rw-r--r--det.awk57
1 files changed, 57 insertions, 0 deletions
diff --git a/det.awk b/det.awk
new file mode 100644
index 0000000..b3e5c1f
--- /dev/null
+++ b/det.awk
@@ -0,0 +1,57 @@
+#!/usr/bin/awk -f
+
+### det.awk
+# determinant via LU
+# input: square array as delimited text
+# output: (scalar) determinant
+
+BEGIN {
+ OFS = "\t"
+ sign = "[+-]?"
+ decimal = "[0-9]+[.]?[0-9]*"
+ fraction = "[.][0-9]*"
+ exponent = "([Ee]" sign "[0-9]+)?"
+ number = "^" sign "(" decimal "|" fraction ")" exponent "$"
+}
+
+NR == 1 {
+ for (y=1; y<=NF; y++)
+ ($y ~ number) ? header[y] = "col" y : header[y] = $y
+}
+
+NF > 0 {
+ (NF > nf_max) ? nf_max = NF : nf_max = nf_max
+
+ ### columns
+ for (y=1; y<=nf_max; y++) {
+ ### rows
+ if ($y !~ number) {
+ continue
+ }
+ else {
+ read_data[NR,y] = $y
+ col_sum[y] += $y
+ row_sum[NR] += $y
+ }
+ }
+}
+
+END {
+ print length(row_sum), length(col_sum)
+ ### columns
+ for (y=1; y<=nf_max; y++) {
+ if (y in col_sum) {
+ ### rows
+ for (x=1; x<=NR; x++) {
+ if (x in row_sum) {
+ printf("[" OFMT "," OFMT "]" OFS OFMT OFS OFMT,
+ x, y, read_data[x,y], col_sum[y])
+ if (x < nf_max)
+ printf(OFS)
+ }
+ }
+ }
+ printf(ORS)
+ }
+}
+