TypeScript: A matrix class

Write a small immutable matrix class with addition, multiplication and transposition.

What to build

A program that reads two matrices and prints their sum, their product and the first one's transpose.

Requirements

  • Write class Matrix with a static fromLines, the methods add, multiply and transpose, each returning a new Matrix, and toString() giving the rows, numbers separated by spaces.
  • Print A + B: and the sum, or A + B: sizes differ; then A x B: and the product, or A x B: cannot multiply; then transpose A: and the transpose.

Input

Line 1: r1 c1. Then r1 rows of A. Next line: r2 c2. Then r2 rows of B.

Output

The three results, as described.

Constraints

1 ≤ r, c ≤ 6; entries are whole numbers with |x| ≤ 100.

Example

Input:

2 2
1 2
3 4
2 2
5 6
7 8

Output:

A + B:
6 8
10 12
A x B:
19 22
43 50
transpose A:
1 3
2 4

Why it works

Methods that return new matrices never change their inputs, so A can be used in all three results safely, and the size checks live in the class, next to the data they protect.

Examples

Example 1 · Input
2 2
1 2
3 4
2 2
5 6
7 8
Expected output
A + B:
6 8
10 12
A x B:
19 22
43 50
transpose A:
1 3
2 4
Example 2 · Input
2 3
1 2 3
4 5 6
3 1
1
0
-1
Expected output
A + B: sizes differ
A x B:
-2
-2
transpose A:
1 4
2 5
3 6

Hints

Hint 1

Store the rows as a private readonly number[][]; methods return new matrices instead of changing this one.

Hint 2

A static method fromLines can build a Matrix from the input lines.

Solution

Show one correct solution (try it yourself first)
const read = (): string => prompt() ?? "";

class Matrix {
  private constructor(private readonly rows: number[][]) {}

  static fromLines(lines: string[]): Matrix {
    return new Matrix(lines.map((line) => line.split(" ").map(Number)));
  }

  get height(): number {
    return this.rows.length;
  }

  get width(): number {
    return this.rows[0].length;
  }

  add(other: Matrix): Matrix | null {
    if (this.height !== other.height || this.width !== other.width) return null;
    return new Matrix(this.rows.map((row, i) => row.map((x, j) => x + other.rows[i][j])));
  }

  multiply(other: Matrix): Matrix | null {
    if (this.width !== other.height) return null;
    const result: number[][] = [];
    for (let i = 0; i < this.height; i++) {
      const row: number[] = [];
      for (let j = 0; j < other.width; j++) {
        let sum = 0;
        for (let k = 0; k < this.width; k++) sum += this.rows[i][k] * other.rows[k][j];
        row.push(sum);
      }
      result.push(row);
    }
    return new Matrix(result);
  }

  transpose(): Matrix {
    return new Matrix(this.rows[0].map((_, j) => this.rows.map((row) => row[j])));
  }

  toString(): string {
    return this.rows.map((r) => r.join(" ")).join("\n");
  }
}

function readMatrix(): Matrix {
  const [r] = read().split(" ").map(Number);
  const lines: string[] = [];
  for (let i = 0; i < r; i++) lines.push(read());
  return Matrix.fromLines(lines);
}

const a = readMatrix();
const b = readMatrix();
const sum = a.add(b);
if (sum) console.log("A + B:\n" + sum.toString());
else console.log("A + B: sizes differ");
const product = a.multiply(b);
if (product) console.log("A x B:\n" + product.toString());
else console.log("A x B: cannot multiply");
console.log("transpose A:\n" + a.transpose().toString());

Lesson: Classes in TypeScript →