Debian Codegraph @

node/colorjs.io/src/multiply-matrices.js

raw
1 /** @import { Matrix3x3, Vector3 } from "./types.js" */
2
3 /**
4 * A is m x n. B is n x p. product is m x p.
5 *
6 * Array arguments are treated like vectors:
7 * - A becomes 1 x n
8 * - B becomes n x 1
9 *
10 * Returns Matrix m x p or equivalent array or number
11 *
12 * @overload
13 * @param {number[]} A Vector 1 x n
14 * @param {number[]} B Vector n x 1
15 * @returns {number} Scalar number
16 *
17 * @overload
18 * @param {number[][]} A Matrix m x n
19 * @param {number[]} B Vector n x 1
20 * @returns {number[]} Array with length m
21 *
22 * @overload
23 * @param {number[]} A Vector 1 x n
24 * @param {number[][]} B Matrix n x p
25 * @returns {number[]} Array with length p
26 *
27 * @overload
28 * @param {number[][]} A Matrix m x n
29 * @param {number[][]} B Matrix n x p
30 * @returns {number[][]} Matrix m x p
31 *
32 * @param {number[] | number[][]} A Matrix m x n or a vector
33 * @param {number[] | number[][]} B Matrix n x p or a vector
34 * @returns {number | number[] | number[][]} Matrix m x p or equivalent array or number
35 */
36 export default function multiplyMatrices (A, B) {
37 let m = A.length;
38 /** @type {number[][]} */
39 let AM;
40 /** @type {number[][]} */
41 let BM;
42 let aVec = false;
43 let bVec = false;
44
45 if (!Array.isArray(A[0])) {
46 // A is vector, convert to [[a, b, c, ...]]
47 AM = [/** @type {number[]} */ (A)];
48 m = AM.length;
49 aVec = true;
50 }
51 else {
52 AM = /** @type {number[][]} */ (A);
53 }
54
55 if (!Array.isArray(B[0])) {
56 // B is vector, convert to [[a], [b], [c], ...]]
57 BM = B.length > 0 ? B.map(x => [x]) : [[]]; // Avoid mapping empty array
58 bVec = true;
59 }
60 else {
61 BM = /** @type {number[][]} */ (B);
62 }
63
64 let p = BM[0].length;
65 let BM_cols = BM[0].map((_, i) => BM.map(x => x[i])); // transpose B
66 /** @type {number[] | number[][]} */
67 let product = AM.map(row =>
68 BM_cols.map(col => {
69 let ret = 0;
70
71 if (!Array.isArray(row)) {
72 for (let c of col) {
73 ret += row * c;
74 }
75
76 return ret;
77 }
78
79 for (let i = 0; i < row.length; i++) {
80 ret += row[i] * (col[i] || 0);
81 }
82
83 return ret;
84 }));
85
86 if (m === 1 && aVec) {
87 product = product[0]; // Avoid [[a, b, c, ...]]
88 }
89 if (p === 1 && bVec) {
90 if (m === 1 && aVec) {
91 return product[0]; // Avoid [[a]], return a number
92 }
93 else {
94 return product.map(x => x[0]); // Avoid [[a], [b], [c], ...]]
95 }
96 }
97
98 return product;
99 }
100
101 // dot3 and transform functions adapted from https://github.com/texel-org/color/blob/9793c7d4d02b51f068e0f3fd37131129a4270396/src/core.js
102 //
103 // The MIT License (MIT)
104 // Copyright (c) 2024 Matt DesLauriers
105
106 // Permission is hereby granted, free of charge, to any person obtaining a copy
107 // of this software and associated documentation files (the "Software"), to deal
108 // in the Software without restriction, including without limitation the rights
109 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
110 // copies of the Software, and to permit persons to whom the Software is
111 // furnished to do so, subject to the following conditions:
112
113 // The above copyright notice and this permission notice shall be included in all
114 // copies or substantial portions of the Software.
115
116 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
117 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
118 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
119 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
120 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
121 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
122 // OR OTHER DEALINGS IN THE SOFTWARE.
123
124 /**
125 * Returns the dot product of two vectors each with a length of 3.
126 *
127 * @param {Vector3} a
128 * @param {Vector3} b
129 * @returns {number}
130 */
131 function dot3 (a, b) {
132 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
133 }
134
135 /**
136 * Transforms a vector of length 3 by a 3x3 matrix. Specify the same input and output
137 * vector to transform in place.
138 *
139 * @param {Vector3} input
140 * @param {Matrix3x3} matrix
141 * @param {Vector3} [out]
142 * @returns {Vector3}
143 */
144 export function multiply_v3_m3x3 (input, matrix, out = [0, 0, 0]) {
145 const x = dot3(input, matrix[0]);
146 const y = dot3(input, matrix[1]);
147 const z = dot3(input, matrix[2]);
148 out[0] = x;
149 out[1] = y;
150 out[2] = z;
151 return out;
152 }