Add matrix module: addition, multiplication, transpose, spiral and rotation programs, dry-run traces, operation counts, seeded law checks, six pitfalls (10 tests)
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b4pD5LRLNWAecBNsj9
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
26ece9cc9b
commit
35ef0872de
@@ -0,0 +1,30 @@
|
||||
Addition and multiplication
|
||||
===========================
|
||||
|
||||
A + B (both 2x3)
|
||||
[1 2 3]
|
||||
[4 5 6]
|
||||
+
|
||||
[6 5 4]
|
||||
[3 2 1]
|
||||
=
|
||||
[7 7 7]
|
||||
[7 7 7]
|
||||
|
||||
X * Y (2x3 times 3x2 gives 2x2)
|
||||
[1 2 3]
|
||||
[4 5 6]
|
||||
*
|
||||
[ 7 8]
|
||||
[ 9 10]
|
||||
[11 12]
|
||||
=
|
||||
[ 58 64]
|
||||
[139 154]
|
||||
|
||||
multiply (i-j-k) and multiplyIkj agree: true
|
||||
X * I (3x3 identity) equals X: true
|
||||
|
||||
add 2x3 + 3x2 -> IllegalArgumentException: cannot add 2x3 and 3x2: both matrices must have the same shape
|
||||
multiply 2x3 * 2x3 -> IllegalArgumentException: cannot multiply 2x3 by 2x3: columns of A (3) must equal rows of B (2)
|
||||
add with a jagged row -> IllegalArgumentException: row 1 has 1 columns but row 0 has 2
|
||||
@@ -0,0 +1,16 @@
|
||||
Dry run: one cell of the product
|
||||
================================
|
||||
|
||||
A (2x3)
|
||||
[1 2 3]
|
||||
[4 5 6]
|
||||
B (3x2)
|
||||
[ 7 8]
|
||||
[ 9 10]
|
||||
[11 12]
|
||||
|
||||
cell C[0][1] = row 0 of A dotted with column 1 of B
|
||||
k=0: A[0][0] * B[0][1] = 1 * 8 = 8 running total = 8
|
||||
k=1: A[0][1] * B[1][1] = 2 * 10 = 20 running total = 28
|
||||
k=2: A[0][2] * B[2][1] = 3 * 12 = 36 running total = 64
|
||||
C[0][1] = 64
|
||||
@@ -0,0 +1,24 @@
|
||||
Transpose
|
||||
=========
|
||||
|
||||
2x3 matrix
|
||||
[1 2 3]
|
||||
[4 5 6]
|
||||
transpose() gives a 3x2 matrix
|
||||
[1 4]
|
||||
[2 5]
|
||||
[3 6]
|
||||
|
||||
3x3 matrix, transposeInPlace()
|
||||
[1 2 3]
|
||||
[4 5 6]
|
||||
[7 8 9]
|
||||
becomes
|
||||
[1 4 7]
|
||||
[2 5 8]
|
||||
[3 6 9]
|
||||
|
||||
isSymmetric([[1,7,3],[7,4,-5],[3,-5,6]]) = true
|
||||
isSymmetric([[1,2],[3,4]]) = false
|
||||
|
||||
transposeInPlace on a 2x3 matrix -> IllegalArgumentException: in-place transpose needs a square matrix but got 2x3
|
||||
@@ -0,0 +1,19 @@
|
||||
Dry run: spiral traversal of a 3x4 matrix
|
||||
=========================================
|
||||
|
||||
[ 1 2 3 4]
|
||||
[ 5 6 7 8]
|
||||
[ 9 10 11 12]
|
||||
|
||||
round 1: top=0 bottom=2 left=0 right=3
|
||||
top row, left to right -> [1, 2, 3, 4]
|
||||
right column, top to bottom -> [8, 12]
|
||||
bottom row, right to left -> [11, 10, 9]
|
||||
left column, bottom to top -> [5]
|
||||
round 2: top=1 bottom=1 left=1 right=2
|
||||
top row, left to right -> [6, 7]
|
||||
right column, top to bottom -> []
|
||||
bottom row skipped (top > bottom)
|
||||
left column, bottom to top -> []
|
||||
|
||||
spiralOrder = [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7]
|
||||
@@ -0,0 +1,19 @@
|
||||
Spiral traversal results
|
||||
========================
|
||||
|
||||
3x4 matrix
|
||||
[ 1 2 3 4]
|
||||
[ 5 6 7 8]
|
||||
[ 9 10 11 12]
|
||||
spiralOrder = [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7]
|
||||
|
||||
single row [[1,2,3]] -> [1, 2, 3]
|
||||
single column [[1],[2],[3]] -> [1, 2, 3]
|
||||
single cell [[5]] -> [5]
|
||||
|
||||
spiralMatrix(4), the reverse problem
|
||||
[ 1 2 3 4]
|
||||
[12 13 14 5]
|
||||
[11 16 15 6]
|
||||
[10 9 8 7]
|
||||
spiralOrder(spiralMatrix(4)) = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
|
||||
@@ -0,0 +1,42 @@
|
||||
Rotation
|
||||
========
|
||||
|
||||
original 3x3
|
||||
[1 2 3]
|
||||
[4 5 6]
|
||||
[7 8 9]
|
||||
|
||||
rotateClockwise
|
||||
[7 4 1]
|
||||
[8 5 2]
|
||||
[9 6 3]
|
||||
|
||||
rotateCounterClockwise
|
||||
[3 6 9]
|
||||
[2 5 8]
|
||||
[1 4 7]
|
||||
|
||||
rotate180
|
||||
[9 8 7]
|
||||
[6 5 4]
|
||||
[3 2 1]
|
||||
|
||||
rotateClockwiseInPlace (transpose, then reverse each row)
|
||||
[7 4 1]
|
||||
[8 5 2]
|
||||
[9 6 3]
|
||||
|
||||
rotateClockwiseLayers (four-cell moves)
|
||||
[7 4 1]
|
||||
[8 5 2]
|
||||
[9 6 3]
|
||||
|
||||
both in-place versions equal rotateClockwise: true
|
||||
|
||||
rectangular 2x3 rotated clockwise becomes 3x2 (in-place is impossible: the shape changes)
|
||||
[1 2 3]
|
||||
[4 5 6]
|
||||
->
|
||||
[4 1]
|
||||
[5 2]
|
||||
[6 3]
|
||||
@@ -0,0 +1,15 @@
|
||||
Dry run: layer-by-layer rotation of a 3x3 matrix
|
||||
================================================
|
||||
|
||||
[1 2 3]
|
||||
[4 5 6]
|
||||
[7 8 9]
|
||||
|
||||
layer 0, i=0: left 7 -> top, bottom 9 -> left, right 3 -> bottom, top 1 -> right
|
||||
[7 2 1]
|
||||
[4 5 6]
|
||||
[9 8 3]
|
||||
layer 0, i=1: left 4 -> top, bottom 8 -> left, right 6 -> bottom, top 2 -> right
|
||||
[7 4 1]
|
||||
[8 5 2]
|
||||
[9 6 3]
|
||||
@@ -0,0 +1,12 @@
|
||||
Seeded random checks (seed 2026, 2000 trials each)
|
||||
==================================================
|
||||
|
||||
rotateClockwise == transpose-then-reverse == layer swaps (n x n, n = 1..8): 2000 agree, 0 disagree
|
||||
four clockwise turns return the original: 2000 agree, 0 disagree
|
||||
clockwise then counter-clockwise returns the original: 2000 agree, 0 disagree
|
||||
transpose twice returns the original (random m x n): 2000 agree, 0 disagree
|
||||
(A * B) transposed == B transposed * A transposed: 2000 agree, 0 disagree
|
||||
A * I == A and I * A == A: 2000 agree, 0 disagree
|
||||
i-j-k product == i-k-j product: 2000 agree, 0 disagree
|
||||
A * (B + C) == A * B + A * C: 2000 agree, 0 disagree
|
||||
spiral order is a permutation of all m * n elements: 2000 agree, 0 disagree
|
||||
@@ -0,0 +1,27 @@
|
||||
Operation counts (counted, not timed)
|
||||
=====================================
|
||||
|
||||
multiplications for an n x n product (i-j-k loop)
|
||||
n = 10 1,000 multiplications
|
||||
n = 100 1,000,000 multiplications
|
||||
n = 200 8,000,000 multiplications
|
||||
doubling n from 100 to 200 multiplies the work by 8
|
||||
|
||||
2x3 times 3x2: 12 multiplications (m * n * p = 2 * 3 * 2)
|
||||
|
||||
in-place transpose swaps, n x n
|
||||
n = 3 3 swaps (n * (n - 1) / 2 = 3)
|
||||
n = 100 4,950 swaps (n * (n - 1) / 2 = 4,950)
|
||||
|
||||
layer rotation four-cell moves, n x n
|
||||
n = 3 2 moves (n * n / 4 rounded down = 2)
|
||||
n = 4 4 moves (n * n / 4 rounded down = 4)
|
||||
n = 100 2,500 moves (n * n / 4 rounded down = 2,500)
|
||||
|
||||
spiral: cells visited vs rows * cols
|
||||
1 x 1 visits = 1 rows * cols = 1
|
||||
1 x 7 visits = 7 rows * cols = 7
|
||||
7 x 1 visits = 7 rows * cols = 7
|
||||
3 x 4 visits = 12 rows * cols = 12
|
||||
5 x 5 visits = 25 rows * cols = 25
|
||||
40 x 25 visits = 1,000 rows * cols = 1,000
|
||||
@@ -0,0 +1,44 @@
|
||||
Six classic mistakes, each reproduced
|
||||
=====================================
|
||||
|
||||
1. in-place transpose with the inner loop starting at j = 0 instead of j = i + 1
|
||||
input
|
||||
[1 2 3]
|
||||
[4 5 6]
|
||||
[7 8 9]
|
||||
result (identical to the input, every pair was swapped twice)
|
||||
[1 2 3]
|
||||
[4 5 6]
|
||||
[7 8 9]
|
||||
|
||||
2. product result allocated as rows(A) x rows(A) instead of rows(A) x cols(B)
|
||||
2x3 times 3x4, correct result shape 2x4
|
||||
wrong version -> ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2
|
||||
|
||||
3. spiral without the 'is there still a row / column left?' checks
|
||||
input [[1, 2, 3]] (a single row)
|
||||
wrong = [1, 2, 3, 2, 1]
|
||||
correct = [1, 2, 3]
|
||||
|
||||
4. layer rotation with i <= last instead of i < last (the corner is moved twice)
|
||||
input
|
||||
[1 2 3]
|
||||
[4 5 6]
|
||||
[7 8 9]
|
||||
wrong
|
||||
[9 4 7]
|
||||
[8 5 2]
|
||||
[3 6 1]
|
||||
correct
|
||||
[7 4 1]
|
||||
[8 5 2]
|
||||
[9 6 3]
|
||||
|
||||
5. 'copy' made with clone() is shallow
|
||||
original = [[1, 2], [3, 4]]; copy = original.clone(); copy[0][0] = 99
|
||||
original[0][0] is now 99 (the rows are shared)
|
||||
copyOf() makes new rows: changing deep[1][1] left original[1][1] at 4
|
||||
|
||||
6. int overflow in the dot product
|
||||
[[50000]] * [[50000]] with plain multiply = -1794967296 (true value 2500000000)
|
||||
multiplyExact -> ArithmeticException: integer overflow
|
||||
Reference in New Issue
Block a user