代码-两个矩阵相乘-CPP

2023-09-08  本文已影响0人  陈成_Adam

mvp.cpp

#include <iostream>

// Function to multiply two matrices
template <typename T, uint32_t M, uint32_t N>
void multiplyMatrices(T A[M][N], T B[N][N], T result[M][N]) {
    for (int i = 0; i < M; ++i) {
        for (int j = 0; j < N; ++j) {
            result[i][j] = 0.0f;
            for (int k = 0; k < N; ++k) {
                result[i][j] += A[i][k] * B[k][j];
            }
        }
    }
}

// Function to display a matrix
template <typename T, uint32_t M, uint32_t N>
void displayMatrix(T matrix[M][N]) {
    for (int i = 0; i < M; ++i) {
        for (int j = 0; j < N; ++j) {
            std::cout << matrix[i][j] << " ";
        }
        std::cout << std::endl;
    }
}

int main() {
    float v0_pos[1][4] = {{ 0.0f,  0.6f, -2.0f, 1.0f}};
    float v1_pos[1][4] = {{-3.0f, -3.0f, -1.0f, 1.0f}};
    float v2_pos[1][4] = {{ 9.0f, -9.0f, -3.0f, 1.0f}};

    float mvp[4][4] = {
            {  1.0,  0.0,  0.0,  0.0},
            {  0.0,  1.0,  0.0,  0.0},
            {  0.0,  0.0, -2.0, -1.0},
            {  0.0,  0.0, -3.0,  0.0}
    };

    float result[1][4];

    multiplyMatrices<float, 1, 4>(v0_pos, mvp, result);

    std::cout << "Result:" << std::endl;
    displayMatrix<float, 1, 4>(result);

    return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.25)
project(mvp CXX)

set(CMAKE_CXX_STANDARD  17)

add_executable(mvp
        mvp.cpp)

build.sh

cmake -B build
cmake --build build
上一篇下一篇

猜你喜欢

热点阅读