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
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.
-
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
- Identifies valid float and integer formats (e.g.,
sum_of_each_row()- Row-wise aggregation using list comprehensionsum_of_each_column()- Column-wise aggregation with nested iterationoriginal_diameter_of_matrix()- Primary diagonal extractionsecondary_diameter_of_matrix()- Anti-diagonal extractiontransposed()- Matrix transposition via index-swapping (supporting operation for multiplication)
-
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
- Dependencies: Calls
-
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
- Base Cases:
-
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) andsub_matrix_of_determinant()helper function - Operation Steps:
- Validates invertibility (determinant ≠ 0)
- For 2×2 matrices: Applies direct formula
- 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
- Dependencies: Leverages both
sum_of_two_matrix()— Element-wise matrix addition with dimension validation
- 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
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
- 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
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 formOutput 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)
- 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
- 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
- Single Module:
Matrix.py— All operations self-contained - Interactive Pipeline: Input validation → Matrix normalization → Operation execution → Result display
✓ 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
Created as a demonstration of advanced algorithmic implementation and matrix mathematics computation.