1 /**********************************************************************
2 
3   Audacity: A Digital Audio Editor
4 
5   Matrix.h
6 
7   Dominic Mazzoni
8 
9 *******************************************************************//*!
10 
11 \file Matrix.h
12 \brief Holds both the Matrix and Vector classes, supporting
13   linear algebra operations, including matrix inversion.
14   Used by InterpolateAudio.
15 
16 \class Matrix
17 \brief Holds a matrix of doubles and supports arithmetic, subsetting,
18   and matrix inversion.  Used by InterpolateAudio.
19 
20 \class Vector
21 \brief Holds a matrix of doubles and supports arithmetic operations,
22   including Vector-Matrix operations.  Used by InterpolateAudio.
23 
24 *//*******************************************************************/
25 
26 #ifndef __AUDACITY_MATRIX__
27 #define __AUDACITY_MATRIX__
28 
29 #include "SampleFormat.h"
30 
31 class Matrix;
32 
33 class Vector
34 {
35  public:
36    Vector();
37    Vector(const Vector& copyFrom);
38    Vector(unsigned len, double *data=NULL);
39    Vector(unsigned len, float *data);
40    Vector& operator=(const Vector &other);
41    ~Vector();
42 
43    void Reinit(unsigned len);
44    void Swap(Vector &that);
45 
46    inline double& operator[](unsigned i) { return mData[i]; }
47    inline double operator[](unsigned i) const { return mData[i]; }
Len()48    inline unsigned Len() const { return mN; }
49 
50    double Sum() const;
51 
52  private:
53    unsigned mN{ 0 };
54    Doubles mData;
55 };
56 
57 class Matrix
58 {
59  public:
60    Matrix(const Matrix& copyFrom);
61    Matrix(unsigned rows, unsigned cols, double **data=NULL);
62    ~Matrix();
63 
64    Matrix& operator=(const Matrix& other);
65 
66    inline Vector& operator[](unsigned i) { return mRowVec[i]; }
67    inline Vector& operator[](unsigned i) const { return mRowVec[i]; }
Rows()68    inline unsigned Rows() const { return mRows; }
Cols()69    inline unsigned Cols() const { return mCols; }
70 
71    void SwapRows(unsigned i, unsigned j);
72 
73  private:
74    void CopyFrom(const Matrix& other);
75 
76    unsigned mRows;
77    unsigned mCols;
78    ArrayOf<Vector> mRowVec;
79 };
80 
81 bool InvertMatrix(const Matrix& input, Matrix& Minv);
82 
83 Matrix TransposeMatrix(const Matrix& M);
84 
85 Matrix IdentityMatrix(unsigned N);
86 
87 Vector operator+(const Vector &left, const Vector &right);
88 Vector operator-(const Vector &left, const Vector &right);
89 Vector operator*(const Vector &left, const Vector &right);
90 Vector operator*(const Vector &left, double right);
91 
92 Vector VectorSubset(const Vector &other, unsigned start, unsigned len);
93 Vector VectorConcatenate(const Vector& left, const Vector& right);
94 
95 Vector operator*(const Vector &left, const Matrix &right);
96 Vector operator*(const Matrix &left, const Vector &right);
97 
98 Matrix operator+(const Matrix &left, const Matrix &right);
99 Matrix operator*(const Matrix &left, const double right);
100 
101 // No operator* on matrices due to ambiguity
102 Matrix ScalarMultiply(const Matrix &left, const Matrix &right);
103 Matrix MatrixMultiply(const Matrix &left, const Matrix &right);
104 
105 Matrix MatrixSubset(const Matrix &M,
106                     unsigned startRow, unsigned numRows,
107                     unsigned startCol, unsigned numCols);
108 
109 Matrix MatrixConcatenateCols(const Matrix& left, const Matrix& right);
110 
111 bool InvertMatrix(const Matrix& M, Matrix& Minv);
112 
113 #endif // __AUDACITY_MATRIX__
114