Blaze 3.9
Invert.h
Go to the documentation of this file.
1//=================================================================================================
33//=================================================================================================
34
35#ifndef _BLAZE_MATH_SHIMS_INVERT_H_
36#define _BLAZE_MATH_SHIMS_INVERT_H_
37
38
39//*************************************************************************************************
40// Includes
41//*************************************************************************************************
42
46#include <blaze/system/Inline.h>
47#include <blaze/util/Assert.h>
48#include <blaze/util/Complex.h>
49#include <blaze/util/EnableIf.h>
50#include <blaze/util/mpl/If.h>
53
54
55namespace blaze {
56
57//=================================================================================================
58//
59// INV SHIMS
60//
61//=================================================================================================
62
63//*************************************************************************************************
75template< typename T, EnableIf_t< IsScalar_v<T> >* = nullptr >
76BLAZE_ALWAYS_INLINE constexpr decltype(auto) inv( T a ) noexcept( IsBuiltin_v<T> )
77{
78 BLAZE_USER_ASSERT( a != T{}, "Division by zero detected" );
79
80 using BT = UnderlyingBuiltin_t<T>;
81 using ST = If_t< IsIntegral_v<BT>, double, BT >;
82
83 return ( ST(1) / a );
84}
85//*************************************************************************************************
86
87
88//*************************************************************************************************
101BLAZE_ALWAYS_INLINE constexpr complex<float> inv( const complex<float>& a ) noexcept
102{
103 const float abs( pow2( real(a) ) + pow2( imag(a) ) );
104 BLAZE_USER_ASSERT( abs != 0.0F, "Division by zero detected" );
105
106 const float iabs( 1.0F / abs );
107 return complex<float>( iabs*real(a), -iabs*imag(a) );
108}
109//*************************************************************************************************
110
111
112//*************************************************************************************************
125BLAZE_ALWAYS_INLINE constexpr complex<double> inv( const complex<double>& a ) noexcept
126{
127 const double abs( pow2( real(a) ) + pow2( imag(a) ) );
128 BLAZE_USER_ASSERT( abs != 0.0, "Division by zero detected" );
129
130 const double iabs( 1.0 / abs );
131 return complex<double>( iabs*real(a), -iabs*imag(a) );
132}
133//*************************************************************************************************
134
135
136//*************************************************************************************************
149BLAZE_ALWAYS_INLINE constexpr complex<long double> inv( const complex<long double>& a ) noexcept
150{
151 const long double abs( pow2( real(a) ) + pow2( imag(a) ) );
152 BLAZE_USER_ASSERT( abs != 0.0L, "Division by zero detected" );
153
154 const long double iabs( 1.0L / abs );
155 return complex<long double>( iabs*real(a), -iabs*imag(a) );
156}
157//*************************************************************************************************
158
159
160
161
162//=================================================================================================
163//
164// INVERT SHIMS
165//
166//=================================================================================================
167
168//*************************************************************************************************
181BLAZE_ALWAYS_INLINE void invert( float& a ) noexcept
182{
183 a = inv( a );
184}
185//*************************************************************************************************
186
187
188//*************************************************************************************************
201BLAZE_ALWAYS_INLINE void invert( double& a ) noexcept
202{
203 a = inv( a );
204}
205//*************************************************************************************************
206
207
208//*************************************************************************************************
221BLAZE_ALWAYS_INLINE void invert( long double& a ) noexcept
222{
223 a = inv( a );
224}
225//*************************************************************************************************
226
227
228//*************************************************************************************************
241BLAZE_ALWAYS_INLINE void invert( complex<float>& a ) noexcept
242{
243 a = inv( a );
244}
245//*************************************************************************************************
246
247
248//*************************************************************************************************
261BLAZE_ALWAYS_INLINE void invert( complex<double>& a ) noexcept
262{
263 a = inv( a );
264}
265//*************************************************************************************************
266
267
268//*************************************************************************************************
281BLAZE_ALWAYS_INLINE void invert( complex<long double>& a ) noexcept
282{
283 a = inv( a );
284}
285//*************************************************************************************************
286
287} // namespace blaze
288
289#endif
Header file for run time assertion macros.
Header file for the complex data type.
Header file for the EnableIf class template.
Header file for the If class template.
Header file for the IsBuiltin type trait.
Header file for the IsIntegral type trait.
Header file for the IsScalar type trait.
Header file for the UnderlyingBuiltin type trait.
decltype(auto) real(const DenseMatrix< MT, SO > &dm)
Returns a matrix containing the real part of each single element of dm.
Definition: DMatMapExpr.h:1529
decltype(auto) abs(const DenseMatrix< MT, SO > &dm)
Applies the abs() function to each single element of the dense matrix dm.
Definition: DMatMapExpr.h:1296
decltype(auto) imag(const DenseMatrix< MT, SO > &dm)
Returns a matrix containing the imaginary part of each single element of dm.
Definition: DMatMapExpr.h:1557
BLAZE_ALWAYS_INLINE void invert(complex< long double > &a) noexcept
In-place inversion of the given extended precision complex number.
Definition: Invert.h:281
BLAZE_ALWAYS_INLINE constexpr complex< long double > inv(const complex< long double > &a) noexcept
Inverting the given extended precision complex number.
Definition: Invert.h:149
typename UnderlyingBuiltin< T >::Type UnderlyingBuiltin_t
Auxiliary alias declaration for the UnderlyingBuiltin type trait.
Definition: UnderlyingBuiltin.h:117
decltype(auto) pow2(const Matrix< MT, SO > &m)
Computes the square for each single element of the matrix m.
Definition: Matrix.h:686
#define BLAZE_USER_ASSERT(expr, msg)
Run time assertion macro for user checks.
Definition: Assert.h:117
#define BLAZE_ALWAYS_INLINE
Platform dependent setup of an enforced inline keyword.
Definition: Inline.h:85
typename If< Condition >::template Type< T1, T2 > If_t
Auxiliary alias template for the If class template.
Definition: If.h:108
Header file for the pow2 shim.
System settings for the inline keywords.