HermitianValue.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_ADAPTORS_HERMITIANMATRIX_HERMITIANVALUE_H_
36 #define _BLAZE_MATH_ADAPTORS_HERMITIANMATRIX_HERMITIANVALUE_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
49 #include <blaze/math/proxy/Proxy.h>
50 #include <blaze/math/shims/Clear.h>
54 #include <blaze/math/shims/IsNaN.h>
55 #include <blaze/math/shims/IsOne.h>
58 #include <blaze/math/shims/Reset.h>
66 #include <blaze/util/Exception.h>
67 #include <blaze/util/InvalidType.h>
68 #include <blaze/util/mpl/If.h>
69 #include <blaze/util/Types.h>
71 
72 
73 namespace blaze {
74 
75 //=================================================================================================
76 //
77 // CLASS DEFINITION
78 //
79 //=================================================================================================
80 
81 //*************************************************************************************************
115 template< typename MT > // Type of the adapted matrix
116 class HermitianValue : public Proxy< HermitianValue<MT> >
117 {
118  private:
119  //**Type definitions****************************************************************************
120  typedef typename MT::Iterator IteratorType;
121  //**********************************************************************************************
122 
123  //**struct BuiltinType**************************************************************************
127  template< typename T >
128  struct BuiltinType { typedef INVALID_TYPE Type; };
130  //**********************************************************************************************
131 
132  //**struct ComplexType**************************************************************************
136  template< typename T >
137  struct ComplexType { typedef typename T::value_type Type; };
139  //**********************************************************************************************
140 
141  public:
142  //**Type definitions****************************************************************************
143  typedef typename MT::ElementType RepresentedType;
144 
146  typedef typename If< IsComplex<RepresentedType>
147  , ComplexType<RepresentedType>
148  , BuiltinType<RepresentedType> >::Type::Type ValueType;
149 
150  typedef ValueType value_type;
151  //**********************************************************************************************
152 
153  //**Constructors********************************************************************************
156  inline HermitianValue( IteratorType pos, MT* matrix, size_t index );
158  //**********************************************************************************************
159 
160  //**Assignment operators************************************************************************
163  inline HermitianValue& operator= ( const HermitianValue& hv );
164  template< typename T > inline HermitianValue& operator= ( const T& value );
165  template< typename T > inline HermitianValue& operator+=( const T& value );
166  template< typename T > inline HermitianValue& operator-=( const T& value );
167  template< typename T > inline HermitianValue& operator*=( const T& value );
168  template< typename T > inline HermitianValue& operator/=( const T& value );
170  //**********************************************************************************************
171 
172  //**Utility functions***************************************************************************
175  inline void reset () const;
176  inline void clear () const;
177  inline void invert() const;
178 
179  inline RepresentedType get() const;
181  //**********************************************************************************************
182 
183  //**Conversion operator*************************************************************************
186  inline operator RepresentedType() const;
188  //**********************************************************************************************
189 
190  //**Complex data access functions***************************************************************
193  inline ValueType real() const;
194  inline void real( ValueType value ) const;
195  inline ValueType imag() const;
196  inline void imag( ValueType value ) const;
198  //**********************************************************************************************
199 
200  private:
201  //**Utility functions***************************************************************************
204  inline void sync() const;
206  //**********************************************************************************************
207 
208  //**Member variables****************************************************************************
209  IteratorType pos_;
210  MT* matrix_;
211  size_t index_;
212  //**********************************************************************************************
213 
214  //**Compile time checks*************************************************************************
226  BLAZE_CONSTRAINT_MUST_BE_NUMERIC_TYPE( RepresentedType );
228  //**********************************************************************************************
229 };
230 //*************************************************************************************************
231 
232 
233 
234 
235 //=================================================================================================
236 //
237 // CONSTRUCTORS
238 //
239 //=================================================================================================
240 
241 //*************************************************************************************************
248 template< typename MT > // Type of the adapted matrix
249 inline HermitianValue<MT>::HermitianValue( IteratorType pos, MT* matrix, size_t index )
250  : pos_ ( pos ) // Iterator to the current sparse Hermitian matrix element
251  , matrix_( matrix ) // The sparse matrix containing the iterator
252  , index_ ( index ) // The row/column index of the iterator
253 {}
254 //*************************************************************************************************
255 
256 
257 
258 
259 //=================================================================================================
260 //
261 // OPERATORS
262 //
263 //=================================================================================================
264 
265 //*************************************************************************************************
272 template< typename MT > // Type of the adapted matrix
274 {
275  const bool isDiagonal( pos_->index() == index );
276 
277  if( IsComplex<RepresentedType>::value && isDiagonal && !isReal( hv.pos_->value() ) ) {
278  BLAZE_THROW_INVALID_ARGUMENT( "Invalid assignment to diagonal matrix element" );
279  }
280 
281  pos_->value() = hv.pos_->value();
282  sync();
283  return *this;
284 }
285 //*************************************************************************************************
286 
287 
288 //*************************************************************************************************
295 template< typename MT > // Type of the adapted matrix
296 template< typename T > // Type of the right-hand side value
298 {
299  const bool isDiagonal( pos_->index() == index );
300 
301  if( IsComplex<RepresentedType>::value && isDiagonal && !isReal( value ) ) {
302  BLAZE_THROW_INVALID_ARGUMENT( "Invalid assignment to diagonal matrix element" );
303  }
304 
305  pos_->value() = value;
306  sync();
307  return *this;
308 }
309 //*************************************************************************************************
310 
311 
312 //*************************************************************************************************
319 template< typename MT > // Type of the adapted matrix
320 template< typename T > // Type of the right-hand side value
322 {
323  const bool isDiagonal( pos_->index() == index );
324 
325  if( IsComplex<RepresentedType>::value && isDiagonal && !isReal( value ) ) {
326  BLAZE_THROW_INVALID_ARGUMENT( "Invalid assignment to diagonal matrix element" );
327  }
328 
329  pos_->value() += value;
330  sync();
331  return *this;
332 }
333 //*************************************************************************************************
334 
335 
336 //*************************************************************************************************
343 template< typename MT > // Type of the adapted matrix
344 template< typename T > // Type of the right-hand side value
346 {
347  const bool isDiagonal( pos_->index() == index );
348 
349  if( IsComplex<RepresentedType>::value && isDiagonal && !isReal( value ) ) {
350  BLAZE_THROW_INVALID_ARGUMENT( "Invalid assignment to diagonal matrix element" );
351  }
352 
353  pos_->value() -= value;
354  sync();
355  return *this;
356 }
357 //*************************************************************************************************
358 
359 
360 //*************************************************************************************************
367 template< typename MT > // Type of the adapted matrix
368 template< typename T > // Type of the right-hand side value
370 {
371  const bool isDiagonal( pos_->index() == index );
372 
373  if( IsComplex<RepresentedType>::value && isDiagonal && !isReal( value ) ) {
374  BLAZE_THROW_INVALID_ARGUMENT( "Invalid assignment to diagonal matrix element" );
375  }
376 
377  pos_->value() *= value;
378  sync();
379  return *this;
380 }
381 //*************************************************************************************************
382 
383 
384 //*************************************************************************************************
391 template< typename MT > // Type of the adapted matrix
392 template< typename T > // Type of the right-hand side value
394 {
395  const bool isDiagonal( pos_->index() == index );
396 
397  if( IsComplex<RepresentedType>::value && isDiagonal && !isReal( value ) ) {
398  BLAZE_THROW_INVALID_ARGUMENT( "Invalid assignment to diagonal matrix element" );
399  }
400 
401  pos_->value() /= value;
402  sync();
403  return *this;
404 }
405 //*************************************************************************************************
406 
407 
408 
409 
410 //=================================================================================================
411 //
412 // UTILITY FUNCTIONS
413 //
414 //=================================================================================================
415 
416 //*************************************************************************************************
423 template< typename MT > // Type of the adapted matrix
424 inline void HermitianValue<MT>::reset() const
425 {
426  using blaze::reset;
427 
428  reset( pos_->value() );
429 
430  if( pos_->index() != index_ )
431  {
432  const size_t row ( ( IsRowMajorMatrix<MT>::value )?( pos_->index() ):( index_ ) );
433  const size_t column( ( IsRowMajorMatrix<MT>::value )?( index_ ):( pos_->index() ) );
434  const IteratorType pos2( matrix_->find( row, column ) );
435 
436  reset( pos2->value() );
437  }
438 }
439 //*************************************************************************************************
440 
441 
442 //*************************************************************************************************
449 template< typename MT > // Type of the adapted matrix
450 inline void HermitianValue<MT>::clear() const
451 {
452  using blaze::clear;
453 
454  clear( pos_->value() );
455 
456  if( pos_->index() != index_ )
457  {
458  const size_t row ( ( IsRowMajorMatrix<MT>::value )?( pos_->index() ):( index_ ) );
459  const size_t column( ( IsRowMajorMatrix<MT>::value )?( index_ ):( pos_->index() ) );
460  const IteratorType pos2( matrix_->find( row, column ) );
461 
462  clear( pos2->value() );
463  }
464 }
465 //*************************************************************************************************
466 
467 
468 //*************************************************************************************************
473 template< typename MT > // Type of the adapted matrix
474 inline void HermitianValue<MT>::invert() const
475 {
476  using blaze::invert;
477 
478  invert( pos_->value() );
479 
480  if( pos_->index() != index_ )
481  {
482  const size_t row ( ( IsRowMajorMatrix<MT>::value )?( pos_->index() ):( index_ ) );
483  const size_t column( ( IsRowMajorMatrix<MT>::value )?( index_ ):( pos_->index() ) );
484  const IteratorType pos2( matrix_->find( row, column ) );
485 
486  pos2->value() = conj( pos_->value() );
487  }
488 }
489 //*************************************************************************************************
490 
491 
492 //*************************************************************************************************
497 template< typename MT > // Type of the adapted matrix
499 {
500  return pos_->value();
501 }
502 //*************************************************************************************************
503 
504 
505 //*************************************************************************************************
510 template< typename MT > // Type of the adapted matrix
511 inline void HermitianValue<MT>::sync() const
512 {
513  if( pos_->index() == index_ || isDefault( pos_->value() ) )
514  return;
515 
516  const size_t row ( ( IsRowMajorMatrix<MT>::value )?( pos_->index() ):( index_ ) );
517  const size_t column( ( IsRowMajorMatrix<MT>::value )?( index_ ):( pos_->index() ) );
518 
519  matrix_->set( row, column, conj( pos_->value() ) );
520 }
521 //*************************************************************************************************
522 
523 
524 
525 
526 //=================================================================================================
527 //
528 // CONVERSION OPERATOR
529 //
530 //=================================================================================================
531 
532 //*************************************************************************************************
537 template< typename MT > // Type of the adapted matrix
539 {
540  return pos_->value();
541 }
542 //*************************************************************************************************
543 
544 
545 
546 
547 //=================================================================================================
548 //
549 // COMPLEX DATA ACCESS FUNCTIONS
550 //
551 //=================================================================================================
552 
553 //*************************************************************************************************
561 template< typename MT > // Type of the adapted matrix
563 {
564  return pos_->value().real();
565 }
566 //*************************************************************************************************
567 
568 
569 //*************************************************************************************************
578 template< typename MT > // Type of the adapted matrix
579 inline void HermitianValue<MT>::real( ValueType value ) const
580 {
581  pos_->value().real() = value;
582  sync();
583 }
584 //*************************************************************************************************
585 
586 
587 //*************************************************************************************************
595 template< typename MT > // Type of the adapted matrix
597 {
598  return pos_->value.imag();
599 }
600 //*************************************************************************************************
601 
602 
603 //*************************************************************************************************
612 template< typename MT > // Type of the adapted matrix
613 inline void HermitianValue<MT>::imag( ValueType value ) const
614 {
615  pos_->value().imag( value );
616  sync();
617 }
618 //*************************************************************************************************
619 
620 
621 
622 
623 //=================================================================================================
624 //
625 // GLOBAL FUNCTIONS
626 //
627 //=================================================================================================
628 
629 //*************************************************************************************************
632 template< typename MT >
634  conj( const HermitianValue<MT>& value );
635 
636 template< typename MT >
637 inline void reset( const HermitianValue<MT>& value );
638 
639 template< typename MT >
640 inline void clear( const HermitianValue<MT>& value );
641 
642 template< typename MT >
643 inline void invert( const HermitianValue<MT>& value );
644 
645 template< typename MT >
646 inline bool isDefault( const HermitianValue<MT>& value );
647 
648 template< typename MT >
649 inline bool isReal( const HermitianValue<MT>& value );
650 
651 template< typename MT >
652 inline bool isZero( const HermitianValue<MT>& value );
653 
654 template< typename MT >
655 inline bool isOne( const HermitianValue<MT>& value );
656 
657 template< typename MT >
658 inline bool isnan( const HermitianValue<MT>& value );
660 //*************************************************************************************************
661 
662 
663 //*************************************************************************************************
674 template< typename MT >
676  conj( const HermitianValue<MT>& value )
677 {
678  using blaze::conj;
679 
680  return conj( (~value).get() );
681 }
682 //*************************************************************************************************
683 
684 
685 //*************************************************************************************************
694 template< typename MT >
695 inline void reset( const HermitianValue<MT>& value )
696 {
697  value.reset();
698 }
699 //*************************************************************************************************
700 
701 
702 //*************************************************************************************************
711 template< typename MT >
712 inline void clear( const HermitianValue<MT>& value )
713 {
714  value.clear();
715 }
716 //*************************************************************************************************
717 
718 
719 //*************************************************************************************************
726 template< typename MT >
727 inline void invert( const HermitianValue<MT>& value )
728 {
729  value.invert();
730 }
731 //*************************************************************************************************
732 
733 
734 //*************************************************************************************************
744 template< typename MT >
745 inline bool isDefault( const HermitianValue<MT>& value )
746 {
747  using blaze::isDefault;
748 
749  return isDefault( value.get() );
750 }
751 //*************************************************************************************************
752 
753 
754 //*************************************************************************************************
766 template< typename MT >
767 inline bool isReal( const HermitianValue<MT>& value )
768 {
769  using blaze::isReal;
770 
771  return isReal( value.get() );
772 }
773 //*************************************************************************************************
774 
775 
776 //*************************************************************************************************
786 template< typename MT >
787 inline bool isZero( const HermitianValue<MT>& value )
788 {
789  using blaze::isZero;
790 
791  return isZero( value.get() );
792 }
793 //*************************************************************************************************
794 
795 
796 //*************************************************************************************************
806 template< typename MT >
807 inline bool isOne( const HermitianValue<MT>& value )
808 {
809  using blaze::isOne;
810 
811  return isOne( value.get() );
812 }
813 //*************************************************************************************************
814 
815 
816 //*************************************************************************************************
826 template< typename MT >
827 inline bool isnan( const HermitianValue<MT>& value )
828 {
829  using blaze::isnan;
830 
831  return isnan( value.get() );
832 }
833 //*************************************************************************************************
834 
835 } // namespace blaze
836 
837 #endif
Header file for the isnan shim.
IteratorType pos_
Iterator to the current sparse Hermitian matrix element.
Definition: HermitianValue.h:209
#define BLAZE_CONSTRAINT_MUST_NOT_BE_CONST(T)
Constraint on the data type.In case the given data type is a const-qualified type, a compilation error is created.
Definition: Const.h:116
#define BLAZE_THROW_INVALID_ARGUMENT(MESSAGE)
Macro for the emission of a std::invalid_argument exceptionThis macro encapsulates the default way of...
Definition: Exception.h:187
bool isOne(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is 1.
Definition: DiagonalProxy.h:609
Constraint on the data type.
bool isDiagonal(const DenseMatrix< MT, SO > &dm)
Checks if the give dense matrix is diagonal.
Definition: DenseMatrix.h:1511
Compile time type selection.The If class template selects one of the two given types T2 and T3 depend...
Definition: If.h:112
Header file for basic type definitions.
ValueType imag() const
Returns the imaginary part of the represented complex number.
Definition: HermitianValue.h:596
Proxy base class.The Proxy class is a base class for all proxy classes within the Blaze library that ...
Definition: Forward.h:51
bool isReal(const DiagonalProxy< MT > &proxy)
Returns whether the matrix element represents a real number.
Definition: DiagonalProxy.h:569
ValueType real() const
Returns the real part of the represented complex number.
Definition: HermitianValue.h:562
Constraint on the data type.
void reset(const DiagonalProxy< MT > &proxy)
Resetting the represented element to the default initial values.
Definition: DiagonalProxy.h:507
void sync() const
Synchronization of the current sparse element to the according paired element.
Definition: HermitianValue.h:511
DisableIf< Or< IsComputation< MT >, IsTransExpr< MT > >, typename ColumnExprTrait< MT >::Type >::Type column(Matrix< MT, SO > &matrix, size_t index)
Creating a view on a specific column of the given matrix.
Definition: Column.h:107
#define BLAZE_CONSTRAINT_MUST_NOT_BE_VOLATILE(T)
Constraint on the data type.In case the given data type is a volatile-qualified type, a compilation error is created.
Definition: Volatile.h:116
Header file for the invert shim.
size_t index_
The row/column index of the iterator.
Definition: HermitianValue.h:211
Constraint on the data type.
Header file for the Proxy class.
bool isDefault(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is in default state.
Definition: DiagonalProxy.h:547
Constraint on the data type.
Constraint on the data type.
ConjExprTrait< typename DiagonalProxy< MT >::RepresentedType >::Type conj(const DiagonalProxy< MT > &proxy)
Computing the complex conjugate of the represented element.
Definition: DiagonalProxy.h:487
MT::Iterator IteratorType
Type of the underlying sparse matrix iterators.
Definition: HermitianValue.h:120
RepresentedType get() const
Access to the represented value.
Definition: HermitianValue.h:498
Constraint on the data type.
void invert(const HermitianProxy< MT > &proxy)
In-place inversion of the represented element.
Definition: HermitianProxy.h:767
MT::ElementType RepresentedType
Type of the represented matrix element.
Definition: HermitianValue.h:143
Header file for the clear shim.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
Header file for the If class template.
Compile time check for row-major matrix types.This type trait tests whether or not the given template...
Definition: IsRowMajorMatrix.h:110
#define BLAZE_CONSTRAINT_MUST_NOT_BE_POINTER_TYPE(T)
Constraint on the data type.In case the given data type T is not a pointer type, a compilation error ...
Definition: Pointer.h:116
HermitianValue & operator=(const HermitianValue &hv)
Copy assignment operator for HermitianValue.
Definition: HermitianValue.h:273
ValueType value_type
Value type of the represented complex element.
Definition: HermitianValue.h:150
Header file for the isZero shim.
Constraint on the data type.
If< IsComplex< RepresentedType >, ComplexType< RepresentedType >, BuiltinType< RepresentedType > >::Type::Type ValueType
Value type of the represented complex element.
Definition: HermitianValue.h:148
bool isnan(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is not a number.
Definition: DiagonalProxy.h:629
#define BLAZE_CONSTRAINT_MUST_NOT_BE_UPPER_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is a upper triangular matrix type...
Definition: Upper.h:118
void clear() const
Clearing the Hermitian value.
Definition: HermitianValue.h:450
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:2586
void clear(const DiagonalProxy< MT > &proxy)
Clearing the represented element.
Definition: DiagonalProxy.h:527
Representation of two synchronized values within a sparse Hermitian matrix.The HermitianValue class r...
Definition: HermitianValue.h:116
Header file for the isOne shim.
Header file for the conjugate shim.
DisableIf< Or< IsComputation< MT >, IsTransExpr< MT > >, typename RowExprTrait< MT >::Type >::Type row(Matrix< MT, SO > &matrix, size_t index)
Creating a view on a specific row of the given matrix.
Definition: Row.h:107
Evaluation of the return type of a complex conjugate expression.Via this type trait it is possible to...
Definition: ConjExprTrait.h:87
#define BLAZE_CONSTRAINT_MUST_NOT_BE_SYMMETRIC_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is a symmetric matrix type, a compilation error is created.
Definition: Symmetric.h:116
Utility type for generic codes.
Constraint on the data type.
Constraint on the data type.
#define BLAZE_CONSTRAINT_MUST_BE_NUMERIC_TYPE(T)
Constraint on the data type.In case the given data type T is not a numeric (integral or floating poin...
Definition: Numeric.h:79
Header file for the reset shim.
#define BLAZE_CONSTRAINT_MUST_NOT_BE_LOWER_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is a lower triangular matrix type...
Definition: Lower.h:118
#define BLAZE_CONSTRAINT_MUST_NOT_BE_REFERENCE_TYPE(T)
Constraint on the data type.In case the given data type T is not a reference type, a compilation error is created.
Definition: Reference.h:116
Element * Iterator
Iterator over non-constant elements.
Definition: CompressedMatrix.h:2591
Header file for the isDefault shim.
void reset() const
Reset the Hermitian value to its default initial value.
Definition: HermitianValue.h:424
Constraint on the data type.
Constraint on the data type.
Compile time check for complex types.This type trait tests whether or not the given template paramete...
Definition: IsComplex.h:76
Header file for the IsRowMajorMatrix type trait.
HermitianValue(IteratorType pos, MT *matrix, size_t index)
Constructor for the HermitianValue class.
Definition: HermitianValue.h:249
void invert() const
In-place inversion of the Hermitian value.
Definition: HermitianValue.h:474
#define BLAZE_CONSTRAINT_MUST_NOT_BE_EXPRESSION_TYPE(T)
Constraint on the data type.In case the given data type T is an expression (i.e. a type derived from ...
Definition: Expression.h:118
bool isZero(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is 0.
Definition: DiagonalProxy.h:589
Header file for the IsComplex type trait.
#define BLAZE_CONSTRAINT_MUST_NOT_BE_HERMITIAN_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is an Hermitian matrix type, a compilation error is created.
Definition: Hermitian.h:116
Header file for exception macros.
MT * matrix_
The sparse matrix containing the iterator.
Definition: HermitianValue.h:210
Header file for the ConjExprTrait class template.
Header file for the isReal shim.
#define BLAZE_CONSTRAINT_MUST_BE_SPARSE_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is not a sparse, N-dimensional matrix type...
Definition: SparseMatrix.h:79