Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit 80e08ad

Browse files
committed
additional 'base' constructors, more asserts
1 parent 7f056fc commit 80e08ad

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

hypercomplex.hpp

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,43 +47,53 @@ namespace hypercomplex {
4747
}
4848
public:
4949
std::vector<float> e;
50-
51-
base(std::vector<float> other) {
52-
assert(valid_dim(other.size()));
53-
e = other;
50+
51+
base(int dim) {
52+
assert(!valid_dim(dim));
53+
for(int x = 0; x < dim; x++) {
54+
e.push_back(0);
55+
}
56+
}
57+
base(std::vector<float> data) {
58+
assert(valid_dim(data.size()));
59+
e = data;
60+
};
61+
base(std::vector<float> data, int dim) {
62+
assert(valid_dim(data.size()) && dim == data.size());
63+
e = data;
5464
};
5565

5666
base operator+(base const& other) {
57-
assert(e.size() != other.e.size());
67+
assert(e.size() == other.e.size());
5868
std::vector<float> E;
5969
for(int x = 0; x < e.size(); x++) {
6070
E.push_back(e[x] + other.e[x]);
6171
}
6272
return base(E);
6373
}
6474
base operator-(base const& other) {
65-
assert(e.size() != other.e.size());
75+
assert(e.size() == other.e.size());
6676
std::vector<float> E;
6777
for(int x = 0; x < e.size(); x++) {
6878
E.push_back(e[x] - other.e[x]);
6979
}
7080
return base(E);
7181
}
7282
void operator+=(base const& other) {
73-
assert(e.size() != other.e.size());
83+
assert(e.size() == other.e.size());
7484
for(int x = 0; x < e.size(); x++)
7585
e[x] += other.e[x];
7686
}
7787
void operator-=(base const& other) {
78-
assert(e.size() != other.e.size());
88+
assert(e.size() == other.e.size());
7989
for(int x = 0; x < e.size(); x++)
8090
e[x] -= other.e[x];
8191
}
8292

8393
base operator*(base y) {
84-
assert(e.size() != y.e.size());
94+
assert(e.size() == y.e.size());
8595
if(e.size() == 1)
86-
return base({e[0]*y.e[0]});
96+
return base(std::vector<float>({e[0]*y.e[0]}));
8797

8898
Cpair<base, base> p1 = split(self());
8999
Cpair<base, base> p2 = split(y);
@@ -112,21 +122,24 @@ namespace hypercomplex {
112122
}
113123

114124
struct complex: hypercomplex::base {
115-
complex(): hypercomplex::base({0, 0}) { };
125+
complex(): hypercomplex::base(2) { };
126+
complex(std::vector<float> data): hypercomplex::base(data, 2) { };
116127
complex(float _a, float _i): hypercomplex::base({_a, _i}) { };
117128

118129
complex(const hypercomplex::base &x) : hypercomplex::base(x) {}
119130
};
120131

121132
struct quaternion: hypercomplex::base {
122-
quaternion(): hypercomplex::base({0, 0, 0, 0}) { };
133+
quaternion(): hypercomplex::base(4) { };
134+
quaternion(std::vector<float> data): hypercomplex::base(data, 4) { };
123135
quaternion(float _a, float _i, float _j, float _k): hypercomplex::base({_a, _i, _j, _k}) { };
124136

125137
quaternion(const hypercomplex::base &x) : hypercomplex::base(x) {}
126138
};
127139

128140
struct octonion: hypercomplex::base {
129-
octonion(): hypercomplex::base({0, 0, 0, 0, 0, 0, 0, 0}) { };
141+
octonion(): hypercomplex::base(8) { };
142+
octonion(std::vector<float> data): hypercomplex::base(data, 8) { };
130143
octonion(float _a, float _i, float _j, float _k, float _e5, float _e6, float _e7, float _e8): hypercomplex::base({_a, _i, _j, _k, _e5, _e6, _e7, _e8}) { };
131144

132145
octonion(const hypercomplex::base &x) : hypercomplex::base(x) {}

0 commit comments

Comments
 (0)