Skip to content

Repository files navigation

Matrix Formulas Implementation

Algorithmic Foundation Work At My Early Months Of Programming That I Used To Design Every Algorithm Myself Refusing To Do Imports In Order To Improve My Critical Thinking

Overview

A high-performance matrix computation module implementing essential linear algebra operations from the ground up. This project demonstrates advanced algorithmic thinking through the implementation of complex matrix operations using nested-loop patterns and recursive algorithms, while maintaining a minimalist dependency footprint.

Note: This project is engineered specifically for mathematical rigor and algorithmic complexity rather than production-scale performance. It serves as a educational reference for matrix mathematics implementation and a portfolio demonstration of sophisticated algorithm design.


Features

Core Matrix Operations

Input Validation & Type Checking

  • integer_identifier__zero_integer_identifier() - Custom integer validation without regex

    • Validates user input against a whitelist of numeric characters
    • Prevents invalid matrix dimensions (zero, null, non-numeric values)
    • Iterative user re-prompting for robust error handling
  • float_identifier() - Intelligent float detection without regex

    • Identifies valid float and integer formats (e.g., 1.2, .5, 5., 5)
    • Guards against malformed numeric input at the validation layer
    • Combined Role: Together, these validators form a dual-layer type system ensuring only valid numeric data enters the matrix structure

Matrix Analysis Operations

  • sum_of_each_row() - Row-wise aggregation using list comprehension
  • sum_of_each_column() - Column-wise aggregation with nested iteration
  • original_diameter_of_matrix() - Primary diagonal extraction
  • secondary_diameter_of_matrix() - Anti-diagonal extraction
  • transposed() - Matrix transposition via index-swapping (supporting operation for multiplication)

Advanced Matrix Computations

  • multiply(matrix1, matrix2) — Matrix multiplication algorithm

    • Dependencies: Calls transposed() internally to restructure matrix2 for efficient row-column dot products
    • Validates dimension compatibility (columns of matrix1 must equal rows of matrix2)
    • Uses nested loops with index mapping for element-wise multiplication and accumulation
  • determinant(matrix) — Recursive determinant calculation

    • Base Cases:
      • 1×1 matrices: Returns message (undefined determinant)
      • 2×2 matrices: Applies direct formula
    • Recursive Case (n > 2):
      • Iterates across the first row
      • Calls sub_matrix_of_determinant() to generate cofactor matrices
      • Recursively computes determinants of cofactor matrices
      • Applies alternating sign pattern (based on index parity)
    • Deep Copy Protection: Uses deepcopy() to preserve original matrix during cofactor extraction
    • Complexity: O(n!) due to recursive expansion along cofactors
  • sub_matrix_of_determinant(matrix, row_index, column_index) — Helper function

    • Removes specified row and column to generate minor matrices
    • Critical supporting function for both determinant and inverse calculations
    • Performs in-place modifications on copied data structures
  • inverse_matrix(matrix) — Matrix inversion using adjugate method

    • Dependencies: Leverages both determinant() (recursive) and sub_matrix_of_determinant() helper function
    • Operation Steps:
      1. Validates invertibility (determinant ≠ 0)
      2. For 2×2 matrices: Applies direct formula
      3. For n×n matrices (n ≥ 3):
        • Constructs cofactor matrix using nested loops
        • Applies checkerboard sign pattern (alternating ±1 based on position parity)
        • Transposes the signed cofactor matrix (adjugate)
        • Scales all elements by 1/determinant
    • Recursive Coordination: Each cofactor calculation triggers recursive determinant computation
    • Complexity: O(n! × n²) due to nested determinant calculations within element iteration

Utility Operations

  • sum_of_two_matrix() — Element-wise matrix addition with dimension validation

Technical Design Choices

Minimal Dependencies Philosophy

  • Single Import: Only copy.deepcopy() used—chosen as essential for safe nested structure manipulation
  • Custom Validation: Integer and float checking implemented algorithmically rather than via regex
  • No External Libraries: All matrix operations built using native Python control structures

Algorithm Complexity

Despite the modest line count relative to other projects, this module implements genuinely complex algorithms:

  • Nested iteration patterns for multi-dimensional array traversal
  • Recursive expansion patterns with significant computational depth
  • Strategic helper function relationships enabling advanced linear algebra

Memory Management

  • Deep copying mechanisms protect original matrices during recursive operations
  • Temporary variables cleared explicitly to optimize memory usage
  • List comprehensions used for efficient data structure construction

Usage

The module prompts for interactive matrix input and performs all operations automatically:

# Run the module
python Matrix.py

# Input Format
# Enter matrix dimensions and values when prompted
# The system validates numeric input and normalizes matrices to square form

Output Operations:

  • Row and column sum vectors
  • Primary and secondary diagonal vectors
  • Transposed matrix
  • Matrix multiplication result(input matrix × input matrix)
  • Determinant value
  • Inverse matrix (if determinant ≠ 0)

Mathematical Foundations

Matrix Operations Reference

  • Transposition: Swaps row and column indices (A^T)
  • Multiplication: Dot product of rows and transposed columns
  • Determinant: Recursive cofactor expansion (Laplace expansion)
  • Inverse: A^(-1) = (1/det(A)) × adj(A), where adj(A) is the transpose of the cofactor matrix

Performance Characteristics

  • Determinant Complexity: O(n!) for n×n matrices via recursive cofactor expansion
  • Inverse Complexity: O(n! × n²) due to iterative cofactor computation
  • Multiplication Complexity: O(n³) for n×n matrices
  • Intended for: Educational reference, algorithmic study, and small-scale matrix computations

Project Structure

  • Single Module: Matrix.py — All operations self-contained
  • Interactive Pipeline: Input validation → Matrix normalization → Operation execution → Result display

Highlights for Technical Review

Algorithmic Sophistication: Complex recursive patterns with nested loop optimization
Clean Dependencies: Intentional minimalism with strategic single import
Robust Input Handling: Custom validation layer avoiding common pitfalls
Code Documentation: Comprehensive inline comments explaining formula implementations
Helper Function Design: Strategic decomposition with clear dependency relationships


Author

Created as a demonstration of advanced algorithmic implementation and matrix mathematics computation.

About

Implements core matrix operations using Python, demonstrating proficiency in nested-loop algorithms and recursive computation. An early deep dive into algorithmic problem-solving before transitioning to enterprise-scale OOP architecture.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages