This is the 7th day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021

Problem description

Given two n by n matrices A and B, find A by B.

Example:

Input: [[1,2], [3,2]], [[3,4], [2,1]]

Output: [[7,6], [13,14]]

To analyze problems

We can use the matrix multiplication rule to solve the problem. For n * n matrix A and matrix B multiply, the elements of the ith row and j column of matrix C can be expressed as Ci,j=Ai,1* B1,j + Ai,2 * B2,j +… Plus Ai,n times Bn,j, is equal to the product of the ith row of A and the JTH column of B.

class Solution: def solve(self , a, b): N =len(a) res=[[0] *n for _ in range(n)] for I in range(0,n): for j in range(0,n): For k in range(0,n): for k in range(0,n): for k in range(0,n): for k in range(0,n): for k in range(0,n): for k in range(0,n): for k in range(0,n): for k in range(0,n)Copy the code

The time complexity of this algorithm is O(N^3) and the space complexity is O(N^2).

We all know that two-dimensional arrays are actually stored sequentially in computer memory, as follows:

When an operating system loads data into the cache, it loads a batch of data in the vicinity of the hit data into the cache, because the operating system assumes that if a memory location is referenced, the program is likely to reference a nearby memory location in the near future. So we optimize by adjusting the order in which the arrays are read, so that the matrices A and B are read sequentially and then fed into the CPU for computation, making the running time faster. Here’s how to do it:

class Solution: def solve(self , a, b): N =len(a) res=[[0] *n for _ in range(n)] for I in range(0,n): for j in range(0,n): Temp = A [I][j] for k in range(0,n): res[I][k] += temp * b[j][k] return resCopy the code

The time complexity of this algorithm is O(N^3). At that time, this algorithm takes advantage of cache optimization to read the elements in array A and array B sequentially, so it is generally faster than the first method. The spatial complexity of this algorithm is O(N^2).