Commit 3b6d4069cf4023611f41e78b6728910699ec2d30

Authored by Jürgen Knödlseder
1 parent ca5ca22e

Add print method and GLog logger for matrix output.

include/GMatrix.hpp
... ... @@ -28,6 +28,9 @@
28 28 #define GMATRIX_HPP
29 29  
30 30 /* __ Includes ___________________________________________________________ */
  31 +#include <string>
  32 +#include <iostream>
  33 +#include "GLog.hpp"
31 34 #include "GMatrixBase.hpp"
32 35  
33 36 /* __ Forward declarations _______________________________________________ */
... ... @@ -48,7 +51,8 @@ class GMatrix : public GMatrixBase {
48 51 friend GMatrix operator/ (const GMatrix& a, const double& b);
49 52  
50 53 // I/O friends
51   - friend std::ostream& operator<< (std::ostream& os, const GMatrix& m);
  54 + friend std::ostream& operator<<(std::ostream& os, const GMatrix& matrix);
  55 + friend GLog& operator<<(GLog& log, const GMatrix& matrix);
52 56  
53 57 // Friend functions
54 58 friend GMatrix transpose(const GMatrix& m);
... ... @@ -80,22 +84,23 @@ public:
80 84 GMatrix& operator/= (const double& s);
81 85  
82 86 // Methods
83   - void clear(void);
84   - void transpose(void);
85   - void invert(void);
86   - void add_col(const GVector& v, int col);
87   - void insert_col(const GVector& v, int col);
88   - GVector extract_row(int row) const;
89   - GVector extract_col(int col) const;
90   - GMatrix extract_lower_triangle(void) const;
91   - GMatrix extract_upper_triangle(void) const;
92   - double fill(void) const;
93   - double min(void) const;
94   - double max(void) const;
95   - double sum(void) const;
96   - void eulerx(const double& angle);
97   - void eulery(const double& angle);
98   - void eulerz(const double& angle);
  87 + void clear(void);
  88 + void transpose(void);
  89 + void invert(void);
  90 + void add_col(const GVector& v, int col);
  91 + void insert_col(const GVector& v, int col);
  92 + GVector extract_row(int row) const;
  93 + GVector extract_col(int col) const;
  94 + GMatrix extract_lower_triangle(void) const;
  95 + GMatrix extract_upper_triangle(void) const;
  96 + double fill(void) const;
  97 + double min(void) const;
  98 + double max(void) const;
  99 + double sum(void) const;
  100 + void eulerx(const double& angle);
  101 + void eulery(const double& angle);
  102 + void eulerz(const double& angle);
  103 + std::string print(void) const;
99 104  
100 105 private:
101 106 // Private methods
... ...
src/linalg/GMatrix.cpp
1 1 /***************************************************************************
2   - * GMatrix.cpp - Matrix class *
  2 + * GMatrix.cpp - General matrix class *
3 3 * ----------------------------------------------------------------------- *
4 4 * copyright (C) 2006-2012 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
... ... @@ -763,9 +763,40 @@ void GMatrix::eulerz(const double&amp; angle)
763 763 }
764 764  
765 765  
  766 +/***********************************************************************//**
  767 + * @brief Print matrix
  768 + ***************************************************************************/
  769 +std::string GMatrix::print(void) const
  770 +{
  771 + // Initialise result string
  772 + std::string result;
  773 +
  774 + // Append header
  775 + result.append("=== GMatrix ===");
  776 + result.append("\n"+parformat("Number of rows")+str(m_rows));
  777 + if (m_rowsel != NULL) {
  778 + result.append(" (compressed "+str(m_num_rowsel)+")");
  779 + }
  780 + result.append("\n"+parformat("Number of columns")+str(m_cols));
  781 + if (m_colsel != NULL) {
  782 + result.append(" (compressed "+str(m_num_colsel)+")");
  783 + }
  784 + result.append("\n"+parformat("Number of elements")+str(m_elements));
  785 + result.append("\n"+parformat("Number of allocated cells")+str(m_alloc));
  786 +
  787 + // Append elements and compression schemes
  788 + result.append(print_elements());
  789 + result.append(print_row_compression());
  790 + result.append(print_col_compression());
  791 +
  792 + // Return result
  793 + return result;
  794 +}
  795 +
  796 +
766 797 /*==========================================================================
767 798 = =
768   - = GMatrix private methods =
  799 + = Private methods =
769 800 = =
770 801 ==========================================================================*/
771 802  
... ... @@ -814,7 +845,7 @@ void GMatrix::constructor(int rows, int cols)
814 845  
815 846 /*==========================================================================
816 847 = =
817   - = GMatrix friends =
  848 + = Friend functions =
818 849 = =
819 850 ==========================================================================*/
820 851  
... ... @@ -822,29 +853,12 @@ void GMatrix::constructor(int rows, int cols)
822 853 * @brief Output operator
823 854 *
824 855 * @param[in] os Output stream.
825   - * @param[in] m Matrix to put in output stream.
  856 + * @param[in] matrix Matrix.
826 857 ***************************************************************************/
827   -std::ostream& operator<< (std::ostream& os, const GMatrix& m)
  858 +std::ostream& operator<< (std::ostream& os, const GMatrix& matrix)
828 859 {
829   - // Put header in stream
830   - os << "=== GMatrix ===" << std::endl;
831   - if (m.m_rowsel != NULL)
832   - os << " Number of rows ............: " << m.m_rows
833   - << " (compressed " << m.m_num_rowsel << ")" << std::endl;
834   - else
835   - os << " Number of rows ............: " << m.m_rows << std::endl;
836   - if (m.m_colsel != NULL)
837   - os << " Number of columns .........: " << m.m_cols
838   - << " (compressed " << m.m_num_colsel << ")" << std::endl;
839   - else
840   - os << " Number of columns .........: " << m.m_cols << std::endl;
841   - os << " Number of elements ........: " << m.m_elements << std::endl;
842   - os << " Number of allocated cells .: " << m.m_alloc << std::endl;
843   -
844   - // Dump elements and compression schemes
845   - m.dump_elements(os);
846   - m.dump_row_comp(os);
847   - m.dump_col_comp(os);
  860 + // Write matrix in output stream
  861 + os << matrix.print();
848 862  
849 863 // Return output stream
850 864 return os;
... ... @@ -852,18 +866,35 @@ std::ostream&amp; operator&lt;&lt; (std::ostream&amp; os, const GMatrix&amp; m)
852 866  
853 867  
854 868 /***********************************************************************//**
  869 + * @brief Log operator
  870 + *
  871 + * @param[in] log Logger.
  872 + * @param[in] matrix Matrix.
  873 + ***************************************************************************/
  874 +GLog& operator<< (GLog& log, const GMatrix& matrix)
  875 +{
  876 + // Write matrix into logger
  877 + log << matrix.print();
  878 +
  879 + // Return logger
  880 + return log;
  881 +}
  882 +
  883 +
  884 +/***********************************************************************//**
855 885 * @brief Return matrix with absolute values of all elements
856 886 *
857   - * @param[in] m Matrix for which absolute values are to be returned.
  887 + * @param[in] matrix Matrix.
858 888 ***************************************************************************/
859   -GMatrix abs(const GMatrix& m)
  889 +GMatrix abs(const GMatrix& matrix)
860 890 {
861 891 // Define result matrix
862   - GMatrix result = m;
  892 + GMatrix result = matrix;
863 893  
864 894 // Convert all elements to absolute values
865   - for (int i = 0; i < result.m_elements; ++i)
  895 + for (int i = 0; i < result.m_elements; ++i) {
866 896 result.m_data[i] = std::abs(result.m_data[i]);
  897 + }
867 898  
868 899 // Return result
869 900 return result;
... ...