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
|
// Boost.Units - A C++ library for zero-overhead dimensional analysis and
// unit/quantity manipulation and conversion
//
// Copyright (C) 2003-2008 Matthias Christian Schabel
// Copyright (C) 2007-2008 Steven Watanabe
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
/**
\file
\brief quaternion.cpp
\details
Demonstrate interoperability with Boost.Quaternion.
Output:
@verbatim
//[quaternion_output_1
+L = (4,3,2,1) m
-L = (-4,-3,-2,-1) m
L+L = (8,6,4,2) m
L-L = (0,0,0,0) m
L*L = (2,24,16,8) m^2
L/L = (1,0,0,0) dimensionless
L^3 = (-104,102,68,34) m^3
//]
//[quaternion_output_2
+L = (4 m,3 m,2 m,1 m)
-L = (-4 m,-3 m,-2 m,-1 m)
L+L = (8 m,6 m,4 m,2 m)
L-L = (0 m,0 m,0 m,0 m)
L^3 = (-104 m^3,102 m^3,68 m^3,34 m^3)
//]
@endverbatim
**/
#include <iostream>
#include <boost/math/quaternion.hpp>
#include <boost/mpl/list.hpp>
#include <boost/units/pow.hpp>
#include <boost/units/quantity.hpp>
#include <boost/units/io.hpp>
#include "test_system.hpp"
#if BOOST_UNITS_HAS_BOOST_TYPEOF
#include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
BOOST_TYPEOF_REGISTER_TEMPLATE(boost::math::quaternion, 1)
#endif
namespace boost {
namespace units {
//[quaternion_class_snippet_1a
/// specialize power typeof helper
template<class Y,long N,long D>
struct power_typeof_helper<boost::math::quaternion<Y>,static_rational<N,D> >
{
// boost::math::quaternion only supports integer powers
BOOST_STATIC_ASSERT(D==1);
typedef boost::math::quaternion<
typename power_typeof_helper<Y,static_rational<N,D> >::type
> type;
static type value(const boost::math::quaternion<Y>& x)
{
return boost::math::pow(x,static_cast<int>(N));
}
};
//]
//[quaternion_class_snippet_1b
/// specialize root typeof helper
template<class Y,long N,long D>
struct root_typeof_helper<boost::math::quaternion<Y>,static_rational<N,D> >
{
// boost::math::quaternion only supports integer powers
BOOST_STATIC_ASSERT(N==1);
typedef boost::math::quaternion<
typename root_typeof_helper<Y,static_rational<N,D> >::type
> type;
static type value(const boost::math::quaternion<Y>& x)
{
return boost::math::pow(x,static_cast<int>(D));
}
};
//]
//[quaternion_class_snippet_2a
/// specialize power typeof helper for quaternion<quantity<Unit,Y> >
template<class Unit,long N,long D,class Y>
struct power_typeof_helper<
boost::math::quaternion<quantity<Unit,Y> >,
static_rational<N,D> >
{
typedef typename power_typeof_helper<
Y,
static_rational<N,D>
>::type value_type;
typedef typename power_typeof_helper<
Unit,
static_rational<N,D>
>::type unit_type;
typedef quantity<unit_type,value_type> quantity_type;
typedef boost::math::quaternion<quantity_type> type;
static type value(const boost::math::quaternion<quantity<Unit,Y> >& x)
{
const boost::math::quaternion<value_type> tmp =
pow<static_rational<N,D> >(boost::math::quaternion<Y>(
x.R_component_1().value(),
x.R_component_2().value(),
x.R_component_3().value(),
x.R_component_4().value()));
return type(quantity_type::from_value(tmp.R_component_1()),
quantity_type::from_value(tmp.R_component_2()),
quantity_type::from_value(tmp.R_component_3()),
quantity_type::from_value(tmp.R_component_4()));
}
};
//]
//[quaternion_class_snippet_2b
/// specialize root typeof helper for quaternion<quantity<Unit,Y> >
template<class Unit,long N,long D,class Y>
struct root_typeof_helper<
boost::math::quaternion<quantity<Unit,Y> >,
static_rational<N,D> >
{
typedef typename root_typeof_helper<
Y,
static_rational<N,D>
>::type value_type;
typedef typename root_typeof_helper<
Unit,
static_rational<N,D>
>::type unit_type;
typedef quantity<unit_type,value_type> quantity_type;
typedef boost::math::quaternion<quantity_type> type;
static type value(const boost::math::quaternion<quantity<Unit,Y> >& x)
{
const boost::math::quaternion<value_type> tmp =
root<static_rational<N,D> >(boost::math::quaternion<Y>(
x.R_component_1().value(),
x.R_component_2().value(),
x.R_component_3().value(),
x.R_component_4().value()));
return type(quantity_type::from_value(tmp.R_component_1()),
quantity_type::from_value(tmp.R_component_2()),
quantity_type::from_value(tmp.R_component_3()),
quantity_type::from_value(tmp.R_component_4()));
}
};
//]
} // namespace units
} // namespace boost
int main(void)
{
using boost::math::quaternion;
using namespace boost::units;
using namespace boost::units::test;
using boost::units::pow;
{
//[quaternion_snippet_1
typedef quantity<length,quaternion<double> > length_dimension;
length_dimension L(quaternion<double>(4.0,3.0,2.0,1.0)*meters);
//]
std::cout << "+L = " << +L << std::endl
<< "-L = " << -L << std::endl
<< "L+L = " << L+L << std::endl
<< "L-L = " << L-L << std::endl
<< "L*L = " << L*L << std::endl
<< "L/L = " << L/L << std::endl
// unfortunately, without qualification msvc still
// finds boost::math::pow by ADL.
<< "L^3 = " << boost::units::pow<3>(L) << std::endl
// << "L^(3/2) = " << pow< static_rational<3,2> >(L) << std::endl
// << "3vL = " << root<3>(L) << std::endl
// << "(3/2)vL = " << root< static_rational<3,2> >(L) << std::endl
<< std::endl;
}
{
//[quaternion_snippet_2
typedef quaternion<quantity<length> > length_dimension;
length_dimension L(4.0*meters,3.0*meters,2.0*meters,1.0*meters);
//]
std::cout << "+L = " << +L << std::endl
<< "-L = " << -L << std::endl
<< "L+L = " << L+L << std::endl
<< "L-L = " << L-L << std::endl
// << "L*L = " << L*L << std::endl
// << "L/L = " << L/L << std::endl
<< "L^3 = " << boost::units::pow<3>(L) << std::endl
// << "L^(3/2) = " << pow< static_rational<3,2> >(L) << std::endl
// << "3vL = " << root<3>(L) << std::endl
// << "(3/2)vL = " << root< static_rational<3,2> >(L) << std::endl
<< std::endl;
}
return 0;
}
|