// // 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.Complex { using Distributions; using Generic; using Storage; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Numerics; using Threading; /// /// A vector using dense storage. /// [Serializable] [DebuggerDisplay("DenseVector {Count}-Complex")] public class DenseVector : Vector { /// /// Number of elements /// readonly int _length; /// /// Gets the vector's data. /// readonly Complex[] _values; /// /// Create a new dense vector straight from an initialized vector storage instance. /// The storage is used directly without copying. /// Intended for advanced scenarios where you're working directly with /// storage for performance or interop reasons. /// public DenseVector(DenseVectorStorage storage) : base(storage) { _length = storage.Length; _values = storage.Data; } /// /// Create a new dense vector with the given length. /// All cells of the vector will be initialized to zero. /// Zero-length vectors are not supported. /// /// If length is less than one. public DenseVector(int length) : this(new DenseVectorStorage(length)) { } /// /// Create a new dense vector with the given length. /// All cells of the vector will be initialized with the provided value. /// Zero-length vectors are not supported. /// /// If length is less than one. public DenseVector(int length, Complex value) : this(length) { for (var index = 0; index < _values.Length; index++) { _values[index] = value; } } /// /// Create a new dense vector as a copy of the given other vector. /// This new vector will be independent from the other vector. /// A new memory block will be allocated for storing the matrix. /// public DenseVector(Vector other) : this(other.Count) { other.Storage.CopyToUnchecked(Storage, skipClearing: true); } /// /// Create a new dense vector directly binding to a raw array. /// The array is used directly without copying. /// Very efficient, but changes to the array and the vector will affect each other. /// public DenseVector(Complex[] storage) : this(new DenseVectorStorage(storage.Length, storage)) { } /// /// Create a new dense vector with values sampled from the provided random distribution. /// public static DenseVector CreateRandom(int size, IContinuousDistribution distribution) { var storage = new DenseVectorStorage(size); for (var i = 0; i < storage.Data.Length; i++) { storage.Data[i] = new Complex(distribution.Sample(), distribution.Sample()); } return new DenseVector(storage); } /// /// Gets the vector's data. /// /// The vector's data. public Complex[] Values { get { return _values; } } /// /// Returns a reference to the internal data structure. /// /// The DenseVector whose internal data we are /// returning. /// /// A reference to the internal date of the given vector. /// public static explicit operator Complex[](DenseVector vector) { if (vector == null) { throw new ArgumentNullException(); } return vector.Values; } /// /// Returns a vector bound directly to a reference of the provided array. /// /// The array to bind to the DenseVector object. /// /// A DenseVector whose values are bound to the given array. /// public static implicit operator DenseVector(Complex[] array) { if (array == null) { throw new ArgumentNullException(); } return new DenseVector(array); } /// /// Creates a matrix with the given dimensions using the same storage type /// as this vector. /// /// /// The number of rows. /// /// /// The number of columns. /// /// /// A matrix with the given dimensions. /// public override Matrix CreateMatrix(int rows, int columns) { return new DenseMatrix(rows, columns); } /// /// Creates a Vector of the given size using the same storage type /// as this vector. /// /// /// The size of the Vector to create. /// /// /// The new Vector. /// public override Vector CreateVector(int size) { return new DenseVector(size); } /// /// Adds a scalar to each element of the vector and stores the result in the result vector. /// /// The scalar to add. /// The vector to store the result of the addition. protected override void DoAdd(Complex scalar, Vector result) { var dense = result as DenseVector; if (dense == null) { base.DoAdd(scalar, result); } else { CommonParallel.For(0, _values.Length, 4096, (a, b) => { for (int i = a; i < b; i++) { dense._values[i] = _values[i] + scalar; } }); } } /// /// Adds another vector to this vector and stores the result into the result vector. /// /// The vector to add to this one. /// The vector to store the result of the addition. protected override void DoAdd(Vector other, Vector result) { var otherDense = other as DenseVector; var resultDense = result as DenseVector; if (otherDense == null || resultDense == null) { base.DoAdd(other, result); } else { Control.LinearAlgebraProvider.AddArrays(_values, otherDense._values, resultDense._values); } } /// /// Adds two Vectors together and returns the results. /// /// One of the vectors to add. /// The other vector to add. /// The result of the addition. /// If and are not the same size. /// If or is . public static Vector operator +(DenseVector leftSide, DenseVector rightSide) { if (leftSide == null) { throw new ArgumentNullException("leftSide"); } return leftSide.Add(rightSide); } /// /// Subtracts a scalar from each element of the vector and stores the result in the result vector. /// /// The scalar to subtract. /// The vector to store the result of the subtraction. protected override void DoSubtract(Complex scalar, Vector result) { var dense = result as DenseVector; if (dense == null) { base.DoSubtract(scalar, result); } else { CommonParallel.For(0, _values.Length, 4096, (a, b) => { for (int i = a; i < b; i++) { dense._values[i] = _values[i] - scalar; } }); } } /// /// Subtracts another vector from this vector and stores the result into the result vector. /// /// The vector to subtract from this one. /// The vector to store the result of the subtraction. protected override void DoSubtract(Vector other, Vector result) { var otherDense = other as DenseVector; var resultDense = result as DenseVector; if (otherDense == null || resultDense == null) { base.DoSubtract(other, result); } else { Control.LinearAlgebraProvider.SubtractArrays(_values, otherDense._values, resultDense._values); } } /// /// Returns a Vector containing the negated values of . /// /// The vector to get the values from. /// A vector containing the negated values as . /// If is . public static Vector operator -(DenseVector rightSide) { if (rightSide == null) { throw new ArgumentNullException("rightSide"); } return rightSide.Negate(); } /// /// Subtracts two Vectors and returns the results. /// /// The vector to subtract from. /// The vector to subtract. /// The result of the subtraction. /// If and are not the same size. /// If or is . public static Vector operator -(DenseVector leftSide, DenseVector rightSide) { if (leftSide == null) { throw new ArgumentNullException("leftSide"); } return leftSide.Subtract(rightSide); } /// /// Negates vector and saves result to /// /// Target vector protected override void DoNegate(Vector result) { var denseResult = result as DenseVector; if (denseResult == null) { base.DoNegate(result); } else { Control.LinearAlgebraProvider.ScaleArray(-Complex.One, _values, denseResult.Values); } } /// /// Multiplies a scalar to each element of the vector and stores the result in the result vector. /// /// The scalar to multiply. /// The vector to store the result of the multiplication. /// protected override void DoMultiply(Complex scalar, Vector result) { var denseResult = result as DenseVector; if (denseResult == null) { base.DoMultiply(scalar, result); } else { Control.LinearAlgebraProvider.ScaleArray(scalar, _values, denseResult.Values); } } /// /// Computes the dot product between this vector and another vector. /// /// The other vector to add. /// s /// The result of the addition. protected override Complex DoDotProduct(Vector other) { var denseVector = other as DenseVector; return denseVector == null ? base.DoDotProduct(other) : Control.LinearAlgebraProvider.DotProduct(_values, denseVector.Values); } /// /// Multiplies a vector with a complex. /// /// The vector to scale. /// The Complex value. /// The result of the multiplication. /// If is . public static DenseVector operator *(DenseVector leftSide, Complex rightSide) { if (leftSide == null) { throw new ArgumentNullException("leftSide"); } return (DenseVector)leftSide.Multiply(rightSide); } /// /// Multiplies a vector with a complex. /// /// The Complex value. /// The vector to scale. /// The result of the multiplication. /// If is . public static DenseVector operator *(Complex leftSide, DenseVector rightSide) { if (rightSide == null) { throw new ArgumentNullException("rightSide"); } return (DenseVector)rightSide.Multiply(leftSide); } /// /// Computes the dot product between two Vectors. /// /// The left row vector. /// The right column vector. /// The dot product between the two vectors. /// If and are not the same size. /// If or is . public static Complex operator *(DenseVector leftSide, DenseVector rightSide) { if (leftSide == null) { throw new ArgumentNullException("leftSide"); } return leftSide.DotProduct(rightSide); } /// /// Divides a vector with a complex. /// /// The vector to divide. /// The Complex value. /// The result of the division. /// If is . public static DenseVector operator /(DenseVector leftSide, Complex rightSide) { if (leftSide == null) { throw new ArgumentNullException("leftSide"); } return (DenseVector)leftSide.Divide(rightSide); } /// /// Returns the index of the absolute minimum element. /// /// The index of absolute minimum element. public override int AbsoluteMinimumIndex() { var index = 0; var min = _values[index].Magnitude; for (var i = 1; i < _length; i++) { var test = _values[i].Magnitude; if (test < min) { index = i; min = test; } } return index; } /// /// Returns the value of the absolute minimum element. /// /// The value of the absolute minimum element. public override Complex AbsoluteMinimum() { return _values[AbsoluteMinimumIndex()].Magnitude; } /// /// Returns the value of the absolute maximum element. /// /// The value of the absolute maximum element. public override Complex AbsoluteMaximum() { return _values[AbsoluteMaximumIndex()].Magnitude; } /// /// Returns the index of the absolute maximum element. /// /// The index of absolute maximum element. public override int AbsoluteMaximumIndex() { var index = 0; var max = _values[index].Magnitude; for (var i = 1; i < _length; i++) { var test = _values[i].Magnitude; if (test > max) { index = i; max = test; } } return index; } /// /// Computes the sum of the vector's elements. /// /// The sum of the vector's elements. public override Complex Sum() { var sum = Complex.Zero; for (var i = 0; i < _length; i++) { sum += _values[i]; } return sum; } /// /// Computes the sum of the absolute value of the vector's elements. /// /// The sum of the absolute value of the vector's elements. public override Complex SumMagnitudes() { var sum = Complex.Zero; for (var i = 0; i < _length; i++) { sum += _values[i].Magnitude; } return sum; } /// /// Pointwise divide this vector with another vector and stores the result into the result vector. /// /// The vector to pointwise divide this one by. /// The vector to store the result of the pointwise division. protected override void DoPointwiseMultiply(Vector other, Vector result) { var denseOther = other as DenseVector; var denseResult = result as DenseVector; if (denseOther == null || denseResult == null) { base.DoPointwiseMultiply(other, result); } else { Control.LinearAlgebraProvider.PointWiseMultiplyArrays(_values, denseOther._values, denseResult._values); } } /// /// Pointwise divide this vector with another vector and stores the result into the result vector. /// /// The vector to pointwise divide this one by. /// The vector to store the result of the pointwise division. /// protected override void DoPointwiseDivide(Vector other, Vector result) { var denseOther = other as DenseVector; var denseResult = result as DenseVector; if (denseOther == null || denseResult == null) { base.DoPointwiseDivide(other, result); } else { Control.LinearAlgebraProvider.PointWiseDivideArrays(_values, denseOther._values, denseResult._values); } } /// /// Outer product of two vectors /// /// First vector /// Second vector /// Matrix M[i,j] = u[i]*v[j] /// If the u vector is . /// If the v vector is . public static DenseMatrix OuterProduct(DenseVector u, DenseVector v) { if (u == null) { throw new ArgumentNullException("u"); } if (v == null) { throw new ArgumentNullException("v"); } var matrix = new DenseMatrix(u.Count, v.Count); CommonParallel.For(0, u.Count, (a, b) => { for (int i = a; i < b; i++) { for (var j = 0; j < v.Count; j++) { matrix.At(i, j, u._values[i]*v._values[j]); } } }); return matrix; } /// /// Outer product of this and another vector. /// /// The vector to operate on. /// /// Matrix M[i,j] = this[i] * v[j]. /// /// public Matrix OuterProduct(DenseVector v) { return OuterProduct(this, v); } /// /// Computes the p-Norm. /// /// The p value. /// Scalar ret = (sum(abs(this[i])^p))^(1/p) public override Complex Norm(double p) { if (p < 0.0) { throw new ArgumentOutOfRangeException("p"); } if (1.0 == p) { return SumMagnitudes(); } if (2.0 == p) { return _values.Aggregate(Complex.Zero, SpecialFunctions.Hypotenuse).Magnitude; } if (Double.IsPositiveInfinity(p)) { return CommonParallel.Aggregate(_values, (i, v) => v.Magnitude, Math.Max, 0d); } var sum = 0.0; for (var i = 0; i < _length; i++) { sum += Math.Pow(_values[i].Magnitude, p); } return Math.Pow(sum, 1.0 / p); } #region Parse Functions /// /// Creates a Complex dense vector based on a string. The string can be in the following formats (without the /// quotes): 'n', 'n;n;..', '(n;n;..)', '[n;n;...]', where n is a Complex. /// /// /// A Complex dense vector containing the values specified by the given string. /// /// /// The string to parse. /// public static DenseVector Parse(string value) { return Parse(value, null); } /// /// Creates a Complex dense vector based on a string. The string can be in the following formats (without the /// quotes): 'n', 'n;n;..', '(n;n;..)', '[n;n;...]', where n is a double. /// /// /// A Complex dense vector containing the values specified by the given string. /// /// /// the string to parse. /// /// /// An that supplies culture-specific formatting information. /// public static DenseVector Parse(string value, IFormatProvider formatProvider) { if (value == null) { throw new ArgumentNullException("value"); } value = value.Trim(); if (value.Length == 0) { throw new FormatException(); } // strip out parens if (value.StartsWith("(", StringComparison.Ordinal)) { if (!value.EndsWith(")", StringComparison.Ordinal)) { throw new FormatException(); } value = value.Substring(1, value.Length - 2).Trim(); } if (value.StartsWith("[", StringComparison.Ordinal)) { if (!value.EndsWith("]", StringComparison.Ordinal)) { throw new FormatException(); } value = value.Substring(1, value.Length - 2).Trim(); } // parsing var strongTokens = value.Split(new[] { formatProvider.GetTextInfo().ListSeparator }, StringSplitOptions.RemoveEmptyEntries); var data = new List(); foreach (string strongToken in strongTokens) { var weakTokens = strongToken.Split(new[] { " ", "\t" }, StringSplitOptions.RemoveEmptyEntries); string current = string.Empty; for (int i = 0; i < weakTokens.Length; i++) { current += weakTokens[i]; if (current.EndsWith("+") || current.EndsWith("-") || current.StartsWith("(") && !current.EndsWith(")")) { continue; } var ahead = i < weakTokens.Length - 1 ? weakTokens[i + 1] : string.Empty; if (ahead.StartsWith("+") || ahead.StartsWith("-")) { continue; } data.Add(current.ToComplex(formatProvider)); current = string.Empty; } if (current != string.Empty) { throw new FormatException(); } } if (data.Count == 0) { throw new FormatException(); } return new DenseVector(data.ToArray()); } /// /// Converts the string representation of a complex dense vector to double-precision dense vector equivalent. /// A return value indicates whether the conversion succeeded or failed. /// /// /// A string containing a complex vector to convert. /// /// /// The parsed value. /// /// /// If the conversion succeeds, the result will contain a complex number equivalent to value. /// Otherwise the result will be null. /// public static bool TryParse(string value, out DenseVector result) { return TryParse(value, null, out result); } /// /// Converts the string representation of a complex dense vector to double-precision dense vector equivalent. /// A return value indicates whether the conversion succeeded or failed. /// /// /// A string containing a complex vector to convert. /// /// /// An that supplies culture-specific formatting information about value. /// /// /// The parsed value. /// /// /// If the conversion succeeds, the result will contain a complex number equivalent to value. /// Otherwise the result will be null. /// public static bool TryParse(string value, IFormatProvider formatProvider, out DenseVector result) { bool ret; try { result = Parse(value, formatProvider); ret = true; } catch (ArgumentNullException) { result = null; ret = false; } catch (FormatException) { result = null; ret = false; } return ret; } #endregion /// /// Conjugates vector and save result to /// /// Target vector protected override void DoConjugate(Vector result) { var resultDense = result as DenseVector; if (resultDense == null) { base.DoConjugate(result); return; } CommonParallel.For(0, _length, 4096, (a, b) => { for (int i = a; i < b; i++) { resultDense._values[i] = _values[i].Conjugate(); } }); } } }