|
|
#region Copyright � 2009, De Santiago-Castillo JA. All rights reserved.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Text;
|
|
|
using System.Diagnostics;
|
|
|
using System.IO;
|
|
|
using DotNumerics.LinearAlgebra.CSLapack;
|
|
|
|
|
|
|
|
|
namespace DotNumerics.LinearAlgebra
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[DebuggerDisplay("[{RowCount},{ColumnCount}]")]
|
|
|
|
|
|
[DebuggerTypeProxy(typeof(MatrixDebuggerDisplay))]
|
|
|
public abstract class BaseMatrix: IMatrix<double>
|
|
|
{
|
|
|
#region Static Fields
|
|
|
|
|
|
internal static DGETRF _dgetrf;
|
|
|
internal static DGETRI _dgetri;
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
#region Fields
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
|
|
|
protected double[] _Data;
|
|
|
|
|
|
|
|
|
|
|
|
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
|
|
|
protected int _RowCount;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
|
|
|
protected int _ColumnCount;
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
#region Public Constructors
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public BaseMatrix(int rows, int columns)
|
|
|
{
|
|
|
if (rows < 1) throw new System.ArgumentException("rows < 1");
|
|
|
if (columns < 1) throw new System.ArgumentException("columns < 1");
|
|
|
|
|
|
this._Data = new double[rows * columns];
|
|
|
this._RowCount = rows;
|
|
|
this._ColumnCount = columns;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
internal BaseMatrix(int rows, int columns, double[] data)
|
|
|
{
|
|
|
if (rows < 1) throw new System.ArgumentException("rows < 1");
|
|
|
if (columns < 1) throw new System.ArgumentException("columns < 1");
|
|
|
this._Data = new double[rows * columns];
|
|
|
this._RowCount = rows;
|
|
|
this._ColumnCount = columns;
|
|
|
|
|
|
data.CopyTo(this._Data, 0);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public BaseMatrix(int size)
|
|
|
{
|
|
|
if (size < 1) throw new System.ArgumentException("size < 1");
|
|
|
|
|
|
this._Data = new double[size * size];
|
|
|
this._RowCount = size;
|
|
|
this._ColumnCount = size;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
internal BaseMatrix(int size, double[] data)
|
|
|
{
|
|
|
if (size < 1) throw new System.ArgumentException("size < 1");
|
|
|
|
|
|
this._Data = new double[size * size];
|
|
|
this._RowCount = size;
|
|
|
this._ColumnCount = size;
|
|
|
|
|
|
data.CopyTo(this._Data, 0);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
#region Public Properties
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
|
|
|
internal double[] Data
|
|
|
{
|
|
|
get { return this._Data; }
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
|
|
|
public int RowCount
|
|
|
{
|
|
|
get { return _RowCount; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
|
|
|
public int ColumnCount
|
|
|
{
|
|
|
get { return _ColumnCount; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool IsSquare
|
|
|
{
|
|
|
get
|
|
|
{
|
|
|
bool isSquare = false;
|
|
|
if (this._ColumnCount == this.RowCount) isSquare = true;
|
|
|
return isSquare;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public virtual double this[int row, int column]
|
|
|
{
|
|
|
get
|
|
|
{
|
|
|
if (column >= this._ColumnCount)
|
|
|
{
|
|
|
throw new ArgumentException("Index was outside the bounds of the matrix.");
|
|
|
}
|
|
|
|
|
|
return this._Data[row + column * this._RowCount];
|
|
|
}
|
|
|
set
|
|
|
{
|
|
|
if (column >= this._ColumnCount)
|
|
|
{
|
|
|
throw new ArgumentException("Index was outside the bounds of the matrix.");
|
|
|
}
|
|
|
|
|
|
this._Data[row + column * this._RowCount] = value;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region Private Methods
|
|
|
|
|
|
|
|
|
internal protected void CheckMatrixDimensions(BaseMatrix B)
|
|
|
{
|
|
|
if (this._RowCount != B.RowCount || B.ColumnCount != this._ColumnCount)
|
|
|
{
|
|
|
throw new System.ArgumentException("Matrix dimensions must agree.");
|
|
|
}
|
|
|
}
|
|
|
#endregion // Private Methods
|
|
|
|
|
|
#region Elementary linear operations
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public virtual void ElementsAbs()
|
|
|
{
|
|
|
for (int i = 0; i < this._Data.Length; i++)
|
|
|
{
|
|
|
this._Data[i] = Math.Abs(this._Data[i]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public virtual void ElemntsDiv(BaseMatrix B)
|
|
|
{
|
|
|
CheckMatrixDimensions(B);
|
|
|
double[] BData = B.Data;
|
|
|
for (int i = 0; i < this._Data.Length; i++)
|
|
|
{
|
|
|
this._Data[i] /= BData[i];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public virtual void ElemntsMult(BaseMatrix B)
|
|
|
{
|
|
|
CheckMatrixDimensions(B);
|
|
|
double[] BData = B.Data;
|
|
|
for (int i = 0; i < this._Data.Length; i++)
|
|
|
{
|
|
|
this._Data[i] *= BData[i];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public virtual Matrix Add(BaseMatrix B)
|
|
|
{
|
|
|
CheckMatrixDimensions(B);
|
|
|
|
|
|
Matrix C = new Matrix(this._RowCount, this._ColumnCount);
|
|
|
|
|
|
double[] BData = B.Data;
|
|
|
double[] dataC = C.Data;
|
|
|
for (int i = 0; i < this._Data.Length; i++)
|
|
|
{
|
|
|
dataC[i] = this._Data[i] + BData[i];
|
|
|
}
|
|
|
|
|
|
return C;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public virtual void MultiplyInplace(double s)
|
|
|
{
|
|
|
for (int i = 0; i < this._Data.Length; i++)
|
|
|
{
|
|
|
this._Data[i] *= s;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Matrix Multiply(double s)
|
|
|
{
|
|
|
Matrix C = new Matrix(this._RowCount, this._ColumnCount);
|
|
|
|
|
|
double[] dataC = C.Data;
|
|
|
for (int i = 0; i < this._Data.Length; i++)
|
|
|
{
|
|
|
dataC[i] = this._Data[i] * s;
|
|
|
}
|
|
|
|
|
|
return C;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Matrix Multiply(BaseMatrix B)
|
|
|
{
|
|
|
|
|
|
if (B.RowCount != this.ColumnCount)
|
|
|
{
|
|
|
throw new System.ArgumentException("Matrix dimensions are not valid.");
|
|
|
}
|
|
|
|
|
|
Matrix C = new Matrix(this.RowCount, B.ColumnCount);
|
|
|
|
|
|
double[] AData = this.Data;
|
|
|
double[] BData = B.Data;
|
|
|
double[] CData = C.Data;
|
|
|
|
|
|
int ARows = this.RowCount;
|
|
|
int AColumns = this.ColumnCount;
|
|
|
|
|
|
int BRows = B.RowCount;
|
|
|
int BColumns = B.ColumnCount;
|
|
|
|
|
|
double Sum = 0.0;
|
|
|
int indexBJ;
|
|
|
int indexAJ;
|
|
|
for (int j = 0; j < BColumns; j++)
|
|
|
{
|
|
|
indexBJ = j * BRows;
|
|
|
indexAJ = j * ARows;
|
|
|
for (int i = 0; i < ARows; i++)
|
|
|
{
|
|
|
Sum = 0.0;
|
|
|
for (int k = 0; k < AColumns; k++)
|
|
|
{
|
|
|
Sum += AData[i + k * ARows] * BData[k + indexBJ];
|
|
|
}
|
|
|
CData[i + indexAJ] = Sum;
|
|
|
}
|
|
|
}
|
|
|
return C;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Matrix Subtract(BaseMatrix B)
|
|
|
{
|
|
|
CheckMatrixDimensions(B);
|
|
|
|
|
|
Matrix C = new Matrix(this._RowCount, this._ColumnCount);
|
|
|
|
|
|
double[] BData = B.Data;
|
|
|
double[] dataC = C.Data;
|
|
|
for (int i = 0; i < this._Data.Length; i++)
|
|
|
{
|
|
|
dataC[i] = this._Data[i] - BData[i];
|
|
|
}
|
|
|
|
|
|
return C;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public virtual void UnaryMinusInplace()
|
|
|
{
|
|
|
for (int i = 0; i < this._Data.Length; i++)
|
|
|
{
|
|
|
this._Data[i] = -this._Data[i];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region Methods
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Matrix Inverse()
|
|
|
{
|
|
|
|
|
|
if (this.IsSquare != true)
|
|
|
{
|
|
|
throw new System.ArgumentException("This is not a square matrix.");
|
|
|
}
|
|
|
|
|
|
if (BaseMatrix._dgetrf == null)
|
|
|
{
|
|
|
BaseMatrix._dgetrf = new DGETRF();
|
|
|
}
|
|
|
if (BaseMatrix._dgetri == null)
|
|
|
{
|
|
|
BaseMatrix._dgetri = new DGETRI();
|
|
|
}
|
|
|
|
|
|
|
|
|
Matrix inverseMatrix= new Matrix(this.RowCount, this.ColumnCount, this.Data);
|
|
|
|
|
|
double[] inverseData= inverseMatrix.Data;
|
|
|
|
|
|
int[] ipiv= new int[this.RowCount];
|
|
|
|
|
|
|
|
|
|
|
|
int Info = 0;
|
|
|
|
|
|
double[] Work = new double[1];
|
|
|
int LWork = -1;
|
|
|
|
|
|
|
|
|
BaseMatrix._dgetri.Run(this.RowCount, ref inverseData, 0, this.RowCount, ipiv, 0, ref Work, 0, LWork, ref Info);
|
|
|
LWork = Convert.ToInt32(Work[0]);
|
|
|
if (LWork > 0)
|
|
|
{
|
|
|
Work = new double[LWork];
|
|
|
|
|
|
BaseMatrix._dgetrf.Run(this.RowCount, this.ColumnCount, ref inverseData, 0, this.RowCount, ref ipiv, 0, ref Info);
|
|
|
|
|
|
|
|
|
#region Error
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (Info < 0)
|
|
|
{
|
|
|
string infoSTg = Math.Abs(Info).ToString();
|
|
|
throw new ArgumentException("the " + infoSTg + " -th argument had an illegal value");
|
|
|
}
|
|
|
else if (Info > 0)
|
|
|
{
|
|
|
string infoSTg = Math.Abs(Info).ToString();
|
|
|
throw new Exception("The matrix is numerically singular..");
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
BaseMatrix._dgetri.Run(this.RowCount, ref inverseData, 0, this.RowCount, ipiv, 0, ref Work, 0, LWork, ref Info);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
#region Error
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (Info < 0)
|
|
|
{
|
|
|
string infoSTg = Math.Abs(Info).ToString();
|
|
|
throw new ArgumentException("the " + infoSTg + " -th argument had an illegal value");
|
|
|
}
|
|
|
else if (Info > 0)
|
|
|
{
|
|
|
string infoSTg = Math.Abs(Info).ToString();
|
|
|
throw new Exception("The matrix is numerically singular..");
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
return inverseMatrix;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public double Determinant()
|
|
|
{
|
|
|
double det = 1.0;
|
|
|
|
|
|
|
|
|
if (this.IsSquare != true)
|
|
|
{
|
|
|
throw new System.ArgumentException("This is not a square matrix.");
|
|
|
}
|
|
|
|
|
|
if (BaseMatrix._dgetrf == null)
|
|
|
{
|
|
|
BaseMatrix._dgetrf = new DGETRF();
|
|
|
}
|
|
|
|
|
|
|
|
|
Matrix clonMatrix = new Matrix(this.RowCount, this.ColumnCount, this.Data);
|
|
|
|
|
|
double[] clonData = clonMatrix.Data;
|
|
|
|
|
|
int[] ipiv = new int[this.RowCount];
|
|
|
|
|
|
|
|
|
|
|
|
int Info = 0;
|
|
|
|
|
|
|
|
|
|
|
|
BaseMatrix._dgetrf.Run(this.RowCount, this.ColumnCount, ref clonData, 0, this.RowCount, ref ipiv, 0, ref Info);
|
|
|
|
|
|
|
|
|
#region Error
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (Info < 0)
|
|
|
{
|
|
|
string infoSTg = Math.Abs(Info).ToString();
|
|
|
throw new ArgumentException("the " + infoSTg + " -th argument had an illegal value");
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < this._RowCount; i++)
|
|
|
{
|
|
|
if (ipiv[i] != (i + 1))
|
|
|
{
|
|
|
det *= -clonMatrix[i, i];
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
det *= clonMatrix[i, i];
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return det;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Vector[] GetColumnVectors()
|
|
|
{
|
|
|
Vector[] columnVects = new Vector[this._ColumnCount];
|
|
|
|
|
|
double[] VectData;
|
|
|
for (int j = 0; j < this._ColumnCount; j++)
|
|
|
{
|
|
|
columnVects[j] = new Vector(VectorType.Column, this._RowCount);
|
|
|
VectData = columnVects[j].Data;
|
|
|
for (int i = 0; i < VectData.Length; i++)
|
|
|
{
|
|
|
VectData[i] = this._Data[i + j * this._RowCount];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return columnVects;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Vector GetColumnVector(int columnIndex)
|
|
|
{
|
|
|
|
|
|
if (columnIndex >= this._ColumnCount)
|
|
|
{
|
|
|
throw new System.ArgumentException("columnIndex >= number of columns.");
|
|
|
}
|
|
|
|
|
|
if (columnIndex < 0)
|
|
|
{
|
|
|
throw new System.ArgumentException("columnIndex < 0");
|
|
|
}
|
|
|
|
|
|
Vector columnVect;
|
|
|
|
|
|
double[] VectData;
|
|
|
columnVect = new Vector(VectorType.Column, this._RowCount);
|
|
|
VectData = columnVect.Data;
|
|
|
for (int i = 0; i < VectData.Length; i++)
|
|
|
{
|
|
|
VectData[i] = this._Data[i + columnIndex * this._RowCount];
|
|
|
}
|
|
|
|
|
|
return columnVect;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public double[] GetColumnArray(int columnIndex)
|
|
|
{
|
|
|
|
|
|
if (columnIndex >= this._ColumnCount)
|
|
|
{
|
|
|
throw new System.ArgumentException("columnIndex >= number of columns.");
|
|
|
}
|
|
|
|
|
|
if (columnIndex < 0)
|
|
|
{
|
|
|
throw new System.ArgumentException("columnIndex < 0");
|
|
|
}
|
|
|
|
|
|
double[] VectData = new double[this._RowCount];
|
|
|
for (int i = 0; i < VectData.Length; i++)
|
|
|
{
|
|
|
VectData[i] = this._Data[i + columnIndex * this._RowCount];
|
|
|
}
|
|
|
|
|
|
return VectData;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Vector[] GetRowVectors()
|
|
|
{
|
|
|
Vector[] rowVects = new Vector[this.RowCount];
|
|
|
|
|
|
double[] VectData;
|
|
|
for (int i = 0; i < this._RowCount; i++)
|
|
|
{
|
|
|
rowVects[i] = new Vector(VectorType.Row, this._ColumnCount);
|
|
|
VectData = rowVects[i].Data;
|
|
|
for (int j = 0; j < VectData.Length; j++)
|
|
|
{
|
|
|
VectData[j] = this._Data[i + j * this._RowCount];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return rowVects;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Vector GetRowVector(int rowIndex)
|
|
|
{
|
|
|
|
|
|
if (rowIndex >= this._RowCount)
|
|
|
{
|
|
|
throw new System.ArgumentException("rowIndex >= number of rows.");
|
|
|
}
|
|
|
|
|
|
if (rowIndex < 0)
|
|
|
{
|
|
|
throw new System.ArgumentException("rowIndex < 0");
|
|
|
}
|
|
|
|
|
|
Vector rowVect;
|
|
|
|
|
|
double[] VectData;
|
|
|
rowVect = new Vector(VectorType.Row, this._ColumnCount);
|
|
|
VectData = rowVect.Data;
|
|
|
for (int j = 0; j < VectData.Length; j++)
|
|
|
{
|
|
|
VectData[j] = this._Data[rowIndex + j * this._RowCount];
|
|
|
}
|
|
|
|
|
|
return rowVect;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public double[] GetRowArray(int rowIndex)
|
|
|
{
|
|
|
|
|
|
if (rowIndex >= this._RowCount)
|
|
|
{
|
|
|
throw new System.ArgumentException("rowIndex >= number of rows.");
|
|
|
}
|
|
|
|
|
|
if (rowIndex < 0)
|
|
|
{
|
|
|
throw new System.ArgumentException("rowIndex < 0");
|
|
|
}
|
|
|
|
|
|
double[] VectData = new double[this._ColumnCount];
|
|
|
|
|
|
for (int j = 0; j < VectData.Length; j++)
|
|
|
{
|
|
|
VectData[j] = this._Data[rowIndex + j * this._RowCount];
|
|
|
}
|
|
|
|
|
|
|
|
|
return VectData;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public string MatrixToString()
|
|
|
{
|
|
|
using (StringWriter writer = new StringWriter())
|
|
|
{
|
|
|
for (int i = 0; i < this._RowCount; i++)
|
|
|
{
|
|
|
for (int j = 0; j < this._ColumnCount; j++)
|
|
|
writer.Write(this[i, j].ToString() + ", ");
|
|
|
writer.WriteLine();
|
|
|
}
|
|
|
return writer.ToString();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public string MatrixToString(string format)
|
|
|
{
|
|
|
using (StringWriter writer = new StringWriter())
|
|
|
{
|
|
|
for (int i = 0; i < this._RowCount; i++)
|
|
|
{
|
|
|
for (int j = 0; j < this._ColumnCount; j++)
|
|
|
writer.Write(this[i, j].ToString(format) + ", ");
|
|
|
writer.WriteLine();
|
|
|
}
|
|
|
return writer.ToString();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public double Norm1()
|
|
|
{
|
|
|
double n = 0.0;
|
|
|
double ColSum = 0.0;
|
|
|
int NRows = this._RowCount;
|
|
|
|
|
|
for (int j = 0; j < this._ColumnCount; j++)
|
|
|
{
|
|
|
ColSum = 0.0;
|
|
|
for (int i = 0; i < this._RowCount; i++)
|
|
|
{
|
|
|
ColSum += Math.Abs(this._Data[i + j * NRows]);
|
|
|
|
|
|
}
|
|
|
n = Math.Max(n, ColSum);
|
|
|
}
|
|
|
return n;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public double NormInf()
|
|
|
{
|
|
|
double n = 0.0;
|
|
|
double RowSum = 0.0;
|
|
|
int NRows = this._RowCount;
|
|
|
for (int i = 0; i < this._RowCount; i++)
|
|
|
{
|
|
|
RowSum = 0.0;
|
|
|
for (int j = 0; j < this._ColumnCount; j++)
|
|
|
{
|
|
|
RowSum += Math.Abs(this._Data[i + j * NRows]);
|
|
|
}
|
|
|
n = Math.Max(n, RowSum);
|
|
|
}
|
|
|
return n;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public double FrobeniusNorm()
|
|
|
{
|
|
|
double n=0;
|
|
|
for(int i=0; i<this._Data.Length; i++)
|
|
|
{
|
|
|
n=this.Hypot(n,this._Data[i]);
|
|
|
}
|
|
|
return n;
|
|
|
}
|
|
|
|
|
|
|
|
|
private double Hypot(double a, double b)
|
|
|
{
|
|
|
double r;
|
|
|
if (Math.Abs(a) > Math.Abs(b))
|
|
|
{
|
|
|
r = b/a;
|
|
|
r = Math.Abs(a) * Math.Sqrt(1 + r * r);
|
|
|
}
|
|
|
else if (b != 0)
|
|
|
{
|
|
|
r = a/b;
|
|
|
r = Math.Abs(b) * Math.Sqrt(1 + r * r);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
r = 0.0;
|
|
|
}
|
|
|
return r;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public double ElementsSum()
|
|
|
{
|
|
|
double TemSum = 0.0;
|
|
|
for (int i = 0; i < this._Data.Length; i++)
|
|
|
{
|
|
|
TemSum += this._Data[i];
|
|
|
}
|
|
|
return TemSum;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Matrix Transpose()
|
|
|
{
|
|
|
Matrix AT = new Matrix(this._ColumnCount, this._RowCount);
|
|
|
int ATRows = AT.RowCount;
|
|
|
int ATColumns = AT.ColumnCount;
|
|
|
double[] ATData = AT.Data;
|
|
|
for (int j = 0; j < this._ColumnCount; j++)
|
|
|
{
|
|
|
for (int i = 0; i < this._RowCount; i++)
|
|
|
{
|
|
|
ATData[j + i * ATRows] = this._Data[i + j * this._RowCount];
|
|
|
}
|
|
|
}
|
|
|
return AT;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public double Trace
|
|
|
{
|
|
|
get
|
|
|
{
|
|
|
double trace = 0;
|
|
|
for (int i = 0; i < Math.Min(this.RowCount, this.ColumnCount ); i++)
|
|
|
{
|
|
|
trace += this[i, i];
|
|
|
}
|
|
|
return trace;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region Matrix-Matrix Multiplication
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static Matrix operator *(BaseMatrix A, BaseMatrix B)
|
|
|
{
|
|
|
return A.Multiply(B);
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region Matrix-Matrix Addition
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static Matrix operator +(BaseMatrix A, BaseMatrix B)
|
|
|
{
|
|
|
return A.Add(B);
|
|
|
}
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region Matrix-Matrix Subtraction
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static Matrix operator -(BaseMatrix A, BaseMatrix B)
|
|
|
{
|
|
|
return A.Subtract(B);
|
|
|
}
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
#region IMatrix<double> Members
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public double[,] CopyToArray()
|
|
|
{
|
|
|
double[,] matrixData = new double[this._RowCount, this._ColumnCount];
|
|
|
|
|
|
for (int j = 0; j < this._ColumnCount; j++)
|
|
|
{
|
|
|
for (int i = 0; i < this._RowCount; i++)
|
|
|
{
|
|
|
matrixData[i,j] = this._Data[i + j * this._RowCount];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return matrixData;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public double[][] CopyToJaggedArray()
|
|
|
{
|
|
|
|
|
|
double[][] newData = new double[this._RowCount][];
|
|
|
for (int i = 0; i < this._RowCount; i++)
|
|
|
{
|
|
|
double[] row = new double[this._ColumnCount];
|
|
|
for (int j = 0; j < this._ColumnCount; j++)
|
|
|
{
|
|
|
row[j] = this._Data[i + j * this._RowCount];
|
|
|
}
|
|
|
|
|
|
newData[i] = row;
|
|
|
}
|
|
|
|
|
|
return newData;
|
|
|
}
|
|
|
|
|
|
public ComplexMatrix CopyToComplex()
|
|
|
{
|
|
|
ComplexMatrix complexMatrix = new ComplexMatrix(this._RowCount, this._ColumnCount);
|
|
|
Complex[] data = complexMatrix.Data;
|
|
|
for (int i = 0; i < this._Data.Length; i++)
|
|
|
{
|
|
|
data[i].Real = this._Data[i];
|
|
|
}
|
|
|
|
|
|
return complexMatrix;
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
}
|
|
|
}
|
|
|
|