Home > Python > Matrix Operation

Ch. 4.2 Matrix Addition

Chapters Index
Program 1

Matrix Addition

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.
Theoretical Principles & Computational Physics Guide

Matrix Addition & Subtraction: Element-Wise Algebraic Operations

Dr. Alorika Chatterjee & Dr. Anirban Shaw Science & Physics Learners Python 3 / NumPy / SciPy

1. Core Principles & Theoretical Framework

Superposition Principle: Matrix addition embodies linear superposition of physical fields, potentials, and state vectors.
Conformability Condition: Both matrices must possess identical row and column dimensions ($m_A = m_B$ and $n_A = n_B$).
Linear Time: Addition scales linearly with total elements: $\mathcal{O}(m \cdot n)$.

2. Mathematical Formulation & Algorithmic Pipeline

Element-wise matrix sum definition:

$$ \mathbf{C} = \mathbf{A} \pm \mathbf{B} \iff C_{ij} = A_{ij} \pm B_{ij} \quad \forall i \in \{1,\dots,m\}, \; j \in \{1,\dots,n\} $$

3. Numerical Mechanics & Implementation Logic

  • Dimension Check: Assert len(A) == len(B) and len(A[0]) == len(B[0]) before looping.
  • Nested Iteration: Loop through row index $i$ and column index $j$, assigning $C_{ij} = A_{ij} + B_{ij}$.
  • In-Place Updates: When memory is constrained, update directly in-place: $A_{ij} \leftarrow A_{ij} + B_{ij}$.

4. Relevant Computational Practice Exercises

Exercise 1
Implement matrix addition using nested loops and verify commutativity $\mathbf{A} + \mathbf{B} = \mathbf{B} + \mathbf{A}$.
Exercise 2
Compute the difference matrix $\mathbf{D} = \mathbf{A} - \mathbf{B}$ between two experimental spatial sensor meshes.
Exercise 3
Write a defensive matrix addition function that throws an informative error if dimensions mismatch.
Standard Literature & References:
  • Gilbert Strang, "Linear Algebra and Its Applications", Cengage (2006).