Is there an algorithm to multiply square matrices in-place?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Matrix multiplication is a fundamental operation in linear algebra with significant applications in various domains such as computer graphics, physics simulations, and machine learning. The focus of this discussion is on the possibility of multiplying square matrices in-place, without requiring additional space proportional to the size of the matrices.
Matrix multiplication involves computing the product of two matrices, a process that typically creates a new matrix. However, an "in-place" algorithm would update one of the input matrices to include the result, thereby conserving space but potentially at the cost of increased computational complexity and complexity in algorithm design.
In-place Matrix Multiplication
In general, an in-place algorithm recomputes values and writes the result over the older input, thereby using limited additional space. When considering matrix multiplication of two square matrices, and , to produce a matrix , traditional methods are not in-place as they compute a new matrix entirely.
An in-place algorithm for square matrices aims to overwrite one of the input matrices (say or ) with the resulting matrix . This method is challenging due to several factors:
- Overlapping Data: Modifying one matrix in-place could destroy data needed for the computation of other elements.
- Intermediate Computations: The computation of each element typically depends on multiple elements, which makes overwriting tricky without auxiliary storage.
- Algorithm Complexity: Ensuring the correctness and efficiency of an in-place multiplication algorithm is non-trivial.
Example: Naïve Approach
To illustrate, consider the naïve approach to in-place matrix multiplication:
- Input: Two matrices, and , each of size .
- Output: Matrix is overwritten to hold the result .
Step-by-step Example
Here's a rough pseudocode outline of a simple, yet non-optimal in-place approach:
- Memory-constrained environments where the space complexity is a more significant concern than the time complexity.
- Systems with limited I/O bandwidth where in-place operations can reduce the necessity to allocate and manage additional data storage.
Related reading
- Is there an edge we can delete without disconnecting the graph?
- Is there an edit distance algorithm that takes chunk transposition into account?
- Is there an efficient algorithm for segmentation of handwritten text?
- Is there an efficient algorithm to generate a 2D concave hull?
- Is there an efficient algorithm to generate random points in general position in the plane?
- Is there an efficient way to count the number of intersections among a given set of line segments?
- Is there an efficient implementation of tetration?
- Is there an efficient way to cluster a graph according to Jaccard similarity?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.