2

我有一个矩阵类的开始。这是代码-

template<int h, int w = h>
class mat {
public:
    mat() : values(h, std::vector<double>(w)) {
        if (w == h) {
            int x = 0;
            for (int y = 0; y < h; y++) {
                values[y][x] = 1;
                x++;
            }
        }
    }
    mat(std::initializer_list<std::vector<double>> matvals){
        values = matvals;
    }
    mat(int val) : values(h, std::vector<double>(w, val)) {}
    mat(const mat& m) {
        values = m.values;
    }
    mat(mat&& m) {
        values = std::move(m.values);
    }
    int width() {
        return w;
    }
    int height() {
        return h;
    }
    template<int mh, int mw = mh>
    auto operator*(const mat<mh, mw>& m) -> mat<h, mw> const {
        if (w != mh) throw std::logic_error{ "Matrices cannot be multiplied" };
        mat<h, mw> temp;
        std::vector<double> mcol(mh);
        for (int y = 0; y < mw; y++) {
            for (int mx = 0; mx < mw; mx++) {
                for (int my = 0; my < mh; my++) {
                    mcol[my] = m.values[my][mx];
                }
                temp.values[y % h][mx] = dot(values[y % h], mcol);
            }
        }
        return temp;
    }
    mat operator+(const mat& m) const {
        mat temp;
        for (int y = 0; y < h; y++) {
            for (int x = 0; x < w; x++) {
                temp.values[y][x] = values[y][x] + m.values[y][x];
            }
        }
        return temp;
    }
    std::vector<double>& operator[](int y) {
        return values[y];
    }
    mat& operator=(const mat& m) {
        values = m.values;
        return *this;
    }
    mat& operator=(mat&& m) {
        values = std::move(m.values);
        return *this;
    }
private:
    std::vector<std::vector<double>> values;

    template<int mw, int mh>
    friend class mat;
};

到目前为止,这是该类的使用方式-

mat<2, 4> mat1 = {
    {1, 2, 3, 4},
    {5, 6, 7, 8},
};

mat<4, 3> mat2 = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9},
    {10, 11, 12}
};

auto mat3 = mat1 * mat2;

注意到冗余了吗?如果用户想要使用std::initializer_list构造函数创建矩阵,那么他们必须首先在模板参数中指定宽度和高度。此外,如果他们使用std::initializer_list的尺寸与模板参数中指定的尺寸不同,那么行为将是未定义的。非类型模板参数如何写推演指南?我知道如何使用基本模板来做到这一点,但是我尝试像您一样尝试做的所有事情通常都会产生许多编译器错误。这是期望的行为-

mat mat1 = {
    {1, 2, 3, 4},
    {5, 6, 7, 8},
};

mat mat2 = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9},
    {10, 11, 12}
};

auto mat3 = mat1 * mat2;

编辑:任何想要制作矩阵类的人都不应该制作宽度和高度模板参数。它只是不必要地使事情复杂化。

4

2 回答 2

1

你可以得到

mat mat2 = {
  row{1, 2, 3},
  row{4, 5, 6},
  row{7, 8, 9},
  row{10, 11, 12}
};

去工作; 编写一个一维行,从中推断其大小Ts&&...并类型检查Ts匹配,然后mat是一维行的行(或重新实现相同的机器)。

我不知道是否有一种方法可以避免必须显式键入(嗯,有点)“行”,同时还获得一个 consteval 长度来推断矩阵类型。

一个有趣的游戏是用行乘法来定义矩阵乘法,矩阵 mult b 是 mult2 b 转置,其中 a mult2 b 是矩阵 a_i 内积 b_j。

当然,那个兔子洞还在继续。

于 2021-03-12T05:56:57.437 回答
1

如果您提供这样的构造函数:

template<int w, int h>
struct A{
    A(double (&&c)[w][h]); // `double const (&c)[w][h]` is also OK.
};

没有任何其他扣除指南,您可以使用它如下:

A a{{
    {1, 2, 3},
    {4, 5, 6}
}};
A b = {{
    {1, 2, 3},
    {4, 5, 6}
}};
A c({
    {1, 2, 3},
    {4, 5, 6}
});

但是我们可能会认为外大括号很糟糕,所以我们必须提供一个特殊的构造函数和推导指南:

namespace Impl{
    template<typename T, typename = void>
    struct helper;
    template<int h1, int... hs>
    struct helper<std::integer_sequence<int, h1, hs...>, std::enable_if_t<((h1 == hs) && ...)>>{
        static constexpr int w = sizeof...(hs) + 1;
        static constexpr int h = h1;
    };
    template<int... hs>
    inline constexpr int helper_w = helper<std::integer_sequence<int, hs...>>::w;
    template<int... hs>
    inline constexpr int helper_h = helper<std::integer_sequence<int, hs...>>::h;
}

template<int w, int h>
struct A{
    template<int... hs, typename = std::enable_if_t<w + 1 == Impl::helper_w<h, hs...>>>
    A(double (&&... head)[hs]);
};

template<int... hs>
A(double (&&... head)[hs]) -> A<Impl::helper_w<hs...>, Impl::helper_h<hs...>>;

然后你可以随意使用它:

A a{
    {1, 2, 3},
    {4, 5, 6}
};
A b = {
    {1, 2, 3},
    {4, 5, 6}
};
于 2021-03-12T06:42:50.160 回答