~scottydelicious666/brewtarget/brewtarget

« back to all changes in this revision

Viewing changes to src/matrix.h

  • Committer: Philip Greggory Lee
  • Date: 2009-08-23 16:53:43 UTC
  • Revision ID: git-v1:f8d1a25135bd92f06c46c562293800e4faa42c61
Made a src/ and ui/ directory and moved everything.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * matrix.h is part of Brewtarget, and is Copyright Philip G. Lee
 
3
 * (rocketman768@gmail.com), 2009.
 
4
 *
 
5
 * Brewtarget is free software: you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation, either version 3 of the License, or
 
8
 * (at your option) any later version.
 
9
 
 
10
 * Brewtarget is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
 */
 
18
 
 
19
#ifndef _MATRIX_H
 
20
#define _MATRIX_H
 
21
 
 
22
#include <iostream>
 
23
#include <vector>
 
24
#include <cmath>
 
25
#include <exception>
 
26
 
 
27
#define EPSILON 0.00001
 
28
 
 
29
using namespace std;
 
30
 
 
31
//======================Class Defns.=============================
 
32
class Matrix;
 
33
class DimensionException;
 
34
class IncomputableException;
 
35
 
 
36
ostream& operator<<( ostream &os, const Matrix &rhs );
 
37
 
 
38
//======================Class: Matrix=============================
 
39
class Matrix
 
40
{
 
41
   friend ostream& operator<<( ostream &os, const Matrix &rhs );
 
42
 
 
43
   public:
 
44
      ~Matrix(); // Destructor
 
45
      Matrix( unsigned int rows, unsigned int cols ); // Constructor
 
46
      Matrix( const vector<Matrix> &colVec ); // Constructor
 
47
      Matrix( const Matrix &m, unsigned int colStart, unsigned int colEnd ); // Constructor
 
48
      Matrix( const Matrix &rhs ); // Copy constructor
 
49
      
 
50
      static Matrix getIdentity( unsigned int n ); // Gets n x n identity matrix.
 
51
      
 
52
      Matrix& operator=( const Matrix &rhs );
 
53
      Matrix& operator+=( const Matrix &rhs );
 
54
      Matrix& operator-=( const Matrix &rhs );
 
55
      const Matrix operator+( const Matrix &other ) const;
 
56
      const Matrix operator-( const Matrix &other ) const;
 
57
      const Matrix operator*( const Matrix &rhs ) const;
 
58
      Matrix getRow( unsigned int row ) const;
 
59
      Matrix getCol( unsigned int col ) const;
 
60
      unsigned int getRows() const;
 
61
      unsigned int getCols() const;
 
62
      inline double getVal( unsigned int row, unsigned int col ) const;
 
63
      inline void setVal( unsigned int row, unsigned int col, double val );
 
64
      void setRow( unsigned int row, vector<double> vec );
 
65
      void setCol( unsigned int col, vector<double> vec );
 
66
      Matrix inverse() const;
 
67
      bool hasInverse() const;
 
68
      
 
69
      void rref();
 
70
      bool hasNonZeroDiags() const;
 
71
      void swapRows( unsigned int row1, unsigned int row2 );
 
72
      void appendCols( const Matrix& other );
 
73
      
 
74
   private:
 
75
      unsigned int _rows;
 
76
      unsigned int _cols;
 
77
      double *_data;
 
78
};
 
79
 
 
80
//======================Class: DimensionException=============================
 
81
class DimensionException: public exception
 
82
{
 
83
   virtual const char* what() const throw()
 
84
   {
 
85
      return "Dimensions of argument were not expected.";
 
86
   }
 
87
   
 
88
   public:
 
89
      DimensionException(unsigned int argRows, unsigned int argCols, bool rowsMatter, bool colsMatter )
 
90
      {
 
91
         _argRows = argRows;
 
92
         _argCols = argCols;
 
93
         _rowsMatter = rowsMatter;
 
94
         _colsMatter = colsMatter;
 
95
      }
 
96
      
 
97
      bool colsMatter(){ return _colsMatter; }
 
98
      bool rowsMatter(){ return _rowsMatter; }
 
99
      unsigned int getArgRows(){ return _argRows; }
 
100
      unsigned int getArgCols(){ return _argCols; }
 
101
      
 
102
   private:
 
103
      unsigned int _argRows;
 
104
      unsigned int _argCols;
 
105
      bool _rowsMatter;
 
106
      bool _colsMatter;
 
107
};
 
108
 
 
109
//======================Class: IncomputableException=============================
 
110
class IncomputableException: public exception
 
111
{
 
112
   virtual const char* what() const throw()
 
113
   {
 
114
      return "Could not compute what you asked.";
 
115
   }
 
116
};
 
117
 
 
118
#endif
 
119