Ejemplo n.º 1
0
        /// <summary>
        /// See <see cref="IIndexable1D.Equals(IIndexable1D, double)"/>.
        /// </summary>
        public bool Equals(IIndexable1D other, double tolerance = 1e-13)
        {
            if (this.Length != other.Length)
            {
                return(false);
            }
            var comparer      = new ValueComparer(tolerance);
            int previousIndex = 0;

            for (int i = 0; i < indices.Length; ++i)
            {
                int index = indices[i];
                for (int j = previousIndex; j < index; ++j) // zero entries between the stored ones
                {
                    if (!comparer.AreEqual(0.0, other[j]))
                    {
                        return(false);
                    }
                }
                if (!comparer.AreEqual(values[i], other[index]))
                {
                    return(false);                                             // Non zero entry
                }
                previousIndex = index + 1;
            }
            return(true);
        }
Ejemplo n.º 2
0
 public static void CheckSubvectorDimensions(IIndexable1D vector, int startIndex, int subvectorLength)
 {
     if (startIndex + vector.Length > vector.Length)
     {
         throw new NonMatchingDimensionsException(
                   "The entries to access exceed the vector's length");
     }
 }
Ejemplo n.º 3
0
 public static void CheckVectorDimensions(IIndexable1D vector1, IIndexable1D vector2)
 {
     if (vector1.Length != vector2.Length)
     {
         string message = string.Format("Vector1 has length of {0}, while vector2 has length of {1}",
                                        vector1.Length, vector2.Length);
         throw new NonMatchingDimensionsException(message);
     }
 }
Ejemplo n.º 4
0
        private void WriteFullVector(IIndexable1D vector, StreamWriter writer)
        {
            string numberFormat = NumericFormat.GetRealNumberFormat();

            for (int i = 0; i < vector.Length - 1; ++i)
            {
                writer.WriteLine(string.Format(numberFormat, vector[i]));
            }
            writer.Write(string.Format(numberFormat, vector[vector.Length - 1]));
        }
        private static void TestWriteOperation(IIndexable1D vector, string referenceFile, FullVectorWriter writer)
        {
            string tempFile = Guid.NewGuid().ToString() + ".txt";

            writer.WriteToFile(vector, tempFile);
            bool success = IOUtilities.AreFilesEquivalent(referenceFile, tempFile);

            File.Delete(tempFile);
            Assert.True(success);
        }
Ejemplo n.º 6
0
        public static Vector LinearCombination(IIndexable1D vector1, double coefficient1, IIndexable1D vector2,
                                               double coefficient2)
        {
            Preconditions.CheckVectorDimensions(vector1, vector2);
            var result = Vector.CreateZero(vector1.Length);

            for (int i = 0; i < vector1.Length; ++i)
            {
                result[i] = coefficient1 * vector1[i] + coefficient2 * vector2[i];
            }
            return(result);
        }
Ejemplo n.º 7
0
 /// <summary>
 /// See <see cref="IIndexable1D.Equals(IIndexable1D, double)"/>.
 /// </summary>
 bool IIndexable1D.Equals(IIndexable1D other, double tolerance)
 {
     if (other.Length != 2)
     {
         return(false);
     }
     else
     {
         var comparer = new ValueComparer(tolerance);
         return(comparer.AreEqual(this.data[0], other[0]) && comparer.AreEqual(this.data[1], other[1]));
     }
 }
Ejemplo n.º 8
0
        internal bool AreEqual(IIndexable1D a, IIndexable1D b)
        {
            int n = a.Length;

            if (b.Length != n)
            {
                return(false);
            }
            for (int i = 0; i < n; ++i)
            {
                if (!valueComparer.AreEqual(a[i], b[i]))
                {
                    return(false);
                }
            }
            return(true);
        }
Ejemplo n.º 9
0
        private void WriteToStream(IIndexable1D vector, StreamWriter writer)
        {
            string numberFormat = NumericFormat.GetRealNumberFormat();
            string separator    = ArrayFormat.Separator;

            if (writeLengthFirst)
            {
                writer.WriteLine(vector.Length);
            }
            writer.Write(ArrayFormat.Start);
            writer.Write(string.Format(numberFormat, vector[0]));
            for (int i = 1; i < vector.Length; ++i)
            {
                writer.Write(separator + string.Format(numberFormat, vector[i]));
            }
            writer.WriteLine(ArrayFormat.End);
        }
Ejemplo n.º 10
0
 /// <summary>
 /// See <see cref="IIndexable1D.Equals(IIndexable1D, double)"/>.
 /// </summary>
 public bool Equals(IIndexable1D other, double tolerance = 1e-13)
 {
     if (other is Vector casted)
     {
         if (this.Length != other.Length)
         {
             return(false);
         }
         var comparer = new ValueComparer(tolerance);
         for (int i = 0; i < Length; ++i)
         {
             if (!comparer.AreEqual(this.data[i], casted.data[i]))
             {
                 return(false);
             }
         }
         return(true);
     }
     else
     {
         return(other.Equals(this, tolerance)); // To avoid accessing zero entries
     }
 }
Ejemplo n.º 11
0
 internal void AssertEqual(IIndexable1D a, IIndexable1D b) => Assert.True(AreEqual(a, b));
Ejemplo n.º 12
0
 /// <summary>
 /// Writes the provided vector to the file at <paramref name="path"/>.
 /// </summary>
 /// <param name="vector">The vector to write.</param>
 /// <param name="path">The absolute path of the file, where <paramref name="vector"/> will be written.</param>
 /// <param name="append">If true, <paramref name="vector"/> will be written after the current contents of the file at
 ///     <paramref name="path"/>. If false, it will overwrite them.</param>
 public void WriteToFile(IIndexable1D vector, string path, bool append = false)
 {
     Utilities.WriteToFile((writer) => WriteFullVector(vector, writer), path, append);
 }
Ejemplo n.º 13
0
 /// <summary>
 /// Writes the provided vector to Console.
 /// </summary>
 /// <param name="vector">The vector to write.</param>
 public void WriteToConsole(IIndexable1D vector)
 {
     Utilities.WriteToConsole((writer) => WriteFullVector(vector, writer));
 }
 private static void TestWriteOperation(IIndexable1D vector, string referenceFile)
 => TestWriteOperation(vector, referenceFile, new FullVectorWriter(false));