|
|
#region Copyright � 2009, De Santiago-Castillo JA. All rights reserved.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Text;
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DotNumerics.LinearAlgebra
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
public sealed class BandMatrix : BaseBandMatrix
|
|
|
{
|
|
|
|
|
|
#region Public Constructors
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public BandMatrix(int rows, int columns, int lowerBandWidth, int upperBandWidth) : base(rows, columns, lowerBandWidth, upperBandWidth) { }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
internal BandMatrix(int rows, int columns, int lowerBandWidth, int upperBandWidth, double[] Data) : base(rows, columns, lowerBandWidth, upperBandWidth, Data) { }
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region Overloading Operators
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static BandMatrix operator +(BandMatrix A, BandMatrix B)
|
|
|
{
|
|
|
if (B.RowCount != A.RowCount || B.ColumnCount != A.ColumnCount || B.LowerBandWidth != A.LowerBandWidth || B.UpperBandWidth != A.UpperBandWidth)
|
|
|
{
|
|
|
throw new System.ArgumentException("Matrix dimensions are not valid.");
|
|
|
}
|
|
|
|
|
|
BandMatrix C = new BandMatrix(A.RowCount, A.ColumnCount, A.LowerBandWidth, B.UpperBandWidth);
|
|
|
|
|
|
double[] AData = A.Data;
|
|
|
double[] BData = B.Data;
|
|
|
double[] CData = C.Data;
|
|
|
|
|
|
for (int i = 0; i < AData.Length; i++)
|
|
|
{
|
|
|
CData[i] = AData[i] + BData[i];
|
|
|
}
|
|
|
|
|
|
return C;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static BandMatrix operator -(BandMatrix A, BandMatrix B)
|
|
|
{
|
|
|
if (B.RowCount != A.RowCount || B.ColumnCount != A.ColumnCount || B.LowerBandWidth != A.LowerBandWidth || B.UpperBandWidth != A.UpperBandWidth)
|
|
|
{
|
|
|
throw new System.ArgumentException("Matrix dimensions are not valid.");
|
|
|
}
|
|
|
|
|
|
BandMatrix C = new BandMatrix(A.RowCount, A.ColumnCount, A.LowerBandWidth, B.UpperBandWidth);
|
|
|
|
|
|
double[] AData = A.Data;
|
|
|
double[] BData = B.Data;
|
|
|
double[] CData = C.Data;
|
|
|
|
|
|
for (int i = 0; i < AData.Length; i++)
|
|
|
{
|
|
|
CData[i] = AData[i] - BData[i];
|
|
|
}
|
|
|
|
|
|
return C;
|
|
|
}
|
|
|
|
|
|
#region Scalar-Matrix Multiplication
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static BandMatrix operator *(double s, BandMatrix A)
|
|
|
{
|
|
|
BandMatrix C = new BandMatrix(A.RowCount, A.ColumnCount, A.LowerBandWidth, A.UpperBandWidth);
|
|
|
|
|
|
double[] AData = A.Data;
|
|
|
double[] CData = C.Data;
|
|
|
|
|
|
|
|
|
Matrix.MultiplicationSM(s, AData, CData);
|
|
|
|
|
|
return C;
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Public Methods
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public BandMatrix Clone()
|
|
|
{
|
|
|
BandMatrix NewBandMatix = new BandMatrix(this._RowCount, this._ColumnCount, this.MeLowerBandWidth, this.MeUpperBandWidth, this._Data);
|
|
|
return NewBandMatix;
|
|
|
}
|
|
|
|
|
|
|
|
|
internal Matrix GetBandPackedMatrix()
|
|
|
{
|
|
|
int MatrixRows = 2 * this.MeLowerBandWidth + this.MeUpperBandWidth + 1;
|
|
|
int MatrixColumns = this._ColumnCount;
|
|
|
Matrix MatrixBandStorageExt = new Matrix(MatrixRows, MatrixColumns);
|
|
|
double[] GeneralBandStorage = MatrixBandStorageExt.Data;
|
|
|
int Index;
|
|
|
int TempInt = this.MeLowerBandWidth + this.MeUpperBandWidth + 1;
|
|
|
for (int colum = 1; colum <= MatrixColumns; colum++)
|
|
|
{
|
|
|
for (int row = Math.Max(1, colum - this.MeUpperBandWidth); row <= Math.Min(this._ColumnCount, colum + this.MeLowerBandWidth); row++)
|
|
|
{
|
|
|
Index = TempInt + row - colum;
|
|
|
GeneralBandStorage[Index - 1 + (colum - 1) * MatrixRows] = this._Data[row - 1 + (colum - 1) * this._RowCount];
|
|
|
}
|
|
|
}
|
|
|
return MatrixBandStorageExt;
|
|
|
}
|
|
|
|
|
|
|
|
|
#region Static methods
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static BandMatrix Random(int rows, int columns, int lowerBandWidth, int upperBandWidth)
|
|
|
{
|
|
|
System.Random random = new System.Random();
|
|
|
|
|
|
BandMatrix X = new BandMatrix(rows, columns, lowerBandWidth, upperBandWidth);
|
|
|
|
|
|
double[] XData = X.Data;
|
|
|
|
|
|
for (int j = 0; j < X.ColumnCount; j++)
|
|
|
{
|
|
|
for (int i = 0; i < X.RowCount; i++)
|
|
|
{
|
|
|
X[i, j] = random.NextDouble();
|
|
|
}
|
|
|
}
|
|
|
return X;
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region Private Methods
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|