forked from tsai/mathnet-numerics
9 changed files with 504 additions and 52 deletions
@ -0,0 +1,179 @@ |
|||
// <copyright file="DenseLU.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
//
|
|||
// Copyright (c) 2009-2010 Math.NET
|
|||
//
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
//
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
//
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
|
|||
namespace MathNet.Numerics.LinearAlgebra.Double.Factorization |
|||
{ |
|||
using System; |
|||
using Properties; |
|||
|
|||
/// <summary>
|
|||
/// <para>A class which encapsulates the functionality of an LU factorization.</para>
|
|||
/// <para>For a matrix A, the LU factorization is a pair of lower triangular matrix L and
|
|||
/// upper triangular matrix U so that A = L*U.</para>
|
|||
/// </summary>
|
|||
/// <remarks>
|
|||
/// The computation of the LU factorization is done at construction time.
|
|||
/// </remarks>
|
|||
public class DenseLU : LU |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="DenseLU"/> class. This object will compute the
|
|||
/// LU factorization when the constructor is called and cache it's factorization.
|
|||
/// </summary>
|
|||
/// <param name="matrix">The matrix to factor.</param>
|
|||
/// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <b>null</b>.</exception>
|
|||
/// <exception cref="ArgumentException">If <paramref name="matrix"/> is not a square matrix.</exception>
|
|||
public DenseLU(DenseMatrix matrix) |
|||
{ |
|||
if (matrix == null) |
|||
{ |
|||
throw new ArgumentNullException("matrix"); |
|||
} |
|||
|
|||
if (matrix.RowCount != matrix.ColumnCount) |
|||
{ |
|||
throw new ArgumentException(Resources.ArgumentMatrixSquare); |
|||
} |
|||
|
|||
// Create an array for the pivot indices.
|
|||
mPivots = new int[matrix.RowCount]; |
|||
|
|||
// Create a new matrix for the LU factors, then perform factorization (while overwriting).
|
|||
var factors = (DenseMatrix)matrix.Clone(); |
|||
Control.LinearAlgebraProvider.LUFactor(factors.Data, factors.RowCount, mPivots); |
|||
mFactors = factors; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Solves a system of linear equations, <b>AX = B</b>, with A LU factorized.
|
|||
/// </summary>
|
|||
/// <param name="input">The right hand side <see cref="Matrix"/>, <b>B</b>.</param>
|
|||
/// <param name="result">The left hand side <see cref="Matrix"/>, <b>X</b>.</param>
|
|||
public override void Solve(Matrix input, Matrix result) |
|||
{ |
|||
// Check for proper arguments.
|
|||
if (input == null) |
|||
{ |
|||
throw new ArgumentNullException("input"); |
|||
} |
|||
|
|||
if (result == null) |
|||
{ |
|||
throw new ArgumentNullException("result"); |
|||
} |
|||
|
|||
// Check for proper dimensions.
|
|||
if (result.RowCount != input.RowCount) |
|||
{ |
|||
throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension); |
|||
} |
|||
|
|||
if (result.ColumnCount != input.ColumnCount) |
|||
{ |
|||
throw new ArgumentException(Resources.ArgumentMatrixSameColumnDimension); |
|||
} |
|||
|
|||
if (input.RowCount != mFactors.RowCount) |
|||
{ |
|||
throw new ArgumentException(Resources.ArgumentMatrixDimensions); |
|||
} |
|||
|
|||
var dinput = input as DenseMatrix; |
|||
if (dinput == null) |
|||
{ |
|||
throw new NotImplementedException("Can only do LU factorization for dense matrices at the moment."); |
|||
} |
|||
|
|||
var dresult = result as DenseMatrix; |
|||
if (dresult == null) |
|||
{ |
|||
throw new NotImplementedException("Can only do LU factorization for dense matrices at the moment."); |
|||
} |
|||
|
|||
// Copy the contents of input to result.
|
|||
Buffer.BlockCopy(dinput.Data, 0, dresult.Data, 0, dinput.Data.Length * Constants.SizeOfDouble); |
|||
|
|||
// LU solve by overwriting result.
|
|||
var dfactors = mFactors as DenseMatrix; |
|||
throw new NotImplementedException(); |
|||
//Control.LinearAlgebraProvider.LUSolveFactored(dfactors.Data, dfactors.RowCount, dresult.Data, dresult.RowCount, dresult.ColumnCount);
|
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Solves a system of linear equations, <b>Ax = b</b>, with A LU factorized.
|
|||
/// </summary>
|
|||
/// <param name="input">The right hand side vector, <b>b</b>.</param>
|
|||
/// <param name="result">The left hand side <see cref="Matrix"/>, <b>x</b>.</param>
|
|||
public override void Solve(Vector input, Vector result) |
|||
{ |
|||
// Check for proper arguments.
|
|||
if (input == null) |
|||
{ |
|||
throw new ArgumentNullException("input"); |
|||
} |
|||
|
|||
if (result == null) |
|||
{ |
|||
throw new ArgumentNullException("result"); |
|||
} |
|||
|
|||
// Check for proper dimensions.
|
|||
if (input.Count != result.Count) |
|||
{ |
|||
throw new ArgumentException(Resources.ArgumentVectorsSameLength); |
|||
} |
|||
|
|||
if (input.Count != mFactors.RowCount) |
|||
{ |
|||
throw new ArgumentException(Resources.ArgumentMatrixDimensions); |
|||
} |
|||
|
|||
var dinput = input as DenseVector; |
|||
if (dinput == null) |
|||
{ |
|||
throw new NotImplementedException("Can only do LU factorization for dense vectors at the moment."); |
|||
} |
|||
|
|||
var dresult = result as DenseVector; |
|||
if (dresult == null) |
|||
{ |
|||
throw new NotImplementedException("Can only do LU factorization for dense vectors at the moment."); |
|||
} |
|||
|
|||
// Copy the contents of input to result.
|
|||
Buffer.BlockCopy(dinput.Data, 0, dresult.Data, 0, dinput.Data.Length * Constants.SizeOfDouble); |
|||
|
|||
// LU solve by overwriting result.
|
|||
var dfactors = mFactors as DenseMatrix; |
|||
throw new NotImplementedException(); |
|||
//Control.LinearAlgebraProvider.LUSolveFactored(dfactors.Data, dfactors.RowCount, dresult.Data, dresult.Count, 1);
|
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,171 @@ |
|||
// <copyright file="LU.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
//
|
|||
// Copyright (c) 2009-2010 Math.NET
|
|||
//
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
//
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
//
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
|
|||
namespace MathNet.Numerics.LinearAlgebra.Double.Factorization |
|||
{ |
|||
using System; |
|||
using Properties; |
|||
|
|||
/// <summary>
|
|||
/// <para>A class which encapsulates the functionality of an LU factorization.</para>
|
|||
/// <para>For a matrix A, the LU factorization is a pair of lower triangular matrix L and
|
|||
/// upper triangular matrix U so that A = L*U.</para>
|
|||
/// <para>In the Math.Net implementation we also store a set of pivot elements for increased
|
|||
/// numerical stability. The pivot elements encode a permutation matrix P such that P*A = L*U.</para>
|
|||
/// </summary>
|
|||
/// <remarks>
|
|||
/// The computation of the LU factorization is done at construction time.
|
|||
/// </remarks>
|
|||
public abstract class LU |
|||
{ |
|||
/// <summary>
|
|||
/// Stores both the L and U factors in the same matrix..
|
|||
/// </summary>
|
|||
protected Matrix mFactors; |
|||
|
|||
/// <summary>
|
|||
/// Stores the pivot indices of the LU factorization.
|
|||
/// </summary>
|
|||
protected int[] mPivots; |
|||
|
|||
/// <summary>
|
|||
/// Internal method which routes the call to perform the LU factorization to the appropriate class.
|
|||
/// </summary>
|
|||
/// <param name="matrix">The matrix to factor.</param>
|
|||
/// <returns>An LU factorization object.</returns>
|
|||
internal static LU Create(Matrix matrix) |
|||
{ |
|||
var dense = matrix as DenseMatrix; |
|||
if (dense != null) |
|||
{ |
|||
return new DenseLU(dense); |
|||
} |
|||
|
|||
throw new NotImplementedException(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns the lower triangular factor.
|
|||
/// </summary>
|
|||
public virtual Matrix L |
|||
{ |
|||
get |
|||
{ |
|||
Matrix result = mFactors.GetLowerTriangle(); |
|||
for (int i = 0; i < result.RowCount; i++) |
|||
{ |
|||
result.At(i, i, 1); |
|||
} |
|||
return result; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns the upper triangular factor.
|
|||
/// </summary>
|
|||
public virtual Matrix U |
|||
{ |
|||
get { return mFactors.GetUpperTriangle(); } |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// The determinant of the matrix for which the LU factorization was computed.
|
|||
/// </summary>
|
|||
public virtual double Determinant |
|||
{ |
|||
get |
|||
{ |
|||
double det = 1.0; |
|||
for (int j = 0; j < mFactors.RowCount; j++) |
|||
{ |
|||
if (mPivots[j] != j) |
|||
{ |
|||
det = -det * mFactors.At(j, j); |
|||
} |
|||
else |
|||
{ |
|||
det *= mFactors.At(j, j); |
|||
} |
|||
} |
|||
return det; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Solves a system of linear equations, <b>AX = B</b>, with A LU factorized.
|
|||
/// </summary>
|
|||
/// <param name="input">The right hand side <see cref="Matrix"/>, <b>B</b>.</param>
|
|||
/// <returns>The left hand side <see cref="Matrix"/>, <b>X</b>.</returns>
|
|||
public virtual Matrix Solve(Matrix input) |
|||
{ |
|||
// Check for proper arguments.
|
|||
if (input == null) |
|||
{ |
|||
throw new ArgumentNullException("input"); |
|||
} |
|||
|
|||
var X = input.CreateMatrix(input.RowCount, input.ColumnCount); |
|||
Solve(input, X); |
|||
return X; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Solves a system of linear equations, <b>AX = B</b>, with A LU factorized.
|
|||
/// </summary>
|
|||
/// <param name="input">The right hand side <see cref="Matrix"/>, <b>B</b>.</param>
|
|||
/// <param name="result">The left hand side <see cref="Matrix"/>, <b>X</b>.</param>
|
|||
public abstract void Solve(Matrix input, Matrix result); |
|||
|
|||
/// <summary>
|
|||
/// Solves a system of linear equations, <b>Ax = b</b>, with A LU factorized.
|
|||
/// </summary>
|
|||
/// <param name="input">The right hand side vector, <b>b</b>.</param>
|
|||
/// <returns>The left hand side <see cref="Vector"/>, <b>x</b>.</returns>
|
|||
public virtual Vector Solve(Vector input) |
|||
{ |
|||
// Check for proper arguments.
|
|||
if (input == null) |
|||
{ |
|||
throw new ArgumentNullException("input"); |
|||
} |
|||
|
|||
var x = input.CreateVector(input.Count); |
|||
Solve(input, x); |
|||
return x; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Solves a system of linear equations, <b>Ax = b</b>, with A LU factorized.
|
|||
/// </summary>
|
|||
/// <param name="input">The right hand side vector, <b>b</b>.</param>
|
|||
/// <param name="result">The left hand side <see cref="Matrix"/>, <b>x</b>.</param>
|
|||
public abstract void Solve(Vector input, Vector result); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue