public bool AreEqual(Matrix a)
        {
            if ((RowsCount != a.RowsCount) || (ColumnsCount != a.ColumnsCount))
            {
                throw new SizesDismatchException($"matrices sizes must match." +
                                                 $"Size of left matrix: [{RowsCount},{ColumnsCount}], " +
                                                 $"size of right matrix: [{a.RowsCount},{a.ColumnsCount}].");
            }

            bool result = true;

            for (int i = 0; i < RowsCount; i++)
            {
                for (int j = 0; j < ColumnsCount; j++)
                {
                    if (!DoubleComparator.AreEqual(a[i, j], this[i, j]))
                    {
                        result = false;
                        break;
                    }
                }
            }

            return(result);
        }
        /// <summary>
        /// Sorts the receiver according
        /// to the order induced by the specified comparatord  All elements in the
        /// range must be <i>mutually comparable</i> by the specified comparator
        /// (that is, <i>c.CompareTo(e1, e2)</i> must not throw a
        /// <i>ClassCastException</i> for any elements <i>e1</i> and
        /// <i>e2</i> in the range).<p>
        ///
        /// The sorting algorithm is a tuned quicksort,
        /// adapted from Jon Ld Bentley and Md Douglas McIlroy's "Engineering a
        /// Sort Function", Software-Practice and Experience, Vold 23(11)
        /// Pd 1249-1265 (November 1993)d  This algorithm offers n*log(n)
        /// performance on many data sets that cause other quicksorts to degrade to
        /// quadratic performance.
        ///
        /// <summary>
        /// <param name="from">the index of the first element (inclusive) to be</param>
        ///        sorted.
        /// <param name="to">the index of the last element (inclusive) to be sorted.</param>
        /// <param name="c">the comparator to determine the order of the receiver.</param>
        /// <exception cref="ClassCastException">if the array contains elements that are not </exception>
        ///	       <i>mutually comparable</i> using the specified comparator.
        /// <exception cref="ArgumentException">if <i>fromIndex &gt; toIndex</i> </exception>
        /// <exception cref="ArrayIndexOutOfRangeException">if <i>fromIndex &lt; 0</i> or </exception>
        ///	       <i>toIndex &gt; a.Length</i>
        /// <see cref="Comparator"></see>
        /// <exception cref="IndexOutOfRangeException">index is out of range (<i>_size()&gt;0 && (from&lt;0 || from&gt;to || to&gt;=_size())</i>). </exception>
        public virtual void QuickSortFromTo(int from, int to, DoubleComparator c)
        {
            int mySize = Size;

            CheckRangeFromTo(from, to, mySize);

            double[] myElements = GetElements();
            Cern.Colt.Sorting.QuickSort(myElements, from, to + 1, c);
            SetElements(myElements);
            SetSizeRaw(mySize);
        }
