//
// 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.
//
namespace MathNet.Numerics.LinearAlgebra.Complex
{
using System;
using System.Collections.Generic;
using System.Numerics;
using Distributions;
using Generic;
using NumberTheory;
using Properties;
using Threading;
///
/// A vector using dense storage.
///
public class DenseVector : Vector
{
///
/// Initializes a new instance of the class with a given size.
///
///
/// the size of the vector.
///
///
/// If is less than one.
///
public DenseVector(int size) : base(size)
{
Data = new Complex[size];
}
///
/// Initializes a new instance of the class with a given size
/// and each element set to the given value;
///
///
/// the size of the vector.
///
///
/// the value to set each element to.
///
///
/// If is less than one.
///
public DenseVector(int size, Complex value) : this(size)
{
for (var index = 0; index < Data.Length; index++)
{
Data[index] = value;
}
}
///
/// Initializes a new instance of the class by
/// copying the values from another.
///
///
/// The vector to create the new vector from.
///
public DenseVector(Vector other) : this(other.Count)
{
CommonParallel.For(
0,
Data.Length,
index => this[index] = other[index]);
}
///
/// Initializes a new instance of the class by
/// copying the values from another.
///
///
/// The vector to create the new vector from.
///
public DenseVector(DenseVector other) : this(other.Count)
{
CommonParallel.For(
0,
Data.Length,
index => Data[index] = other.Data[index]);
}
///
/// Initializes a new instance of the class for an array.
///
/// The array to create this vector from.
/// The vector does not copy the array, but keeps a reference to it. Any
/// changes to the vector will also change the array.
public DenseVector(Complex[] array) : base(array.Length)
{
Data = array;
}
///
/// Gets the vector's internal data.
///
/// The vector's internal data.
/// Changing values in the array also changes the corresponding value in vector. Use with care.
internal Complex[] Data
{
get;
private set;
}
///
/// 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 implicit operator Complex[](DenseVector vector)
{
if (vector == null)
{
throw new ArgumentNullException();
}
return vector.Data;
}
///
/// 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);
}
///
/// Create a matrix based on this vector in column form (one single column).
///
/// This vector as a column matrix.
public override Matrix ToColumnMatrix()
{
var matrix = new DenseMatrix(Count, 1);
for (var i = 0; i < Data.Length; i++)
{
matrix[i, 0] = Data[i];
}
return matrix;
}
///
/// Create a matrix based on this vector in row form (one single row).
///
/// This vector as a row matrix.
public override Matrix ToRowMatrix()
{
var matrix = new DenseMatrix(1, Count);
for (var i = 0; i < Data.Length; i++)
{
matrix[0, i] = Data[i];
}
return matrix;
}
/// Gets or sets the value at the given .
/// The index of the value to get or set.
/// The value of the vector at the given .
/// If is negative or
/// greater than the size of the vector.
public override Complex this[int index]
{
get
{
return Data[index];
}
set
{
Data[index] = value;
}
}
///
/// 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 complex to each element of the vector.
///
/// The complex to add.
/// A copy of the vector with the complex added.
public override Vector Add(Complex complex)
{
if (complex == Complex.Zero)
{
return Clone();
}
var copy = (DenseVector)Clone();
CommonParallel.For(
0,
Data.Length,
index => copy.Data[index] += complex);
return copy;
}
///
/// Adds a complex to each element of the vector and stores the result in the result vector.
///
/// The complex to add.
/// The vector to store the result of the addition.
/// If the result vector is .
/// If this vector and are not the same size.
public override void Add(Complex complex, Vector result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
var dense = result as DenseVector;
if (dense == null)
{
base.Add(complex, result);
}
else
{
CommonParallel.For(
0,
Data.Length,
index => dense.Data[index] = Data[index] + complex);
}
}
///
/// Adds another vector to this vector.
///
/// The vector to add to this one.
/// A new vector containing the sum of both vectors.
/// If the other vector is .
/// If this vector and are not the same size.
public override Vector Add(Vector other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
var denseVector = other as DenseVector;
if (denseVector == null)
{
return base.Add(other);
}
var copy = (DenseVector)Clone();
Control.LinearAlgebraProvider.AddVectorToScaledVector(copy.Data, Complex.One, denseVector.Data);
return copy;
}
///
/// 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.
/// If the other vector is .
/// If the result vector is .
/// If this vector and are not the same size.
/// If this vector and are not the same size.
public override void Add(Vector other, Vector result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
if (ReferenceEquals(this, result) || ReferenceEquals(other, result))
{
var tmp = Add(other);
tmp.CopyTo(result);
}
else
{
var rdense = result as DenseVector;
var odense = other as DenseVector;
if (rdense != null && odense != null)
{
CopyTo(result);
Control.LinearAlgebraProvider.AddVectorToScaledVector(rdense.Data, Complex.One, odense.Data);
}
else
{
CommonParallel.For(
0,
Data.Length,
index => result[index] = Data[index] + other[index]);
}
}
}
///
/// Returns a Vector containing the same values of .
///
/// This method is included for completeness.
/// The vector to get the values from.
/// A vector containing a the same values as .
/// If is .
public static Vector operator +(DenseVector rightSide)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
return rightSide.Plus();
}
///
/// 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 (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
if (leftSide.Count != rightSide.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rightSide");
}
return leftSide.Add(rightSide);
}
///
/// Subtracts a complex from each element of the vector.
///
/// The complex to subtract.
/// A new vector containing the subtraction of this vector and the complex.
public override Vector Subtract(Complex complex)
{
if (complex == Complex.Zero)
{
return Clone();
}
var copy = (DenseVector)Clone();
CommonParallel.For(
0,
Data.Length,
index => copy.Data[index] -= complex);
return copy;
}
///
/// Subtracts a complex from each element of the vector and stores the result in the result vector.
///
/// The complex to subtract.
/// The vector to store the result of the subtraction.
/// If the result vector is .
/// If this vector and are not the same size.
public override void Subtract(Complex complex, Vector result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
var dense = result as DenseVector;
if (dense == null)
{
base.Subtract(complex, result);
}
else
{
CommonParallel.For(
0,
Data.Length,
index => dense.Data[index] = Data[index] - complex);
}
}
///
/// Subtracts another vector from this vector.
///
/// The vector to subtract from this one.
/// A new vector containing the subtraction of the the two vectors.
/// If the other vector is .
/// If this vector and are not the same size.
public override Vector Subtract(Vector other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
var denseVector = other as DenseVector;
if (denseVector == null)
{
return base.Subtract(other);
}
var copy = (DenseVector)Clone();
Control.LinearAlgebraProvider.AddVectorToScaledVector(copy.Data, -Complex.One, denseVector.Data);
return copy;
}
///
/// Subtracts another vector to 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.
/// If the other vector is .
/// If the result vector is .
/// If this vector and are not the same size.
/// If this vector and are not the same size.
public override void Subtract(Vector other, Vector result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
if (ReferenceEquals(this, result) || ReferenceEquals(other, result))
{
var tmp = Subtract(other);
tmp.CopyTo(result);
}
else
{
var rdense = result as DenseVector;
var odense = other as DenseVector;
if (rdense != null && odense != null)
{
CopyTo(result);
Control.LinearAlgebraProvider.AddVectorToScaledVector(rdense.Data, -Complex.One, odense.Data);
}
else
{
CommonParallel.For(
0,
Data.Length,
index => result[index] = Data[index] - other[index]);
}
}
}
///
/// 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 (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
if (leftSide.Count != rightSide.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rightSide");
}
return leftSide.Subtract(rightSide);
}
///
/// Returns a negated vector.
///
/// The negated vector.
/// Added as an alternative to the unary negation operator.
public override Vector Negate()
{
var result = new DenseVector(Count);
CommonParallel.For(
0,
Data.Length,
index => result[index] = -Data[index]);
return result;
}
///
/// Multiplies a complex to each element of the vector.
///
/// The complex to multiply.
/// A new vector that is the multiplication of the vector and the complex.
public override Vector Multiply(Complex complex)
{
if (complex == Complex.One)
{
return Clone();
}
var copy = (DenseVector)Clone();
Control.LinearAlgebraProvider.ScaleArray(complex, copy.Data);
return copy;
}
///
/// Computes the dot product between this vector and another vector.
///
/// The other vector to add.
/// The result of the addition.
/// If is not of the same size.
/// If is .
public override Complex DotProduct(Vector other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
var denseVector = other as DenseVector;
if (denseVector == null)
{
return base.DotProduct(other);
}
return Control.LinearAlgebraProvider.DotProduct(Data, denseVector.Data);
}
///
/// 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 (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
if (leftSide.Count != rightSide.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rightSide");
}
return Control.LinearAlgebraProvider.DotProduct(leftSide.Data, rightSide.Data);
}
///
/// 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.Multiply(Complex.One / rightSide);
}
///
/// Returns the index of the absolute minimum element.
///
/// The index of absolute minimum element.
public override int AbsoluteMinimumIndex()
{
var index = 0;
var min = Data[index].Magnitude;
for (var i = 1; i < Count; i++)
{
var test = Data[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 double AbsoluteMinimum()
{
return Data[AbsoluteMinimumIndex()].Magnitude;
}
///
/// Returns the value of the absolute maximum element.
///
/// The value of the absolute maximum element.
public override double AbsoluteMaximum()
{
return Data[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 = Data[index].Magnitude;
for (var i = 1; i < Count; i++)
{
var test = Data[i].Magnitude;
if (test > max)
{
index = i;
max = test;
}
}
return index;
}
///
/// Creates a vector containing specified elements.
///
/// The first element to begin copying from.
/// The number of elements to copy.
/// A vector containing a copy of the specified elements.
/// - If is not positive or
/// greater than or equal to the size of the vector.
/// - If + is greater than or equal to the size of the vector.
///
/// If is not positive.
public override Vector SubVector(int index, int length)
{
if (index < 0 || index >= Count)
{
throw new ArgumentOutOfRangeException("index");
}
if (length <= 0)
{
throw new ArgumentOutOfRangeException("length");
}
if (index + length > Count)
{
throw new ArgumentOutOfRangeException("length");
}
var result = new DenseVector(length);
CommonParallel.For(
index,
index + length,
i => result.Data[i - index] = Data[i]);
return result;
}
///
/// Set the values of this vector to the given values.
///
/// The array containing the values to use.
/// If is .
/// If is not the same size as this vector.
public override void SetValues(Complex[] values)
{
if (values == null)
{
throw new ArgumentNullException("values");
}
if (values.Length != Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "values");
}
CommonParallel.For(
0,
values.Length,
i => Data[i] = values[i]);
}
///
/// Computes the sum of the vector's elements.
///
/// The sum of the vector's elements.
public override Complex Sum()
{
var result = Complex.Zero;
for (var i = 0; i < Count; i++)
{
result += Data[i];
}
return result;
}
///
/// 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 double SumMagnitudes()
{
double result = 0;
for (var i = 0; i < Count; i++)
{
result += Data[i].Magnitude;
}
return result;
}
///
/// Pointwise multiplies this vector with another vector.
///
/// The vector to pointwise multiply with this one.
/// A new vector which is the pointwise multiplication of the two vectors.
/// If the other vector is .
/// If this vector and are not the same size.
public override Vector PointwiseMultiply(Vector other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
var denseVector = other as DenseVector;
if (denseVector == null)
{
return base.PointwiseMultiply(other);
}
var copy = (DenseVector)Clone();
CommonParallel.For(
0,
Count,
index => copy[index] *= other[index]);
return copy;
}
///
/// Pointwise multiplies this vector with another vector and stores the result into the result vector.
///
/// The vector to pointwise multiply with this one.
/// The vector to store the result of the pointwise multiplication.
/// If the other vector is .
/// If the result vector is .
/// If this vector and are not the same size.
/// If this vector and are not the same size.
public override void PointwiseMultiply(Vector other, Vector result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
if (ReferenceEquals(this, result) || ReferenceEquals(other, result))
{
var tmp = PointwiseMultiply(other);
tmp.CopyTo(result);
}
else
{
var dense = result as DenseVector;
if (dense == null)
{
base.PointwiseMultiply(other, result);
}
else
{
CommonParallel.For(
0,
Data.Length,
index => dense.Data[index] = Data[index] * other[index]);
}
}
}
///
/// Pointwise divide this vector with another vector.
///
/// The vector to pointwise divide this one by.
/// A new vector which is the pointwise division of the two vectors.
/// If the other vector is .
/// If this vector and are not the same size.
public override Vector PointwiseDivide(Vector other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
var denseVector = other as DenseVector;
if (denseVector == null)
{
return base.PointwiseMultiply(other);
}
var copy = (DenseVector)Clone();
CommonParallel.For(
0,
Count,
index => copy[index] /= other[index]);
return copy;
}
///
/// 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.
/// If the other vector is .
/// If the result vector is .
/// If this vector and are not the same size.
/// If this vector and are not the same size.
public override void PointwiseDivide(Vector other, Vector result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
if (ReferenceEquals(this, result) || ReferenceEquals(other, result))
{
var tmp = PointwiseDivide(other);
tmp.CopyTo(result);
}
else
{
var dense = result as DenseVector;
if (dense == null)
{
base.PointwiseDivide(other, result);
}
else
{
CommonParallel.For(
0,
Data.Length,
index => dense.Data[index] = Data[index] / other[index]);
}
}
}
///
/// 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,
i =>
{
for (var j = 0; j < v.Count; j++)
{
matrix.At(i, j, u.Data[i] * v.Data[j]);
}
});
return matrix;
}
///
/// Generates a vector with random elements
///
/// Number of elements in the vector.
/// Continuous Random Distribution or Source
///
/// A vector with n-random elements distributed according
/// to the specified random distribution.
///
/// If the n vector is non positive.
public override Vector Random(int length, IContinuousDistribution randomDistribution)
{
if (length < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "length");
}
var v = (DenseVector)CreateVector(length);
for (var index = 0; index < v.Data.Length; index++)
{
v.Data[index] = randomDistribution.Sample();
}
return v;
}
///
/// Generates a vector with random elements
///
/// Number of elements in the vector.
/// Continuous Random Distribution or Source
///
/// A vector with n-random elements distributed according
/// to the specified random distribution.
///
/// If the n vector is non positive.
public override Vector Random(int length, IDiscreteDistribution randomDistribution)
{
if (length < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "length");
}
var v = (DenseVector)CreateVector(length);
for (var index = 0; index < v.Data.Length; index++)
{
v.Data[index] = randomDistribution.Sample();
}
return v;
}
///
/// Tensor Product (Dyadic) of this and another vector.
///
/// The vector to operate on.
///
/// Matrix M[i,j] = this[i] * v[j].
///
///
public Matrix TensorMultiply(DenseVector v)
{
return OuterProduct(this, v);
}
///
/// Computes the p-Norm.
///
/// The p value.
/// Scalar ret = (sum(abs(this[i])^p))^(1/p)
public override double Norm(double p)
{
if (p < 0.0)
{
throw new ArgumentOutOfRangeException("p");
}
if (1.0 == p)
{
return CommonParallel.Aggregate(
0,
Count,
index => Data[index].Magnitude);
}
if (Double.IsPositiveInfinity(p))
{
return CommonParallel.Select(
0,
Count,
(index, localData) => Math.Max(localData, Data[index].Magnitude),
Math.Max);
}
var sum = CommonParallel.Aggregate(
0,
Count,
index => Math.Pow(Data[index].Magnitude, p));
return Math.Pow(sum, 1.0 / p);
}
///
/// Normalizes this vector to a unit vector with respect to the p-norm.
///
///
/// The p value.
///
///
/// This vector normalized to a unit vector with respect to the p-norm.
///
public override Vector Normalize(double p)
{
if (p < 0.0)
{
throw new ArgumentOutOfRangeException("p");
}
var norm = Norm(p);
var clone = Clone();
if (norm == 0.0)
{
return clone;
}
clone.Multiply(1.0 / norm, clone);
return clone;
}
#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();
}
// keywords
var textInfo = formatProvider.GetTextInfo();
var keywords = new[] { textInfo.ListSeparator };
// lexing
var tokens = new LinkedList();
GlobalizationHelper.Tokenize(tokens.AddFirst(value), keywords, 0);
var token = tokens.First;
if (token == null || tokens.Count.IsEven())
{
throw new FormatException();
}
// parsing
var data = new Complex[(tokens.Count + 1) >> 1];
for (var i = 0; i < data.Length; i++)
{
if (token == null || token.Value == textInfo.ListSeparator)
{
throw new FormatException();
}
data[i] = token.Value.ToComplex(formatProvider);
token = token.Next;
if (token != null)
{
token = token.Next;
}
}
return new DenseVector(data);
}
///
/// 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
///
/// Returns the index of the absolute maximum element.
///
/// The index of absolute maximum element.
public override int MaximumIndex()
{
throw new NotSupportedException();
}
///
/// Returns the index of the minimum element.
///
/// The index of minimum element.
public override int MinimumIndex()
{
throw new NotSupportedException();
}
///
/// Resets all values to zero.
///
public override void Clear()
{
Array.Clear(Data, 0, Data.Length);
}
///
/// Conjugates vector and save result to
///
/// Target vector
public override void Conjugate(Vector target)
{
if (target == null)
{
throw new ArgumentNullException("target");
}
if (Count != target.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "target");
}
if (ReferenceEquals(this, target))
{
var tmp = CreateVector(Count);
Conjugate(tmp);
tmp.CopyTo(target);
}
var otherVector = target as DenseVector;
if (otherVector == null)
{
base.Conjugate(target);
}
else
{
CommonParallel.For(
0,
Count,
index => otherVector.Data[index] = Data[index].Conjugate());
}
}
#region Simple arithmetic of type T
///
/// Add two values T+T
///
/// Left operand value
/// Right operand value
/// Result of addition
protected sealed override Complex AddT(Complex val1, Complex val2)
{
return val1 + val2;
}
///
/// Subtract two values T-T
///
/// Left operand value
/// Right operand value
/// Result of subtract
protected sealed override Complex SubtractT(Complex val1, Complex val2)
{
return val1 - val2;
}
///
/// Multiply two values T*T
///
/// Left operand value
/// Right operand value
/// Result of multiplication
protected sealed override Complex MultiplyT(Complex val1, Complex val2)
{
return val1 * val2;
}
///
/// Divide two values T/T
///
/// Left operand value
/// Right operand value
/// Result of divide
protected sealed override Complex DivideT(Complex val1, Complex val2)
{
return val1 / val2;
}
///
/// Take absolute value
///
/// Source alue
/// True if one; otherwise false
protected sealed override double AbsoluteT(Complex val1)
{
return val1.Magnitude;
}
#endregion
}
}