Esempio n. 1
0
 /// <summary>
 /// Outer product 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>
 public Matrix<float> OuterProduct(SparseVector v)
 {
     return OuterProduct(this, v);
 }
Esempio n. 2
0
 /// <summary>
 /// Converts the string representation of a real sparse vector to float-precision sparse 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 SparseVector result)
 {
     return TryParse(value, null, out result);
 }
Esempio n. 3
0
        /// <summary>
        /// Converts the string representation of a real sparse vector to float-precision sparse 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 SparseVector result)
        {
            bool ret;
            try
            {
                result = Parse(value, formatProvider);
                ret = true;
            }
            catch (ArgumentNullException)
            {
                result = null;
                ret = false;
            }
            catch (FormatException)
            {
                result = null;
                ret = false;
            }

            return ret;
        }
Esempio n. 4
0
        /// <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 SparseVector(length);
            for (var i = index; i < index + length; i++)
            {
                result[i - index] = this[i];
            }

            return result;
        }
Esempio n. 5
0
        /// <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 Matrix<float> OuterProduct(SparseVector u, SparseVector v)
        {
            if (u == null)
            {
                throw new ArgumentNullException("u");
            }

            if (v == null)
            {
                throw new ArgumentNullException("v");
            }

            var matrix = new SparseMatrix(u.Count, v.Count);
            for (var i = 0; i < u._storage.ValueCount; i++)
            {
                for (var j = 0; j < v._storage.ValueCount; j++)
                {
                    if (u._storage.Indices[i] == v._storage.Indices[j])
                    {
                        matrix.At(i, j, u._storage.Values[i] * v._storage.Values[j]);
                    }
                }
            }

            return matrix;
        }
Esempio n. 6
0
        /// <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.0f)
            {
                return Clone();
            }

            if (scalar == 0f)
            {
                return new SparseVector(Count);
            }

            var copy = new SparseVector(this);
            Control.LinearAlgebraProvider.ScaleArray(scalar, copy._storage.Values, copy._storage.Values);
            return copy;
        }
Esempio n. 7
0
 /// <summary>
 /// Converts the string representation of a real sparse vector to float-precision sparse 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 SparseVector result)
 {
     return(TryParse(value, null, out result));
 }
Esempio n. 8
0
 /// <summary>
 /// Outer product 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>
 public Matrix <float> OuterProduct(SparseVector v)
 {
     return(OuterProduct(this, v));
 }
Esempio n. 9
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<float> Negate()
        {
            var result = new SparseVector(Count)
                         {
                             _nonZeroValues = new float[NonZerosCount], 
                             _nonZeroIndices = new int[NonZerosCount], 
                             NonZerosCount = NonZerosCount
                         };

            if (NonZerosCount != 0)
            {
                CommonParallel.For(
                    0,
                    NonZerosCount,
                    index => result._nonZeroValues[index] = -_nonZeroValues[index]);
                Buffer.BlockCopy(_nonZeroIndices, 0, result._nonZeroIndices, 0, NonZerosCount * Constants.SizeOfInt);
            }

            return result;
        }
Esempio n. 10
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SparseVector"/> class by
        /// copying the values from another.
        /// </summary>
        /// <param name="other">
        /// The vector to create the new vector from.
        /// </param>
        public SparseVector(SparseVector other) : this(other.Count)
        {
            // Lets copy only needed data. Portion of needed data is determined by NonZerosCount value
            _nonZeroValues = new float[other.NonZerosCount];
            _nonZeroIndices = new int[other.NonZerosCount];
            NonZerosCount = other.NonZerosCount;

            Buffer.BlockCopy(other._nonZeroValues, 0, _nonZeroValues, 0, other.NonZerosCount * Constants.SizeOfFloat);
            Buffer.BlockCopy(other._nonZeroIndices, 0, _nonZeroIndices, 0, other.NonZerosCount * Constants.SizeOfInt);
        }