Linear Algebra Foundations #7 - The 1000th Power of a Matrix

  • + 0 comments

    It may be solved programmatically:

    #include <array>
    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    static const array<long, 4> kIdentityMatrix = array<long, 4>{1, 0, 0, 1};
    
    void printMatrix(const array<long, 4>& matrix) {
        cout << matrix[0] << endl << matrix[1] << endl;
        cout << matrix[2] << endl << matrix[3] << endl;
    }
    
    array<long, 4> multiply(const array<long, 4>& matrix1, const array<long, 4>& matrix2) {
        return array<long, 4>{
            matrix1[0] * matrix2[0] + matrix1[1] * matrix2[2],
            matrix1[0] * matrix2[1] + matrix1[1] * matrix2[3],
            matrix1[2] * matrix2[0] + matrix1[3] * matrix2[2],
            matrix1[2] * matrix2[1] + matrix1[3] * matrix2[3]
        };
    }
    
    array<long, 4> power(const array<long, 4>& matrix, int powerValue) {
        array<long, 4> result = kIdentityMatrix;
        for (long i = 0; i < powerValue; ++i) {
            result = std::move(multiply(result, matrix));
        }
        return result;
    }
    

    int main() { array sourceMatrix {-2, -9, 1, 4}; printMatrix(power(sourceMatrix, 1000)); return 0; }