Browse Source

Issue #35 Diagonal Matrix SubMatrix bug fix

(cherry picked from commit fc2b42c673e847311cb48538f83955c1caaccf09)
la-knuth
manyue 14 years ago
committed by Christoph Ruegg
parent
commit
646c62bfd6
  1. 14
      src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs
  2. 14
      src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs
  3. 14
      src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs
  4. 39
      src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs

14
src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs

@ -54,7 +54,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// </exception> /// </exception>
public DiagonalMatrix(int order) : base(order) public DiagonalMatrix(int order) : base(order)
{ {
Data = new Complex[order * order]; Data = new Complex[order];
} }
/// <summary> /// <summary>
@ -1194,16 +1194,20 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
if (rowIndex > columnIndex && columnIndex + columnCount > rowIndex) if (rowIndex > columnIndex && columnIndex + columnCount > rowIndex)
{ {
for (var i = 0; rowIndex - columnIndex + i < Math.Min(columnCount, rowCount); i++) int columnInit = rowIndex - columnIndex;
int end = Math.Min(columnCount, rowCount + columnInit);
for (var i = 0; columnInit + i < end; i++)
{ {
result[i, rowIndex - columnIndex + i] = Data[rowIndex + i]; result[i, columnInit + i] = Data[rowIndex + i];
} }
} }
else if (rowIndex < columnIndex && rowIndex + rowCount > columnIndex) else if (rowIndex < columnIndex && rowIndex + rowCount > columnIndex)
{ {
for (var i = 0; rowIndex - columnIndex + i < Math.Min(columnCount, rowCount); i++) int rowInit = columnIndex - rowIndex;
int end = Math.Min(columnCount + rowInit, rowCount);
for (var i = 0; rowInit + i < end; i++)
{ {
result[columnIndex - rowIndex + i, i] = Data[columnIndex + i]; result[rowInit + i, i] = Data[columnIndex + i];
} }
} }
else else

14
src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs

@ -55,7 +55,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
public DiagonalMatrix(int order) public DiagonalMatrix(int order)
: base(order) : base(order)
{ {
Data = new Complex32[order * order]; Data = new Complex32[order];
} }
/// <summary> /// <summary>
@ -1199,16 +1199,20 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
if (rowIndex > columnIndex && columnIndex + columnCount > rowIndex) if (rowIndex > columnIndex && columnIndex + columnCount > rowIndex)
{ {
for (var i = 0; rowIndex - columnIndex + i < Math.Min(columnCount, rowCount); i++) int columnInit = rowIndex - columnIndex;
int end = Math.Min(columnCount, rowCount + columnInit);
for (var i = 0; columnInit + i < end; i++)
{ {
result[i, rowIndex - columnIndex + i] = Data[rowIndex + i]; result[i, columnInit + i] = Data[rowIndex + i];
} }
} }
else if (rowIndex < columnIndex && rowIndex + rowCount > columnIndex) else if (rowIndex < columnIndex && rowIndex + rowCount > columnIndex)
{ {
for (var i = 0; rowIndex - columnIndex + i < Math.Min(columnCount, rowCount); i++) int rowInit = columnIndex - rowIndex;
int end = Math.Min(columnCount + rowInit, rowCount);
for (var i = 0; rowInit + i < end; i++)
{ {
result[columnIndex - rowIndex + i, i] = Data[columnIndex + i]; result[rowInit + i, i] = Data[columnIndex + i];
} }
} }
else else

14
src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs

@ -53,7 +53,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// </exception> /// </exception>
public DiagonalMatrix(int order) : base(order) public DiagonalMatrix(int order) : base(order)
{ {
Data = new double[order * order]; Data = new double[order];
} }
/// <summary> /// <summary>
@ -1188,16 +1188,20 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (rowIndex > columnIndex && columnIndex + columnCount > rowIndex) if (rowIndex > columnIndex && columnIndex + columnCount > rowIndex)
{ {
for (var i = 0; rowIndex - columnIndex + i < Math.Min(columnCount, rowCount); i++) int columnInit = rowIndex - columnIndex;
int end = Math.Min(columnCount, rowCount + columnInit);
for (var i = 0; columnInit + i < end; i++)
{ {
result[i, rowIndex - columnIndex + i] = Data[rowIndex + i]; result[i, columnInit + i] = Data[rowIndex + i];
} }
} }
else if (rowIndex < columnIndex && rowIndex + rowCount > columnIndex) else if (rowIndex < columnIndex && rowIndex + rowCount > columnIndex)
{ {
for (var i = 0; rowIndex - columnIndex + i < Math.Min(columnCount, rowCount); i++) int rowInit = columnIndex - rowIndex;
int end = Math.Min(columnCount + rowInit, rowCount);
for (var i = 0; rowInit + i < end; i++)
{ {
result[columnIndex - rowIndex + i, i] = Data[columnIndex + i]; result[rowInit + i, i] = Data[columnIndex + i];
} }
} }
else else

39
src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs

