Skip to content

Commit 60a4f48

Browse files
committed
Added tests of different operations in general-purpose tables (much like Cayley, but with a general binary function parameter)
1 parent 5ceae18 commit 60a4f48

File tree

1 file changed

+152
-3
lines changed

1 file changed

+152
-3
lines changed

app/dual.cpp

+152-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <iostream>
33
#include <iomanip>
44
#include <vector>
5+
#include <map>
56

67
#ifdef DUAL2
78
#include "dual2.hpp"
@@ -15,16 +16,164 @@
1516
#define DUAL_IMPL "unknown"
1617
#endif
1718

19+
#include "streams.tpp"
20+
#include "utility.hpp"
21+
22+
template<class T, unsigned N, unsigned M>
23+
std::vector<std::string> get_table(std::string const& title,
24+
const T (&LHS)[N], const T (&RHS)[M], T (*fn)(T const&, T const&)) {
25+
using namespace std;
26+
using Streams::center;
27+
28+
vector<string> out;
29+
ostringstream os;
30+
char b_n = '-', b_ne = '.', b_e = '|', b_se = '\'',
31+
b_s = '-', b_sw = '\'', b_w = '|', b_nw = '.',
32+
b_c = '+', b_none = ' ';
33+
string utitle = "(u)", vtitle = "(v)", pad = " ";
34+
long wu = utitle.length(), wv = vtitle.length(), wtitle = title.length(),
35+
rwv = (wv-1)/2, lwv = wv-1-rwv, wcell = max(3l, wu),
36+
wpad = pad.length(), wpcell = wcell + wpad * 2, wrow = wpcell * M,
37+
dtitle = wtitle + 2;
38+
string lv = string(lwv, ' '), rv = string(rwv, ' '),
39+
north = b_none + string(wpcell + lwv, ' ') + b_nw
40+
+ center(center(title, dtitle, ' '), wrow + rwv, '-') + b_ne,
41+
middle = b_nw + center(utitle, wpcell+lwv, '-', false) + b_c + string(wrow+rwv, '-') + b_c,
42+
bottom = b_sw + string(wpcell+lwv, '-') + b_c + string(wrow+rwv, '-') + b_se;
43+
out.emplace_back(north);
44+
string header = b_none + string(wpcell, ' ') + vtitle;
45+
for(auto const& rhs : RHS)
46+
header += pad + center(rhs, wcell, ' ') + pad;
47+
header += b_e;
48+
out.emplace_back(header);
49+
out.emplace_back(middle);
50+
for(auto const& lhs : LHS) {
51+
string str = b_w + center(string(lhs), wpcell+lwv, ' ') + b_w + string(rwv, ' ');
52+
for(auto const& rhs : RHS) {
53+
str += pad + center(fn(lhs, rhs), wcell, ' ', false) + pad;
54+
}
55+
str += b_e;
56+
out.emplace_back(str);
57+
}
58+
out.emplace_back(bottom);
59+
return out;
60+
}
61+
1862
int main(int argc, const char *argv[]) {
1963
using namespace std;
64+
using Streams::center;
65+
66+
cout << "Using dual quaternions as implemented in " << DUAL_IMPL << '.' << endl;
2067

2168
typedef float T;
2269
typedef DualQuaternion<T> DQ;
70+
typedef DQ (binary) (DQ const&, DQ const&);
71+
72+
/*DQ p1 = 1_e + 1_I, p2 = 1_e + 1_J, dp = p2 - p1;
73+
// Line: p1 + dp * t
74+
DQ x = 1_e + 1_I + 1_J + 1_k, dxp = x - p1,
75+
projection = p1 + dp * dot(dxp, dp),
76+
rejection = dxp - projection;
77+
cout << "Line l from " << p1 << " to " << p2 << " is given by l(t) = "
78+
<< p1 << " + (" << dp << ")t" << endl;
79+
cout << "Point x = " << x << " is " << dxp << " from " << p1 << endl;
80+
cout << " Projection = " << projection << "\n"
81+
" Rejection = " << rejection << endl;
82+
83+
return 0;*/
2384

24-
cout << "Using " << DUAL_IMPL << " to implement dual quaternions!\n" << endl;
85+
DQ LHS[] = {1_e, 1_i, 1_j, 1_k, 1_E, 1_I, 1_J, 1_K},
86+
RHS[] = {1_i, 1_i + 1_J /* (1_i + 1_j)*T(sqrtf(2)/2) */ };
2587

26-
DQ LHS[] = {{1}, {0,1}, {0,0,1}, {0,0,0,1},
27-
{0,0,0,0,1}, {0,0,0,0,0,1}, {0,0,0,0,0,0,1}, {0,0,0,0,0,0,0,1}};
88+
map<string, binary*> functions = {
89+
{"W(u, v) = uv", [] (DQ const& l, DQ const& r) -> DQ { return l * r; }},
90+
{"W(u, v) = u v *u", [] (DQ const& l, DQ const& r) -> DQ { return l ^ r; }},
91+
{"W(u, v) = v u *v", [] (DQ const& l, DQ const& r) -> DQ { return r ^ l; }}
92+
};
93+
/*auto ntables = functions.size();
94+
vector<ostringstream> oss;
95+
vector<istringstream> iss;
96+
for(auto i = 0; i < ntables; i++) {
97+
oss.emplace_back();
98+
print_table(oss[i], LHS, LHS, functions[i]);
99+
iss.emplace_back(oss[i].str());
100+
}*/
101+
102+
cout << "Sandwich operators: " << endl;
103+
vector<vector<std::string>> lines;
104+
for(auto const& fn : functions)
105+
lines.emplace_back(get_table(fn.first, LHS, LHS, fn.second));
106+
for(long k = 0, dk = 2, n = lines.size(); k < n; k += dk) {
107+
for(long j = 0, m = lines[0].size(); j < m; j++) {
108+
for(long i = k; i < k+dk && i < n; i++) {
109+
cout << lines[i][j] << " ";
110+
}
111+
cout << endl;
112+
}
113+
}
114+
/*string line;
115+
bool cont = true;
116+
for(unsigned i = 0; i < ntables; i++) {
117+
if(i) cout << " ";
118+
if(!std::getline(iss[i], line)) {
119+
cont = false;
120+
break;
121+
}
122+
cout << line;
123+
}
124+
cout << endl;
125+
if(!cont) break;
126+
}*/
127+
128+
/*std::vector<std::vector<std::string>> cols = {
129+
{"u"}, {"v"}, {"uv"}, {"uvu*"},
130+
{"v"}, {"u"}, {"vu"}, {"vuv*"}
131+
};
132+
for(auto const& lhs : LHS) {
133+
for(auto const& rhs : LHS) {
134+
cols[0].emplace_back(to_string(lhs, 3));
135+
cols[1].emplace_back(to_string(rhs, 3));
136+
cols[2].emplace_back(to_string(rhs * lhs, 3));
137+
cols[3].emplace_back(to_string(lhs ^ rhs, 3));
138+
cols[4].emplace_back(to_string(rhs, 3));
139+
cols[5].emplace_back(to_string(lhs, 3));
140+
cols[6].emplace_back(to_string(lhs * rhs, 3));
141+
cols[7].emplace_back(to_string(rhs ^ lhs, 3));
142+
}
143+
}
144+
std::vector<long> maxes;
145+
for(long j = 0, m = cols.size(); j < m; j++)
146+
maxes.emplace_back(minimax(cols[j]).second);
147+
for(long i = 0, n = cols[0].size(); i < n; i++) {
148+
for(long j = 0, m = cols.size(); j < m; j++) {
149+
cout << Streams::center(cols[j][i], maxes[j]);
150+
if(j == 3) cout << "##";
151+
else cout << " ";
152+
}
153+
if(!i) {
154+
cout << " | ";
155+
for(long j = 0, m = cols.size(); j < m; j++)
156+
cout << Streams::center(cols[j][i], maxes[j]) << " ";
157+
}
158+
long k = i-1;
159+
if(k & 1) endl(cout);
160+
//if(i && !(i&1)) endl(cout);
161+
else cout << " | ";
162+
}*/
163+
164+
/*cout << setw(3) << "" << " | ";
165+
for(auto const& rhs : LHS)
166+
cout << setw(3) << rhs << " ";
167+
cout << endl;
168+
for(auto const& lhs : LHS) {
169+
cout << setw(3) << lhs << " | ";
170+
for(auto const& rhs : LHS) {
171+
cout << setw(3) << (lhs ^ rhs) << " ";
172+
}
173+
cout << endl;
174+
}*/
175+
/*DQ LHS[] = {{1}, {0,1}, {0,0,1}, {0,0,0,1},
176+
{0,0,0,0,1}, {0,0,0,0,0,1}, {0,0,0,0,0,0,1}, {0,0,0,0,0,0,0,1}};*/
28177
for(auto const& lhs : LHS) {
29178
for(auto const& rhs : LHS) {
30179
cout << setw(3) << (lhs * rhs) << " ";

0 commit comments

Comments
 (0)