Browse Source

LA: Collect operators on sparse vectors, add modulo where missing

v2
Christoph Ruegg 14 years ago
parent
commit
7c9f77b750
  1. 172
      src/Numerics/LinearAlgebra/Complex/SparseVector.cs
  2. 172
      src/Numerics/LinearAlgebra/Complex32/SparseVector.cs
  3. 203
      src/Numerics/LinearAlgebra/Double/SparseVector.cs
  4. 203
      src/Numerics/LinearAlgebra/Single/SparseVector.cs

172
src/Numerics/LinearAlgebra/Complex/SparseVector.cs

@ -36,7 +36,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
using System.Numerics;
using Generic;
using NumberTheory;
using Properties;
using Storage;
using Threading;
@ -67,8 +66,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
_storage = storage;
}
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="SparseVector"/> class with a given size.
/// </summary>
@ -143,8 +140,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
}
}
#endregion
/// <summary>
/// Creates a matrix with the given dimensions using the same storage type
/// as this vector.
@ -178,8 +173,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return new SparseVector(size);
}
#region Operators and supplementary functions
/// <summary>
/// Conjugates vector and save result to <paramref name="target"/>
/// </summary>
@ -351,41 +344,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
}
}
/// <summary>
/// Returns a <strong>Vector</strong> containing the same values of <paramref name="rightSide"/>.
/// </summary>
/// <remarks>This method is included for completeness.</remarks>
/// <param name="rightSide">The vector to get the values from.</param>
/// <returns>A vector containing a the same values as <paramref name="rightSide"/>.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static SparseVector operator +(SparseVector rightSide)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
return (SparseVector)rightSide.Plus();
}
/// <summary>
/// Adds two <strong>Vectors</strong> together and returns the results.
/// </summary>
/// <param name="leftSide">One of the vectors to add.</param>
/// <param name="rightSide">The other vector to add.</param>
/// <returns>The result of the addition.</returns>
/// <exception cref="ArgumentException">If <paramref name="leftSide"/> and <paramref name="rightSide"/> are not the same size.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="leftSide"/> or <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static SparseVector operator +(SparseVector leftSide, SparseVector rightSide)
{
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
return (SparseVector)leftSide.Add(rightSide);
}
/// <summary>
/// Subtracts a scalar from each element of the vector and stores the result in the result vector.
/// </summary>
@ -489,40 +447,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
}
}
/// <summary>
/// Returns a <strong>Vector</strong> containing the negated values of <paramref name="rightSide"/>.
/// </summary>
/// <param name="rightSide">The vector to get the values from.</param>
/// <returns>A vector containing the negated values as <paramref name="rightSide"/>.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static SparseVector operator -(SparseVector rightSide)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
return (SparseVector)rightSide.Negate();
}
/// <summary>
/// Subtracts two <strong>Vectors</strong> and returns the results.
/// </summary>
/// <param name="leftSide">The vector to subtract from.</param>
/// <param name="rightSide">The vector to subtract.</param>
/// <returns>The result of the subtraction.</returns>
/// <exception cref="ArgumentException">If <paramref name="leftSide"/> and <paramref name="rightSide"/> are not the same size.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="leftSide"/> or <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static SparseVector operator -(SparseVector leftSide, SparseVector rightSide)
{
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
return (SparseVector)leftSide.Subtract(rightSide);
}
/// <summary>
/// Returns a negated vector.
/// </summary>
@ -610,6 +534,75 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return result;
}
/// <summary>
/// Returns a <strong>Vector</strong> containing the same values of <paramref name="rightSide"/>.
/// </summary>
/// <remarks>This method is included for completeness.</remarks>
/// <param name="rightSide">The vector to get the values from.</param>
/// <returns>A vector containing a the same values as <paramref name="rightSide"/>.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static SparseVector operator +(SparseVector rightSide)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
return (SparseVector)rightSide.Plus();
}
/// <summary>
/// Adds two <strong>Vectors</strong> together and returns the results.
/// </summary>
/// <param name="leftSide">One of the vectors to add.</param>
/// <param name="rightSide">The other vector to add.</param>
/// <returns>The result of the addition.</returns>
/// <exception cref="ArgumentException">If <paramref name="leftSide"/> and <paramref name="rightSide"/> are not the same size.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="leftSide"/> or <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static SparseVector operator +(SparseVector leftSide, SparseVector rightSide)
{
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
return (SparseVector)leftSide.Add(rightSide);
}
/// <summary>
/// Returns a <strong>Vector</strong> containing the negated values of <paramref name="rightSide"/>.
/// </summary>
/// <param name="rightSide">The vector to get the values from.</param>
/// <returns>A vector containing the negated values as <paramref name="rightSide"/>.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static SparseVector operator -(SparseVector rightSide)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
return (SparseVector)rightSide.Negate();
}
/// <summary>
/// Subtracts two <strong>Vectors</strong> and returns the results.
/// </summary>
/// <param name="leftSide">The vector to subtract from.</param>
/// <param name="rightSide">The vector to subtract.</param>
/// <returns>The result of the subtraction.</returns>
/// <exception cref="ArgumentException">If <paramref name="leftSide"/> and <paramref name="rightSide"/> are not the same size.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="leftSide"/> or <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static SparseVector operator -(SparseVector leftSide, SparseVector rightSide)
{
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
return (SparseVector)leftSide.Subtract(rightSide);
}
/// <summary>
/// Multiplies a vector with a complex.
/// </summary>
@ -679,6 +672,23 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return (SparseVector)leftSide.Divide(rightSide);
}
/// <summary>
/// Computes the modulus of each element of the vector of the given divisor.
/// </summary>
/// <param name="leftSide">The vector whose elements we want to compute the modulus of.</param>
/// <param name="rightSide">The divisor to use,</param>
/// <returns>The result of the calculation</returns>
/// <exception cref="ArgumentNullException">If <paramref name="leftSide"/> is <see langword="null" />.</exception>
public static SparseVector operator %(SparseVector leftSide, Complex rightSide)
{
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
return (SparseVector)leftSide.Modulus(rightSide);
}
/// <summary>
/// Returns the index of the absolute minimum element.
/// </summary>
@ -831,10 +841,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return OuterProduct(this, v);
}
#endregion
#region Vector Norms
/// <summary>
/// Computes the p-Norm.
/// </summary>
@ -871,8 +877,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return Math.Pow(sum, 1.0 / p);
}
#endregion
#region Parse Functions
/// <summary>
@ -1031,8 +1035,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
}
#endregion
#region System.Object override
public override string ToString(string format, IFormatProvider formatProvider)
{
if (Count > 20)
@ -1043,8 +1045,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return base.ToString(format, formatProvider);
}
#endregion
/// <summary>
/// Returns an <see cref="IEnumerator{T}"/> that contains the position and value of the element.
/// </summary>