@ -44,16 +44,17 @@ namespace MathNet.Numerics.LinearAlgebra.Single
[Serializable] [Serializable]
public class DiagonalMatrix : Matrix public class DiagonalMatrix : Matrix
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="DiagonalMatrix"/> class. This matrix is square with a given size. /// Initializes a new instance of the <see cref="DiagonalMatrix"/> class. This matrix is square with a given size.
/// </summary> /// </summary>
/// <param name="order">the size of the square matrix.</param> /// <param name="order">the size of the square matrix.</param>
/// <exception cref="ArgumentException"> /// <exception cref="ArgumentException">
/// If <paramref name="order"/> is less than one. /// If <paramref name="order"/> is less than one.
/// </exception> /// </exception>
public DiagonalMatrix(int order) : base(order) public DiagonalMatrix(int order)
: base(order)
{ {
Data = new float[order * order]; Data = new float[order];
} }
/// <summary> /// <summary>
@ -65,7 +66,8 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="columns"> /// <param name="columns">
/// The number of columns. /// The number of columns.
/// </param> /// </param>
public DiagonalMatrix(int rows, int columns) : base(rows, columns) public DiagonalMatrix(int rows, int columns)
: base(rows, columns)
{ {
Data = new float[Math.Min(rows, columns)]; Data = new float[Math.Min(rows, columns)];
} }
@ -80,7 +82,8 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// The number of columns. /// The number of columns.
/// </param> /// </param>
/// <param name="value">The value which we assign to each element of the matrix.</param> /// <param name="value">The value which we assign to each element of the matrix.</param>
public DiagonalMatrix(int rows, int columns, float value) : base(rows, columns) public DiagonalMatrix(int rows, int columns, float value)
: base(rows, columns)
{ {
Data = new float[Math.Min(rows, columns)]; Data = new float[Math.Min(rows, columns)];
for (var i = 0; i < Data.Length; i++) for (var i = 0; i < Data.Length; i++)
@ -96,7 +99,8 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="rows">The number of rows.</param> /// <param name="rows">The number of rows.</param>
/// <param name="columns">The number of columns.</param> /// <param name="columns">The number of columns.</param>
/// <param name="diagonalArray">The one dimensional array which contain diagonal elements.</param> /// <param name="diagonalArray">The one dimensional array which contain diagonal elements.</param>
public DiagonalMatrix(int rows, int columns, float[] diagonalArray) : base(rows, columns) public DiagonalMatrix(int rows, int columns, float[] diagonalArray)
: base(rows, columns)
{ {
Data = diagonalArray; Data = diagonalArray;
} }
@ -108,7 +112,8 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <exception cref="IndexOutOfRangeException">When <paramref name="array"/> contains an off-diagonal element.</exception> /// <exception cref="IndexOutOfRangeException">When <paramref name="array"/> contains an off-diagonal element.</exception>
/// <exception cref="IndexOutOfRangeException">Depending on the implementation, an <see cref="IndexOutOfRangeException"/> /// <exception cref="IndexOutOfRangeException">Depending on the implementation, an <see cref="IndexOutOfRangeException"/>
/// may be thrown if one of the indices is outside the dimensions of the matrix.</exception> /// may be thrown if one of the indices is outside the dimensions of the matrix.</exception>
public DiagonalMatrix(float[,] array) : this(array.GetLength(0), array.GetLength(1)) public DiagonalMatrix(float[,] array)
: this(array.GetLength(0), array.GetLength(1))
{ {
var rows = array.GetLength(0); var rows = array.GetLength(0);
var columns = array.GetLength(1); var columns = array.GetLength(1);
@ -350,7 +355,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
} }
else else
{ {
Control.LinearAlgebraProvider.AddArrays(Data, diagOther.Data, diagResult.Data); Control.LinearAlgebraProvider.AddArrays(Data, diagOther.Data, diagResult.Data);
} }
} }
@ -450,7 +455,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
{ {
throw new ArgumentException(Resources.ArgumentArraysSameLength, "source"); throw new ArgumentException(Resources.ArgumentArraysSameLength, "source");
} }
Buffer.BlockCopy(source, 0, Data, 0, source.Length * Constants.SizeOfFloat); Buffer.BlockCopy(source, 0, Data, 0, source.Length * Constants.SizeOfFloat);
} }
@ -478,7 +483,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "source"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "source");
} }
Buffer.BlockCopy(denseSource.Data, 0, Data, 0, denseSource.Data.Length * Constants.SizeOfFloat); Buffer.BlockCopy(denseSource.Data, 0, Data, 0, denseSource.Data.Length * Constants.SizeOfFloat);
} }
/// <summary> /// <summary>
@ -979,7 +984,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
{ {
if (RowCount != ColumnCount) if (RowCount != ColumnCount)
{ {
throw new ArgumentException(Resources.ArgumentMatrixSquare); throw new ArgumentException(Resources.ArgumentMatrixSquare);
} }
var inverse = (DiagonalMatrix)Clone(); var inverse = (DiagonalMatrix)Clone();
@ -1188,16 +1193,20 @@ namespace MathNet.Numerics.LinearAlgebra.Single
if (rowIndex > columnIndex && columnIndex + columnCount > rowIndex) if (rowIndex > columnIndex && columnIndex + columnCount > rowIndex)
{ {
for (var i = 0; rowIndex - columnIndex + i < Math.Min(columnCount, rowCount); i++) int columnInit = rowIndex - columnIndex;
int end = Math.Min(columnCount, rowCount + columnInit);
for (var i = 0; columnInit + i < end; i++)
{ {
result[i, rowIndex - columnIndex + i] = Data[rowIndex + i]; result[i, columnInit + i] = Data[rowIndex + i];
} }
} }
else if (rowIndex < columnIndex && rowIndex + rowCount > columnIndex) else if (rowIndex < columnIndex && rowIndex + rowCount > columnIndex)
{ {
for (var i = 0; rowIndex - columnIndex + i < Math.Min(columnCount, rowCount); i++) int rowInit = columnIndex - rowIndex;
int end = Math.Min(columnCount + rowInit, rowCount);
for (var i = 0; rowInit + i < end; i++)
{ {
result[columnIndex - rowIndex + i, i] = Data[columnIndex + i]; result[rowInit + i, i] = Data[columnIndex + i];
} }
} }
else else

Loading…
Cancel
Save