// // Math.NET Numerics, part of the Math.NET Project // http://numerics.mathdotnet.com // http://github.com/mathnet/mathnet-numerics // // Copyright (c) 2009-2015 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.Factorization; using IStation.Numerics.LinearAlgebra.Single.Factorization; using IStation.Numerics.LinearAlgebra.Storage; namespace IStation.Numerics.LinearAlgebra.Single { /// /// float version of the class. /// [Serializable] public abstract class Matrix : Matrix { /// /// Initializes a new instance of the Matrix class. /// protected Matrix(MatrixStorage storage) : base(storage) { } /// /// Set all values whose absolute value is smaller than the threshold to zero. /// public override void CoerceZero(double threshold) { MapInplace(x => Math.Abs(x) < threshold ? 0f : x, Zeros.AllowSkip); } /// /// Returns the conjugate transpose of this matrix. /// /// The conjugate transpose of this matrix. public sealed override Matrix ConjugateTranspose() { return Transpose(); } /// /// Puts the conjugate transpose of this matrix into the result matrix. /// public sealed override void ConjugateTranspose(Matrix result) { Transpose(result); } /// /// Complex conjugates each element of this matrix and place the results into the result matrix. /// /// The result of the conjugation. protected sealed override void DoConjugate(Matrix result) { if (ReferenceEquals(this, result)) { return; } CopyTo(result); } /// /// 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) { Map(x => -x, result, Zeros.AllowSkip); } /// /// Add a scalar to each element of the matrix and stores the result in the result vector. /// /// The scalar to add. /// The matrix to store the result of the addition. protected override void DoAdd(float scalar, Matrix result) { Map(x => x + scalar, result, Zeros.Include); } /// /// 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) { Map2((x, y) => x + y, other, result, Zeros.AllowSkip); } /// /// Subtracts a scalar from each element of the vector and stores the result in the result vector. /// /// The scalar to subtract. /// The matrix to store the result of the subtraction. protected override void DoSubtract(float scalar, Matrix result) { Map(x => x - scalar, result, Zeros.Include); } /// /// 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) { Map2((x, y) => x - y, other, result, Zeros.AllowSkip); } /// /// 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(float scalar, Matrix result) { Map(x => x*scalar, result, Zeros.AllowSkip); } /// /// 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 = 0.0f; for (var j = 0; j < ColumnCount; j++) { s += At(i, j)*rightSide[j]; } result[i] = s; } } /// /// 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 i = 0; i < RowCount; i++) { for (var j = 0; j < other.ColumnCount; j++) { var s = 0.0f; for (var k = 0; k < ColumnCount; k++) { s += At(i, k)*other.At(k, j); } result.At(i, j, 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(float divisor, Matrix result) { Map(x => x/divisor, result, divisor == 0.0f ? Zeros.Include : Zeros.AllowSkip); } /// /// Divides a scalar by each element of the matrix and stores the result in the result matrix. /// /// The scalar to divide by each element of the matrix. /// The matrix to store the result of the division. protected override void DoDivideByThis(float dividend, Matrix result) { Map(x => dividend/x, result, Zeros.Include); } /// /// 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 = 0.0f; for (var k = 0; k < ColumnCount; k++) { s += At(i, k)*other.At(j, k); } result.At(i, j, s); } } } /// /// Multiplies this matrix with the conjugate transpose of another matrix and places the results into the result matrix. /// /// The matrix to multiply with. /// The result of the multiplication. protected sealed override void DoConjugateTransposeAndMultiply(Matrix other, Matrix result) { DoTransposeAndMultiply(other, result); } /// /// 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 = 0.0f; for (var k = 0; k < RowCount; k++) { s += At(k, i)*other.At(k, j); } 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 sealed override void DoConjugateTransposeThisAndMultiply(Matrix other, Matrix result) { DoTransposeThisAndMultiply(other, result); } /// /// 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 j = 0; j < ColumnCount; j++) { var s = 0.0f; for (var i = 0; i < RowCount; i++) { s += At(i, j)*rightSide[i]; } result[j] = s; } } /// /// Multiplies the conjugate 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 sealed override void DoConjugateTransposeThisAndMultiply(Vector rightSide, Vector result) { DoTransposeThisAndMultiply(rightSide, result); } /// /// Computes the canonical modulus, where the result has the sign of the divisor, /// for the given divisor each element of the matrix. /// /// The scalar denominator to use. /// Matrix to store the results in. protected override void DoModulus(float divisor, Matrix result) { Map(x => Euclid.Modulus(x, divisor), result, Zeros.Include); } /// /// Computes the canonical modulus, where the result has the sign of the divisor, /// for the given dividend for each element of the matrix. /// /// The scalar numerator to use. /// A vector to store the results in. protected override void DoModulusByThis(float dividend, Matrix result) { Map(x => Euclid.Modulus(dividend, x), result, Zeros.Include); } /// /// Computes the remainder (% operator), where the result has the sign of the dividend, /// for the given divisor each element of the matrix. /// /// The scalar denominator to use. /// Matrix to store the results in. protected override void DoRemainder(float divisor, Matrix result) { Map(x => Euclid.Remainder(x, divisor), result, Zeros.Include); } /// /// Computes the remainder (% operator), where the result has the sign of the dividend, /// for the given dividend for each element of the matrix. /// /// The scalar numerator to use. /// A vector to store the results in. protected override void DoRemainderByThis(float dividend, Matrix result) { Map(x => Euclid.Remainder(dividend, x), result, Zeros.Include); } /// /// 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) { Map2((x, y) => x*y, other, result, Zeros.AllowSkip); } /// /// 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 divisor, Matrix result) { Map2((x, y) => x/y, divisor, result, Zeros.Include); } /// /// Pointwise raise this matrix to an exponent and store the result into the result matrix. /// /// The exponent to raise this matrix values to. /// The matrix to store the result of the pointwise power. protected override void DoPointwisePower(float exponent, Matrix result) { Map(x => (float)Math.Pow(x, exponent), result, exponent > 0.0f ? Zeros.AllowSkip : Zeros.Include); } /// /// Pointwise raise this matrix to an exponent and store the result into the result matrix. /// /// The exponent to raise this matrix values to. /// The vector to store the result of the pointwise power. protected override void DoPointwisePower(Matrix exponent, Matrix result) { Map2((x, y) => (float)Math.Pow(x, y), result, Zeros.Include); } /// /// Pointwise canonical modulus, where the result has the sign of the divisor, /// of this matrix with another matrix and stores the result into the result matrix. /// /// The pointwise denominator matrix to use /// The result of the modulus. protected override void DoPointwiseModulus(Matrix divisor, Matrix result) { Map2(Euclid.Modulus, divisor, result, Zeros.Include); } /// /// Pointwise remainder (% operator), where the result has the sign of the dividend, /// of this matrix with another matrix and stores the result into the result matrix. /// /// The pointwise denominator matrix to use /// The result of the modulus. protected override void DoPointwiseRemainder(Matrix divisor, Matrix result) { Map2(Euclid.Remainder, divisor, result, Zeros.Include); } /// /// Pointwise applies the exponential function to each value and stores the result into the result matrix. /// /// The matrix to store the result. protected override void DoPointwiseExp(Matrix result) { Map(x => (float)Math.Exp(x), result, Zeros.Include); } /// /// Pointwise applies the natural logarithm function to each value and stores the result into the result matrix. /// /// The matrix to store the result. protected override void DoPointwiseLog(Matrix result) { Map(x => (float)Math.Log(x), result, Zeros.Include); } protected override void DoPointwiseAbs(Matrix result) { Map(x => (float)Math.Abs(x), result, Zeros.AllowSkip); } protected override void DoPointwiseAcos(Matrix result) { Map(x => (float)Math.Acos(x), result, Zeros.Include); } protected override void DoPointwiseAsin(Matrix result) { Map(x => (float)Math.Asin(x), result, Zeros.AllowSkip); } protected override void DoPointwiseAtan(Matrix result) { Map(x => (float)Math.Atan(x), result, Zeros.AllowSkip); } protected override void DoPointwiseAtan2(Matrix other, Matrix result) { Map2((x, y) => (float)Math.Atan2((double)x, (double)y), other, result, Zeros.Include); } protected override void DoPointwiseCeiling(Matrix result) { Map(x => (float)Math.Ceiling(x), result, Zeros.AllowSkip); } protected override void DoPointwiseCos(Matrix result) { Map(x => (float)Math.Cos(x), result, Zeros.Include); } protected override void DoPointwiseCosh(Matrix result) { Map(x => (float)Math.Cosh(x), result, Zeros.Include); } protected override void DoPointwiseFloor(Matrix result) { Map(x => (float)Math.Floor(x), result, Zeros.AllowSkip); } protected override void DoPointwiseLog10(Matrix result) { Map(x => (float)Math.Log10(x), result, Zeros.Include); } protected override void DoPointwiseRound(Matrix result) { Map(x => (float)Math.Round(x), result, Zeros.AllowSkip); } protected override void DoPointwiseSign(Matrix result) { Map(x => (float)Math.Sign(x), result, Zeros.AllowSkip); } protected override void DoPointwiseSin(Matrix result) { Map(x => (float)Math.Sin(x), result, Zeros.AllowSkip); } protected override void DoPointwiseSinh(Matrix result) { Map(x => (float)Math.Sinh(x), result, Zeros.AllowSkip); } protected override void DoPointwiseSqrt(Matrix result) { Map(x => (float)Math.Sqrt(x), result, Zeros.AllowSkip); } protected override void DoPointwiseTan(Matrix result) { Map(x => (float)Math.Tan(x), result, Zeros.AllowSkip); } protected override void DoPointwiseTanh(Matrix result) { Map(x => (float)Math.Tanh(x), result, Zeros.AllowSkip); } /// /// Computes the Moore-Penrose Pseudo-Inverse of this matrix. /// public override Matrix PseudoInverse() { var svd = Svd(true); var w = svd.W; var s = svd.S; float tolerance = (float)(Math.Max(RowCount, ColumnCount) * svd.L2Norm * Precision.SinglePrecision); for (int i = 0; i < s.Count; i++) { s[i] = s[i] < tolerance ? 0 : 1/s[i]; } w.SetDiagonal(s); return (svd.U * (w * svd.VT)).Transpose(); } /// /// Computes the trace of this matrix. /// /// The trace of this matrix /// If the matrix is not square public override float Trace() { if (RowCount != ColumnCount) { throw new ArgumentException("Matrix must be square."); } var sum = 0.0f; for (var i = 0; i < RowCount; i++) { sum += At(i, i); } return sum; } protected override void DoPointwiseMinimum(float scalar, Matrix result) { Map(x => Math.Min(scalar, x), result, scalar >= 0d ? Zeros.AllowSkip : Zeros.Include); } protected override void DoPointwiseMaximum(float scalar, Matrix result) { Map(x => Math.Max(scalar, x), result, scalar <= 0d ? Zeros.AllowSkip : Zeros.Include); } protected override void DoPointwiseAbsoluteMinimum(float scalar, Matrix result) { float absolute = Math.Abs(scalar); Map(x => Math.Min(absolute, Math.Abs(x)), result, Zeros.AllowSkip); } protected override void DoPointwiseAbsoluteMaximum(float scalar, Matrix result) { float absolute = Math.Abs(scalar); Map(x => Math.Max(absolute, Math.Abs(x)), result, Zeros.Include); } protected override void DoPointwiseMinimum(Matrix other, Matrix result) { Map2(Math.Min, other, result, Zeros.AllowSkip); } protected override void DoPointwiseMaximum(Matrix other, Matrix result) { Map2(Math.Max, other, result, Zeros.AllowSkip); } protected override void DoPointwiseAbsoluteMinimum(Matrix other, Matrix result) { Map2((x, y) => Math.Min(Math.Abs(x), Math.Abs(y)), other, result, Zeros.AllowSkip); } protected override void DoPointwiseAbsoluteMaximum(Matrix other, Matrix result) { Map2((x, y) => Math.Max(Math.Abs(x), Math.Abs(y)), other, result, Zeros.AllowSkip); } /// Calculates the induced L1 norm of this matrix. /// The maximum absolute column sum of the matrix. public override double L1Norm() { var norm = 0d; for (var j = 0; j < ColumnCount; j++) { var s = 0d; for (var i = 0; i < RowCount; i++) { s += Math.Abs(At(i, j)); } norm = Math.Max(norm, s); } return norm; } /// Calculates the induced infinity norm of this matrix. /// The maximum absolute row sum of the matrix. public override double InfinityNorm() { var norm = 0d; for (var i = 0; i < RowCount; i++) { var s = 0d; for (var j = 0; j < ColumnCount; j++) { s += Math.Abs(At(i, j)); } norm = Math.Max(norm, s); } return norm; } /// Calculates the entry-wise Frobenius norm of this matrix. /// The square root of the sum of the squared values. public override double FrobeniusNorm() { var transpose = Transpose(); var aat = this*transpose; var norm = 0d; for (var i = 0; i < RowCount; i++) { norm += aat.At(i, i); } return Math.Sqrt(norm); } /// /// Calculates the p-norms of all row vectors. /// Typical values for p are 1.0 (L1, Manhattan norm), 2.0 (L2, Euclidean norm) and positive infinity (infinity norm) /// public override Vector RowNorms(double norm) { if (norm <= 0.0) { throw new ArgumentOutOfRangeException(nameof(norm), "Value must be positive."); } var ret = new double[RowCount]; if (norm == 2.0) { Storage.FoldByRowUnchecked(ret, (s, x) => s + x*x, (x, c) => Math.Sqrt(x), ret, Zeros.AllowSkip); } else if (norm == 1.0) { Storage.FoldByRowUnchecked(ret, (s, x) => s + Math.Abs(x), (x, c) => x, ret, Zeros.AllowSkip); } else if (double.IsPositiveInfinity(norm)) { Storage.FoldByRowUnchecked(ret, (s, x) => Math.Max(s, Math.Abs(x)), (x, c) => x, ret, Zeros.AllowSkip); } else { double invnorm = 1.0/norm; Storage.FoldByRowUnchecked(ret, (s, x) => s + Math.Pow(Math.Abs(x), norm), (x, c) => Math.Pow(x, invnorm), ret, Zeros.AllowSkip); } return Vector.Build.Dense(ret); } /// /// Calculates the p-norms of all column vectors. /// Typical values for p are 1.0 (L1, Manhattan norm), 2.0 (L2, Euclidean norm) and positive infinity (infinity norm) /// public override Vector ColumnNorms(double norm) { if (norm <= 0.0) { throw new ArgumentOutOfRangeException(nameof(norm), "Value must be positive."); } var ret = new double[ColumnCount]; if (norm == 2.0) { Storage.FoldByColumnUnchecked(ret, (s, x) => s + x*x, (x, c) => Math.Sqrt(x), ret, Zeros.AllowSkip); } else if (norm == 1.0) { Storage.FoldByColumnUnchecked(ret, (s, x) => s + Math.Abs(x), (x, c) => x, ret, Zeros.AllowSkip); } else if (double.IsPositiveInfinity(norm)) { Storage.FoldByColumnUnchecked(ret, (s, x) => Math.Max(s, Math.Abs(x)), (x, c) => x, ret, Zeros.AllowSkip); } else { double invnorm = 1.0/norm; Storage.FoldByColumnUnchecked(ret, (s, x) => s + Math.Pow(Math.Abs(x), norm), (x, c) => Math.Pow(x, invnorm), ret, Zeros.AllowSkip); } return Vector.Build.Dense(ret); } /// /// Normalizes all row vectors to a unit p-norm. /// Typical values for p are 1.0 (L1, Manhattan norm), 2.0 (L2, Euclidean norm) and positive infinity (infinity norm) /// public sealed override Matrix NormalizeRows(double norm) { var norminv = ((DenseVectorStorage)RowNorms(norm).Storage).Data; for (int i = 0; i < norminv.Length; i++) { norminv[i] = norminv[i] == 0d ? 1d : 1d/norminv[i]; } var result = Build.SameAs(this, RowCount, ColumnCount); Storage.MapIndexedTo(result.Storage, (i, j, x) => (float)norminv[i]*x, Zeros.AllowSkip, ExistingData.AssumeZeros); return result; } /// /// Normalizes all column vectors to a unit p-norm. /// Typical values for p are 1.0 (L1, Manhattan norm), 2.0 (L2, Euclidean norm) and positive infinity (infinity norm) /// public sealed override Matrix NormalizeColumns(double norm) { var norminv = ((DenseVectorStorage)ColumnNorms(norm).Storage).Data; for (int i = 0; i < norminv.Length; i++) { norminv[i] = norminv[i] == 0d ? 1d : 1d/norminv[i]; } var result = Build.SameAs(this, RowCount, ColumnCount); Storage.MapIndexedTo(result.Storage, (i, j, x) => (float)norminv[j]*x, Zeros.AllowSkip, ExistingData.AssumeZeros); return result; } /// /// Calculates the value sum of each row vector. /// public override Vector RowSums() { var ret = new float[RowCount]; Storage.FoldByRowUnchecked(ret, (s, x) => s + x, (x, c) => x, ret, Zeros.AllowSkip); return Vector.Build.Dense(ret); } /// /// Calculates the absolute value sum of each row vector. /// public override Vector RowAbsoluteSums() { var ret = new float[RowCount]; Storage.FoldByRowUnchecked(ret, (s, x) => s + Math.Abs(x), (x, c) => x, ret, Zeros.AllowSkip); return Vector.Build.Dense(ret); } /// /// Calculates the value sum of each column vector. /// public override Vector ColumnSums() { var ret = new float[ColumnCount]; Storage.FoldByColumnUnchecked(ret, (s, x) => s + x, (x, c) => x, ret, Zeros.AllowSkip); return Vector.Build.Dense(ret); } /// /// Calculates the absolute value sum of each column vector. /// public override Vector ColumnAbsoluteSums() { var ret = new float[ColumnCount]; Storage.FoldByColumnUnchecked(ret, (s, x) => s + Math.Abs(x), (x, c) => x, ret, Zeros.AllowSkip); return Vector.Build.Dense(ret); } /// /// Evaluates whether this matrix is Hermitian (conjugate symmetric). /// public sealed override bool IsHermitian() { return IsSymmetric(); } public override Cholesky Cholesky() { return UserCholesky.Create(this); } public override LU LU() { return UserLU.Create(this); } public override QR QR(QRMethod method = QRMethod.Thin) { return UserQR.Create(this, method); } public override GramSchmidt GramSchmidt() { return UserGramSchmidt.Create(this); } public override Svd Svd(bool computeVectors = true) { return UserSvd.Create(this, computeVectors); } public override Evd Evd(Symmetricity symmetricity = Symmetricity.Unknown) { return UserEvd.Create(this, symmetricity); } } }