172
src/Numerics/LinearAlgebra/Complex32/SparseVector.cs

@ -36,7 +36,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
using Generic;
using NumberTheory;
using Numerics;
using Properties;
using Storage;
using Threading;
@ -67,8 +66,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
_storage = storage;
}
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="SparseVector"/> class with a given size.
/// </summary>
@ -143,8 +140,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
}
}
#endregion
/// <summary>
/// Creates a matrix with the given dimensions using the same storage type
/// as this vector.
@ -178,8 +173,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return new SparseVector(size);
}
#region Operators and supplementary functions
/// <summary>
/// Conjugates vector and save result to <paramref name="target"/>
/// </summary>
@ -351,41 +344,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
}
}
/// <summary>
/// Returns a <strong>Vector</strong> containing the same values of <paramref name="rightSide"/>.
/// </summary>
/// <remarks>This method is included for completeness.</remarks>
/// <param name="rightSide">The vector to get the values from.</param>
/// <returns>A vector containing a the same values as <paramref name="rightSide"/>.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static SparseVector operator +(SparseVector rightSide)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
return (SparseVector)rightSide.Plus();
}
/// <summary>
/// Adds two <strong>Vectors</strong> together and returns the results.
/// </summary>
/// <param name="leftSide">One of the vectors to add.</param>
/// <param name="rightSide">The other vector to add.</param>
/// <returns>The result of the addition.</returns>
/// <exception cref="ArgumentException">If <paramref name="leftSide"/> and <paramref name="rightSide"/> are not the same size.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="leftSide"/> or <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static SparseVector operator +(SparseVector leftSide, SparseVector rightSide)
{
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
return (SparseVector)leftSide.Add(rightSide);
}
/// <summary>
/// Subtracts a scalar from each element of the vector and stores the result in the result vector.
/// </summary>
@ -489,40 +447,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
}
}
/// <summary>
/// Returns a <strong>Vector</strong> containing the negated values of <paramref name="rightSide"/>.
/// </summary>
/// <param name="rightSide">The vector to get the values from.</param>
/// <returns>A vector containing the negated values as <paramref name="rightSide"/>.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static SparseVector operator -(SparseVector rightSide)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
return (SparseVector)rightSide.Negate();
}
/// <summary>
/// Subtracts two <strong>Vectors</strong> and returns the results.
/// </summary>
/// <param name="leftSide">The vector to subtract from.</param>
/// <param name="rightSide">The vector to subtract.</param>
/// <returns>The result of the subtraction.</returns>
/// <exception cref="ArgumentException">If <paramref name="leftSide"/> and <paramref name="rightSide"/> are not the same size.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="leftSide"/> or <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static SparseVector operator -(SparseVector leftSide, SparseVector rightSide)
{
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
return (SparseVector)leftSide.Subtract(rightSide);
}
/// <summary>
/// Returns a negated vector.
/// </summary>
@ -610,6 +534,75 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return result;
}
/// <summary>
/// Returns a <strong>Vector</strong> containing the same values of <paramref name="rightSide"/>.
/// </summary>
/// <remarks>This method is included for completeness.</remarks>
/// <param name="rightSide">The vector to get the values from.</param>
/// <returns>A vector containing a the same values as <paramref name="rightSide"/>.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static SparseVector operator +(SparseVector rightSide)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
return (SparseVector)rightSide.Plus();
}
/// <summary>
/// Adds two <strong>Vectors</strong> together and returns the results.
/// </summary>
/// <param name="leftSide">One of the vectors to add.</param>
/// <param name="rightSide">The other vector to add.</param>
/// <returns>The result of the addition.</returns>
/// <exception cref="ArgumentException">If <paramref name="leftSide"/> and <paramref name="rightSide"/> are not the same size.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="leftSide"/> or <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static SparseVector operator +(SparseVector leftSide, SparseVector rightSide)
{
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
return (SparseVector)leftSide.Add(rightSide);
}
/// <summary>
/// Returns a <strong>Vector</strong> containing the negated values of <paramref name="rightSide"/>.
/// </summary>
/// <param name="rightSide">The vector to get the values from.</param>
/// <returns>A vector containing the negated values as <paramref name="rightSide"/>.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static SparseVector operator -(SparseVector rightSide)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
return (SparseVector)rightSide.Negate();
}
/// <summary>
/// Subtracts two <strong>Vectors</strong> and returns the results.
/// </summary>
/// <param name="leftSide">The vector to subtract from.</param>
/// <param name="rightSide">The vector to subtract.</param>
/// <returns>The result of the subtraction.</returns>
/// <exception cref="ArgumentException">If <paramref name="leftSide"/> and <paramref name="rightSide"/> are not the same size.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="leftSide"/> or <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static SparseVector operator -(SparseVector leftSide, SparseVector rightSide)
{
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
return (SparseVector)leftSide.Subtract(rightSide);
}
/// <summary>
/// Multiplies a vector with a complex.
/// </summary>
@ -679,6 +672,23 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return (SparseVector)leftSide.Divide(rightSide);
}
/// <summary>
/// Computes the modulus of each element of the vector of the given divisor.
/// </summary>
/// <param name="leftSide">The vector whose elements we want to compute the modulus of.</param>
/// <param name="rightSide">The divisor to use,</param>
/// <returns>The result of the calculation</returns>
/// <exception cref="ArgumentNullException">If <paramref name="leftSide"/> is <see langword="null" />.</exception>
public static SparseVector operator %(SparseVector leftSide, Complex32 rightSide)
{
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
return (SparseVector)leftSide.Modulus(rightSide);
}
/// <summary>
/// Returns the index of the absolute minimum element.
/// </summary>
@ -831,10 +841,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return OuterProduct(this, v);
}
#endregion
#region Vector Norms
/// <summary>
/// Computes the p-Norm.
/// </summary>
@ -871,8 +877,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return (float)Math.Pow(sum, 1.0 / p);
}
#endregion
#region Parse Functions
/// <summary>
@ -1031,8 +1035,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
}
#endregion
#region System.Object override
public override string ToString(string format, IFormatProvider formatProvider)
{
if (Count > 20)
@ -1043,8 +1045,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return base.ToString(format, formatProvider);
}
#endregion
/// <summary>
/// Returns an <see cref="IEnumerator{T}"/> that contains the position and value of the element.
/// </summary>

