/// <summary> /// Creates a double sparse 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 double. /// </summary> /// <returns> /// A double sparse 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 SparseVector Parse(string value, IFormatProvider formatProvider = null) { 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(); } // parsing var tokens = value.Split(new[] { formatProvider.GetTextInfo().ListSeparator, " ", "\t" }, StringSplitOptions.RemoveEmptyEntries); var data = tokens.Select(t => double.Parse(t, NumberStyles.Any, formatProvider)).ToList(); if (data.Count == 0) { throw new FormatException(); } return(new SparseVector(SparseVectorStorage <double> .OfEnumerable(data))); }
public IndexedVector(int[] index, float[] values, int numOfDimensions, string label = null) { var tuples = new Tuple <int, float> [Math.Min(index.Length, numOfDimensions)]; for (int i = 0; i < index.Length; i++) { if (i == numOfDimensions) { break; } tuples[i] = new Tuple <int, float>(index[i], values[i]); } Value = CreateVector.Sparse( SparseVectorStorage <float> .OfIndexedEnumerable(numOfDimensions, tuples)); Label = label; }
public IndexedVector(int[] index, float[] values, int vectorWidth, Memory <char>?data = null) { var tuples = new Tuple <int, float> [Math.Min(index.Length, vectorWidth)]; for (int i = 0; i < index.Length; i++) { if (i == vectorWidth) { break; } tuples[i] = new Tuple <int, float>(index[i], values[i]); } Value = CreateVector.Sparse( SparseVectorStorage <float> .OfIndexedEnumerable(vectorWidth, tuples)); ComponentCount = tuples.Length; Data = data; }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var serializationModel = serializer.Deserialize <VectorSerializationModel>(reader); if (serializationModel.IsDense) { var storage = DenseVectorStorage <double> .OfEnumerable( serializationModel.Storage ); var vector = new DenseVector(storage); return(vector); } else { var storage = SparseVectorStorage <double> .OfEnumerable( serializationModel.Storage ); var vector = new SparseVector(storage); return(vector); } }
public IndexedVector(IList <float> values, int vectorWidth, Memory <char>?data = null) { var tuples = new Tuple <int, float> [Math.Min(values.Count, vectorWidth)]; var i = 0; foreach (var x in values) { if (i == (vectorWidth)) { break; } tuples[i] = new Tuple <int, float>(i, x); i++; } Value = CreateVector.Sparse( SparseVectorStorage <float> .OfIndexedEnumerable(vectorWidth, tuples)); ComponentCount = tuples.Length; Data = data; }
/// <summary> /// Create a new sparse vector and initialize each value using the provided value. /// </summary> public static SparseVector Create(int length, Complex32 value) { return(new SparseVector(SparseVectorStorage <Complex32> .OfValue(length, value))); }
/// <summary> /// Create a new sparse vector straight from an initialized vector storage instance. /// The storage is used directly without copying. /// Intended for advanced scenarios where you're working directly with /// storage for performance or interop reasons. /// </summary> public SparseVector(SparseVectorStorage<double> storage) : base(storage) { _storage = storage; }
/// <summary> /// Create a new sparse vector as a copy of the given enumerable. /// This new vector will be independent from the enumerable. /// A new memory block will be allocated for storing the vector. /// </summary> public static SparseVector OfEnumerable(IEnumerable <double> enumerable) { return(new SparseVector(SparseVectorStorage <double> .OfEnumerable(enumerable))); }
/// <summary> /// Create a new sparse vector straight from an initialized vector storage instance. /// The storage is used directly without copying. /// Intended for advanced scenarios where you're working directly with /// storage for performance or interop reasons. /// </summary> public SparseVector(SparseVectorStorage <double> storage) : base(storage) { _storage = storage; }
/// <summary> /// Create a new sparse vector and initialize each value using the provided value. /// </summary> public static SparseVector Create(int length, double value) { return(new SparseVector(SparseVectorStorage <double> .OfValue(length, value))); }
public SparseVector(Vector <Complex32> other) : this(SparseVectorStorage <Complex32> .OfVector(other.Storage)) { }
/// <summary> /// Create a new sparse vector straight from an initialized vector storage instance. /// The storage is used directly without copying. /// Intended for advanced scenarios where you're working directly with /// storage for performance or interop reasons. /// </summary> public SparseVector(SparseVectorStorage<Complex32> storage) : base(storage) { _storage = storage; }
public SparseVector(Vector <float> other) : this(SparseVectorStorage <float> .OfVector(other.Storage)) { }
internal SparseVector(SparseVectorStorage<Complex32> storage) : base(storage) { _storage = storage; }
/// <summary> /// Create a new sparse vector straight from an initialized vector storage instance. /// The storage is used directly without copying. /// Intended for advanced scenarios where you're working directly with /// storage for performance or interop reasons. /// </summary> public SparseVector(SparseVectorStorage <Complex32> storage) : base(storage) { _storage = storage; }
/// <summary> /// Create a new sparse vector as a copy of the given enumerable. /// This new vector will be independent from the enumerable. /// A new memory block will be allocated for storing the vector. /// </summary> public static SparseVector OfEnumerable(IEnumerable <Complex32> enumerable) { return(new SparseVector(SparseVectorStorage <Complex32> .OfEnumerable(enumerable))); }
public SparseVector(IEnumerable <float> other) : this(SparseVectorStorage <float> .OfEnumerable(other)) { }
public void SparseVectorStorageBuilderMethods_ZeroLength_DoNotThrowException() { Assert.DoesNotThrow(() => new SparseVectorStorage <double>(0)); Assert.DoesNotThrow(() => SparseVectorStorage <double> .OfValue(0, 42)); Assert.DoesNotThrow(() => SparseVectorStorage <double> .OfInit(0, index => 42)); }
public void SparseVectorStorageOfValue_NegativeLength_ThrowsArgumentException() { Assert.That(() => SparseVectorStorage <double> .OfValue(-1, 42), Throws.TypeOf <ArgumentOutOfRangeException>() .With.Message.Contains("Value must not be negative (zero is ok).")); }
public SparseVector(IEnumerable <Complex32> other) : this(SparseVectorStorage <Complex32> .OfEnumerable(other)) { }
internal SparseVector(SparseVectorStorage<float> storage) : base(storage) { _storage = storage; }
/// <summary> /// Create a new sparse vector and initialize each value using the provided init function. /// </summary> public static SparseVector Create(int length, Func <int, double> init) { return(new SparseVector(SparseVectorStorage <double> .OfInit(length, init))); }
/// <summary> /// Create a new sparse vector and initialize each value using the provided value. /// </summary> public static SparseVector Create(int length, float value) { return(new SparseVector(SparseVectorStorage <float> .OfValue(length, value))); }
/// <summary> /// Create a new sparse vector as a copy of the given other vector. /// This new vector will be independent from the other vector. /// A new memory block will be allocated for storing the vector. /// </summary> public static SparseVector OfVector(Vector <double> vector) { return(new SparseVector(SparseVectorStorage <double> .OfVector(vector.Storage))); }
/// <summary> /// Create a new sparse vector straight from an initialized vector storage instance. /// The storage is used directly without copying. /// Intended for advanced scenarios where you're working directly with /// storage for performance or interop reasons. /// </summary> public SparseVector(SparseVectorStorage <float> storage) : base(storage) { _storage = storage; }
/// <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<Complex32> Negate() { var result = new SparseVectorStorage<Complex32>(Count); var valueCount = result.ValueCount = _storage.ValueCount; var indices = result.Indices = new int[valueCount]; var values = result.Values = new Complex32[valueCount]; if (valueCount != 0) { CommonParallel.For(0, valueCount, index => values[index] = -_storage.Values[index]); Buffer.BlockCopy(_storage.Indices, 0, indices, 0, valueCount * Constants.SizeOfInt); } return new SparseVector(result); }
/// <summary> /// Create a new sparse vector as a copy of the given other vector. /// This new vector will be independent from the other vector. /// A new memory block will be allocated for storing the vector. /// </summary> public static SparseVector OfVector(Vector <float> vector) { return(new SparseVector(SparseVectorStorage <float> .OfVector(vector.Storage))); }
/// <summary> /// Create a new sparse vector as a copy of the given indexed enumerable. /// Keys must be provided at most once, zero is assumed if a key is omitted. /// This new vector will be independent from the enumerable. /// A new memory block will be allocated for storing the vector. /// </summary> public static SparseVector OfIndexedEnumerable(int length, IEnumerable <Tuple <int, Complex32> > enumerable) { return(new SparseVector(SparseVectorStorage <Complex32> .OfIndexedEnumerable(length, enumerable))); }
public SparseVector(int length, double value) : this(SparseVectorStorage <double> .OfInit(length, i => value)) { }
/// <summary> /// Create a new sparse vector and initialize each value using the provided init function. /// </summary> public static SparseVector Create(int length, Func <int, Complex32> init) { return(new SparseVector(SparseVectorStorage <Complex32> .OfInit(length, init))); }
public SparseVector(Vector <double> other) : this(SparseVectorStorage <double> .OfVector(other.Storage)) { }
/// <summary> /// Create a new sparse vector as a copy of the given other vector. /// This new vector will be independent from the other vector. /// A new memory block will be allocated for storing the vector. /// </summary> public static SparseVector OfVector(Vector <Complex32> vector) { return(new SparseVector(SparseVectorStorage <Complex32> .OfVector(vector.Storage))); }
public SparseVector(IEnumerable <double> other) : this(SparseVectorStorage <double> .OfEnumerable(other)) { }
/// <summary> /// Create a new sparse vector straight from an initialized vector storage instance. /// The storage is used directly without copying. /// Intended for advanced scenarios where you're working directly with /// storage for performance or interop reasons. /// </summary> public static Vector <T> Sparse <T>(SparseVectorStorage <T> storage) where T : struct, IEquatable <T>, IFormattable { return(Vector <T> .Build.Sparse(storage)); }
public SparseVector(int length, Complex32 value) : this(SparseVectorStorage <Complex32> .OfInit(length, i => value)) { }
/// <summary> /// Create a new sparse vector straight from an initialized vector storage instance. /// The storage is used directly without copying. /// Intended for advanced scenarios where you're working directly with /// storage for performance or interop reasons. /// </summary> public SparseVector(SparseVectorStorage<float> storage) : base(storage) { _storage = storage; }
internal SparseVector(SparseVectorStorage<double> storage) : base(storage) { _storage = storage; }