File size: 7,630 Bytes
b1b3bae |
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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
#region Copyright � 2009, De Santiago-Castillo JA. All rights reserved.
//Copyright � 2009 Jose Antonio De Santiago-Castillo
//E-mail:JAntonioDeSantiago@gmail.com
//Web: www.DotNumerics.com
//
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
//IBM Def
//A general band matrix has its nonzero elements arranged uniformly near the diagonal, such that:
//aij = 0 if (i-j) > ml or (j-i) > mu
//where ml and mu are the lower and upper band widths, respectively, and ml+mu+1 is the total band width.
namespace DotNumerics.LinearAlgebra
{
/// <summary>
/// Represents a Band Matrix.
/// </summary>
public sealed class BandMatrix : BaseBandMatrix
{
#region Public Constructors
/// <summary>
/// Initializes a new instance of the BandMatrix class of the given size.
/// </summary>
/// <param name="rows">Number of rows.</param>
/// <param name="columns">Number of columns.</param>
/// <param name="lowerBandWidth">Number of bands below the main diagonal</param>
/// <param name="upperBandWidth">Number of bands above the main diagonal</param>
public BandMatrix(int rows, int columns, int lowerBandWidth, int upperBandWidth) : base(rows, columns, lowerBandWidth, upperBandWidth) { }
/// <summary>
/// Initializes a new instance of the BandMatrix class of the given size using a array
/// </summary>
/// <param name="rows">Number of rows.</param>
/// <param name="columns">Number of columns.</param>
/// <param name="lowerBandWidth">Number of bands below the main diagonal</param>
/// <param name="upperBandWidth">Number of bands above the main diagonal</param>
/// <param name="Data">The matix data </param>
internal BandMatrix(int rows, int columns, int lowerBandWidth, int upperBandWidth, double[] Data) : base(rows, columns, lowerBandWidth, upperBandWidth, Data) { }
#endregion
#region Overloading Operators
/// <summary>
/// Matrix addition.
/// </summary>
/// <param name="A">The left side matrix of the addition operator.</param>
/// <param name="B">The right side matrix of the addition operator.</param>
/// <returns>A matrix that represents the result of the matrix addition.</returns>
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;
}
/// <summary>
/// Matrix subtraction.
/// </summary>
/// <param name="A"> The left side matrix of the subtraction operator.</param>
/// <param name="B">The right side matrix of the subtraction operator.</param>
/// <returns>A matrix that represents the result of the matrix subtraction.</returns>
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
/// <summary>
/// Scalar-Matrix multiplication.
/// </summary>
/// <param name="s"> The left side scalar of the multiplication operator.</param>
/// <param name="A">The right side matrix of the multiplication operator.</param>
/// <returns>A matrix that represents the result of the multiplication.</returns>
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
/// <summary>
/// Creates a copy of the matrix.
/// </summary>
/// <returns>The copy of the Matrix.</returns>
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
/// <summary>Generate a BandMatrix with random elements</summary>
/// <param name="rows">Number of rows.</param>
/// <param name="columns">Number of columns.</param>
/// <param name="lowerBandWidth">Number of bands below the main diagonal</param>
/// <param name="upperBandWidth">Number of bands above the main diagonal</param>
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
}
}
|