gpt4 book ai didi

c++ - 制作模板元函数以在编译时创建单位矩阵

转载 作者:太空狗 更新时间:2023-10-29 20:35:11 25 4
gpt4 key购买 nike

我在问自己,如果大小已知,是否可以在编译时创建一个单位矩阵。到目前为止,我只是写了一个示例,它创建了一个基于 std::vector 的固定大小的矩阵,例如4x4。但是,我不确定如何设置这些值。我想,我需要递归:/

// Example program
#include <iostream>
#include <string>
#include <vector>

template <class T>
using vec1D = std::vector<T>;

template <class T>
using vec2D = std::vector<std::vector<T>>;

template <class T, int size>
vec2D<T> make_mat() {
vec2D<T> mat(size, vec1D<T>(size));
return mat;
}

int main()
{
vec2D<float> unit = make_mat<float, 4>();
std::cout
<< unit[0][0] << unit[0][1] << unit[0][2] << unit[0][3] << std::endl
<< unit[1][0] << unit[1][1] << unit[1][2] << unit[1][3] << std::endl
<< unit[2][0] << unit[2][1] << unit[2][2] << unit[2][3] << std::endl
<< unit[3][0] << unit[3][1] << unit[3][2] << unit[3][3] << std::endl;
}

最佳答案

I was asking myself if it is possible to create a unit matrix at compile time if size is known.

如果您的矩阵是基于 std::vector 的,我不这么认为。

但如果它基于 std::array 并且您希望将其初始化为零(如您的示例所示),则可以:这是可能的。

// Example program
#include <iostream>
#include <array>

template <typename T, std::size_t D1, std::size_t D2 = D1>
constexpr std::array<std::array<T, D2>, D1> doA ()
{ return { }; }

int main()
{
constexpr auto unit = doA<float, 4U>();

std::cout
<< unit[0][0] << unit[0][1] << unit[0][2] << unit[0][3] << std::endl
<< unit[1][0] << unit[1][1] << unit[1][2] << unit[1][3] << std::endl
<< unit[2][0] << unit[2][1] << unit[2][2] << unit[2][3] << std::endl
<< unit[3][0] << unit[3][1] << unit[3][2] << unit[3][3] << std::endl;
}

但我认为这不是一个好主意:考虑到 std::array 使用栈,而不是堆。所以这个解决方案只对小(非常小)的矩阵有用。

--- 编辑 ---

OP(正确)观察

question was whether on could init the matrix as unit matrix: 100 010 001

是的,单位矩阵也是可能的;有点复杂,但可能。

// Example program
#include <iostream>
#include <utility>
#include <array>

template <typename T, std::size_t I, std::size_t ... Is>
constexpr auto doU_helper2 (std::index_sequence<Is...> const &)
{ return std::array<T, sizeof...(Is)>
{ { (Is == I ? T{1} : T{0})... } }; }

template <typename T, std::size_t ... Is>
constexpr auto doU_helper1 (std::index_sequence<Is...> const & is)
{ return std::array<std::array<T, sizeof...(Is)>, sizeof...(Is)>
{ { doU_helper2<T, Is>(is)... } }; }

template <typename T, std::size_t Dim>
constexpr auto doU ()
{ return doU_helper1<T>(std::make_index_sequence<Dim>{}); }

int main()
{
constexpr auto unit = doU<float, 4U>();

std::cout
<< unit[0][0] << unit[0][1] << unit[0][2] << unit[0][3] << std::endl
<< unit[1][0] << unit[1][1] << unit[1][2] << unit[1][3] << std::endl
<< unit[2][0] << unit[2][1] << unit[2][2] << unit[2][3] << std::endl
<< unit[3][0] << unit[3][1] << unit[3][2] << unit[3][3] << std::endl;
}

考虑到这段代码使用了 std::make_index_sequence{}std::index_sequence,这是 C++14 的特性。

如果您需要一个 C++11 解决方案,创建一个字幕并不太难;询问您是否需要它(我已经完成的某个地方;我只需要检索)。

关于c++ - 制作模板元函数以在编译时创建单位矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44084081/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com