From 646c62bfd68fc327c92ee005d014af6a1ea03a1c Mon Sep 17 00:00:00 2001 From: manyue Date: Sat, 14 Jul 2012 18:06:16 +0100 Subject: [PATCH] Issue #35 Diagonal Matrix SubMatrix bug fix (cherry picked from commit fc2b42c673e847311cb48538f83955c1caaccf09) --- .../LinearAlgebra/Complex/DiagonalMatrix.cs | 14 ++++--- .../LinearAlgebra/Complex32/DiagonalMatrix.cs | 14 ++++--- .../LinearAlgebra/Double/DiagonalMatrix.cs | 14 ++++--- .../LinearAlgebra/Single/DiagonalMatrix.cs | 39 ++++++++++++------- 4 files changed, 51 insertions(+), 30 deletions(-) diff --git a/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs index aa52f271..acbcbb50 100644 --- a/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs @@ -54,7 +54,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// public DiagonalMatrix(int order) : base(order) { - Data = new Complex[order * order]; + Data = new Complex[order]; } /// @@ -1194,16 +1194,20 @@ namespace MathNet.Numerics.LinearAlgebra.Complex 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) { - 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 diff --git a/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs index fa1ee64b..7db86d9c 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs @@ -55,7 +55,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 public DiagonalMatrix(int order) : base(order) { - Data = new Complex32[order * order]; + Data = new Complex32[order]; } /// @@ -1199,16 +1199,20 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 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) { - 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 diff --git a/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs index a3bccb01..26a86bf5 100644 --- a/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs @@ -53,7 +53,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// public DiagonalMatrix(int order) : base(order) { - Data = new double[order * order]; + Data = new double[order]; } /// @@ -1188,16 +1188,20 @@ namespace MathNet.Numerics.LinearAlgebra.Double 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) { - 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 diff --git a/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs index bd5f28fd..ee16feaa 100644 --- a/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs @@ -44,16 +44,17 @@ namespace MathNet.Numerics.LinearAlgebra.Single [Serializable] public class DiagonalMatrix : Matrix { - /// + /// /// Initializes a new instance of the class. This matrix is square with a given size. /// /// the size of the square matrix. /// /// If is less than one. /// - public DiagonalMatrix(int order) : base(order) + public DiagonalMatrix(int order) + : base(order) { - Data = new float[order * order]; + Data = new float[order]; } /// @@ -65,7 +66,8 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// /// The number of columns. /// - 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)]; } @@ -80,7 +82,8 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// The number of columns. /// /// The value which we assign to each element of the matrix. - 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)]; for (var i = 0; i < Data.Length; i++) @@ -96,7 +99,8 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// The number of rows. /// The number of columns. /// The one dimensional array which contain diagonal elements. - public DiagonalMatrix(int rows, int columns, float[] diagonalArray) : base(rows, columns) + public DiagonalMatrix(int rows, int columns, float[] diagonalArray) + : base(rows, columns) { Data = diagonalArray; } @@ -108,7 +112,8 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// When contains an off-diagonal element. /// Depending on the implementation, an /// may be thrown if one of the indices is outside the dimensions of the matrix. - 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 columns = array.GetLength(1); @@ -350,7 +355,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single } 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"); } - + 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"); } - Buffer.BlockCopy(denseSource.Data, 0, Data, 0, denseSource.Data.Length * Constants.SizeOfFloat); + Buffer.BlockCopy(denseSource.Data, 0, Data, 0, denseSource.Data.Length * Constants.SizeOfFloat); } /// @@ -979,7 +984,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single { if (RowCount != ColumnCount) { - throw new ArgumentException(Resources.ArgumentMatrixSquare); + throw new ArgumentException(Resources.ArgumentMatrixSquare); } var inverse = (DiagonalMatrix)Clone(); @@ -1188,16 +1193,20 @@ namespace MathNet.Numerics.LinearAlgebra.Single 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) { - 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