Ejemplo n.º 1
0
        /// <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)));
        }
Ejemplo n.º 2
0
        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;
        }
Ejemplo n.º 3
0
        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;
        }
Ejemplo n.º 4
0
        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);
            }
        }
Ejemplo n.º 5
0
        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;
        }
Ejemplo n.º 6
0
 /// <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)));
 }
Ejemplo n.º 7
0
 /// <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;
 }
Ejemplo n.º 8
0
 /// <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)));
 }
Ejemplo n.º 9
0
 /// <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;
 }
Ejemplo n.º 10
0
 /// <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)));
 }
Ejemplo n.º 11
0
 public SparseVector(Vector <Complex32> other)
     : this(SparseVectorStorage <Complex32> .OfVector(other.Storage))
 {
 }
Ejemplo n.º 12
0
 /// <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;
 }
Ejemplo n.º 13
0
 public SparseVector(Vector <float> other)
     : this(SparseVectorStorage <float> .OfVector(other.Storage))
 {
 }
Ejemplo n.º 14
0
 internal SparseVector(SparseVectorStorage<Complex32> storage)
     : base(storage)
 {
     _storage = storage;
 }
Ejemplo n.º 15
0
 /// <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;
 }
Ejemplo n.º 16
0
 /// <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)));
 }
Ejemplo n.º 17
0
 public SparseVector(IEnumerable <float> other)
     : this(SparseVectorStorage <float> .OfEnumerable(other))
 {
 }
Ejemplo n.º 18
0
 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));
 }
Ejemplo n.º 19
0
 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)."));
 }
Ejemplo n.º 20
0
 public SparseVector(IEnumerable <Complex32> other)
     : this(SparseVectorStorage <Complex32> .OfEnumerable(other))
 {
 }
Ejemplo n.º 21
0
 internal SparseVector(SparseVectorStorage<float> storage)
     : base(storage)
 {
     _storage = storage;
 }
Ejemplo n.º 22
0
 /// <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)));
 }
Ejemplo n.º 23
0
 /// <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)));
 }
Ejemplo n.º 24
0
 /// <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)));
 }
Ejemplo n.º 25
0
 /// <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;
 }
Ejemplo n.º 26
0
        /// <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);
        }
Ejemplo n.º 27
0
 /// <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)));
 }
Ejemplo n.º 28
0
 /// <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)));
 }
Ejemplo n.º 29
0
 public SparseVector(int length, double value)
     : this(SparseVectorStorage <double> .OfInit(length, i => value))
 {
 }
Ejemplo n.º 30
0
 /// <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)));
 }
Ejemplo n.º 31
0
 public SparseVector(Vector <double> other)
     : this(SparseVectorStorage <double> .OfVector(other.Storage))
 {
 }
Ejemplo n.º 32
0
 /// <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)));
 }
Ejemplo n.º 33
0
 public SparseVector(IEnumerable <double> other)
     : this(SparseVectorStorage <double> .OfEnumerable(other))
 {
 }
Ejemplo n.º 34
0
 /// <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));
 }
Ejemplo n.º 35
0
 public SparseVector(int length, Complex32 value)
     : this(SparseVectorStorage <Complex32> .OfInit(length, i => value))
 {
 }
Ejemplo n.º 36
0
 /// <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;
 }
Ejemplo n.º 37
0
 internal SparseVector(SparseVectorStorage<double> storage)
     : base(storage)
 {
     _storage = storage;
 }