diff --git a/src/Numerics/LinearAlgebra/Generic/Vector.BCL.cs b/src/Numerics/LinearAlgebra/Generic/Vector.BCL.cs index daae62e8..8b3d5d1a 100644 --- a/src/Numerics/LinearAlgebra/Generic/Vector.BCL.cs +++ b/src/Numerics/LinearAlgebra/Generic/Vector.BCL.cs @@ -73,7 +73,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// Returns a hash code for this instance. /// /// - /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. + /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. /// public override sealed int GetHashCode() { @@ -86,11 +86,25 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// /// A that represents this instance. /// - public override string ToString() + public override sealed string ToString() { return ToString(null, null); } + /// + /// Returns a that represents this instance. + /// + /// + /// An that supplies culture-specific formatting information. + /// + /// + /// A that represents this instance. + /// + public string ToString(IFormatProvider formatProvider) + { + return ToString(null, formatProvider); + } + #if !PORTABLE #region ICloneable diff --git a/src/Numerics/LinearAlgebra/Generic/Vector.cs b/src/Numerics/LinearAlgebra/Generic/Vector.cs index 51dce321..dbd27fb3 100644 --- a/src/Numerics/LinearAlgebra/Generic/Vector.cs +++ b/src/Numerics/LinearAlgebra/Generic/Vector.cs @@ -81,8 +81,8 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// 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 + /// The value of the vector at the given . + /// If is negative or /// greater than the size of the vector. public T this[int index] { @@ -141,57 +141,6 @@ namespace MathNet.Numerics.LinearAlgebra.Generic Storage.Clear(index, count); } - /// - /// Returns a deep-copy clone of the vector. - /// - /// A deep-copy clone of the vector. - public Vector Clone() - { - var result = CreateVector(Count); - Storage.CopyToUnchecked(result.Storage, skipClearing: true); - return result; - } - - /// - /// Copies the values of this vector into the target vector. - /// - /// The vector to copy elements into. - /// If is . - /// If is not the same size as this vector. - public void CopyTo(Vector target) - { - if (target == null) - { - throw new ArgumentNullException("target"); - } - - Storage.CopyTo(target.Storage); - } - - /// - /// Copies the requested elements from this vector to another. - /// - /// The vector to copy the elements to. - /// The element to start copying from. - /// The element to start copying to. - /// The number of elements to copy. - public void CopySubVectorTo(Vector destination, int sourceIndex, int targetIndex, int count) - { - if (destination == null) - { - throw new ArgumentNullException("destination"); - } - - // TODO: refactor range checks - Storage.CopySubVectorTo(destination.Storage, sourceIndex, targetIndex, count); - } - - [Obsolete("Use CopySubVectorTo instead. Scheduled for removal in v3.0.")] - public void CopyTo(Vector destination, int sourceIndex, int targetIndex, int count) - { - CopySubVectorTo(destination, sourceIndex, targetIndex, count); - } - /// /// Creates a matrix with the given dimensions using the same storage type /// as this vector. @@ -209,8 +158,6 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// The new Vector. public abstract Vector CreateVector(int size); - #region Elementary operations - /// /// Adds a scalar to each element of the vector. /// @@ -444,6 +391,42 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// The vector to store the result of the subtraction. protected abstract void DoSubtract(Vector other, Vector result); + /// + /// Return vector with complex conjugate values of the source vector + /// + /// Conjugated vector + public Vector Conjugate() + { + var retrunVector = CreateVector(Count); + Conjugate(retrunVector); + return retrunVector; + } + + /// + /// Complex conjugates vector and save result to + /// + /// Target vector + public void Conjugate(Vector target) + { + if (target == null) + { + throw new ArgumentNullException("target"); + } + + if (Count != target.Count) + { + throw new ArgumentException(Resources.ArgumentVectorsSameLength, "target"); + } + + DoConjugate(target); + } + + /// + /// Complex conjugates vector and save result to + /// + /// Target vector + protected abstract void DoConjugate(Vector target); + /// /// Multiplies a scalar to each element of the vector. /// @@ -588,12 +571,51 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// The vector to store the result of the division. protected abstract void DoDivide(T scalar, Vector result); + /// + /// Computes the modulus for each element of the vector for the given divisor. + /// + /// The divisor to use. + /// A vector containing the result. + public Vector Modulus(T divisor) + { + var result = CreateVector(Count); + DoModulus(divisor, result); + return result; + } + + /// + /// Computes the modulus for each element of the vector for the given divisor. + /// + /// The divisor to use. + /// A vector to store the results in. + public void Modulus(T divisor, Vector result) + { + if (result == null) + { + throw new ArgumentNullException("result"); + } + + if (Count != result.Count) + { + throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); + } + + DoModulus(divisor, result); + } + + /// + /// Computes the modulus for each element of the vector for the given divisor. + /// + /// The divisor to use. + /// A vector to store the results in. + protected abstract void DoModulus(T divisor, Vector 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 the other vector is . /// If this vector and are not the same size. public Vector PointwiseMultiply(Vector other) { @@ -617,8 +639,8 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// /// 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 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 void PointwiseMultiply(Vector other, Vector result) @@ -658,7 +680,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// /// 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 the other vector is . /// If this vector and are not the same size. public Vector PointwiseDivide(Vector other) { @@ -682,8 +704,8 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// /// 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 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 void PointwiseDivide(Vector other, Vector result) @@ -724,8 +746,8 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// First vector /// Second vector /// Matrix M[i,j] = u[i]*v[j] - /// If the u vector is . - /// If the v vector is . + /// If the u vector is . + /// If the v vector is . public static Matrix OuterProduct(Vector u, Vector v) { if (u == null) @@ -744,7 +766,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic { matrix.SetRow(i, v.Multiply(u[i])); } - + return matrix; } @@ -762,116 +784,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic } /// - /// Returns the value of the absolute minimum element. - /// - /// The value of the absolute minimum element. - public abstract T AbsoluteMinimum(); - - /// - /// Returns the index of the absolute minimum element. - /// - /// The index of absolute minimum element. - public abstract int AbsoluteMinimumIndex(); - - /// - /// Returns the value of the absolute maximum element. - /// - /// The value of the absolute maximum element. - public abstract T AbsoluteMaximum(); - - /// - /// Returns the index of the absolute maximum element. - /// - /// The index of absolute maximum element. - public abstract int AbsoluteMaximumIndex(); - - /// - /// Returns the value of maximum element. - /// - /// The value of maximum element. - public T Maximum() - { - return this[MaximumIndex()]; - } - - /// - /// Returns the index of the absolute maximum element. - /// - /// The index of absolute maximum element. - public abstract int MaximumIndex(); - - /// - /// Returns the value of the minimum element. - /// - /// The value of the minimum element. - public T Minimum() - { - return this[MinimumIndex()]; - } - - /// - /// Returns the index of the minimum element. - /// - /// The index of minimum element. - public abstract int MinimumIndex(); - - /// - /// Computes the sum of the vector's elements. - /// - /// The sum of the vector's elements. - public abstract T 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 abstract T SumMagnitudes(); - - /// - /// Computes the modulus for each element of the vector for the given divisor. - /// - /// The divisor to use. - /// A vector containing the result. - public Vector Modulus(T divisor) - { - var result = CreateVector(Count); - DoModulus(divisor, result); - return result; - } - - /// - /// Computes the modulus for each element of the vector for the given divisor. - /// - /// The divisor to use. - /// A vector to store the results in. - public void Modulus(T divisor, Vector result) - { - if (result == null) - { - throw new ArgumentNullException("result"); - } - - if (Count != result.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); - } - - DoModulus(divisor, result); - } - - /// - /// Computes the modulus for each element of the vector for the given divisor. - /// - /// The divisor to use. - /// A vector to store the results in. - protected abstract void DoModulus(T divisor, Vector result); - - #endregion - - #region Arithmetic Operator Overloading - - /// - /// Returns a Vector containing the same values of . + /// Returns a Vector containing the same values of . /// /// This method is included for completeness. /// The vector to get the values from. @@ -916,7 +829,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic } /// - /// Returns a Vector containing the negated values of . + /// Returns a Vector containing the negated values of . /// /// The vector to get the values from. /// A vector containing the negated values as . @@ -1055,10 +968,6 @@ namespace MathNet.Numerics.LinearAlgebra.Generic return leftSide.Modulus(rightSide); } - #endregion - - #region Vector Norms - /// /// Computes the p-Norm. /// @@ -1081,83 +990,109 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// public abstract Vector Normalize(double p); - #endregion + /// + /// Returns the value of the absolute minimum element. + /// + /// The value of the absolute minimum element. + public abstract T AbsoluteMinimum(); /// - /// Return vector with conjugate values of the source vector + /// Returns the index of the absolute minimum element. /// - /// Conjugated vector - public Vector Conjugate() - { - var retrunVector = CreateVector(Count); - Conjugate(retrunVector); - return retrunVector; - } + /// The index of absolute minimum element. + public abstract int AbsoluteMinimumIndex(); /// - /// Conjugates vector and save result to + /// Returns the value of the absolute maximum element. /// - /// Target vector - public void Conjugate(Vector target) + /// The value of the absolute maximum element. + public abstract T AbsoluteMaximum(); + + /// + /// Returns the index of the absolute maximum element. + /// + /// The index of absolute maximum element. + public abstract int AbsoluteMaximumIndex(); + + /// + /// Returns the value of maximum element. + /// + /// The value of maximum element. + public T Maximum() { - if (target == null) - { - throw new ArgumentNullException("target"); - } + return this[MaximumIndex()]; + } - if (Count != target.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "target"); - } + /// + /// Returns the index of the absolute maximum element. + /// + /// The index of absolute maximum element. + public abstract int MaximumIndex(); - DoConjugate(target); + /// + /// Returns the value of the minimum element. + /// + /// The value of the minimum element. + public T Minimum() + { + return this[MinimumIndex()]; } /// - /// Conjugates vector and save result to + /// Returns the index of the minimum element. /// - /// Target vector - protected abstract void DoConjugate(Vector target); + /// The index of minimum element. + public abstract int MinimumIndex(); + + /// + /// Computes the sum of the vector's elements. + /// + /// The sum of the vector's elements. + public abstract T Sum(); - #region Copying and Conversion + /// + /// Computes the sum of the absolute value of the vector's elements. + /// + /// The sum of the absolute value of the vector's elements. + public abstract T SumMagnitudes(); /// - /// Returns the data contained in the vector as an array. + /// Returns a deep-copy clone of the vector. /// - /// - /// The vector's data as an array. - /// - public T[] ToArray() + /// A deep-copy clone of the vector. + public Vector Clone() { - var result = new DenseVectorStorage(Count); - Storage.CopyToUnchecked(result, skipClearing: true); - return result.Data; + var result = CreateVector(Count); + Storage.CopyToUnchecked(result.Storage, skipClearing: true); + return result; } /// - /// Create a matrix based on this vector in column form (one single column). + /// Set the values of this vector to the given values. /// - /// - /// This vector as a column matrix. - /// - public virtual Matrix ToColumnMatrix() + /// The array containing the values to use. + /// If is . + /// If is not the same size as this vector. + public void SetValues(T[] values) { - var result = CreateMatrix(Count, 1); - result.Storage.CopyColumnFrom(Storage, 0, skipClearing: true); - return result; + var source = new DenseVectorStorage(Count, values); + source.CopyTo(Storage); } /// - /// Create a matrix based on this vector in row form (one single row). + /// Copies the values of this vector into the target vector. /// - /// - /// This vector as a row matrix. - /// - public virtual Matrix ToRowMatrix() + /// The vector to copy elements into. + /// If is . + /// If is not the same size as this vector. + public void CopyTo(Vector target) { - var result = CreateMatrix(1, Count); - result.Storage.CopyRowFrom(Storage, 0, skipClearing: true); - return result; + if (target == null) + { + throw new ArgumentNullException("target"); + } + + Storage.CopyTo(target.Storage); } /// @@ -1196,18 +1131,67 @@ namespace MathNet.Numerics.LinearAlgebra.Generic } /// - /// Set the values of this vector to the given values. + /// Copies the requested elements from this vector to another. /// - /// The array containing the values to use. - /// If is . - /// If is not the same size as this vector. - public void SetValues(T[] values) + /// The vector to copy the elements to. + /// The element to start copying from. + /// The element to start copying to. + /// The number of elements to copy. + public void CopySubVectorTo(Vector destination, int sourceIndex, int targetIndex, int count) { - var source = new DenseVectorStorage(Count, values); - source.CopyTo(Storage); + if (destination == null) + { + throw new ArgumentNullException("destination"); + } + + // TODO: refactor range checks + Storage.CopySubVectorTo(destination.Storage, sourceIndex, targetIndex, count); } - #endregion + [Obsolete("Use CopySubVectorTo instead. Scheduled for removal in v3.0.")] + public void CopyTo(Vector destination, int sourceIndex, int targetIndex, int count) + { + CopySubVectorTo(destination, sourceIndex, targetIndex, count); + } + + /// + /// Returns the data contained in the vector as an array. + /// + /// + /// The vector's data as an array. + /// + public T[] ToArray() + { + var result = new DenseVectorStorage(Count); + Storage.CopyToUnchecked(result, skipClearing: true); + return result.Data; + } + + /// + /// Create a matrix based on this vector in column form (one single column). + /// + /// + /// This vector as a column matrix. + /// + public virtual Matrix ToColumnMatrix() + { + var result = CreateMatrix(Count, 1); + result.Storage.CopyColumnFrom(Storage, 0, skipClearing: true); + return result; + } + + /// + /// Create a matrix based on this vector in row form (one single row). + /// + /// + /// This vector as a row matrix. + /// + public virtual Matrix ToRowMatrix() + { + var result = CreateMatrix(1, Count); + result.Storage.CopyRowFrom(Storage, 0, skipClearing: true); + return result; + } /// /// Returns an enumerator that iterates through the collection. @@ -1231,9 +1215,9 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// element. /// /// - /// The enumerator returns a + /// The enumerator returns a /// - /// with the first value being the element index and the second value + /// with the first value being the element index and the second value /// being the value of the element at that index. For sparse vectors, the enumerator will exclude all elements /// with a zero value. /// @@ -1245,22 +1229,6 @@ namespace MathNet.Numerics.LinearAlgebra.Generic } } - - #region IFormattable - /// - /// Returns a that represents this instance. - /// - /// - /// An that supplies culture-specific formatting information. - /// - /// - /// A that represents this instance. - /// - public string ToString(IFormatProvider formatProvider) - { - return ToString(null, formatProvider); - } - /// /// Returns a that represents this instance. /// @@ -1276,17 +1244,17 @@ namespace MathNet.Numerics.LinearAlgebra.Generic public virtual string ToString(string format, IFormatProvider formatProvider) { var stringBuilder = new StringBuilder(); + var separator = formatProvider.GetTextInfo().ListSeparator; for (var index = 0; index < Count; index++) { stringBuilder.Append(this[index].ToString(format, formatProvider)); if (index != Count - 1) { - stringBuilder.Append(formatProvider.GetTextInfo().ListSeparator); + stringBuilder.Append(separator); } } return stringBuilder.ToString(); } - #endregion } }