File: Matrix.h

package info (click to toggle)
audacity 3.2.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 106,704 kB
  • sloc: cpp: 277,038; ansic: 73,623; lisp: 7,761; python: 3,305; sh: 2,715; perl: 821; xml: 275; makefile: 119
file content (113 lines) | stat: -rw-r--r-- 3,152 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**********************************************************************

  Audacity: A Digital Audio Editor

  Matrix.h

  Dominic Mazzoni

*******************************************************************//*!

\file Matrix.h
\brief Holds both the Matrix and Vector classes, supporting
  linear algebra operations, including matrix inversion.
  Used by InterpolateAudio.

\class Matrix
\brief Holds a matrix of doubles and supports arithmetic, subsetting,
  and matrix inversion.  Used by InterpolateAudio.

\class Vector
\brief Holds a matrix of doubles and supports arithmetic operations,
  including Vector-Matrix operations.  Used by InterpolateAudio.

*//*******************************************************************/

#ifndef __AUDACITY_MATRIX__
#define __AUDACITY_MATRIX__

#include "SampleFormat.h"

class Matrix;

class Vector
{
 public:
   Vector();
   Vector(const Vector& copyFrom);
   Vector(unsigned len, double *data=NULL);
   Vector(unsigned len, float *data);
   Vector& operator=(const Vector &other);
   ~Vector();

   void Reinit(unsigned len);
   void Swap(Vector &that);

   inline double& operator[](unsigned i) { return mData[i]; }
   inline double operator[](unsigned i) const { return mData[i]; }
   inline unsigned Len() const { return mN; }

   double Sum() const;

 private:
   unsigned mN{ 0 };
   Doubles mData;
};

class Matrix
{
 public:
   Matrix(const Matrix& copyFrom);
   Matrix(unsigned rows, unsigned cols, double **data=NULL);
   ~Matrix();

   Matrix& operator=(const Matrix& other);

   inline Vector& operator[](unsigned i) { return mRowVec[i]; }
   inline Vector& operator[](unsigned i) const { return mRowVec[i]; }
   inline unsigned Rows() const { return mRows; }
   inline unsigned Cols() const { return mCols; }

   void SwapRows(unsigned i, unsigned j);

 private:
   void CopyFrom(const Matrix& other);

   unsigned mRows;
   unsigned mCols;
   ArrayOf<Vector> mRowVec;
};

bool InvertMatrix(const Matrix& input, Matrix& Minv);

Matrix TransposeMatrix(const Matrix& M);

Matrix IdentityMatrix(unsigned N);

Vector operator+(const Vector &left, const Vector &right);
Vector operator-(const Vector &left, const Vector &right);
Vector operator*(const Vector &left, const Vector &right);
Vector operator*(const Vector &left, double right);

Vector VectorSubset(const Vector &other, unsigned start, unsigned len);
Vector VectorConcatenate(const Vector& left, const Vector& right);

Vector operator*(const Vector &left, const Matrix &right);
Vector operator*(const Matrix &left, const Vector &right);

Matrix operator+(const Matrix &left, const Matrix &right);
Matrix operator*(const Matrix &left, const double right);

// No operator* on matrices due to ambiguity
Matrix ScalarMultiply(const Matrix &left, const Matrix &right);
Matrix MatrixMultiply(const Matrix &left, const Matrix &right);

Matrix MatrixSubset(const Matrix &M,
                    unsigned startRow, unsigned numRows,
                    unsigned startCol, unsigned numCols);

Matrix MatrixConcatenateCols(const Matrix& left, const Matrix& right);

bool InvertMatrix(const Matrix& M, Matrix& Minv);

#endif // __AUDACITY_MATRIX__