203
src/Numerics/LinearAlgebra/Double/SparseVector.cs

@ -36,7 +36,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double
using System.Linq;
using Generic;
using NumberTheory;
using Properties;
using Storage;
using Threading;
@ -67,8 +66,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double
_storage = storage;
}
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="SparseVector"/> class with a given size.
/// </summary>
@ -143,8 +140,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
}
#endregion
/// <summary>
/// Creates a matrix with the given dimensions using the same storage type
/// as this vector.
@ -178,8 +173,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return new SparseVector(size);
}
#region Operators and supplementary functions
/// <summary>
/// Adds a scalar to each element of the vector and stores the result in the result vector.
/// Warning, the new 'sparse vector' with a non-zero scalar added to it will be a 100% filled
@ -319,41 +312,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
}
/// <summary>
/// Returns a <strong>Vector</strong> containing the same values of <paramref name="rightSide"/>.
/// </summary>
/// <remarks>This method is included for completeness.</remarks>
/// <param name="rightSide">The vector to get the values from.</param>
/// <returns>A vector containing a the same values as <paramref name="rightSide"/>.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static SparseVector operator +(SparseVector rightSide)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
return (SparseVector)rightSide.Plus();
}
/// <summary>
/// Adds two <strong>Vectors</strong> together and returns the results.
/// </summary>
/// <param name="leftSide">One of the vectors to add.</param>
/// <param name="rightSide">The other vector to add.</param>
/// <returns>The result of the addition.</returns>
/// <exception cref="ArgumentException">If <paramref name="leftSide"/> and <paramref name="rightSide"/> are not the same size.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="leftSide"/> or <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static SparseVector operator +(SparseVector leftSide, SparseVector rightSide)
{
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
return (SparseVector)leftSide.Add(rightSide);
}
/// <summary>
/// Subtracts a scalar from each element of the vector and stores the result in the result vector.
/// </summary>
@ -457,40 +415,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
}
/// <summary>
/// Returns a <strong>Vector</strong> containing the negated values of <paramref name="rightSide"/>.
/// </summary>
/// <param name="rightSide">The vector to get the values from.</param>
/// <returns>A vector containing the negated values as <paramref name="rightSide"/>.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static SparseVector operator -(SparseVector rightSide)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
return (SparseVector)rightSide.Negate();
}
/// <summary>
/// Subtracts two <strong>Vectors</strong> and returns the results.
/// </summary>
/// <param name="leftSide">The vector to subtract from.</param>
/// <param name="rightSide">The vector to subtract.</param>
/// <returns>The result of the subtraction.</returns>
/// <exception cref="ArgumentException">If <paramref name="leftSide"/> and <paramref name="rightSide"/> are not the same size.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="leftSide"/> or <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static SparseVector operator -(SparseVector leftSide, SparseVector rightSide)
{
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
return (SparseVector)leftSide.Subtract(rightSide);
}
/// <summary>
/// Returns a negated vector.
/// </summary>
@ -578,6 +502,99 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return result;
}
/// <summary>
/// Computes the modulus for each element of the vector for the given divisor.
/// </summary>
/// <param name="divisor">The divisor to use.</param>
/// <param name="result">A vector to store the results in.</param>
protected override void DoModulus(double divisor, Vector<double> result)
{
if (ReferenceEquals(this, result))
{
for (var index = 0; index < _storage.ValueCount; index++)
{
_storage.Values[index] %= divisor;
}
}
else
{
result.Clear();
for (var index = 0; index < _storage.ValueCount; index++)
{
result.At(_storage.Indices[index], _storage.Values[index] % divisor);
}
}
}
/// <summary>
/// Returns a <strong>Vector</strong> containing the same values of <paramref name="rightSide"/>.
/// </summary>
/// <remarks>This method is included for completeness.</remarks>
/// <param name="rightSide">The vector to get the values from.</param>
/// <returns>A vector containing a the same values as <paramref name="rightSide"/>.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static SparseVector operator +(SparseVector rightSide)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
return (SparseVector)rightSide.Plus();
}
/// <summary>
/// Adds two <strong>Vectors</strong> together and returns the results.
/// </summary>
/// <param name="leftSide">One of the vectors to add.</param>
/// <param name="rightSide">The other vector to add.</param>
/// <returns>The result of the addition.</returns>
/// <exception cref="ArgumentException">If <paramref name="leftSide"/> and <paramref name="rightSide"/> are not the same size.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="leftSide"/> or <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static SparseVector operator +(SparseVector leftSide, SparseVector rightSide)
{
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
return (SparseVector)leftSide.Add(rightSide);
}
/// <summary>
/// Returns a <strong>Vector</strong> containing the negated values of <paramref name="rightSide"/>.
/// </summary>
/// <param name="rightSide">The vector to get the values from.</param>
/// <returns>A vector containing the negated values as <paramref name="rightSide"/>.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static SparseVector operator -(SparseVector rightSide)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
return (SparseVector)rightSide.Negate();
}
/// <summary>
/// Subtracts two <strong>Vectors</strong> and returns the results.
/// </summary>
/// <param name="leftSide">The vector to subtract from.</param>
/// <param name="rightSide">The vector to subtract.</param>
/// <returns>The result of the subtraction.</returns>
/// <exception cref="ArgumentException">If <paramref name="leftSide"/> and <paramref name="rightSide"/> are not the same size.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="leftSide"/> or <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static SparseVector operator -(SparseVector leftSide, SparseVector rightSide)
{
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
return (SparseVector)leftSide.Subtract(rightSide);
}
/// <summary>
/// Multiplies a vector with a scalar.
/// </summary>
@ -647,30 +664,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return (SparseVector)leftSide.Divide(rightSide);
}
/// <summary>
/// Computes the modulus for each element of the vector for the given divisor.
/// </summary>
/// <param name="divisor">The divisor to use.</param>
/// <param name="result">A vector to store the results in.</param>
protected override void DoModulus(double divisor, Vector<double> result)
{
if (ReferenceEquals(this, result))
{
for (var index = 0; index < _storage.ValueCount; index++)
{
_storage.Values[index] %= divisor;
}
}
else
{
result.Clear();
for (var index = 0; index < _storage.ValueCount; index++)
{
result.At(_storage.Indices[index], _storage.Values[index] % divisor);
}
}
}
/// <summary>
/// Computes the modulus of each element of the vector of the given divisor.
/// </summary>
@ -887,10 +880,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return OuterProduct(this, v);
}
#endregion
#region Vector Norms
/// <summary>
/// Computes the p-Norm.
/// </summary>
@ -927,8 +916,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return Math.Pow(sum, 1.0 / p);
}
#endregion
#region Parse Functions
/// <summary>
@ -1088,8 +1075,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double
#endregion
#region System.Object override
public override string ToString(string format, IFormatProvider formatProvider)
{
if (Count > 20)
@ -1100,8 +1085,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return base.ToString(format, formatProvider);
}
#endregion
/// <summary>
/// Returns an <see cref="IEnumerator{T}"/> that contains the position and value of the element.
/// </summary>

