trilean-linear 0.0.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Joseph Mearman
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # trilean-linear
2
+
3
+ `solveLinear(A, b)` for [trilean](../trilean/README.md): a registry function that solves a square linear system over [matrix values](../trilean/README.md#matrix-values) by Gaussian elimination with partial pivoting.
4
+
5
+ Solving is not closed-form, so it is not a node kind or a built-in function of `trilean`, whose scope is rules and closed-form formulas. A consumer that wants it merges this registry with the built-in one:
6
+
7
+ ```ts
8
+ import { builtinFunctions, createSyncEvaluator } from "trilean";
9
+ import { linearFunctions } from "trilean-linear";
10
+
11
+ const evaluator = createSyncEvaluator({
12
+ functions: { ...builtinFunctions, ...linearFunctions },
13
+ });
14
+ ```
15
+
16
+ A tree then names it in a `call` node, `{ "kind": "call", "fn": "solveLinear", "args": [A, b] }`, where `A` is an n by n matrix and `b` an n by 1 matrix, and the result is the n by 1 matrix `x` with `A x = b`. Entries may be real or complex. The solution's unit is the unit of `b` over the unit of `A`.
17
+
18
+ - An `A` that is not square, or a `b` that is not a column of the same height, is a `domain-error`.
19
+ - A singular `A`, one whose elimination meets a pivot of exactly zero, is a `domain-error` with the message `solveLinear requires a non-singular matrix`, so it is an indeterminate result like every other failure, never a thrown error. A matrix that is only nearly singular is solved, and its entries are as large and as inexact as the arithmetic gives.
20
+ - Anything that is not a matrix is a `wrong-type`.
21
+ - The work is cubic in the height. `trilean`'s `maxValueEntries` bounds it, since `A` holds the square of the height in entries, and every entry is charged to the node budget before the function runs.
22
+
23
+ Iterating to a tolerance, such as Newton's method, is also not closed-form and is not part of this package: a host loop evaluates a step tree and tests a convergence predicate on each round.
24
+
25
+ ## Build, test, and lint
26
+
27
+ ```sh
28
+ pnpm --dir packages/trilean-linear build
29
+ pnpm --dir packages/trilean-linear lint
30
+ pnpm --dir packages/trilean-linear typecheck
31
+ pnpm --dir packages/trilean-linear test
32
+ ```
33
+
34
+ ## Licence
35
+
36
+ MIT, see [LICENSE](LICENSE).
@@ -0,0 +1,2 @@
1
+ import { linearFunctions, solveLinear } from "./solve-linear.js";
2
+ export { linearFunctions, solveLinear };
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export { linearFunctions, solveLinear } from "./solve-linear.js";
@@ -0,0 +1,9 @@
1
+ import { ComputedValue, FunctionFailure, FunctionRegistry } from "trilean";
2
+ type FunctionResult = ComputedValue | FunctionFailure;
3
+ /**
4
+ * Solves `A x = b` for a square `A` and a column `b`, both matrices, by Gaussian elimination with partial pivoting, over real or complex entries, and returns `x` as a matrix of one column whose unit is the unit of `b` over the unit of `A`. An `A` that is not square, a `b` that is not a column of the same height, and a matrix whose pivot is exactly zero, which is a singular system, are a `domain-error`; a non-matrix argument is a `wrong-type`. The work is cubic in the height, which `maxValueEntries` bounds, since `A` holds the square of it in entries.
5
+ */
6
+ export declare function solveLinear(args: readonly ComputedValue[]): FunctionResult;
7
+ /** The registry that holds `solveLinear`, to merge with the built-in functions when a tree names it. */
8
+ export declare const linearFunctions: FunctionRegistry;
9
+ export {};
@@ -0,0 +1,115 @@
1
+ // src/solve-linear.ts
2
+ const isMatrix = (value) => value?.kind === "matrix";
3
+ const magnitude = ({ re, im }) => Math.hypot(re, im);
4
+ const multiply = (a, b) => ({
5
+ re: a.re * b.re - a.im * b.im,
6
+ im: a.re * b.im + a.im * b.re
7
+ });
8
+ const subtract = (a, b) => ({
9
+ re: a.re - b.re,
10
+ im: a.im - b.im
11
+ });
12
+ /** `a / b` by the conjugate: `b` is never zero here, since a zero pivot ends the elimination first. */
13
+ function divide(a, b) {
14
+ const scale = b.re * b.re + b.im * b.im;
15
+ return {
16
+ re: (a.re * b.re + a.im * b.im) / scale,
17
+ im: (a.im * b.re - a.re * b.im) / scale
18
+ };
19
+ }
20
+ function cellsOf(matrix) {
21
+ return matrix.rows.map((row, r) => row.map((re, c) => ({
22
+ re,
23
+ im: matrix.imaginary?.[r]?.[c] ?? 0
24
+ })));
25
+ }
26
+ /** The unit of `b` divided by the unit of `A`: the solution's entries are measured in it. */
27
+ function quotientUnit(numerator, denominator) {
28
+ const quotient = { ...numerator };
29
+ for (const [dimension, exponent] of Object.entries(denominator ?? {})) quotient[dimension] = (quotient[dimension] ?? 0) - exponent;
30
+ const kept = Object.entries(quotient).filter(([, exponent]) => exponent !== 0);
31
+ return kept.length === 0 ? void 0 : Object.fromEntries(kept);
32
+ }
33
+ /**
34
+ * Solves `A x = b` for a square `A` and a column `b`, both matrices, by Gaussian elimination with partial pivoting, over real or complex entries, and returns `x` as a matrix of one column whose unit is the unit of `b` over the unit of `A`. An `A` that is not square, a `b` that is not a column of the same height, and a matrix whose pivot is exactly zero, which is a singular system, are a `domain-error`; a non-matrix argument is a `wrong-type`. The work is cubic in the height, which `maxValueEntries` bounds, since `A` holds the square of it in entries.
35
+ */
36
+ function solveLinear(args) {
37
+ const [matrixA, matrixB, ...extra] = args;
38
+ if (!isMatrix(matrixA) || !isMatrix(matrixB) || extra.length > 0) return { wrongType: "solveLinear requires a matrix and a column matrix" };
39
+ const size = matrixA.rows.length;
40
+ if ((matrixA.rows[0]?.length ?? 0) !== size || matrixB.rows.length !== size || (matrixB.rows[0]?.length ?? 0) !== 1) return { domainError: "solveLinear requires a square matrix and a column of the same height" };
41
+ const a = cellsOf(matrixA);
42
+ const b = cellsOf(matrixB);
43
+ for (let column = 0; column < size; column += 1) {
44
+ let pivot = column;
45
+ for (let row = column + 1; row < size; row += 1) if (magnitude(a[row]?.[column] ?? {
46
+ re: 0,
47
+ im: 0
48
+ }) > magnitude(a[pivot]?.[column] ?? {
49
+ re: 0,
50
+ im: 0
51
+ })) pivot = row;
52
+ const pivotRow = a[pivot];
53
+ const pivotCell = pivotRow?.[column];
54
+ if (pivotRow === void 0 || pivotCell === void 0 || magnitude(pivotCell) === 0) return { domainError: "solveLinear requires a non-singular matrix" };
55
+ [a[column], a[pivot]] = [pivotRow, a[column] ?? pivotRow];
56
+ [b[column], b[pivot]] = [b[pivot] ?? [], b[column] ?? []];
57
+ for (let row = column + 1; row < size; row += 1) {
58
+ const factor = divide(a[row]?.[column] ?? {
59
+ re: 0,
60
+ im: 0
61
+ }, pivotCell);
62
+ for (let k = column; k < size; k += 1) {
63
+ const target = a[row];
64
+ if (target !== void 0) target[k] = subtract(target[k] ?? {
65
+ re: 0,
66
+ im: 0
67
+ }, multiply(factor, a[column]?.[k] ?? {
68
+ re: 0,
69
+ im: 0
70
+ }));
71
+ }
72
+ const rhs = b[row];
73
+ if (rhs !== void 0) rhs[0] = subtract(rhs[0] ?? {
74
+ re: 0,
75
+ im: 0
76
+ }, multiply(factor, b[column]?.[0] ?? {
77
+ re: 0,
78
+ im: 0
79
+ }));
80
+ }
81
+ }
82
+ const solution = Array.from({ length: size }, () => ({
83
+ re: 0,
84
+ im: 0
85
+ }));
86
+ for (let row = size - 1; row >= 0; row -= 1) {
87
+ let sum = b[row]?.[0] ?? {
88
+ re: 0,
89
+ im: 0
90
+ };
91
+ for (let k = row + 1; k < size; k += 1) sum = subtract(sum, multiply(a[row]?.[k] ?? {
92
+ re: 0,
93
+ im: 0
94
+ }, solution[k] ?? {
95
+ re: 0,
96
+ im: 0
97
+ }));
98
+ solution[row] = divide(sum, a[row]?.[row] ?? {
99
+ re: 1,
100
+ im: 0
101
+ });
102
+ }
103
+ if (!solution.every((cell) => Number.isFinite(cell.re) && Number.isFinite(cell.im))) return { domainError: "solveLinear produced an entry that is not finite" };
104
+ const unit = quotientUnit(matrixB.unit, matrixA.unit);
105
+ const complex = solution.some((cell) => cell.im !== 0);
106
+ return {
107
+ kind: "matrix",
108
+ rows: solution.map((cell) => [cell.re]),
109
+ ...complex ? { imaginary: solution.map((cell) => [cell.im]) } : {},
110
+ ...unit === void 0 ? {} : { unit }
111
+ };
112
+ }
113
+ /** The registry that holds `solveLinear`, to merge with the built-in functions when a tree names it. */
114
+ const linearFunctions = Object.freeze({ solveLinear });
115
+ export { linearFunctions, solveLinear };
package/package.json CHANGED
@@ -1,5 +1,84 @@
1
1
  {
2
2
  "name": "trilean-linear",
3
- "version": "0.0.0",
4
- "private": false
3
+ "description": "A linear-system solver for trilean: solveLinear, a registry function that solves a square system over matrices by Gaussian elimination with partial pivoting, and refuses a singular one as an indeterminate domain-error. It is kept out of the evaluator because solving is not closed-form.",
4
+ "version": "1.0.0",
5
+ "bugs": {
6
+ "url": "https://github.com/ExaDev/trilean/issues"
7
+ },
8
+ "dependencies": {
9
+ "trilean": "3.3.2"
10
+ },
11
+ "devDependencies": {
12
+ "@arethetypeswrong/cli": "^0.18.5",
13
+ "@exadev/eslint-config": "3.1.0",
14
+ "@types/node": "^26.6.6",
15
+ "@vitest/coverage-v8": "^5.1.1",
16
+ "eslint": "^10.15.0",
17
+ "eslint-config-prettier": "^10.1.8",
18
+ "eslint-plugin-prettier": "^5.5.7",
19
+ "globals": "^17.14.0",
20
+ "prettier": "^3.10.1",
21
+ "publint": "^0.3.25",
22
+ "tsdown": "^0.23.7",
23
+ "turbo": "^2.11.13",
24
+ "typescript": "^6.0.3",
25
+ "typescript-eslint": "^8.73.0",
26
+ "vitest": "^5.1.1"
27
+ },
28
+ "engines": {
29
+ "node": "^20.19.0 || >=22.12.0"
30
+ },
31
+ "exports": {
32
+ ".": {
33
+ "types": "./dist/index.d.ts",
34
+ "default": "./dist/index.js"
35
+ },
36
+ "./*": {
37
+ "types": "./dist/*.d.ts",
38
+ "default": "./dist/*.js"
39
+ }
40
+ },
41
+ "files": [
42
+ "dist"
43
+ ],
44
+ "homepage": "https://github.com/ExaDev/trilean/tree/main/packages/trilean-linear",
45
+ "keywords": [
46
+ "gaussian-elimination",
47
+ "linear-algebra",
48
+ "linear-system",
49
+ "matrix",
50
+ "rules-engine",
51
+ "three-valued-logic",
52
+ "trilean"
53
+ ],
54
+ "license": "MIT",
55
+ "publishConfig": {
56
+ "access": "public",
57
+ "provenance": true,
58
+ "registry": "https://registry.npmjs.org/"
59
+ },
60
+ "repository": {
61
+ "type": "git",
62
+ "url": "git+https://github.com/ExaDev/trilean.git",
63
+ "directory": "packages/trilean-linear"
64
+ },
65
+ "scripts": {
66
+ "_build": "tsdown",
67
+ "_lint": "eslint . --fix --cache --max-warnings 0",
68
+ "_prepush": "true",
69
+ "_test": "vitest run --project unit",
70
+ "_test:coverage": "vitest run --project unit --coverage",
71
+ "_typecheck": "tsc -p tsconfig.json && tsc -p tsconfig.node.json",
72
+ "_typecheck:attw": "attw --pack --profile esm-only",
73
+ "build": "turbo run _build",
74
+ "lint": "turbo run _lint",
75
+ "prepublishOnly": "pnpm run lint && pnpm run typecheck && pnpm run test && pnpm run build && publint && attw --pack --profile esm-only",
76
+ "prepush": "turbo run _prepush",
77
+ "test": "turbo run _test",
78
+ "test:coverage": "turbo run _test:coverage",
79
+ "typecheck": "turbo run _typecheck _typecheck:attw"
80
+ },
81
+ "sideEffects": false,
82
+ "type": "module",
83
+ "types": "./dist/index.d.ts"
5
84
  }