.. _program_listing_file_src_tap_algorithms_cmsis_mat.hpp: Program Listing for File cmsis_mat.hpp ====================================== |exhale_lsh| :ref:`Return to documentation for file ` (``src/tap/algorithms/cmsis_mat.hpp``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp /* * Copyright (c) 2020-2021 Advanced Robotics at the University of Washington * * This file is part of Taproot. * * Taproot is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Taproot is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Taproot. If not, see . */ #ifndef TAPROOT_CMSIS_MAT_HPP_ #define TAPROOT_CMSIS_MAT_HPP_ #include #include #include #include #include "modm/architecture/utils.hpp" #include "arm_math.h" namespace tap::algorithms { template struct CMSISMat { std::array data; arm_matrix_instance_f32 matrix; CMSISMat() : data(), matrix{ROWS, COLS, data.data()} {} CMSISMat(const float (&initialData)[ROWS * COLS]) { copyData(initialData); arm_mat_init_f32(&matrix, ROWS, COLS, data.data()); } CMSISMat(const CMSISMat &other) : matrix{ROWS, COLS, data.data()} { memcpy(&this->data, &other.data, sizeof(this->data)); } // Move semantics. CMSISMat(CMSISMat &&other) { this->data = std::move(other.data); matrix.numRows = ROWS; matrix.numCols = COLS; matrix.pData = data.data(); } CMSISMat &operator=(const CMSISMat &other) { memcpy(&this->data, &other.data, sizeof(this->data)); return *this; } // Move semantics. CMSISMat &operator=(CMSISMat &&other) { this->data = std::move(other.data); matrix.numRows = ROWS; matrix.numCols = COLS; matrix.pData = data.data(); return *this; } inline void copyData(const float (&other)[ROWS * COLS]) { for (size_t i = 0; i < data.size(); i++) { data[i] = other[i]; } } bool constructIdentityMatrix() { if (ROWS != COLS) { return false; } for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { data[i * COLS + j] = (i == j) ? 1 : 0; } } return true; } inline CMSISMat inverse() const { CMSISMat ret; assert(ARM_MATH_SUCCESS == arm_mat_inverse_f32(&this->matrix, &ret.matrix)); return ret; } inline CMSISMat transpose() const { CMSISMat ret; assert(ARM_MATH_SUCCESS == arm_mat_trans_f32(&this->matrix, &ret.matrix)); return ret; } }; /* Begin definitions */ template inline CMSISMat operator+( const CMSISMat &a, const CMSISMat &b) { static_assert( A_ROWS == B_ROWS && A_COLS == B_COLS, "Invalid size of CMSISMat matricies in operator+"); CMSISMat c; assert(ARM_MATH_SUCCESS == arm_mat_add_f32(&a.matrix, &b.matrix, &c.matrix)); return c; } template inline CMSISMat operator-( const CMSISMat &a, const CMSISMat &b) { static_assert( A_ROWS == B_ROWS && A_COLS == B_COLS, "Invalid size of CMSISMat matricies in operator-"); CMSISMat c; assert(ARM_MATH_SUCCESS == arm_mat_sub_f32(&a.matrix, &b.matrix, &c.matrix)); return c; } template inline CMSISMat operator-(const CMSISMat &a) { float scale(-1); CMSISMat c; assert(ARM_MATH_SUCCESS == arm_mat_scale_f32(&a.matrix, scale, &c.matrix)); return c; } template inline CMSISMat operator*( const CMSISMat &a, const CMSISMat &b) { static_assert(A_COLS == B_ROWS, "Invalid size of CMSISMat matricies in operator*"); CMSISMat c; assert(ARM_MATH_SUCCESS == arm_mat_mult_f32(&a.matrix, &b.matrix, &c.matrix)); return c; } template inline CMSISMat operator*(const CMSISMat &a, float b) { CMSISMat c; assert(ARM_MATH_SUCCESS == arm_mat_scale_f32(&a.matrix, b, &c.matrix)); return c; } template inline CMSISMat operator*(float b, const CMSISMat &a) { CMSISMat c; assert(ARM_MATH_SUCCESS == arm_mat_scale_f32(&a.matrix, b, &c.matrix)); return c; } template inline CMSISMat operator/(const CMSISMat &a, float b) { b = 1 / b; CMSISMat c; assert(ARM_MATH_SUCCESS == arm_mat_scale_f32(&a.matrix, b, &c.matrix)); return c; } } // namespace tap::algorithms #endif // TAPROOT_CMSIS_MAT_HPP_