美文网首页
代码-两个矩阵相乘-CPP

代码-两个矩阵相乘-CPP

作者: 陈成_Adam | 来源:发表于2023-09-08 14:05 被阅读0次

    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
    

    相关文章

      网友评论

          本文标题:代码-两个矩阵相乘-CPP

          本文链接:https://www.haomeiwen.com/subject/rmqdvdtx.html