nngn
Loading...
Searching...
No Matches
mat.h
Go to the documentation of this file.
1#ifndef NNGN_MATH_MAT_H
2#define NNGN_MATH_MAT_H
3
4#include <array>
5
6#include "vec.h"
7
8namespace nngn {
9
10template<template<typename> typename CRTP, typename T, std::size_t N>
11struct mat {
12 using type = T;
14 static constexpr std::size_t n_dim = N;
15 constexpr vec_type &operator[](std::size_t i);
16 constexpr const vec_type &operator[](std::size_t i) const;
17 T *data(void) { return this->m.data(); }
18 const T *data(void) const { return this->m.data(); }
19 constexpr vec_type row(std::size_t i) const;
20 constexpr vec_type col(std::size_t i) const;
21 std::array<T, N * N> m = {};
22};
23
24template<template<typename> typename CRTP, typename T, std::size_t N>
25constexpr auto mat<CRTP, T, N>::operator[](std::size_t i)
26 -> vec_type&
27{
28 return *static_cast<vec_type*>(
29 static_cast<void*>(this->data() + i * mat::n_dim));
30}
31
32template<template<typename> typename CRTP, typename T, std::size_t N>
33constexpr auto mat<CRTP, T, N>::operator[](std::size_t i) const
34 -> const vec_type&
35{
36 return *static_cast<const vec_type*>(
37 static_cast<const void*>(this->data() + i * mat::n_dim));
38}
39
40template<template<typename> typename CRTP, typename T, std::size_t N>
41constexpr auto mat<CRTP, T, N>::row(std::size_t i) const -> vec_type {
42 vec_type ret = {};
43 for(std::size_t c = 0; c != N; ++c)
44 ret[c] = this->m[N * c + i];
45 return ret;
46}
47
48template<template<typename> typename CRTP, typename T, std::size_t N>
49constexpr auto mat<CRTP, T, N>::col(std::size_t i) const -> vec_type {
50 vec_type ret = {};
51 for(std::size_t r = 0; r != N; ++r)
52 ret[r] = this->m[N * i + r];
53 return ret;
54}
55
56template<template<typename> typename CRTP, typename T, std::size_t N>
57constexpr bool operator==(
58 const mat<CRTP, T, N> &m0, const mat<CRTP, T, N> &m1)
59{
60 return static_cast<const CRTP<T>&>(m0).m
61 == static_cast<const CRTP<T>&>(m1).m;
62}
63
64template<template<typename> typename CRTP, typename T, std::size_t N>
65inline constexpr CRTP<T> operator*(T s, const mat<CRTP, T, N> &m) {
66 CRTP<T> ret = {};
67 for(std::size_t i = 0; i != N; ++i)
68 ret[i] = s * m[i];
69 return ret;
70}
71
72template<template<typename> typename CRTP, typename T, std::size_t N>
73inline constexpr CRTP<T> operator*(
74 const mat<CRTP, T, N> &m0, const mat<CRTP, T, N> &m1)
75{
76 CRTP<T> ret = {};
77 for(std::size_t c = 0; c != N; ++c)
78 for(std::size_t r = 0; r != N; ++r)
79 for(std::size_t i = 0; i != N; ++i)
80 ret[c][r] += m0[i][r] * m1[c][i];
81 return ret;
82}
83
84template<template<typename> typename CRTP, typename T, std::size_t N>
85inline constexpr vec_type_t<T, N> operator*(
86 const mat<CRTP, T, N> &m, const vec_type_t<T, N> &v)
87{
88 vec_type_t<T, N> ret = {};
89 for(std::size_t i = 0; i != N; ++i) {
90 const auto x = m.row(i) * v;
91 ret[i] = std::reduce(begin(x), end(x));
92 }
93 return ret;
94}
95
96}
97
98#endif
end
Definition: entities.lua:9
constexpr int N
Definition: lua.cpp:9
user_type T
Definition: register_test.cpp:22
Definition: debug.h:45
constexpr const R * begin(const T< R > &v)
Definition: vec.h:207
typename vec_type< T, N >::type vec_type_t
Definition: vec.h:19
constexpr bool operator==(const mat< CRTP, T, N > &m0, const mat< CRTP, T, N > &m1)
Definition: mat.h:57
constexpr CRTP< T > operator*(T s, const mat< CRTP, T, N > &m)
Definition: mat.h:65
m
Definition: input.lua:19
v[1]
Definition: math.lua:19
Definition: mat.h:11
vec_type_t< T, N > vec_type
Definition: mat.h:13
T * data(void)
Definition: mat.h:17
constexpr vec_type & operator[](std::size_t i)
Definition: mat.h:25
std::array< T, N *N > m
Definition: mat.h:21
constexpr const vec_type & operator[](std::size_t i) const
Definition: mat.h:33
constexpr vec_type row(std::size_t i) const
Definition: mat.h:41
const T * data(void) const
Definition: mat.h:18
T type
Definition: mat.h:12
static constexpr std::size_t n_dim
Definition: mat.h:14
constexpr vec_type col(std::size_t i) const
Definition: mat.h:49
std::chrono::seconds s
Definition: timing.cpp:6