Peano
Loading...
Searching...
No Matches
numvec.h
Go to the documentation of this file.
1#pragma once
2
3#include <cmath>
4#include <cstdarg>
5#include <cstdlib>
6#include <initializer_list>
7#include <iostream>
8
9#include <lang/assert.h>
10#include <lang/type.h>
11
12
13template<typename T, u32 LENGTH>
14class NumVec {
15 T vec[LENGTH];
16
17public:
18 explicit NumVec() = default;
19
20 explicit NumVec(T val) {
21 for (u32 i = 0; i < LENGTH; i++) this->vec[i] = val;
22 }
23
24 NumVec(std::initializer_list<T> list) {
25 assert(list.size() == LENGTH)
26 for (u32 i = 0; i < LENGTH; i++) {
27 this->vec[i] = list.begin()[i];
28 }
29 }
30
31 explicit NumVec(const T(&list)[LENGTH]) {
32 for (u32 i = 0; i < LENGTH; i++) {
33 this->vec[i] = list[i];
34 }
35 }
36
37 NumVec(const NumVec &other) {
38 for (u32 i = 0; i < LENGTH; i++) {
39 this->vec[i] = other.vec[i];
40 }
41 }
42
43 static constexpr u32 size() {
44 return LENGTH;
45 }
46
47private:
48 template<u64 idx>
49 void init(T arg0) {
50 this->vec[idx] = arg0;
51 }
52
53 template<u64 idx, typename... Args>
54 void init(T arg0, Args... args) {
55 this->init<idx>(arg0);
56 this->init<idx+1>(args...);
57 }
58
59public:
60 template<typename... Args>
61 NumVec(T arg0, Args... args) {
62 this->init<0>(arg0, args...);
63 }
64
65 bool operator==(const NumVec &other) const {
66 for (u32 i = 0; i < LENGTH; i++) {
67 if (this->vec[i] != other.vec[i]) return false;
68 }
69 return true;
70 }
71
72 bool operator!=(const NumVec &other) const {
73#pragma clang diagnostic push
74#pragma ide diagnostic ignored "Simplify"
75 return !(*this == other);
76#pragma clang diagnostic pop
77 }
78
79 bool operator>(const NumVec &other) const {
80 for (u32 i = 0; i < LENGTH; i++) {
81 if (this->vec[i] <= other.vec[i]) return false;
82 }
83 return true;
84 }
85
86 bool operator>=(const NumVec &other) const {
87 for (u32 i = 0; i < LENGTH; i++) {
88 if (this->vec[i] < other.vec[i]) return false;
89 }
90 return true;
91 }
92
93 bool operator<(const NumVec &other) const {
94 for (u32 i = 0; i < LENGTH; i++) {
95 if (this->vec[i] >= other.vec[i]) return false;
96 }
97 return true;
98 }
99
100 bool operator<=(const NumVec &other) const {
101 for (u32 i = 0; i < LENGTH; i++) {
102 if (this->vec[i] > other.vec[i]) return false;
103 }
104 return true;
105 }
106
107 T &operator[](u32 idx) {
108 assert(idx < LENGTH)
109 return this->vec[idx];
110 }
111
112 const T &operator[](u32 idx) const {
113 assert(idx < LENGTH)
114 return this->vec[idx];
115 }
116
117#define SCALAR_OP(op) \
118 NumVec operator STRCAT(op, =) (const T &scalar) { \
119 for (u32 i = 0; i < LENGTH; i++) { \
120 this->vec[i] STRCAT(op, =) scalar; \
121 } \
122 return NumVec(*this); \
123 } \
124 \
125 NumVec operator op (const T &scalar) const { \
126 return NumVec(*this) STRCAT(op, =) scalar; \
127 } \
128
129
130 SCALAR_OP(+)
131
132 SCALAR_OP(-)
133
134 SCALAR_OP(*)
135
136 SCALAR_OP(/)
137
138#undef SCALAR_OP
139
140#define VECTOR_OP(op) \
141 NumVec operator STRCAT(op, =) (const NumVec &other) { \
142 for (u32 i = 0; i < LENGTH; i++) { \
143 this->vec[i] STRCAT(op, =) other.vec[i]; \
144 } \
145 return NumVec(*this); \
146 } \
147 \
148 NumVec operator op (const NumVec &other) const { \
149 return NumVec(*this) STRCAT(op, =) other; \
150 } \
151
152
153 VECTOR_OP(+)
154
155 VECTOR_OP(-)
156
157 VECTOR_OP(*)
158
159 VECTOR_OP(/)
160
161#undef VECTOR_OP
162
163 bool eq(const NumVec &other, const T eps = std::numeric_limits<T>::epsilon()) const {
164 return (*this - other).abs() <= NumVec(eps);
165 }
166
167 T sum() const {
168 T val = 0;
169 for (u32 i = 0; i < LENGTH; i++) val += this->vec[i];
170 return val;
171 }
172
173 T prod() const {
174 T val = 1;
175 for (u32 i = 0; i < LENGTH; i++) val *= this->vec[i];
176 return val;
177 }
178
179 NumVec floor() const {
180 auto val = NumVec(*this);
181 for (u32 i = 0; i < LENGTH; i++) val[i] = std::floor(val[i]);
182 return val;
183 }
184
185 NumVec ceil() const {
186 auto val = NumVec(*this);
187 for (u32 i = 0; i < LENGTH; i++) val[i] = std::ceil(val[i]);
188 return val;
189 }
190
191 NumVec abs() const {
192 auto val = NumVec(*this);
193 for (u32 i = 0; i < LENGTH; i++) val[i] = std::abs(val[i]);
194 return val;
195 }
196
197 NumVec pow(float pow) const {
198 auto val = NumVec(*this);
199 for (u32 i = 0; i < LENGTH; i++) val[i] = std::pow(val[i], pow);
200 return val;
201 }
202
203 template<typename Q>
205 NumVec<Q, LENGTH> result;
206
207 for (u32 i = 0; i < LENGTH; i++) {
208 result[i] = (Q) this->vec[i];
209 }
210
211 return result;
212 }
213
214 friend std::ostream &operator<<(std::ostream &stream, const NumVec &value) {
215 stream << '[';
216 for (u32 i = 0; i < LENGTH - 1; i++) stream << value.vec[i] << ", ";
217 stream << value.vec[LENGTH - 1] << ']';
218
219 return stream;
220 }
221
222};
#define assert(...)
Definition LinuxAMD.h:28
bool operator>(const NumVec &other) const
Definition numvec.h:79
T prod() const
Definition numvec.h:173
bool operator!=(const NumVec &other) const
Definition numvec.h:72
friend std::ostream & operator<<(std::ostream &stream, const NumVec &value)
Definition numvec.h:214
NumVec ceil() const
Definition numvec.h:185
NumVec(const NumVec &other)
Definition numvec.h:37
bool operator<(const NumVec &other) const
Definition numvec.h:93
NumVec abs() const
Definition numvec.h:191
void init(T arg0, Args... args)
Definition numvec.h:54
NumVec(T arg0, Args... args)
Definition numvec.h:61
NumVec floor() const
Definition numvec.h:179
void init(T arg0)
Definition numvec.h:49
NumVec< Q, LENGTH > castAs() const
Definition numvec.h:204
static constexpr u32 size()
Definition numvec.h:43
bool eq(const NumVec &other, const T eps=std::numeric_limits< T >::epsilon()) const
Definition numvec.h:163
NumVec(std::initializer_list< T > list)
Definition numvec.h:24
bool operator==(const NumVec &other) const
Definition numvec.h:65
T & operator[](u32 idx)
Definition numvec.h:107
NumVec()=default
bool operator<=(const NumVec &other) const
Definition numvec.h:100
NumVec(T val)
Definition numvec.h:20
const T & operator[](u32 idx) const
Definition numvec.h:112
NumVec(const T(&list)[LENGTH])
Definition numvec.h:31
T vec[LENGTH]
Definition numvec.h:15
bool operator>=(const NumVec &other) const
Definition numvec.h:86
NumVec pow(float pow) const
Definition numvec.h:197
T sum() const
Definition numvec.h:167
CF abs(const CF &cf)
#define VECTOR_OP(op)
Definition numvec.h:140
#define SCALAR_OP(op)
Definition numvec.h:117
std::uint32_t u32
Definition type.h:11
std::uint64_t u64
Definition type.h:13