203
src/Numerics/LinearAlgebra/Single/SparseVector.cs

@ -36,7 +36,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single
using System.Linq;
using Generic;
using NumberTheory;
using Properties;
using Storage;
using Threading;
@ -67,8 +66,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single
_storage = storage;
}
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="SparseVector"/> class with a given size.
/// </summary>
@ -143,8 +140,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single
}
}
#endregion
/// <summary>
/// Creates a matrix with the given dimensions using the same storage type
/// as this vector.
@ -178,8 +173,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return new SparseVector(size);
}
#region Operators and supplementary functions
/// <summary>
/// Adds a scalar to each element of the vector and stores the result in the result vector.
/// Warning, the new 'sparse vector' with a non-zero scalar added to it will be a 100% filled
@ -320,41 +313,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single
}
}
/// <summary>
/// Returns a <strong>Vector</strong> containing the same values of <paramref name="rightSide"/>.
/// </summary>
/// <remarks>This method is included for completeness.</remarks>
/// <param name="rightSide">The vector to get the values from.</param>
/// <returns>A vector containing a the same values as <paramref name="rightSide"/>.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static SparseVector operator +(SparseVector rightSide)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
return (SparseVector)rightSide.Plus();
}
/// <summary>
/// Adds two <strong>Vectors</strong> together and returns the results.
/// </summary>
/// <param name="leftSide">One of the vectors to add.</param>
/// <param name="rightSide">The other vector to add.</param>
/// <returns>The result of the addition.</returns>
/// <exception cref="ArgumentException">If <paramref name="leftSide"/> and <paramref name="rightSide"/> are not the same size.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="leftSide"/> or <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static SparseVector operator +(SparseVector leftSide, SparseVector rightSide)
{
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
return (SparseVector)leftSide.Add(rightSide);
}
/// <summary>
/// Subtracts a scalar from each element of the vector and stores the result in the result vector.
/// </summary>
@ -458,40 +416,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single
}
}
/// <summary>
/// Returns a <strong>Vector</strong> containing the negated values of <paramref name="rightSide"/>.
/// </summary>
/// <param name="rightSide">The vector to get the values from.</param>
/// <returns>A vector containing the negated values as <paramref name="rightSide"/>.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static SparseVector operator -(SparseVector rightSide)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
return (SparseVector)rightSide.Negate();
}
/// <summary>
/// Subtracts two <strong>Vectors</strong> and returns the results.
/// </summary>
/// <param name="leftSide">The vector to subtract from.</param>
/// <param name="rightSide">The vector to subtract.</param>
/// <returns>The result of the subtraction.</returns>
/// <exception cref="ArgumentException">If <paramref name="leftSide"/> and <paramref name="rightSide"/> are not the same size.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="leftSide"/> or <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static SparseVector operator -(SparseVector leftSide, SparseVector rightSide)
{
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
return (SparseVector)leftSide.Subtract(rightSide);
}
/// <summary>
/// Returns a negated vector.
/// </summary>
@ -579,6 +503,99 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return result;
}
/// <summary>
/// Computes the modulus for each element of the vector for the given divisor.
/// </summary>
/// <param name="divisor">The divisor to use.</param>
/// <param name="result">A vector to store the results in.</param>
protected override void DoModulus(float divisor, Vector<float> result)
{
if (ReferenceEquals(this, result))
{
for (var index = 0; index < _storage.ValueCount; index++)
{
_storage.Values[index] %= divisor;
}
}
else
{
result.Clear();
for (var index = 0; index < _storage.ValueCount; index++)
{
result.At(_storage.Indices[index], _storage.Values[index] % divisor);
}
}
}
/// <summary>
/// Returns a <strong>Vector</strong> containing the same values of <paramref name="rightSide"/>.
/// </summary>
/// <remarks>This method is included for completeness.</remarks>
/// <param name="rightSide">The vector to get the values from.</param>
/// <returns>A vector containing a the same values as <paramref name="rightSide"/>.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static SparseVector operator +(SparseVector rightSide)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
return (SparseVector)rightSide.Plus();
}
/// <summary>
/// Adds two <strong>Vectors</strong> together and returns the results.
/// </summary>
/// <param name="leftSide">One of the vectors to add.</param>
/// <param name="rightSide">The other vector to add.</param>
/// <returns>The result of the addition.</returns>
/// <exception cref="ArgumentException">If <paramref name="leftSide"/> and <paramref name="rightSide"/> are not the same size.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="leftSide"/> or <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static SparseVector operator +(SparseVector leftSide, SparseVector rightSide)
{
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
return (SparseVector)leftSide.Add(rightSide);
}
/// <summary>
/// Returns a <strong>Vector</strong> containing the negated values of <paramref name="rightSide"/>.
/// </summary>
/// <param name="rightSide">The vector to get the values from.</param>
/// <returns>A vector containing the negated values as <paramref name="rightSide"/>.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static SparseVector operator -(SparseVector rightSide)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
return (SparseVector)rightSide.Negate();
}
/// <summary>
/// Subtracts two <strong>Vectors</strong> and returns the results.
/// </summary>
/// <param name="leftSide">The vector to subtract from.</param>
/// <param name="rightSide">The vector to subtract.</param>
/// <returns>The result of the subtraction.</returns>
/// <exception cref="ArgumentException">If <paramref name="leftSide"/> and <paramref name="rightSide"/> are not the same size.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="leftSide"/> or <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static SparseVector operator -(SparseVector leftSide, SparseVector rightSide)
{
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
return (SparseVector)leftSide.Subtract(rightSide);
}
/// <summary>
/// Multiplies a vector with a scalar.
/// </summary>
@ -648,30 +665,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return (SparseVector)leftSide.Divide(rightSide);
}
/// <summary>
/// Computes the modulus for each element of the vector for the given divisor.
/// </summary>
/// <param name="divisor">The divisor to use.</param>
/// <param name="result">A vector to store the results in.</param>
protected override void DoModulus(float divisor, Vector<float> result)
{
if (ReferenceEquals(this, result))
{
for (var index = 0; index < _storage.ValueCount; index++)
{
_storage.Values[index] %= divisor;
}
}
else
{
result.Clear();
for (var index = 0; index < _storage.ValueCount; index++)
{
result.At(_storage.Indices[index], _storage.Values[index] % divisor);
}
}
}
/// <summary>
/// Computes the modulus of each element of the vector of the given divisor.
/// </summary>
@ -891,10 +884,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return OuterProduct(this, v);
}
#endregion
#region Vector Norms
/// <summary>
/// Computes the p-Norm.
/// </summary>
@ -931,8 +920,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return (float)Math.Pow(sum, 1.0 / p);
}
#endregion
#region Parse Functions
/// <summary>
@ -1092,8 +1079,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single
#endregion
#region System.Object override
public override string ToString(string format, IFormatProvider formatProvider)
{
if (Count > 20)
@ -1104,8 +1089,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return base.ToString(format, formatProvider);
}
#endregion
/// <summary>
/// Returns an <see cref="IEnumerator{T}"/> that contains the position and value of the element.
/// </summary>

Loading…
Cancel
Save