// // Math.NET Numerics, part of the Math.NET Project // http://numerics.mathdotnet.com // http://github.com/mathnet/mathnet-numerics // // 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 System.Runtime.CompilerServices; using IStation.Numerics.LinearAlgebra.Storage; namespace IStation.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 !NETSTANDARD1_3 , 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 length or number of dimensions of this vector. /// 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] { #if !NET40 [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")] get { return Storage[index]; } #if !NET40 [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")] 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 . #if !NET40 [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")] 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. #if !NET40 [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")] 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(nameof(count), "Value must be positive."); } if (index + count > Count || index < 0) { throw new ArgumentOutOfRangeException(nameof(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(nameof(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 copy. 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(nameof(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(nameof(destination)); } // TODO: refactor range checks Storage.CopySubVectorTo(destination.Storage, sourceIndex, targetIndex, count); } /// /// Returns the data contained in the vector as an array. /// The returned array will be independent from this vector. /// A new memory block will be allocated for the array. /// /// The vector's data as an array. public T[] ToArray() { return Storage.ToArray(); } /// /// Returns the internal array of this vector if, and only if, this vector is stored by such an array internally. /// Otherwise returns null. Changes to the returned array and the vector will affect each other. /// Use ToArray instead if you always need an independent array. /// public T[] AsArray() { return Storage.AsArray(); } /// /// 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(); } } /// /// 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.MapInplace(f, zeros); } /// /// 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.MapIndexedInplace(f, zeros); } /// /// 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) { if (ReferenceEquals(this, result)) { Storage.MapInplace(f, zeros); } else { 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) { if (ReferenceEquals(this, result)) { Storage.MapIndexedInplace(f, zeros); } else { Storage.MapIndexedTo(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. /// 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 MapConvert(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 MapIndexedConvert(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; } /// /// Applies a function to each value pair of two vectors and replaces the value in the result vector. /// public void Map2(Func f, Vector other, Vector result, Zeros zeros = Zeros.AllowSkip) { Storage.Map2To(result.Storage, other.Storage, f, zeros, ExistingData.Clear); } /// /// Applies a function to each value pair of two vectors and returns the results as a new vector. /// public Vector Map2(Func f, Vector other, Zeros zeros = Zeros.AllowSkip) { var result = Build.SameAs(this); Storage.Map2To(result.Storage, other.Storage, f, zeros, ExistingData.AssumeZeros); return result; } /// /// Applies a function to update the status with each value pair of two vectors and returns the resulting status. /// public TState Fold2(Func f, TState state, Vector other, Zeros zeros = Zeros.AllowSkip) where TOther : struct, IEquatable, IFormattable { return Storage.Fold2(other.Storage, f, state, zeros); } /// /// Returns a tuple with the index and value of the first element satisfying a predicate, or null if none is found. /// Zero elements may be skipped on sparse data structures if allowed (default). /// public Tuple Find(Func predicate, Zeros zeros = Zeros.AllowSkip) { return Storage.Find(predicate, zeros); } /// /// Returns a tuple with the index and values of the first element pair of two vectors of the same size satisfying a predicate, or null if none is found. /// Zero elements may be skipped on sparse data structures if allowed (default). /// public Tuple Find2(Func predicate, Vector other, Zeros zeros = Zeros.AllowSkip) where TOther : struct, IEquatable, IFormattable { return Storage.Find2(other.Storage, predicate, zeros); } /// /// Returns true if at least one element satisfies a predicate. /// Zero elements may be skipped on sparse data structures if allowed (default). /// public bool Exists(Func predicate, Zeros zeros = Zeros.AllowSkip) { return Storage.Find(predicate, zeros) != null; } /// /// Returns true if at least one element pairs of two vectors of the same size satisfies a predicate. /// Zero elements may be skipped on sparse data structures if allowed (default). /// public bool Exists2(Func predicate, Vector other, Zeros zeros = Zeros.AllowSkip) where TOther : struct, IEquatable, IFormattable { return Storage.Find2(other.Storage, predicate, zeros) != null; } /// /// Returns true if all elements satisfy a predicate. /// Zero elements may be skipped on sparse data structures if allowed (default). /// public bool ForAll(Func predicate, Zeros zeros = Zeros.AllowSkip) { return Storage.Find(x => !predicate(x), zeros) == null; } /// /// Returns true if all element pairs of two vectors of the same size satisfy a predicate. /// Zero elements may be skipped on sparse data structures if allowed (default). /// public bool ForAll2(Func predicate, Vector other, Zeros zeros = Zeros.AllowSkip) where TOther : struct, IEquatable, IFormattable { return Storage.Find2(other.Storage, (x, y) => !predicate(x, y), zeros) == null; } } }