//
// 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-2013 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.
//
namespace MathNet.Numerics.LinearAlgebra.Complex32
{
using System;
using Generic;
using Numerics;
using Properties;
using Storage;
///
/// Complex32 version of the class.
///
[Serializable]
public abstract class Matrix : Matrix
{
///
/// Initializes a new instance of the Matrix class.
///
protected Matrix(MatrixStorage storage)
: base(storage)
{
}
/// Calculates the L1 norm.
/// The L1 norm of the matrix.
public override Complex32 L1Norm()
{
var norm = 0.0f;
for (var j = 0; j < ColumnCount; j++)
{
var s = 0.0f;
for (var i = 0; i < RowCount; i++)
{
s += At(i, j).Magnitude;
}
norm = Math.Max(norm, s);
}
return norm;
}
///
/// Returns the conjugate transpose of this matrix.
///
/// The conjugate transpose of this matrix.
public override Matrix ConjugateTranspose()
{
var ret = CreateMatrix(ColumnCount, RowCount);
for (var j = 0; j < ColumnCount; j++)
{
for (var i = 0; i < RowCount; i++)
{
ret.At(j, i, At(i, j).Conjugate());
}
}
return ret;
}
/// Calculates the Frobenius norm of this matrix.
/// The Frobenius norm of this matrix.
public override Complex32 FrobeniusNorm()
{
var transpose = ConjugateTranspose();
var aat = this * transpose;
var norm = 0.0f;
for (var i = 0; i < RowCount; i++)
{
norm += aat.At(i, i).Magnitude;
}
norm = Convert.ToSingle(Math.Sqrt(norm));
return norm;
}
/// Calculates the infinity norm of this matrix.
/// The infinity norm of this matrix.
public override Complex32 InfinityNorm()
{
var norm = 0.0f;
for (var i = 0; i < RowCount; i++)
{
var s = 0.0f;
for (var j = 0; j < ColumnCount; j++)
{
s += At(i, j).Magnitude;
}
norm = Math.Max(norm, s);
}
return norm;
}
///
/// Adds another matrix to this matrix.
///
/// The matrix to add to this matrix.
/// The matrix to store the result of the addition.
/// If the other matrix is .
/// If the two matrices don't have the same dimensions.
protected override void DoAdd(Matrix other, Matrix result)
{
for (var i = 0; i < RowCount; i++)
{
for (var j = 0; j < ColumnCount; j++)
{
result.At(i, j, At(i, j) + other.At(i, j));
}
}
}
///
/// Subtracts another matrix from this matrix.
///
/// The matrix to subtract to this matrix.
/// The matrix to store the result of subtraction.
/// If the other matrix is .
/// If the two matrices don't have the same dimensions.
protected override void DoSubtract(Matrix other, Matrix result)
{
for (var i = 0; i < RowCount; i++)
{
for (var j = 0; j < ColumnCount; j++)
{
result.At(i, j, At(i, j) - other.At(i, j));
}
}
}
///
/// Multiplies each element of the matrix by a scalar and places results into the result matrix.
///
/// The scalar to multiply the matrix with.
/// The matrix to store the result of the multiplication.
protected override void DoMultiply(Complex32 scalar, Matrix result)
{
for (var i = 0; i < RowCount; i++)
{
for (var j = 0; j < ColumnCount; j++)
{
result.At(i, j, At(i, j) * scalar);
}
}
}
///
/// Multiplies this matrix with a vector and places the results into the result vector.
///
/// The vector to multiply with.
/// The result of the multiplication.
protected override void DoMultiply(Vector rightSide, Vector result)
{
for (var i = 0; i < RowCount; i++)
{
var s = Complex32.Zero;
for (var j = 0; j != ColumnCount; j++)
{
s += At(i, j) * rightSide[j];
}
result[i] = s;
}
}
///
/// Divides each element of the matrix by a scalar and places results into the result matrix.
///
/// The scalar to divide the matrix with.
/// The matrix to store the result of the division.
protected override void DoDivide(Complex32 scalar, Matrix result)
{
DoMultiply(1.0f / scalar, result);
}
///
/// Multiplies this matrix with another matrix and places the results into the result matrix.
///
/// The matrix to multiply with.
/// The result of the multiplication.
protected override void DoMultiply(Matrix other, Matrix result)
{
for (var j = 0; j < RowCount; j++)
{
for (var i = 0; i != other.ColumnCount; i++)
{
var s = Complex32.Zero;
for (var l = 0; l < ColumnCount; l++)
{
s += At(j, l) * other.At(l, i);
}
result.At(j, i, s);
}
}
}
///
/// Multiplies this matrix with transpose of another matrix and places the results into the result matrix.
///
/// The matrix to multiply with.
/// The result of the multiplication.
protected override void DoTransposeAndMultiply(Matrix other, Matrix result)
{
for (var j = 0; j < other.RowCount; j++)
{
for (var i = 0; i < RowCount; i++)
{
var s = Complex32.Zero;
for (var l = 0; l < ColumnCount; l++)
{
s += At(i, l) * other.At(j, l);
}
result.At(i, j, s);
}
}
}
///
/// Multiplies the transpose of this matrix with another matrix and places the results into the result matrix.
///
/// The matrix to multiply with.
/// The result of the multiplication.
protected override void DoTransposeThisAndMultiply(Matrix other, Matrix result)
{
for (var j = 0; j < other.ColumnCount; j++)
{
for (var i = 0; i < ColumnCount; i++)
{
var s = Complex32.Zero;
for (var l = 0; l < RowCount; l++)
{
s += At(l, i) * other.At(l, j);
}
result.At(i, j, s);
}
}
}
///
/// Multiplies the transpose of this matrix with a vector and places the results into the result vector.
///
/// The vector to multiply with.
/// The result of the multiplication.
protected override void DoTransposeThisAndMultiply(Vector rightSide, Vector result)
{
for (var i = 0; i < ColumnCount; i++)
{
var s = Complex32.Zero;
for (var j = 0; j != RowCount; j++)
{
s += At(j, i) * rightSide[j];
}
result[i] = s;
}
}
///
/// Negate each element of this matrix and place the results into the result matrix.
///
/// The result of the negation.
protected override void DoNegate(Matrix result)
{
for (var i = 0; i < RowCount; i++)
{
for (var j = 0; j != ColumnCount; j++)
{
result.At(i, j, -At(i, j));
}
}
}
///
/// Complex conjugates each element of this matrix and place the results into the result matrix.
///
/// The result of the conjugation.
protected override void DoConjugate(Matrix result)
{
for (var i = 0; i < RowCount; i++)
{
for (var j = 0; j != ColumnCount; j++)
{
result.At(i, j, At(i, j).Conjugate());
}
}
}
///
/// Pointwise multiplies this matrix with another matrix and stores the result into the result matrix.
///
/// The matrix to pointwise multiply with this one.
/// The matrix to store the result of the pointwise multiplication.
protected override void DoPointwiseMultiply(Matrix other, Matrix result)
{
for (var j = 0; j < ColumnCount; j++)
{
for (var i = 0; i < RowCount; i++)
{
result.At(i, j, At(i, j) * other.At(i, j));
}
}
}
///
/// Pointwise divide this matrix by another matrix and stores the result into the result matrix.
///
/// The matrix to pointwise divide this one by.
/// The matrix to store the result of the pointwise division.
protected override void DoPointwiseDivide(Matrix other, Matrix result)
{
for (var j = 0; j < ColumnCount; j++)
{
for (var i = 0; i < RowCount; i++)
{
result.At(i, j, At(i, j) / other.At(i, j));
}
}
}
///
/// Computes the modulus for each element of the matrix.
///
/// The divisor to use.
/// Matrix to store the results in.
protected override void DoModulus(Complex32 divisor, Matrix result)
{
throw new NotImplementedException();
}
///
/// Computes the trace of this matrix.
///
/// The trace of this matrix
/// If the matrix is not square
public override Complex32 Trace()
{
if (RowCount != ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSquare);
}
var sum = Complex32.Zero;
for (var i = 0; i < RowCount; i++)
{
sum += At(i, i);
}
return sum;
}
}
}