//
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
//
// 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.
//
using System;
using IStation.Numerics.LinearAlgebra.Solvers;
namespace IStation.Numerics.LinearAlgebra.Complex32.Solvers
{
using Numerics;
///
/// A diagonal preconditioner. The preconditioner uses the inverse
/// of the matrix diagonal as preconditioning values.
///
public sealed class DiagonalPreconditioner : IPreconditioner
{
///
/// The inverse of the matrix diagonal.
///
Complex32[] _inverseDiagonals;
///
/// Returns the decomposed matrix diagonal.
///
/// The matrix diagonal.
internal DiagonalMatrix DiagonalEntries()
{
var result = new DiagonalMatrix(_inverseDiagonals.Length);
for (var i = 0; i < _inverseDiagonals.Length; i++)
{
result[i, i] = 1/_inverseDiagonals[i];
}
return result;
}
///
/// Initializes the preconditioner and loads the internal data structures.
///
///
/// The upon which this preconditioner is based.
/// If is .
/// If is not a square matrix.
public void Initialize(Matrix matrix)
{
if (matrix.RowCount != matrix.ColumnCount)
{
throw new ArgumentException("Matrix must be square.", nameof(matrix));
}
_inverseDiagonals = new Complex32[matrix.RowCount];
for (var i = 0; i < matrix.RowCount; i++)
{
_inverseDiagonals[i] = 1/matrix[i, i];
}
}
///
/// Approximates the solution to the matrix equation Ax = b.
///
/// The right hand side vector.
/// The left hand side vector. Also known as the result vector.
public void Approximate(Vector rhs, Vector lhs)
{
if (_inverseDiagonals == null)
{
throw new ArgumentException("The requested matrix does not exist.");
}
if ((lhs.Count != rhs.Count) || (lhs.Count != _inverseDiagonals.Length))
{
throw new ArgumentException("All vectors must have the same dimensionality.", nameof(rhs));
}
for (var i = 0; i < _inverseDiagonals.Length; i++)
{
lhs[i] = rhs[i]*_inverseDiagonals[i];
}
}
}
}