-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathКватернионы.cpp
275 lines (240 loc) · 5.73 KB
/
Кватернионы.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <iostream>
#include <math.h>
#include <fstream>
using namespace std;
class Quaternion
{
private:
double Im;
double Re_i;
double Re_j;
double Re_k;
public:
Quaternion() : Im(0), Re_i(0), Re_j(0), Re_k(0)
{}
Quaternion(const double a, const double b, const double c, const double d) : Im(a), Re_i(b), Re_j(c), Re_k(d)
{}
void printQ()
{
cout << Im << " " << Re_i << " " << Re_j << " " << Re_k << endl;
}
Quaternion operator = (const Quaternion &a)
{
Im = a.Im;
Re_i = a.Re_i;
Re_j = a.Re_j;
Re_k = a.Re_k;
return *this;
}
Quaternion operator + (const Quaternion &a) const
{
Quaternion Rez;
Rez.Im = Im + a.Im;
Rez.Re_i = Re_i + a.Re_i;
Rez.Re_j = Re_j + a.Re_j;
Rez.Re_k = Re_k + a.Re_k;
return Rez;
}
Quaternion operator - (const Quaternion &a) const
{
Quaternion Rez;
Rez.Im = Im - a.Im;
Rez.Re_i = Re_i - a.Re_i;
Rez.Re_j = Re_j - a.Re_j;
Rez.Re_k = Re_k - a.Re_k;
return Rez;
}
Quaternion operator += (const Quaternion &a)
{
Im += a.Im;
Re_i += a.Re_i;
Re_j += a.Re_j;
Re_k += a.Re_k;
return *this;
}
Quaternion operator -= (const Quaternion &a)
{
Im -= a.Im;
Re_i -= a.Re_i;
Re_j -= a.Re_j;
Re_k -= a.Re_k;
return *this;
}
Quaternion operator * (const Quaternion &a) const
{
Quaternion Rez;
Rez.Im = (Im * a.Im) - (Re_i * a.Re_i) - (Re_j * a.Re_j) - (Re_k * a.Re_k);
Rez.Re_i = (Im * a.Re_i) + (Re_i * a.Im) + (Re_j * a.Re_k) - (Re_k * a.Re_j);
Rez.Re_j = (Im * a.Re_j) - (Re_i * a.Re_k) + (Re_j * a.Im) + (Re_k * a.Re_i);
Rez.Re_k = (Im * a.Re_k) + (Re_i * a.Re_j) - (Re_j * a.Re_i) + (Re_k * a.Im);
return Rez;
}
Quaternion operator *= (const Quaternion &a)
{
Quaternion Rez;
Rez.Im = (Im * a.Im) - (Re_i * a.Re_i) - (Re_j * a.Re_j) - (Re_k * a.Re_k);
Rez.Re_i = (Im * a.Re_i) + (Re_i * a.Im) + (Re_j * a.Re_k) - (Re_k * a.Re_j);
Rez.Re_j = (Im * a.Re_j) - (Re_i * a.Re_k) + (Re_j * a.Im) + (Re_k * a.Re_i);
Rez.Re_k = (Im * a.Re_k) + (Re_i * a.Re_j) - (Re_j * a.Re_i) + (Re_k * a.Im);
*this = Rez;
return *this;
}
Quaternion operator / (double n) const
{
Quaternion Rez;
Rez.Im = Im / n;
Rez.Re_i = Re_i / n;
Rez.Re_j = Re_j / n;
Rez.Re_k = Re_k / n;
return Rez;
}
Quaternion operator /= (double n)
{
Im /= n;
Re_i /= n;
Re_j /= n;
Re_k /= n;
return *this;
}
Quaternion Conjugation() //ñîïðÿæåííûé êâàòåðíèîí
{
Re_i = -Re_i;
Re_j = -Re_j;
Re_k = -Re_k;
return *this;
}
/*double Norm() //ìîäóëü êâàòåðíèîíà
{
double a = sqrt((Im*Im) + (Re_i*Re_i) + (Re_j*Re_j) + (Re_k*Re_k));
return a;
}*/
/*Quaternion Reciprocal() // êâàòåðíèîí À â ñòåïåíè -1
{
double n;
Quaternion A = *this;
//n = A.Norm();
n = (Im*Im) + (Re_i*Re_i) + (Re_j*Re_j) + (Re_k*Re_k);
A.Conjugation();
A /= n;
return A;
}*/
Quaternion operator / (const Quaternion &a) const
{
Quaternion Rez, Help;
Help = a;
Help = Help.Conjugation();
Rez = *this * Help;
Rez /= (a.Im*a.Im) + (a.Re_i*a.Re_i) + (a.Re_j*a.Re_j) + (a.Re_k*a.Re_k);
return Rez;
}
Quaternion operator /= (const Quaternion &a)
{
Quaternion Rez, Help;
Help = a;
Help = Help.Conjugation();
Rez = *this * Help;
Rez /= (a.Im*a.Im) + (a.Re_i*a.Re_i) + (a.Re_j*a.Re_j) + (a.Re_k*a.Re_k);
*this = Rez;
return *this;
}
bool operator == (const Quaternion &a) const
{
return ((Im == a.Im) && (Re_i == a.Re_i) && (Re_j == a.Re_j) && (Re_k == a.Re_k));
}
bool operator != (const Quaternion &a) const
{
return (!(*this == a));
}
double DotProduct(const Quaternion &a) const
{
double Rez = (Im*a.Im) + (Re_i*a.Re_i) + (Re_j*a.Re_j) + (Re_k*a.Re_k);
return Rez;
}
Quaternion CrossProduct(Quaternion &a) const
{
Quaternion Help = *this;
Quaternion Rez = ((Help * a) - (a * Help)) / 2;
return Rez;
}
friend ostream& operator << (ostream& os, Quaternion &a);
friend istream& operator >> (istream& is, Quaternion &a);
};
ostream& operator << (ostream& os, Quaternion &a)
{
os << a.Im << a.Re_i << a.Re_j << a.Re_k;
return os;
}
istream& operator >> (istream& is, Quaternion &a)
{
is >> a.Im >> a.Re_i >> a.Re_j >> a.Re_k;
return is;
}
int main()
{
ifstream infile("Test_quaternion.txt");
int Number_of_Tests, i;
int Operation_Number; // 1 - (+), 2 - (*), 3 - (/), 4 - (Conjugation), 5 - (DotProduct), 6 - (CrossProduct)
infile >> Number_of_Tests;
for (i = 1; i <= Number_of_Tests; i++)
{
infile >> Operation_Number;
switch (Operation_Number)
{
case 1:
{
Quaternion TestOne, TestTwo, Rez;
infile >> TestOne >> TestTwo >> Rez;
if ((TestOne = TestOne + TestTwo) != Rez)
cout << "Test " << i << " is not success!!!" << endl;
break;
}
case 2:
{
Quaternion TestOne, TestTwo, Rez;
infile >> TestOne >> TestTwo >> Rez;
if ((TestOne = TestOne * TestTwo) != Rez)
cout << "Test " << i << " is not success!!!" << endl;
break;
}
case 3:
{
Quaternion TestOne, TestTwo, Rez;
infile >> TestOne >> TestTwo >> Rez;
if ((TestOne = TestOne / TestTwo) != Rez)
cout << "Test " << i << " is not success!!!" << endl;
break;
}
case 4:
{
Quaternion TestOne, Rez;
infile >> TestOne >> Rez;
if ((TestOne = TestOne.Conjugation()) != Rez)
cout << "Test " << i << " is not success!!!" << endl;
break;
}
case 5:
{
Quaternion TestOne, TestTwo;
double Rez, Test;
infile >> TestOne >> TestTwo >> Rez;
if ((Test = TestOne.DotProduct(TestTwo)) != Rez)
cout << "Test " << i << " is not success!!!" << endl;
break;
}
case 6:
{
Quaternion TestOne, TestTwo, Rez;
infile >> TestOne >> TestTwo >> Rez;
if ((TestOne = TestOne.CrossProduct(TestTwo)) != Rez)
cout << "Test " << i << " is not success!!!" << endl;
break;
}
default:
{
cout << "Error!!! Uncorrect Operation_Number!!! (Test " << i << ")" << endl;
break;
}
}
}
return 0;
}