Beispiel #3
0
        /// <summary>
        /// Sorts the vector into ascending order, according to the order induced by the specified comparator.
        /// </summary>
        /// <param name="vector">
        /// The vector to be sorted.
        /// </param>
        /// <param name="c">
        /// The comparator to determine the order.
        /// </param>
        /// <returns>
        /// A new matrix view sorted as specified.
        /// </returns>
        public DoubleMatrix1D Sort(DoubleMatrix1D vector, DoubleComparator c)
        {
            var indexes = new int[vector.Size]; // row indexes to reorder instead of matrix itself

            for (int i = indexes.Length; --i >= 0;)
            {
                indexes[i] = i;
            }

            RunSort(indexes, 0, indexes.Length, (a, b) => c(vector[a], vector[b]));

            return(vector.ViewSelection(indexes));
        }
        public void IndexGetTest()
        {
            // arrange
            Vector v1 = _vectorFactory.CreateVector(new List <double> {
                1, 2, 3
            });

            // act
            double result = v1[1];

            // assert
            double expected = 2;

            Assert.IsTrue(DoubleComparator.AreEqual(expected, result));
        }
        public void GetElementTest()
        {
            // arrange
            double[,] matrix1 = new double[3, 3] {
                { 5, 8, -4 },
                { 6, 9, -5 },
                { 4, 7, -3 }
            };
            Matrix m1 = _matrixFactory.CreateMatrix(matrix1);

            // act
            double result = m1[1, 2];

            // assert
            double expected = -5;

            Assert.IsTrue(DoubleComparator.AreEqual(expected, result));
        }
        public void ScalarMultipleTest1()
        {
            // arrange
            Vector v1 = _vectorFactory.CreateVector(new List <double> {
                1, 2, 3
            });
            Vector v2 = _vectorFactory.CreateVector(new List <double> {
                4, 5, 6
            });

            // act
            double result = v1.ScalarMultiply(v2);

            // assert
            double expected = 32;

            Assert.IsTrue(DoubleComparator.AreEqual(result, expected));
        }
        public bool AreEqual(Vector a)
        {
            if (Size != a.Size)
            {
                throw new SizesDismatchException($"Error in multiply two vectors: their sizes must match." +
                                                 $"Size of left vector: {Size}, size of right vector: {a.Size}.");
            }

            bool result = true;

            for (int i = 0; i < Size; i++)
            {
                if (!DoubleComparator.AreEqual(a[i], this[i]))
                {
                    result = false;
                    break;
                }
            }

            return(result);
        }
        public void ConvertToEnumerableTest()
        {
            // arrange
            Vector v1 = _vectorFactory.CreateVector(new List <double> {
                1, 2, 3
            });

            // act
            List <double> result = v1.ConvertToEnumerable().ToList();

            // assert
            bool areEqual = true;

            for (int i = 0; i < v1.Size; i++)
            {
                if (!DoubleComparator.AreEqual(v1[i], result[i]))
                {
                    areEqual = false;
                    break;
                }
            }
            Assert.IsTrue(areEqual);
        }
        public static void SaveOrderIntoFile(string prologue, List<PaymentRecord> content, List<List<int>> filialMarks,
            string epilogue)
        {
            List<String> lines = new List<String>();

            int count = content.Count;

            for (int i = 0; i < count; i++)
            {
                if (filialMarks[i].Count == 1)
                {
                    int filialIndex = Array.IndexOf(filialSpecialAccounts, content[i].correspondentAccount);
                    int paperAccountIndex = Array.IndexOf(paperAccounts, content[i].correspondentAccount);

                    if (filialIndex != -1)
                    {
                        lines.Add(ChangedPaymentRecord(content[i], filialIndex, true, paperAccountIndex));
                    }
                    else
                        lines.Add(ChangedPaymentRecord(content[i], filialMarks[i].First(), false, paperAccountIndex));
                }
                else
                {
                    int filialCount = filialMarks[i].Count;
                    int paperAccountIndex = Array.IndexOf(paperAccounts, content[i].correspondentAccount);

                    if (paperAccountIndex > -1)
                    {
                        for (int j = 0; j < filialCount; j++)
                            if (j == 0)
                                lines.Add(ChangedPaymentRecord(content[i], filialMarks[i][j],  true, true, false, paperAccountIndex));
                            else
                                lines.Add(ChangedPaymentRecord(content[i], filialMarks[i][j],  true, false, false, paperAccountIndex));
                    }
                    else
                    {
                        for (int j = 0; j < filialCount; j++)
                        {
                            if (j == 0)
                                lines.Add(ChangedPaymentRecord(content[i], filialMarks[i][j], true, true, false));
                            else
                                lines.Add(ChangedPaymentRecord(content[i], filialMarks[i][j], true, false, false));
                        }
                    }
                }
            }

            DoubleComparator doubleComparator = new DoubleComparator();

            foreach (KeyValuePair<String, double> val in generalSumDictionary)
            {
                if (doubleComparator.Compare(val.Value, 0) != 0)
                {
                    Clipboard.SetText(val.Key);
                    MessageBox.Show("Обнаружено несоответствие. Не совпадают итоговая сумма и сумма по филиалам:\nНомер док. " + 
                        val.Key, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }

            String allLines = "";

            foreach (String line in lines)
                allLines += line + "\r\n";

            SaveFileDialog dialog = new SaveFileDialog();
            dialog.Filter = "txt files (*.txt)|*.txt";
            dialog.RestoreDirectory = true;
            dialog.FileName = MainForm.fileName.Remove(MainForm.fileName.Length - 4) + "_П_";
            resultFileName = dialog.FileName;

            if (dialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            File.WriteAllText(dialog.FileName, prologue, Encoding.Default);
            File.AppendAllText(dialog.FileName, allLines, Encoding.Default);
            File.AppendAllText(dialog.FileName, epilogue, Encoding.Default);
        }