//
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime;
using MathNet.Numerics.LinearAlgebra.Storage;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.LinearAlgebra
{
///
/// Defines the generic class for Vector classes.
///
/// Supported data types are double, single, , and .
[Serializable]
public abstract partial class Vector :
IFormattable, IEquatable>, IList, IList
#if !PORTABLE
, ICloneable
#endif
where T : struct, IEquatable, IFormattable
{
///
/// Initializes a new instance of the Vector class.
///
protected Vector(VectorStorage storage)
{
Storage = storage;
Count = storage.Length;
}
public static readonly VectorBuilder Build = BuilderInstance.Vector;
///
/// Gets the raw vector data storage.
///
public VectorStorage Storage { get; private set; }
///
/// Gets the number of items.
///
public int Count { get; private set; }
/// Gets or sets the value at the given .
/// The index of the value to get or set.
/// The value of the vector at the given .
/// If is negative or
/// greater than the size of the vector.
public T this[int index]
{
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
//[MethodImpl(MethodImplOptions.AggressiveInlining)] .Net 4.5 only
get { return Storage[index]; }
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
//[MethodImpl(MethodImplOptions.AggressiveInlining)] .Net 4.5 only
set { Storage[index] = value; }
}
/// Gets the value at the given without range checking..
/// The index of the value to get or set.
/// The value of the vector at the given .
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
//[MethodImpl(MethodImplOptions.AggressiveInlining)] .Net 4.5 only
public T At(int index)
{
return Storage.At(index);
}
/// Sets the at the given without range checking..
/// The index of the value to get or set.
/// The value to set.
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
//[MethodImpl(MethodImplOptions.AggressiveInlining)] .Net 4.5 only
public void At(int index, T value)
{
Storage.At(index, value);
}
///
/// Resets all values to zero.
///
public void Clear()
{
Storage.Clear();
}
///
/// Sets all values of a subvector to zero.
///
public void ClearSubVector(int index, int count)
{
if (count < 1)
{
throw new ArgumentOutOfRangeException("count", Resources.ArgumentMustBePositive);
}
if (index + count > Count || index < 0)
{
throw new ArgumentOutOfRangeException("index");
}
Storage.Clear(index, count);
}
///
/// Set all values whose absolute value is smaller than the threshold to zero, in-place.
///
public abstract void CoerceZero(double threshold);
///
/// Set all values that meet the predicate to zero, in-place.
///
public void CoerceZero(Func zeroPredicate)
{
MapInplace(x => zeroPredicate(x) ? Zero : x, Zeros.AllowSkip);
}
///
/// Returns a deep-copy clone of the vector.
///
/// A deep-copy clone of the vector.
public Vector Clone()
{
var result = Build.SameAs(this);
Storage.CopyToUnchecked(result.Storage, ExistingData.AssumeZeros);
return result;
}
///
/// Set the values of this vector to the given values.
///
/// The array containing the values to use.
/// If is .
/// If is not the same size as this vector.
public void SetValues(T[] values)
{
var source = new DenseVectorStorage(Count, values);
source.CopyTo(Storage);
}
///
/// 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);
}
///
/// Creates a vector containing specified elements.
///
/// The first element to begin copying from.
/// The number of elements to copy.
/// A vector containing a copy of the specified elements.
/// - If is not positive or
/// greater than or equal to the size of the vector.
/// - If + is greater than or equal to the size of the vector.
///
/// If is not positive.
public Vector SubVector(int index, int count)
{
var target = Build.SameAs(this, count);
Storage.CopySubVectorTo(target.Storage, index, 0, count, ExistingData.AssumeZeros);
return target;
}
///
/// Copies the values of a given vector into a region in this vector.
///
/// The field to start copying to
/// The number of fields to cpy. Must be positive.
/// The sub-vector to copy from.
/// If is
public void SetSubVector(int index, int count, Vector subVector)
{
if (subVector == null)
{
throw new ArgumentNullException("subVector");
}
subVector.Storage.CopySubVectorTo(Storage, 0, index, count);
}
///
/// 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);
}
///
/// 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, ExistingData.AssumeZeros);
return result.Data;
}
///
/// Create a matrix based on this vector in column form (one single column).
///
///
/// This vector as a column matrix.
///
public Matrix ToColumnMatrix()
{
var result = Matrix.Build.SameAs(this, Count, 1);
Storage.CopyToColumnUnchecked(result.Storage, 0, ExistingData.AssumeZeros);
return result;
}
///
/// Create a matrix based on this vector in row form (one single row).
///
///
/// This vector as a row matrix.
///
public Matrix ToRowMatrix()
{
var result = Matrix.Build.SameAs(this, 1, Count);
Storage.CopyToRowUnchecked(result.Storage, 0, ExistingData.AssumeZeros);
return result;
}
///
/// Returns an IEnumerable that can be used to iterate through all values of the vector.
///
///
/// The enumerator will include all values, even if they are zero.
///
public IEnumerable Enumerate()
{
return Storage.Enumerate();
}
///
/// Returns an IEnumerable that can be used to iterate through all values of the vector.
///
///
/// The enumerator will include all values, even if they are zero.
///
public IEnumerable Enumerate(Zeros zeros = Zeros.Include)
{
switch (zeros)
{
case Zeros.AllowSkip:
return Storage.EnumerateNonZero();
default:
return Storage.Enumerate();
}
}
///
/// Returns an IEnumerable that can be used to iterate through all values of the vector and their index.
///
///
/// The enumerator returns a Tuple with the first value being the element index
/// and the second value being the value of the element at that index.
/// The enumerator will include all values, even if they are zero.
///
public IEnumerable> EnumerateIndexed()
{
return Storage.EnumerateIndexed();
}
///
/// Returns an IEnumerable that can be used to iterate through all values of the vector and their index.
///
///
/// The enumerator returns a Tuple with the first value being the element index
/// and the second value being the value of the element at that index.
/// The enumerator will include all values, even if they are zero.
///
public IEnumerable> EnumerateIndexed(Zeros zeros = Zeros.Include)
{
switch (zeros)
{
case Zeros.AllowSkip:
return Storage.EnumerateNonZeroIndexed();
default:
return Storage.EnumerateIndexed();
}
}
///
/// Returns an IEnumerable that can be used to iterate through all non-zero values of the vector.
///
///
/// The enumerator will skip all elements with a zero value.
///
[Obsolete("Use Enumerate(Zeros.AllowSkip) instead. Will be removed in v4.")]
public IEnumerable EnumerateNonZero()
{
return Storage.EnumerateNonZero();
}
///
/// Returns an IEnumerable that can be used to iterate through all non-zero values of the vector and their index.
///
///
/// The enumerator returns a Tuple with the first value being the element index
/// and the second value being the value of the element at that index.
/// The enumerator will skip all elements with a zero value.
///
[Obsolete("Use EnumerateIndexed(Zeros.AllowSkip) instead. Will be removed in v4.")]
public IEnumerable> EnumerateNonZeroIndexed()
{
return Storage.EnumerateNonZeroIndexed();
}
///
/// Applies a function to each value of this vector and replaces the value with its result.
/// If forceMapZero is not set to true, zero values may or may not be skipped depending
/// on the actual data storage implementation (relevant mostly for sparse vectors).
///
public void MapInplace(Func f, Zeros zeros = Zeros.AllowSkip)
{
Storage.MapToUnchecked(Storage, f, zeros, ExistingData.AssumeZeros);
}
///
/// Applies a function to each value of this vector and replaces the value with its result.
/// The index of each value (zero-based) is passed as first argument to the function.
/// If forceMapZero is not set to true, zero values may or may not be skipped depending
/// on the actual data storage implementation (relevant mostly for sparse vectors).
///
public void MapIndexedInplace(Func f, Zeros zeros = Zeros.AllowSkip)
{
Storage.MapIndexedToUnchecked(Storage, f, zeros, ExistingData.AssumeZeros);
}
///
/// Applies a function to each value of this vector and replaces the value in the result vector.
/// If forceMapZero is not set to true, zero values may or may not be skipped depending
/// on the actual data storage implementation (relevant mostly for sparse vectors).
///
public void Map(Func f, Vector result, Zeros zeros = Zeros.AllowSkip)
where TU : struct, IEquatable, IFormattable
{
Storage.MapTo(result.Storage, f, zeros, zeros == Zeros.Include ? ExistingData.AssumeZeros : ExistingData.Clear);
}
///
/// Applies a function to each value of this vector and replaces the value in the result vector.
/// The index of each value (zero-based) is passed as first argument to the function.
/// If forceMapZero is not set to true, zero values may or may not be skipped depending
/// on the actual data storage implementation (relevant mostly for sparse vectors).
///
public void MapIndexed(Func f, Vector result, Zeros zeros = Zeros.AllowSkip)
where TU : struct, IEquatable, IFormattable
{
Storage.MapIndexedTo(result.Storage, f, zeros, zeros == Zeros.Include ? ExistingData.AssumeZeros : ExistingData.Clear);
}
///
/// Applies a function to each value of this vector and returns the results as a new vector.
/// If forceMapZero is not set to true, zero values may or may not be skipped depending
/// on the actual data storage implementation (relevant mostly for sparse vectors).
///
public Vector Map(Func f, Zeros zeros = Zeros.AllowSkip)
where TU : struct, IEquatable, IFormattable
{
var result = Vector.Build.SameAs(this);
Storage.MapToUnchecked(result.Storage, f, zeros, ExistingData.AssumeZeros);
return result;
}
///
/// Applies a function to each value of this vector and returns the results as a new vector.
/// The index of each value (zero-based) is passed as first argument to the function.
/// If forceMapZero is not set to true, zero values may or may not be skipped depending
/// on the actual data storage implementation (relevant mostly for sparse vectors).
///
public Vector MapIndexed(Func f, Zeros zeros = Zeros.AllowSkip)
where TU : struct, IEquatable, IFormattable
{
var result = Vector.Build.SameAs(this);
Storage.MapIndexedToUnchecked(result.Storage, f, zeros, ExistingData.AssumeZeros);
return result;
}
}
}