mirror of
https://github.com/cookiengineer/audacity
synced 2026-02-09 05:01:57 +01:00
Remove naked new[] in: linear algebra functions for Repair
This commit is contained in:
214
src/Matrix.cpp
214
src/Matrix.cpp
@@ -17,89 +17,74 @@
|
||||
|
||||
Vector::Vector()
|
||||
{
|
||||
mCopy = false;
|
||||
mN = 0;
|
||||
mData = NULL;
|
||||
}
|
||||
|
||||
Vector::Vector(int len, double *data, bool copy)
|
||||
Vector::Vector(unsigned len, double *data)
|
||||
: mN{ len }
|
||||
, mData(len)
|
||||
{
|
||||
mN = len;
|
||||
mCopy = copy;
|
||||
if (mCopy || !data) {
|
||||
mCopy = true;
|
||||
mData = new double[mN];
|
||||
int i;
|
||||
for(i=0; i<mN; i++)
|
||||
if (data)
|
||||
mData[i] = data[i];
|
||||
else
|
||||
mData[i] = 0.0;
|
||||
}
|
||||
else {
|
||||
mCopy = false;
|
||||
mData = data;
|
||||
}
|
||||
if (data)
|
||||
std::copy(data, data + len, mData.get());
|
||||
else
|
||||
std::fill(mData.get(), mData.get() + len, 0.0);
|
||||
}
|
||||
|
||||
Vector::Vector(unsigned len, float *data)
|
||||
: mN{ len }
|
||||
, mData{ len }
|
||||
{
|
||||
if (data)
|
||||
std::copy(data, data + len, mData.get());
|
||||
else
|
||||
std::fill(mData.get(), mData.get() + len, 0.0);
|
||||
}
|
||||
|
||||
Vector& Vector::operator=(const Vector &other)
|
||||
{
|
||||
wxASSERT(Len() == other.Len());
|
||||
int i;
|
||||
for(i=0; i<Len(); i++)
|
||||
mData[i] = other.mData[i];
|
||||
std::copy(other.mData.get(), other.mData.get() + mN, mData.get());
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector::Vector(const Vector &other)
|
||||
: mN{ other.Len() }
|
||||
, mData{ mN }
|
||||
{
|
||||
CopyFrom(other);
|
||||
}
|
||||
|
||||
void Vector::CopyFrom(const Vector &other)
|
||||
{
|
||||
mN = other.Len();
|
||||
mCopy = true;
|
||||
mData = new double[mN];
|
||||
int i;
|
||||
for(i=0; i<mN; i++)
|
||||
mData[i] = other.mData[i];
|
||||
std::copy(other.mData.get(), other.mData.get() + mN, mData.get());
|
||||
}
|
||||
|
||||
Vector::~Vector()
|
||||
{
|
||||
if (mCopy)
|
||||
delete[] mData;
|
||||
}
|
||||
|
||||
Vector::Vector(int len, float *data)
|
||||
void Vector::Reinit(unsigned len)
|
||||
{
|
||||
mCopy = true;
|
||||
mN = len;
|
||||
mData = new double[mN];
|
||||
int i;
|
||||
for(i=0; i<mN; i++)
|
||||
mData[i] = (double)data[i];
|
||||
Vector temp(len);
|
||||
Swap(temp);
|
||||
}
|
||||
|
||||
void Vector::Swap(Vector &that)
|
||||
{
|
||||
std::swap(mN, that.mN);
|
||||
mData.swap(that.mData);
|
||||
}
|
||||
|
||||
double Vector::Sum() const
|
||||
{
|
||||
int i;
|
||||
double sum = 0.0;
|
||||
for(i=0; i<Len(); i++)
|
||||
for(unsigned i = 0; i < Len(); i++)
|
||||
sum += mData[i];
|
||||
return sum;
|
||||
}
|
||||
|
||||
Matrix::Matrix(int rows, int cols, double **data)
|
||||
Matrix::Matrix(unsigned rows, unsigned cols, double **data)
|
||||
: mRows{ rows }
|
||||
, mCols{ cols }
|
||||
, mRowVec{ mRows }
|
||||
{
|
||||
mRows = rows;
|
||||
mCols = cols;
|
||||
mRowVec = new Vector *[mRows];
|
||||
int i, j;
|
||||
for(i=0; i<mRows; i++) {
|
||||
mRowVec[i] = new Vector(mCols);
|
||||
for(j=0; j<mCols; j++) {
|
||||
for(unsigned i = 0; i < mRows; i++) {
|
||||
mRowVec[i].Reinit( mCols );
|
||||
for(unsigned j = 0; j < mCols; j++) {
|
||||
if (data)
|
||||
(*this)[i][j] = data[i][j];
|
||||
else
|
||||
@@ -123,44 +108,26 @@ void Matrix::CopyFrom(const Matrix &other)
|
||||
{
|
||||
mRows = other.mRows;
|
||||
mCols = other.mCols;
|
||||
mRowVec = new Vector *[mRows];
|
||||
int i;
|
||||
for(i=0; i<mRows; i++) {
|
||||
mRowVec[i] = new Vector(mCols);
|
||||
*mRowVec[i] = *other.mRowVec[i];
|
||||
mRowVec.reinit(mRows);
|
||||
for (unsigned i = 0; i < mRows; i++) {
|
||||
mRowVec[i].Reinit( mCols );
|
||||
mRowVec[i] = other.mRowVec[i];
|
||||
}
|
||||
}
|
||||
|
||||
Matrix::~Matrix()
|
||||
{
|
||||
int i;
|
||||
for(i=0; i<mRows; i++)
|
||||
delete mRowVec[i];
|
||||
delete[] mRowVec;
|
||||
}
|
||||
|
||||
void Matrix::SwapRows(int i, int j)
|
||||
void Matrix::SwapRows(unsigned i, unsigned j)
|
||||
{
|
||||
Vector *tmp = mRowVec[i];
|
||||
mRowVec[i] = mRowVec[j];
|
||||
mRowVec[j] = tmp;
|
||||
mRowVec[i].Swap(mRowVec[j]);
|
||||
}
|
||||
|
||||
double Matrix::Sum() const
|
||||
{
|
||||
int i, j;
|
||||
double sum = 0.0;
|
||||
for(i=0; i<Rows(); i++)
|
||||
for(j=0; j<Cols(); j++)
|
||||
sum += (*mRowVec[i])[j];
|
||||
return sum;
|
||||
}
|
||||
|
||||
Matrix IdentityMatrix(int N)
|
||||
Matrix IdentityMatrix(unsigned N)
|
||||
{
|
||||
Matrix M(N, N);
|
||||
int i;
|
||||
for(i=0; i<N; i++)
|
||||
for(unsigned i = 0; i < N; i++)
|
||||
M[i][i] = 1.0;
|
||||
return M;
|
||||
}
|
||||
@@ -169,8 +136,7 @@ Vector operator+(const Vector &left, const Vector &right)
|
||||
{
|
||||
wxASSERT(left.Len() == right.Len());
|
||||
Vector v(left.Len());
|
||||
int i;
|
||||
for(i=0; i<left.Len(); i++)
|
||||
for(unsigned i = 0; i < left.Len(); i++)
|
||||
v[i] = left[i] + right[i];
|
||||
return v;
|
||||
}
|
||||
@@ -179,8 +145,7 @@ Vector operator-(const Vector &left, const Vector &right)
|
||||
{
|
||||
wxASSERT(left.Len() == right.Len());
|
||||
Vector v(left.Len());
|
||||
int i;
|
||||
for(i=0; i<left.Len(); i++)
|
||||
for(unsigned i = 0; i < left.Len(); i++)
|
||||
v[i] = left[i] - right[i];
|
||||
return v;
|
||||
}
|
||||
@@ -189,8 +154,7 @@ Vector operator*(const Vector &left, const Vector &right)
|
||||
{
|
||||
wxASSERT(left.Len() == right.Len());
|
||||
Vector v(left.Len());
|
||||
int i;
|
||||
for(i=0; i<left.Len(); i++)
|
||||
for(unsigned i = 0; i < left.Len(); i++)
|
||||
v[i] = left[i] * right[i];
|
||||
return v;
|
||||
}
|
||||
@@ -198,17 +162,15 @@ Vector operator*(const Vector &left, const Vector &right)
|
||||
Vector operator*(const Vector &left, double right)
|
||||
{
|
||||
Vector v(left.Len());
|
||||
int i;
|
||||
for(i=0; i<left.Len(); i++)
|
||||
for(unsigned i = 0; i < left.Len(); i++)
|
||||
v[i] = left[i] * right;
|
||||
return v;
|
||||
}
|
||||
|
||||
Vector VectorSubset(const Vector &other, int start, int len)
|
||||
Vector VectorSubset(const Vector &other, unsigned start, unsigned len)
|
||||
{
|
||||
Vector v(len);
|
||||
int i;
|
||||
for(i=0; i<len; i++)
|
||||
for(unsigned i = 0; i < len; i++)
|
||||
v[i] = other[start+i];
|
||||
return v;
|
||||
}
|
||||
@@ -216,10 +178,9 @@ Vector VectorSubset(const Vector &other, int start, int len)
|
||||
Vector VectorConcatenate(const Vector& left, const Vector& right)
|
||||
{
|
||||
Vector v(left.Len() + right.Len());
|
||||
int i;
|
||||
for(i=0; i<left.Len(); i++)
|
||||
for(unsigned i = 0; i < left.Len(); i++)
|
||||
v[i] = left[i];
|
||||
for(i=0; i<right.Len(); i++)
|
||||
for(unsigned i = 0; i < right.Len(); i++)
|
||||
v[i + left.Len()] = right[i];
|
||||
return v;
|
||||
}
|
||||
@@ -228,10 +189,9 @@ Vector operator*(const Vector &left, const Matrix &right)
|
||||
{
|
||||
wxASSERT(left.Len() == right.Rows());
|
||||
Vector v(right.Cols());
|
||||
int i, j;
|
||||
for(i=0; i<right.Cols(); i++) {
|
||||
for(unsigned i = 0; i < right.Cols(); i++) {
|
||||
v[i] = 0.0;
|
||||
for(j=0; j<right.Rows(); j++)
|
||||
for(unsigned j = 0; j < right.Rows(); j++)
|
||||
v[i] += left[j] * right[j][i];
|
||||
}
|
||||
return v;
|
||||
@@ -241,10 +201,9 @@ Vector operator*(const Matrix &left, const Vector &right)
|
||||
{
|
||||
wxASSERT(left.Cols() == right.Len());
|
||||
Vector v(left.Rows());
|
||||
int i, j;
|
||||
for(i=0; i<left.Rows(); i++) {
|
||||
for(unsigned i = 0; i < left.Rows(); i++) {
|
||||
v[i] = 0.0;
|
||||
for(j=0; j<left.Cols(); j++)
|
||||
for(unsigned j = 0; j < left.Cols(); j++)
|
||||
v[i] += left[i][j] * right[j];
|
||||
}
|
||||
return v;
|
||||
@@ -255,9 +214,8 @@ Matrix operator+(const Matrix &left, const Matrix &right)
|
||||
wxASSERT(left.Rows() == right.Rows());
|
||||
wxASSERT(left.Cols() == right.Cols());
|
||||
Matrix M(left.Rows(), left.Cols());
|
||||
int i, j;
|
||||
for(i=0; i<left.Rows(); i++)
|
||||
for(j=0; j<left.Cols(); j++)
|
||||
for(unsigned i = 0; i < left.Rows(); i++)
|
||||
for(unsigned j = 0; j < left.Cols(); j++)
|
||||
M[i][j] = left[i][j] + right[i][j];
|
||||
return M;
|
||||
}
|
||||
@@ -265,9 +223,8 @@ Matrix operator+(const Matrix &left, const Matrix &right)
|
||||
Matrix operator*(const Matrix &left, const double right)
|
||||
{
|
||||
Matrix M(left.Rows(), left.Cols());
|
||||
int i, j;
|
||||
for(i=0; i<left.Rows(); i++)
|
||||
for(j=0; j<left.Cols(); j++)
|
||||
for(unsigned i = 0; i < left.Rows(); i++)
|
||||
for(unsigned j = 0; j < left.Cols(); j++)
|
||||
M[i][j] = left[i][j] * right;
|
||||
return M;
|
||||
}
|
||||
@@ -277,9 +234,8 @@ Matrix ScalarMultiply(const Matrix &left, const Matrix &right)
|
||||
wxASSERT(left.Rows() == right.Rows());
|
||||
wxASSERT(left.Cols() == right.Cols());
|
||||
Matrix M(left.Rows(), left.Cols());
|
||||
int i, j;
|
||||
for(i=0; i<left.Rows(); i++)
|
||||
for(j=0; j<left.Cols(); j++)
|
||||
for(unsigned i = 0; i < left.Rows(); i++)
|
||||
for(unsigned j = 0; j < left.Cols(); j++)
|
||||
M[i][j] = left[i][j] * right[i][j];
|
||||
return M;
|
||||
}
|
||||
@@ -288,23 +244,22 @@ Matrix MatrixMultiply(const Matrix &left, const Matrix &right)
|
||||
{
|
||||
wxASSERT(left.Cols() == right.Rows());
|
||||
Matrix M(left.Rows(), right.Cols());
|
||||
int i, j, k;
|
||||
for(i=0; i<left.Rows(); i++)
|
||||
for(j=0; j<right.Cols(); j++) {
|
||||
for(unsigned i = 0; i < left.Rows(); i++)
|
||||
for(unsigned j = 0; j < right.Cols(); j++) {
|
||||
M[i][j] = 0.0;
|
||||
for(k=0; k<left.Cols(); k++)
|
||||
for(unsigned k = 0; k < left.Cols(); k++)
|
||||
M[i][j] += left[i][k] * right[k][j];
|
||||
}
|
||||
return M;
|
||||
}
|
||||
|
||||
Matrix MatrixSubset(const Matrix &input,
|
||||
int startRow, int numRows, int startCol, int numCols)
|
||||
unsigned startRow, unsigned numRows,
|
||||
unsigned startCol, unsigned numCols)
|
||||
{
|
||||
Matrix M(numRows, numCols);
|
||||
int i, j;
|
||||
for(i=0; i<numRows; i++)
|
||||
for(j=0; j<numCols; j++)
|
||||
for(unsigned i = 0; i < numRows; i++)
|
||||
for(unsigned j = 0; j < numCols; j++)
|
||||
M[i][j] = input[startRow+i][startCol+j];
|
||||
return M;
|
||||
}
|
||||
@@ -313,11 +268,10 @@ Matrix MatrixConcatenateCols(const Matrix& left, const Matrix& right)
|
||||
{
|
||||
wxASSERT(left.Rows() == right.Rows());
|
||||
Matrix M(left.Rows(), left.Cols() + right.Cols());
|
||||
int i, j;
|
||||
for(i=0; i<left.Rows(); i++) {
|
||||
for(j=0; j<left.Cols(); j++)
|
||||
for(unsigned i = 0; i < left.Rows(); i++) {
|
||||
for(unsigned j = 0; j < left.Cols(); j++)
|
||||
M[i][j] = left[i][j];
|
||||
for(j=0; j<right.Cols(); j++)
|
||||
for(unsigned j = 0; j < right.Cols(); j++)
|
||||
M[i][j+left.Cols()] = right[i][j];
|
||||
}
|
||||
return M;
|
||||
@@ -326,9 +280,8 @@ Matrix MatrixConcatenateCols(const Matrix& left, const Matrix& right)
|
||||
Matrix TransposeMatrix(const Matrix& other)
|
||||
{
|
||||
Matrix M(other.Cols(), other.Rows());
|
||||
int i, j;
|
||||
for(i=0; i<other.Rows(); i++)
|
||||
for(j=0; j<other.Cols(); j++)
|
||||
for(unsigned i = 0; i < other.Rows(); i++)
|
||||
for(unsigned j = 0; j < other.Cols(); j++)
|
||||
M[j][i] = other[i][j];
|
||||
return M;
|
||||
}
|
||||
@@ -340,20 +293,19 @@ bool InvertMatrix(const Matrix& input, Matrix& Minv)
|
||||
// Returns true if successful
|
||||
|
||||
wxASSERT(input.Rows() == input.Cols());
|
||||
int N = input.Rows();
|
||||
int i, j, k;
|
||||
auto N = input.Rows();
|
||||
|
||||
Matrix M = input;
|
||||
Minv = IdentityMatrix(N);
|
||||
|
||||
// Do the elimination one column at a time
|
||||
for(i=0; i<N; i++) {
|
||||
for(unsigned i = 0; i < N; i++) {
|
||||
// Pivot the row with the largest absolute value in
|
||||
// column i, into row i
|
||||
double absmax = 0.0;
|
||||
int argmax=0;
|
||||
int argmax = 0;
|
||||
|
||||
for(j=i; j<N; j++)
|
||||
for(unsigned j = i; j < N; j++)
|
||||
if (fabs(M[j][i]) > absmax) {
|
||||
absmax = fabs(M[j][i]);
|
||||
argmax = j;
|
||||
@@ -375,13 +327,13 @@ bool InvertMatrix(const Matrix& input, Matrix& Minv)
|
||||
Minv[i] = Minv[i] * factor;
|
||||
|
||||
// Eliminate the rest of the column
|
||||
for(j=0; j<N; j++) {
|
||||
if (j==i)
|
||||
for(unsigned j = 0; j < N; j++) {
|
||||
if (j == i)
|
||||
continue;
|
||||
if (fabs(M[j][i]) > 0) {
|
||||
// Subtract a multiple of row i from row j
|
||||
double factor = M[j][i];
|
||||
for(k=0; k<N; k++) {
|
||||
for(unsigned k = 0; k < N; k++) {
|
||||
M[j][k] -= (M[i][k] * factor);
|
||||
Minv[j][k] -= (Minv[i][k] * factor);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user