Docker-in-Docker (DinD) capabilities of public runners deactivated. More info

tests.cpp 3.32 KB
Newer Older
Lucas Serrano's avatar
Lucas Serrano committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#include<iostream>
#include"fast_matrix.hpp"

template <typename T>
void print_matrix(T* matrix, int n, int m) {
    for (int i=0; i<n; i++) {
        for (int j=0; j<m; j++) {
            std::cout << matrix[i*m+j] << (j == m-1 ? "" : ", ");
        }
        std::cout << std::endl;
    }
}

void test_matrix_add() {
    alignas(32) float input_a[3*3] = {
        1, 2, 0,
        2, 5, 1,
        1, 1, 0.1,
    };

    alignas(32) float input_b[3*3] = {
        2, 2, 4,
        1, 0, 0.5,
        1, 1, 2,
    };

    alignas(32) float truth[3*3] = {
        3, 4, 4,
        3, 5, 1.5,
        2, 2, 2.1,
    };

33
    BaseMatrix<float, 3, 3, 8> A(input_a), B(input_b), C;
Lucas Serrano's avatar
Lucas Serrano committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    matrix_add(A, B, C);
    float* output = C.dump_array();


    std::cout << "Testing: Matrix Add" << std::endl;
    std::cout << "\tOutput: " << std::endl;
    print_matrix<float>(output, 3, 3);
    std::cout << std::endl;
    std::cout << "\tTruth: " << std::endl;
    print_matrix<float>(truth, 3, 3);
    std::cout << std::endl;

}

void test_matrix_sub() {
    alignas(32) float input_a[3*3] = {
        1, 2, 0,
        2, 5, 1,
        1, 1, 0.1,
    };

    alignas(32) float input_b[3*3] = {
        2, 2, 4,
        1, 0, 0.5,
        1, 1, 2,
    };

    alignas(32) float truth[3*3] = {
        -1, 0, -4,
        1, 5, 0.5,
        0, 0, -1.9,
    };

67
    BaseMatrix<float, 3, 3, 8> A(input_a), B(input_b), C;
Lucas Serrano's avatar
Lucas Serrano committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    matrix_sub(A, B, C);
    float* output = C.dump_array();


    std::cout << "Testing: Matrix sub" << std::endl;
    std::cout << "\tOutput: " << std::endl;
    print_matrix<float>(output, 3, 3);
    std::cout << std::endl;
    std::cout << "\tTruth: " << std::endl;
    print_matrix<float>(truth, 3, 3);
    std::cout << std::endl;

}

void test_matrix_mul_m_m() {
    alignas(32) float input_a[3*3] = {
        1, 2, 0,
        2, 5, 1,
        1, 1, 0.1,
    };

    alignas(32) float input_b[3*3] = {
        2, 2, 4,
        1, 0, 0.5,
        1, 1, 2,
    };

    alignas(32) float truth[3*3] = {
        4, 2, 5,
        10, 5, 12.5,
        3.1, 2.1, 4.7,
    };

101
    BaseMatrix<float, 3, 3, 8> A(input_a), B(input_b), C;
Lucas Serrano's avatar
Lucas Serrano committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    matrix_mul_m_m(A, B, C);
    float* output = C.dump_array();


    std::cout << "Testing: Matrix Mul M M" << std::endl;
    std::cout << "\tOutput: " << std::endl;
    print_matrix<float>(output, 3, 3);
    std::cout << std::endl;
    std::cout << "\tTruth: " << std::endl;
    print_matrix<float>(truth, 3, 3);
    std::cout << std::endl;

}

void test_identity() {
Lucas Serrano's avatar
Lucas Serrano committed
117
    //IdentityMatrix<float, 5, 8> A;
118
    BaseMatrix<float, 5, 5, 8> A(1);
Lucas Serrano's avatar
Lucas Serrano committed
119 120 121 122
    std::cout << "Testing identity matrix" << std::endl;
    std::cout << A;
}

Lucas Serrano's avatar
Lucas Serrano committed
123 124 125 126 127 128 129 130 131 132 133 134
void test_matrix_inv() {
    alignas(32) float input[3*3] = {
        1, 0, 0,
        0, 2, 0,
        0, 0, 4,
    };
    alignas(32) float truth[3*3] = {
        1, 0, 0,
        0, 0.5, 0,
        0, 0, 0.25,
    };

135 136
    BaseMatrix<float, 3, 3, 4> A(input), I;
    Inverse<float, 3, 3, 4>::inverse(A, I);
Lucas Serrano's avatar
Lucas Serrano committed
137 138 139 140 141 142 143 144
    std::cout << "Testing Matrix inversion" << std::endl;
    std::cout << "\tOutput: " << std::endl;
    print_matrix<float>(I.dump_array(), 3, 3);
    std::cout << "\tTruth: " << std::endl;
    print_matrix<float>(truth, 3, 3);
    std::cout << std::endl;
}

Lucas Serrano's avatar
Lucas Serrano committed
145 146 147 148 149
int main(int argc, char **argv) {
    test_matrix_add();
    test_matrix_sub();
    test_matrix_mul_m_m();
    test_identity();
Lucas Serrano's avatar
Lucas Serrano committed
150
    test_matrix_inv();
Lucas Serrano's avatar
Lucas Serrano committed
151 152
    return 0;
}