@siteimprove/alfa-math 0.108.2 → 0.109.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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @siteimprove/alfa-math
2
2
 
3
+ ## 0.109.0
4
+
5
+ ### Patch Changes
6
+
7
+ - **Added:** `Matrix.multiply` now accepts vectors as the left/right operand, which is turned into a single row/column matrix. ([#1944](https://github.com/Siteimprove/alfa/pull/1944))
8
+
3
9
  ## 0.108.2
4
10
 
5
11
  ## 0.108.1
package/dist/matrix.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { Vector } from "./vector.js";
1
2
  /**
2
3
  * {@link https://en.wikipedia.org/wiki/Matrix_(mathematics)}
3
4
  *
@@ -23,7 +24,7 @@ export declare namespace Matrix {
23
24
  /**
24
25
  * {@link https://en.wikipedia.org/wiki/Matrix_multiplication}
25
26
  */
26
- function multiply(m: Matrix, n: number | Matrix): Matrix;
27
+ function multiply(m: Matrix | Vector, n: number | Matrix | Vector): Matrix;
27
28
  /**
28
29
  * Compute the transpose of a matrix.
29
30
  *
package/dist/matrix.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { Real } from "./real.js";
2
+ import { Vector } from "./vector.js";
2
3
  /**
3
4
  * @public
4
5
  */
@@ -63,9 +64,15 @@ export var Matrix;
63
64
  * {@link https://en.wikipedia.org/wiki/Matrix_multiplication}
64
65
  */
65
66
  function multiply(m, n) {
66
- return typeof n === "number"
67
- ? m.map((r) => r.map((v) => v * n))
68
- : m.map((r, i) => n[0].map((_, j) => r.reduce((s, _, k) => s + m[i][k] * n?.[k]?.[j], 0)));
67
+ // If m is a vector, treat it as a single row matrix.
68
+ // [a, b, c] => [[a, b, c]]
69
+ const left = Vector.isVector(m) ? [m] : m;
70
+ // If n is a vector, treat it as a single column matrix.
71
+ // [a, b, c] => [[a], [b], [c]]
72
+ const right = Vector.isVector(n) ? n.map((x) => [x]) : n;
73
+ return typeof right === "number"
74
+ ? left.map((r) => r.map((v) => v * right))
75
+ : left.map((r, i) => right[0].map((_, j) => r.reduce((s, _, k) => s + left[i][k] * right?.[k]?.[j], 0)));
69
76
  }
70
77
  Matrix.multiply = multiply;
71
78
  /**
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "$schema": "http://json.schemastore.org/package",
3
3
  "name": "@siteimprove/alfa-math",
4
4
  "homepage": "https://alfa.siteimprove.com",
5
- "version": "0.108.2",
5
+ "version": "0.109.0",
6
6
  "license": "MIT",
7
7
  "description": "Functionality for working with numbers and mathematical structures, such as vectors and matrices",
8
8
  "repository": {
@@ -22,7 +22,7 @@
22
22
  "dist/**/*.d.ts"
23
23
  ],
24
24
  "devDependencies": {
25
- "@siteimprove/alfa-test": "^0.108.2"
25
+ "@siteimprove/alfa-test": "^0.109.0"
26
26
  },
27
27
  "publishConfig": {
28
28
  "access": "public",