csharpfftfsharpintegrationinterpolationlinear-algebramathdifferentiationmatrixnumericsrandomregressionstatisticsmathnet
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1557 lines
55 KiB
1557 lines
55 KiB
// <copyright file="DenseVector.cs" company="Math.NET">
|
|
// 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.
|
|
// </copyright>
|
|
|
|
namespace MathNet.Numerics.LinearAlgebra.Single
|
|
{
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using Distributions;
|
|
using Generic;
|
|
using NumberTheory;
|
|
using Properties;
|
|
using Threading;
|
|
|
|
/// <summary>
|
|
/// A vector using dense storage.
|
|
/// </summary>
|
|
public class DenseVector : Vector<float>
|
|
{
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="DenseVector"/> class with a given size.
|
|
/// </summary>
|
|
/// <param name="size">
|
|
/// the size of the vector.
|
|
/// </param>
|
|
/// <exception cref="ArgumentException">
|
|
/// If <paramref name="size"/> is less than one.
|
|
/// </exception>
|
|
public DenseVector(int size)
|
|
: base(size)
|
|
{
|
|
Data = new float[size];
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="DenseVector"/> class with a given size
|
|
/// and each element set to the given value;
|
|
/// </summary>
|
|
/// <param name="size">
|
|
/// the size of the vector.
|
|
/// </param>
|
|
/// <param name="value">
|
|
/// the value to set each element to.
|
|
/// </param>
|
|
/// <exception cref="ArgumentException">
|
|
/// If <paramref name="size"/> is less than one.
|
|
/// </exception>
|
|
public DenseVector(int size, float value)
|
|
: this(size)
|
|
{
|
|
for (var index = 0; index < Data.Length; index++)
|
|
{
|
|
Data[index] = value;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="DenseVector"/> class by
|
|
/// copying the values from another.
|
|
/// </summary>
|
|
/// <param name="other">
|
|
/// The vector to create the new vector from.
|
|
/// </param>
|
|
public DenseVector(Vector<float> other)
|
|
: this(other.Count)
|
|
{
|
|
var vector = other as DenseVector;
|
|
if (vector == null)
|
|
{
|
|
CommonParallel.For(
|
|
0,
|
|
Data.Length,
|
|
index => this[index] = other[index]);
|
|
}
|
|
else
|
|
{
|
|
Buffer.BlockCopy(vector.Data, 0, Data, 0, Data.Length * Constants.SizeOfFloat);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="DenseVector"/> class by
|
|
/// copying the values from another.
|
|
/// </summary>
|
|
/// <param name="other">
|
|
/// The vector to create the new vector from.
|
|
/// </param>
|
|
public DenseVector(DenseVector other)
|
|
: this(other.Count)
|
|
{
|
|
Buffer.BlockCopy(other.Data, 0, Data, 0, Data.Length * Constants.SizeOfFloat);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="DenseVector"/> class for an array.
|
|
/// </summary>
|
|
/// <param name="array">The array to create this vector from.</param>
|
|
/// <remarks>The vector does not copy the array, but keeps a reference to it. Any
|
|
/// changes to the vector will also change the array.</remarks>
|
|
public DenseVector(float[] array) : base(array.Length)
|
|
{
|
|
Data = array;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the vector's internal data.
|
|
/// </summary>
|
|
/// <value>The vector's internal data.</value>
|
|
/// <remarks>Changing values in the array also changes the corresponding value in vector. Use with care.</remarks>
|
|
internal float[] Data
|
|
{
|
|
get;
|
|
private set;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a reference to the internal data structure.
|
|
/// </summary>
|
|
/// <param name="vector">The <c>DenseVector</c> whose internal data we are
|
|
/// returning.</param>
|
|
/// <returns>
|
|
/// A reference to the internal date of the given vector.
|
|
/// </returns>
|
|
public static implicit operator float[](DenseVector vector)
|
|
{
|
|
if (vector == null)
|
|
{
|
|
throw new ArgumentNullException();
|
|
}
|
|
|
|
return vector.Data;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a vector bound directly to a reference of the provided array.
|
|
/// </summary>
|
|
/// <param name="array">The array to bind to the <c>DenseVector</c> object.</param>
|
|
/// <returns>
|
|
/// A <c>DenseVector</c> whose values are bound to the given array.
|
|
/// </returns>
|
|
public static implicit operator DenseVector(float[] array)
|
|
{
|
|
if (array == null)
|
|
{
|
|
throw new ArgumentNullException();
|
|
}
|
|
|
|
return new DenseVector(array);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a matrix based on this vector in column form (one single column).
|
|
/// </summary>
|
|
/// <returns>This vector as a column matrix.</returns>
|
|
public override Matrix<float> ToColumnMatrix()
|
|
{
|
|
var matrix = new DenseMatrix(Count, 1);
|
|
for (var i = 0; i < Data.Length; i++)
|
|
{
|
|
matrix[i, 0] = Data[i];
|
|
}
|
|
|
|
return matrix;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a matrix based on this vector in row form (one single row).
|
|
/// </summary>
|
|
/// <returns>This vector as a row matrix.</returns>
|
|
public override Matrix<float> ToRowMatrix()
|
|
{
|
|
var matrix = new DenseMatrix(1, Count);
|
|
for (var i = 0; i < Data.Length; i++)
|
|
{
|
|
matrix[0, i] = Data[i];
|
|
}
|
|
|
|
return matrix;
|
|
}
|
|
|
|
/// <summary>Gets or sets the value at the given <paramref name="index"/>.</summary>
|
|
/// <param name="index">The index of the value to get or set.</param>
|
|
/// <returns>The value of the vector at the given <paramref name="index"/>.</returns>
|
|
/// <exception cref="IndexOutOfRangeException">If <paramref name="index"/> is negative or
|
|
/// greater than the size of the vector.</exception>
|
|
public override float this[int index]
|
|
{
|
|
get
|
|
{
|
|
return Data[index];
|
|
}
|
|
|
|
set
|
|
{
|
|
Data[index] = value;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a matrix with the given dimensions using the same storage type
|
|
/// as this vector.
|
|
/// </summary>
|
|
/// <param name="rows">
|
|
/// The number of rows.
|
|
/// </param>
|
|
/// <param name="columns">
|
|
/// The number of columns.
|
|
/// </param>
|
|
/// <returns>
|
|
/// A matrix with the given dimensions.
|
|
/// </returns>
|
|
public override Matrix<float> CreateMatrix(int rows, int columns)
|
|
{
|
|
return new DenseMatrix(rows, columns);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a <strong>Vector</strong> of the given size using the same storage type
|
|
/// as this vector.
|
|
/// </summary>
|
|
/// <param name="size">
|
|
/// The size of the <strong>Vector</strong> to create.
|
|
/// </param>
|
|
/// <returns>
|
|
/// The new <c>Vector</c>.
|
|
/// </returns>
|
|
public override Vector<float> CreateVector(int size)
|
|
{
|
|
return new DenseVector(size);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Copies the values of this vector into the target vector.
|
|
/// </summary>
|
|
/// <param name="target">
|
|
/// The vector to copy elements into.
|
|
/// </param>
|
|
/// <exception cref="ArgumentNullException">
|
|
/// If <paramref name="target"/> is <see langword="null"/>.
|
|
/// </exception>
|
|
/// <exception cref="ArgumentException">
|
|
/// If <paramref name="target"/> is not the same size as this vector.
|
|
/// </exception>
|
|
public override void CopyTo(Vector<float> target)
|
|
{
|
|
if (target == null)
|
|
{
|
|
throw new ArgumentNullException("target");
|
|
}
|
|
|
|
if (Count != target.Count)
|
|
{
|
|
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "target");
|
|
}
|
|
|
|
if (ReferenceEquals(this, target))
|
|
{
|
|
return;
|
|
}
|
|
|
|
var otherVector = target as DenseVector;
|
|
if (otherVector == null)
|
|
{
|
|
CommonParallel.For(
|
|
0,
|
|
Data.Length,
|
|
index => target[index] = Data[index]);
|
|
}
|
|
else
|
|
{
|
|
Buffer.BlockCopy(Data, 0, otherVector.Data, 0, Data.Length * Constants.SizeOfFloat);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a scalar to each element of the vector.
|
|
/// </summary>
|
|
/// <param name="scalar">The scalar to add.</param>
|
|
/// <returns>A copy of the vector with the scalar added.</returns>
|
|
public override Vector<float> Add(float scalar)
|
|
{
|
|
if (scalar == 0.0)
|
|
{
|
|
return Clone();
|
|
}
|
|
|
|
var copy = (DenseVector)Clone();
|
|
CommonParallel.For(
|
|
0,
|
|
Data.Length,
|
|
index => copy.Data[index] += scalar);
|
|
return copy;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a scalar to each element of the vector and stores the result in the result vector.
|
|
/// </summary>
|
|
/// <param name="scalar">The scalar to add.</param>
|
|
/// <param name="result">The vector to store the result of the addition.</param>
|
|
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
|
|
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
|
|
public override void Add(float scalar, Vector<float> 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(scalar, result);
|
|
}
|
|
else
|
|
{
|
|
CommonParallel.For(
|
|
0,
|
|
Data.Length,
|
|
index => dense.Data[index] = Data[index] + scalar);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds another vector to this vector.
|
|
/// </summary>
|
|
/// <param name="other">The vector to add to this one.</param>
|
|
/// <returns>A new vector containing the sum of both vectors.</returns>
|
|
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
|
|
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
|
|
public override Vector<float> Add(Vector<float> 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, 1.0f, denseVector.Data);
|
|
return copy;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds another vector to this vector and stores the result into the result vector.
|
|
/// </summary>
|
|
/// <param name="other">The vector to add to this one.</param>
|
|
/// <param name="result">The vector to store the result of the addition.</param>
|
|
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
|
|
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
|
|
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
|
|
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
|
|
public override void Add(Vector<float> other, Vector<float> 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, 1.0f, odense.Data);
|
|
}
|
|
else
|
|
{
|
|
CommonParallel.For(
|
|
0,
|
|
Data.Length,
|
|
index => result[index] = Data[index] + other[index]);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <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 Vector<float> operator +(DenseVector rightSide)
|
|
{
|
|
if (rightSide == null)
|
|
{
|
|
throw new ArgumentNullException("rightSide");
|
|
}
|
|
|
|
return 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 Vector<float> 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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Subtracts a scalar from each element of the vector.
|
|
/// </summary>
|
|
/// <param name="scalar">The scalar to subtract.</param>
|
|
/// <returns>A new vector containing the subtraction of this vector and the scalar.</returns>
|
|
public override Vector<float> Subtract(float scalar)
|
|
{
|
|
if (scalar == 0.0)
|
|
{
|
|
return Clone();
|
|
}
|
|
|
|
var copy = (DenseVector)Clone();
|
|
CommonParallel.For(
|
|
0,
|
|
Data.Length,
|
|
index => copy.Data[index] -= scalar);
|
|
return copy;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Subtracts a scalar from each element of the vector and stores the result in the result vector.
|
|
/// </summary>
|
|
/// <param name="scalar">The scalar to subtract.</param>
|
|
/// <param name="result">The vector to store the result of the subtraction.</param>
|
|
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null"/>.</exception>
|
|
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
|
|
public override void Subtract(float scalar, Vector<float> 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(scalar, result);
|
|
}
|
|
else
|
|
{
|
|
CommonParallel.For(
|
|
0,
|
|
Data.Length,
|
|
index => dense.Data[index] = Data[index] - scalar);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Subtracts another vector from this vector.
|
|
/// </summary>
|
|
/// <param name="other">The vector to subtract from this one.</param>
|
|
/// <returns>A new vector containing the subtraction of the the two vectors.</returns>
|
|
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null"/>.</exception>
|
|
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
|
|
public override Vector<float> Subtract(Vector<float> 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, -1.0f, denseVector.Data);
|
|
return copy;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Subtracts another vector to this vector and stores the result into the result vector.
|
|
/// </summary>
|
|
/// <param name="other">The vector to subtract from this one.</param>
|
|
/// <param name="result">The vector to store the result of the subtraction.</param>
|
|
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null"/>.</exception>
|
|
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null"/>.</exception>
|
|
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
|
|
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
|
|
public override void Subtract(Vector<float> other, Vector<float> 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, -1.0f, odense.Data);
|
|
}
|
|
else
|
|
{
|
|
CommonParallel.For(
|
|
0,
|
|
Data.Length,
|
|
index => result[index] = Data[index] - other[index]);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <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 Vector<float> operator -(DenseVector rightSide)
|
|
{
|
|
if (rightSide == null)
|
|
{
|
|
throw new ArgumentNullException("rightSide");
|
|
}
|
|
|
|
return 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 Vector<float> 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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a negated vector.
|
|
/// </summary>
|
|
/// <returns>The negated vector.</returns>
|
|
/// <remarks>Added as an alternative to the unary negation operator.</remarks>
|
|
public override Vector<float> Negate()
|
|
{
|
|
var result = new DenseVector(Count);
|
|
CommonParallel.For(
|
|
0,
|
|
Data.Length,
|
|
index => result[index] = -Data[index]);
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Multiplies a scalar to each element of the vector.
|
|
/// </summary>
|
|
/// <param name="scalar">The scalar to multiply.</param>
|
|
/// <returns>A new vector that is the multiplication of the vector and the scalar.</returns>
|
|
public override Vector<float> Multiply(float scalar)
|
|
{
|
|
if (scalar == 1.0)
|
|
{
|
|
return Clone();
|
|
}
|
|
|
|
var copy = (DenseVector)Clone();
|
|
Control.LinearAlgebraProvider.ScaleArray(scalar, copy.Data);
|
|
return copy;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Computes the dot product between this vector and another vector.
|
|
/// </summary>
|
|
/// <param name="other">The other vector to add.</param>
|
|
/// <returns>The result of the addition.</returns>
|
|
/// <exception cref="ArgumentException">If <paramref name="other"/> is not of the same size.</exception>
|
|
/// <exception cref="ArgumentNullException">If <paramref name="other"/> is <see langword="null" />.</exception>
|
|
public override float DotProduct(Vector<float> 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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Multiplies a vector with a scalar.
|
|
/// </summary>
|
|
/// <param name="leftSide">The vector to scale.</param>
|
|
/// <param name="rightSide">The scalar value.</param>
|
|
/// <returns>The result of the multiplication.</returns>
|
|
/// <exception cref="ArgumentNullException">If <paramref name="leftSide"/> is <see langword="null" />.</exception>
|
|
public static DenseVector operator *(DenseVector leftSide, float rightSide)
|
|
{
|
|
if (leftSide == null)
|
|
{
|
|
throw new ArgumentNullException("leftSide");
|
|
}
|
|
|
|
return (DenseVector)leftSide.Multiply(rightSide);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Multiplies a vector with a scalar.
|
|
/// </summary>
|
|
/// <param name="leftSide">The scalar value.</param>
|
|
/// <param name="rightSide">The vector to scale.</param>
|
|
/// <returns>The result of the multiplication.</returns>
|
|
/// <exception cref="ArgumentNullException">If <paramref name="rightSide"/> is <see langword="null" />.</exception>
|
|
public static DenseVector operator *(float leftSide, DenseVector rightSide)
|
|
{
|
|
if (rightSide == null)
|
|
{
|
|
throw new ArgumentNullException("rightSide");
|
|
}
|
|
|
|
return (DenseVector)rightSide.Multiply(leftSide);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Computes the dot product between two <strong>Vectors</strong>.
|
|
/// </summary>
|
|
/// <param name="leftSide">The left row vector.</param>
|
|
/// <param name="rightSide">The right column vector.</param>
|
|
/// <returns>The dot product between the two vectors.</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 float 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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Divides a vector with a scalar.
|
|
/// </summary>
|
|
/// <param name="leftSide">The vector to divide.</param>
|
|
/// <param name="rightSide">The scalar value.</param>
|
|
/// <returns>The result of the division.</returns>
|
|
/// <exception cref="ArgumentNullException">If <paramref name="leftSide"/> is <see langword="null" />.</exception>
|
|
public static DenseVector operator /(DenseVector leftSide, float rightSide)
|
|
{
|
|
if (leftSide == null)
|
|
{
|
|
throw new ArgumentNullException("leftSide");
|
|
}
|
|
|
|
return (DenseVector)leftSide.Multiply(1f / rightSide);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the index of the absolute minimum element.
|
|
/// </summary>
|
|
/// <returns>The index of absolute minimum element.</returns>
|
|
public override int AbsoluteMinimumIndex()
|
|
{
|
|
var index = 0;
|
|
var min = Math.Abs(Data[index]);
|
|
for (var i = 1; i < Count; i++)
|
|
{
|
|
var test = Math.Abs(Data[i]);
|
|
if (test < min)
|
|
{
|
|
index = i;
|
|
min = test;
|
|
}
|
|
}
|
|
|
|
return index;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the value of the absolute minimum element.
|
|
/// </summary>
|
|
/// <returns>The value of the absolute minimum element.</returns>
|
|
public override double AbsoluteMinimum()
|
|
{
|
|
return Math.Abs(Data[AbsoluteMinimumIndex()]);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the value of the absolute maximum element.
|
|
/// </summary>
|
|
/// <returns>The value of the absolute maximum element.</returns>
|
|
public override double AbsoluteMaximum()
|
|
{
|
|
return Math.Abs(Data[AbsoluteMaximumIndex()]);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the index of the absolute maximum element.
|
|
/// </summary>
|
|
/// <returns>The index of absolute maximum element.</returns>
|
|
public override int AbsoluteMaximumIndex()
|
|
{
|
|
var index = 0;
|
|
var max = Math.Abs(Data[index]);
|
|
for (var i = 1; i < Count; i++)
|
|
{
|
|
var test = Math.Abs(Data[i]);
|
|
if (test > max)
|
|
{
|
|
index = i;
|
|
max = test;
|
|
}
|
|
}
|
|
|
|
return index;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a vector containing specified elements.
|
|
/// </summary>
|
|
/// <param name="index">The first element to begin copying from.</param>
|
|
/// <param name="length">The number of elements to copy.</param>
|
|
/// <returns>A vector containing a copy of the specified elements.</returns>
|
|
/// <exception cref="ArgumentOutOfRangeException"><list><item>If <paramref name="index"/> is not positive or
|
|
/// greater than or equal to the size of the vector.</item>
|
|
/// <item>If <paramref name="index"/> + <paramref name="length"/> is greater than or equal to the size of the vector.</item>
|
|
/// </list></exception>
|
|
/// <exception cref="ArgumentException">If <paramref name="length"/> is not positive.</exception>
|
|
public override Vector<float> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set the values of this vector to the given values.
|
|
/// </summary>
|
|
/// <param name="values">The array containing the values to use.</param>
|
|
/// <exception cref="ArgumentNullException">If <paramref name="values"/> is <see langword="null" />.</exception>
|
|
/// <exception cref="ArgumentException">If <paramref name="values"/> is not the same size as this vector.</exception>
|
|
public override void SetValues(float[] 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]);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the index of the absolute maximum element.
|
|
/// </summary>
|
|
/// <returns>The index of absolute maximum element.</returns>
|
|
public override int MaximumIndex()
|
|
{
|
|
var index = 0;
|
|
var max = Data[0];
|
|
for (var i = 1; i < Count; i++)
|
|
{
|
|
if (max < Data[i])
|
|
{
|
|
index = i;
|
|
max = Data[i];
|
|
}
|
|
}
|
|
|
|
return index;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the index of the minimum element.
|
|
/// </summary>
|
|
/// <returns>The index of minimum element.</returns>
|
|
public override int MinimumIndex()
|
|
{
|
|
var index = 0;
|
|
var min = Data[0];
|
|
for (var i = 1; i < Count; i++)
|
|
{
|
|
if (min > Data[i])
|
|
{
|
|
index = i;
|
|
min = Data[i];
|
|
}
|
|
}
|
|
|
|
return index;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Computes the sum of the vector's elements.
|
|
/// </summary>
|
|
/// <returns>The sum of the vector's elements.</returns>
|
|
public override float Sum()
|
|
{
|
|
float result = 0;
|
|
for (var i = 0; i < Count; i++)
|
|
{
|
|
result += Data[i];
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Computes the sum of the absolute value of the vector's elements.
|
|
/// </summary>
|
|
/// <returns>The sum of the absolute value of the vector's elements.</returns>
|
|
public override double SumMagnitudes()
|
|
{
|
|
float result = 0;
|
|
for (var i = 0; i < Count; i++)
|
|
{
|
|
result += Math.Abs(Data[i]);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Pointwise multiplies this vector with another vector.
|
|
/// </summary>
|
|
/// <param name="other">The vector to pointwise multiply with this one.</param>
|
|
/// <returns>A new vector which is the pointwise multiplication of the two vectors.</returns>
|
|
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
|
|
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
|
|
public override Vector<float> PointwiseMultiply(Vector<float> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Pointwise multiplies this vector with another vector and stores the result into the result vector.
|
|
/// </summary>
|
|
/// <param name="other">The vector to pointwise multiply with this one.</param>
|
|
/// <param name="result">The vector to store the result of the pointwise multiplication.</param>
|
|
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
|
|
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
|
|
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
|
|
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
|
|
public override void PointwiseMultiply(Vector<float> other, Vector<float> 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]);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Pointwise divide this vector with another vector.
|
|
/// </summary>
|
|
/// <param name="other">The vector to pointwise divide this one by.</param>
|
|
/// <returns>A new vector which is the pointwise division of the two vectors.</returns>
|
|
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
|
|
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
|
|
public override Vector<float> PointwiseDivide(Vector<float> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Pointwise divide this vector with another vector and stores the result into the result vector.
|
|
/// </summary>
|
|
/// <param name="other">The vector to pointwise divide this one by.</param>
|
|
/// <param name="result">The vector to store the result of the pointwise division.</param>
|
|
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
|
|
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
|
|
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
|
|
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
|
|
public override void PointwiseDivide(Vector<float> other, Vector<float> 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]);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Outer product of two vectors
|
|
/// </summary>
|
|
/// <param name="u">First vector</param>
|
|
/// <param name="v">Second vector</param>
|
|
/// <returns>Matrix M[i,j] = u[i]*v[j] </returns>
|
|
/// <exception cref="ArgumentNullException">If the u vector is <see langword="null" />.</exception>
|
|
/// <exception cref="ArgumentNullException">If the v vector is <see langword="null" />.</exception>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Generates a vector with random elements
|
|
/// </summary>
|
|
/// <param name="length">Number of elements in the vector.</param>
|
|
/// <param name="randomDistribution">Continuous Random Distribution or Source</param>
|
|
/// <returns>
|
|
/// A vector with n-random elements distributed according
|
|
/// to the specified random distribution.
|
|
/// </returns>
|
|
/// <exception cref="ArgumentNullException">If the n vector is non positive<see langword="null" />.</exception>
|
|
public override Vector<float> 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] = (float)randomDistribution.Sample();
|
|
}
|
|
|
|
return v;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Generates a vector with random elements
|
|
/// </summary>
|
|
/// <param name="length">Number of elements in the vector.</param>
|
|
/// <param name="randomDistribution">Continuous Random Distribution or Source</param>
|
|
/// <returns>
|
|
/// A vector with n-random elements distributed according
|
|
/// to the specified random distribution.
|
|
/// </returns>
|
|
/// <exception cref="ArgumentNullException">If the n vector is non positive<see langword="null" />.</exception>
|
|
public override Vector<float> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Tensor Product (Dyadic) of this and another vector.
|
|
/// </summary>
|
|
/// <param name="v">The vector to operate on.</param>
|
|
/// <returns>
|
|
/// Matrix M[i,j] = this[i] * v[j].
|
|
/// </returns>
|
|
/// <seealso cref="OuterProduct"/>
|
|
public Matrix<float> TensorMultiply(DenseVector v)
|
|
{
|
|
return OuterProduct(this, v);
|
|
}
|
|
|
|
#region Vector Norms
|
|
|
|
/// <summary>
|
|
/// Computes the p-Norm.
|
|
/// </summary>
|
|
/// <param name="p">The p value.</param>
|
|
/// <returns>Scalar <c>ret = (sum(abs(this[i])^p))^(1/p)</c></returns>
|
|
public override double Norm(double p)
|
|
{
|
|
if (p < 0.0)
|
|
{
|
|
throw new ArgumentOutOfRangeException("p");
|
|
}
|
|
|
|
if (1.0 == p)
|
|
{
|
|
return CommonParallel.Aggregate(
|
|
0,
|
|
Count,
|
|
index => Math.Abs(Data[index]));
|
|
}
|
|
|
|
if (2.0 == p)
|
|
{
|
|
return Data.Aggregate(0f, SpecialFunctions.Hypotenuse);
|
|
}
|
|
|
|
if (Double.IsPositiveInfinity(p))
|
|
{
|
|
return CommonParallel.Select(
|
|
0,
|
|
Count,
|
|
(index, localData) => Math.Max(localData, Math.Abs(Data[index])),
|
|
Math.Max);
|
|
}
|
|
|
|
var sum = CommonParallel.Aggregate(
|
|
0,
|
|
Count,
|
|
index => Math.Pow(Math.Abs(Data[index]), p));
|
|
|
|
return Math.Pow(sum, 1.0 / p);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Normalizes this vector to a unit vector with respect to the p-norm.
|
|
/// </summary>
|
|
/// <param name="p">
|
|
/// The p value.
|
|
/// </param>
|
|
/// <returns>
|
|
/// This vector normalized to a unit vector with respect to the p-norm.
|
|
/// </returns>
|
|
public override Vector<float> 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.0f / (float)norm, clone);
|
|
|
|
return clone;
|
|
}
|
|
#endregion
|
|
|
|
#region Parse Functions
|
|
|
|
/// <summary>
|
|
/// Creates a float 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 float.
|
|
/// </summary>
|
|
/// <returns>
|
|
/// A float dense vector containing the values specified by the given string.
|
|
/// </returns>
|
|
/// <param name="value">
|
|
/// The string to parse.
|
|
/// </param>
|
|
public static DenseVector Parse(string value)
|
|
{
|
|
return Parse(value, null);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a float 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 float.
|
|
/// </summary>
|
|
/// <returns>
|
|
/// A float dense vector containing the values specified by the given string.
|
|
/// </returns>
|
|
/// <param name="value">
|
|
/// the string to parse.
|
|
/// </param>
|
|
/// <param name="formatProvider">
|
|
/// An <see cref="IFormatProvider"/> that supplies culture-specific formatting information.
|
|
/// </param>
|
|
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<string>();
|
|
GlobalizationHelper.Tokenize(tokens.AddFirst(value), keywords, 0);
|
|
var token = tokens.First;
|
|
|
|
if (token == null || tokens.Count.IsEven())
|
|
{
|
|
throw new FormatException();
|
|
}
|
|
|
|
// parsing
|
|
var data = new float[(tokens.Count + 1) >> 1];
|
|
for (var i = 0; i < data.Length; i++)
|
|
{
|
|
if (token == null || token.Value == textInfo.ListSeparator)
|
|
{
|
|
throw new FormatException();
|
|
}
|
|
|
|
data[i] = float.Parse(token.Value, NumberStyles.Any, formatProvider);
|
|
|
|
token = token.Next;
|
|
if (token != null)
|
|
{
|
|
token = token.Next;
|
|
}
|
|
}
|
|
|
|
return new DenseVector(data);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts the string representation of a real dense vector to float-precision dense vector equivalent.
|
|
/// A return value indicates whether the conversion succeeded or failed.
|
|
/// </summary>
|
|
/// <param name="value">
|
|
/// A string containing a real vector to convert.
|
|
/// </param>
|
|
/// <param name="result">
|
|
/// The parsed value.
|
|
/// </param>
|
|
/// <returns>
|
|
/// If the conversion succeeds, the result will contain a complex number equivalent to value.
|
|
/// Otherwise the result will be <c>null</c>.
|
|
/// </returns>
|
|
public static bool TryParse(string value, out DenseVector result)
|
|
{
|
|
return TryParse(value, null, out result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts the string representation of a real dense vector to float-precision dense vector equivalent.
|
|
/// A return value indicates whether the conversion succeeded or failed.
|
|
/// </summary>
|
|
/// <param name="value">
|
|
/// A string containing a real vector to convert.
|
|
/// </param>
|
|
/// <param name="formatProvider">
|
|
/// An <see cref="IFormatProvider"/> that supplies culture-specific formatting information about value.
|
|
/// </param>
|
|
/// <param name="result">
|
|
/// The parsed value.
|
|
/// </param>
|
|
/// <returns>
|
|
/// If the conversion succeeds, the result will contain a complex number equivalent to value.
|
|
/// Otherwise the result will be <c>null</c>.
|
|
/// </returns>
|
|
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
|
|
|
|
/// <summary>
|
|
/// Resets all values to zero.
|
|
/// </summary>
|
|
public override void Clear()
|
|
{
|
|
Array.Clear(Data, 0, Data.Length);
|
|
}
|
|
|
|
#region Simple arithmetic of type T
|
|
/// <summary>
|
|
/// Add two values T+T
|
|
/// </summary>
|
|
/// <param name="val1">Left operand value</param>
|
|
/// <param name="val2">Right operand value</param>
|
|
/// <returns>Result of addition</returns>
|
|
protected sealed override float AddT(float val1, float val2)
|
|
{
|
|
return val1 + val2;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Subtract two values T-T
|
|
/// </summary>
|
|
/// <param name="val1">Left operand value</param>
|
|
/// <param name="val2">Right operand value</param>
|
|
/// <returns>Result of subtract</returns>
|
|
protected sealed override float SubtractT(float val1, float val2)
|
|
{
|
|
return val1 - val2;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Multiply two values T*T
|
|
/// </summary>
|
|
/// <param name="val1">Left operand value</param>
|
|
/// <param name="val2">Right operand value</param>
|
|
/// <returns>Result of multiplication</returns>
|
|
protected sealed override float MultiplyT(float val1, float val2)
|
|
{
|
|
return val1 * val2;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Divide two values T/T
|
|
/// </summary>
|
|
/// <param name="val1">Left operand value</param>
|
|
/// <param name="val2">Right operand value</param>
|
|
/// <returns>Result of divide</returns>
|
|
protected sealed override float DivideT(float val1, float val2)
|
|
{
|
|
return val1 / val2;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Take absolute value
|
|
/// </summary>
|
|
/// <param name="val1">Source alue</param>
|
|
/// <returns>True if one; otherwise false</returns>
|
|
protected sealed override double AbsoluteT(float val1)
|
|
{
|
|
return Math.Abs(val1);
|
|
}
|
|
#endregion
|
|
}
|
|
}
|
|
|