Skip to content

Commit bb7afaa

Browse files
authored
Feat(maths): add matrix multiplication (#211)
* Feat(maths): add matrix multiplication * Refactor maths/matrix_multiplication tabbing
1 parent c7a13e7 commit bb7afaa

File tree

3 files changed

+215
-0
lines changed

3 files changed

+215
-0
lines changed

DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
* [Is Square Free](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_square_free.ts)
112112
* [Juggler Sequence](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/juggler_sequence.ts)
113113
* [Lowest Common Multiple](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/lowest_common_multiple.ts)
114+
* [Matrix Multiplication](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/matrix_multiplication.ts)
114115
* [Number Of Digits](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/number_of_digits.ts)
115116
* [Pascals Triangle](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/pascals_triangle.ts)
116117
* [Perfect Cube](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/perfect_cube.ts)

maths/matrix_multiplication.ts

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @function matrixMultiplication
3+
* @description Multiply a matrix with either another matrix, a vector or a scalar
4+
* @param {Number[][]} matA - An array of an array of numbers
5+
* @param {Number[][] | Number[] | Number} b - Either an array of an array of numbers, an array of numbers, or a number
6+
* @return {Number[][] | Number[]} - Either an array of an array of numbers, or an array of numbers
7+
* @example matrixMultiplication([[1, 2], [3, 4]], [[1, 2], [3, 4]]) = [[7, 10], [15, 22]]
8+
* @example GreatestCommonFactor([[1, 2], [3, 4]], 2) = [[2, 4], [6, 8]]
9+
* @example GreatestCommonFactor([[1, 2], [3, 4]], [1, 2]) = [5, 11]
10+
*/
11+
12+
function matrixMultiplication(matA: number[][], b: number[][]): number[][];
13+
function matrixMultiplication(matA: number[][], b: number): number[][];
14+
function matrixMultiplication(matA: number[][], b: number[]): number[];
15+
16+
function matrixMultiplication(matA: number[][], b: any): Number[][] | Number[] | null {
17+
let matC: any = null;
18+
19+
if (typeof b === 'number') {
20+
matC = matA.map(row => row.map(colVal => colVal * b));
21+
} else {
22+
if (matA[0].length !== b.length) {
23+
return null;
24+
}
25+
26+
if (typeof b[0] === 'number') {
27+
matC = matA.map(row => row.reduce((sum, colVal, i) => sum + colVal * b[i], 0));
28+
} else {
29+
matC = new Array(matA.length).fill(null).map(() => new Array(b[0].length).fill(0));
30+
let i: number, j: number, k: number;
31+
32+
for (i = 0; i < matA.length; i++) {
33+
for (j = 0; j < b[0].length; j++) {
34+
for (k = 0; k < matA[0].length; k++) {
35+
matC[i][j] += matA[i][k] * b[k][j];
36+
}
37+
}
38+
}
39+
}
40+
}
41+
return matC;
42+
}
43+
44+
export { matrixMultiplication };
+170
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
import { matrixMultiplication } from '../matrix_multiplication';
2+
3+
describe('Matrix-matrix multiplication', () => {
4+
it.each([
5+
[
6+
[
7+
[1, 2],
8+
[3, 4]
9+
],
10+
[
11+
[1, 2],
12+
[3, 4]
13+
],
14+
[
15+
[7, 10],
16+
[15, 22]
17+
]
18+
],
19+
[
20+
[
21+
[1, 2],
22+
[3, 4]
23+
],
24+
[
25+
[4, 3],
26+
[2, 1]
27+
],
28+
[
29+
[8, 5],
30+
[20, 13]
31+
]
32+
],
33+
[
34+
[
35+
[1, 2],
36+
[3, 4]
37+
],
38+
[
39+
[-1, 3],
40+
[2, -4]
41+
],
42+
[
43+
[3, -5],
44+
[5, -7]
45+
]
46+
],
47+
[
48+
[
49+
[1, 2],
50+
[3, 4]
51+
],
52+
[
53+
[1, 2]
54+
],
55+
null
56+
],
57+
[
58+
[
59+
[1, 2],
60+
[3, 4]
61+
],
62+
[
63+
[1, 2],
64+
[3, 4],
65+
[5, 6]
66+
],
67+
null
68+
],
69+
])('Multiplying %j with %j should return %j', (matA, matB, expected) => {
70+
expect(matrixMultiplication(matA, matB)).toEqual(expected);
71+
});
72+
});
73+
74+
describe('Matrix-scalar multiplication', () => {
75+
it.each([
76+
[
77+
[
78+
[1, 2],
79+
[3, 4]
80+
],
81+
0,
82+
[
83+
[0, 0],
84+
[0, 0]
85+
]
86+
],
87+
[
88+
[
89+
[1, 2],
90+
[3, 4]
91+
],
92+
1,
93+
[
94+
[1, 2],
95+
[3, 4]
96+
]
97+
],
98+
[
99+
[
100+
[1, 2],
101+
[3, 4]
102+
],
103+
2,
104+
[
105+
[2, 4],
106+
[6, 8]
107+
]
108+
],
109+
[
110+
[
111+
[1, 2],
112+
[3, 4]
113+
],
114+
-3,
115+
[
116+
[-3, -6],
117+
[-9, -12]
118+
]
119+
],
120+
])('Multiplying %j with %i should return %j', (matA, scalar, expected) => {
121+
expect(matrixMultiplication(matA, scalar)).toEqual(expected);
122+
});
123+
});
124+
125+
describe('Matrix-vector multiplication', () => {
126+
it.each([
127+
[
128+
[
129+
[1, 2],
130+
[3, 4]
131+
],
132+
[1, 2],
133+
[5, 11]
134+
],
135+
[
136+
[
137+
[1, 2],
138+
[3, 4]
139+
],
140+
[3, 4],
141+
[11, 25]
142+
],
143+
[
144+
[
145+
[1, 2],
146+
[3, 4]
147+
],
148+
[-1, 0],
149+
[-1, -3]
150+
],
151+
[
152+
[
153+
[1, 2],
154+
[3, 4]
155+
],
156+
[1],
157+
null
158+
],
159+
[
160+
[
161+
[1, 2],
162+
[3, 4]
163+
],
164+
[1, 2, 3],
165+
null
166+
],
167+
])('Multiplying %j with %j should return %j', (matA, vector, expected) => {
168+
expect(matrixMultiplication(matA, vector)).toEqual(expected);
169+
});
170+
});

0 commit comments

Comments
 (0)