예제 #1
0
        public void TestMultiplication()
        {
            ///Arrange
            ///
            double[,] matrix1 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
            double[] vector1 = { 2, 4, 5 };

            //DenseMatrix
            DenseMatrix denseMatrix = new DenseMatrix(matrix1);

            //CSR
            CompressedSparseRows csr = new CompressedSparseRows(matrix1);

            //TransposeMatrix
            Transpose transposeMatrix = new Transpose();

            double[,] matrixT = transposeMatrix.TransposeMatrix(matrix1);

            //COOrdinateListRowMajor
            COOrdinateListRowMajor rowCOO = new COOrdinateListRowMajor(matrix1);

            ///Act
            ///
            double[] expectedProduct1 = denseMatrix.Multiplication(vector1);
            double[] expectedProduct2 = csr.MultiplicationAx(vector1);
            double[] expectedProduct3 = csr.MultiplicationATx(vector1);
            double[] expectedProduct4 = rowCOO.Multiplication(vector1);

            ///
            double[] actualResult = new double[] { 25, 58, 91 };
            double[] actualTransMatrixMultiplic = new double[] { 53, 64, 75 };

            ///Assert
            ///
            for (int i = 0; i < vector1.Length; i++)
            {
                Assert.Equal(actualResult[i], expectedProduct1[i]);
            }

            Assert.Equal(actualResult, expectedProduct2);
            Assert.Equal(actualTransMatrixMultiplic, expectedProduct3);
            Assert.Equal(actualResult, expectedProduct4);
        }
예제 #2
0
        public (double[], int, double) CG(double[,] A, double[] b, double accuracy, int maxIterations)
        {
            CompressedSparseRows csr = new CompressedSparseRows(A);

            int rows    = csr.Rows;
            int columns = csr.Columns;

            int[]    rowoffsets = csr.RowOffsets;
            int[]    colindices = csr.ColIndices;
            double[] values     = csr.Values;

            double[] x          = new double[columns];
            double[] residual   = new double[b.Length];
            double[] r0         = new double[b.Length];
            double[] d          = new double[b.Length];
            double   rTr        = 0.0;
            double   dTAd       = 0.0;
            int      iterations = 0;

            var res = 0.0;

            for (int i = 0; i < rows; i++)
            {
                var sum = 0.0;
                for (int j = 0; j < columns; j++)
                {
                    sum        += A[i, j] * x[j];
                    d[i]        = b[i] - sum;
                    residual[i] = d[i];
                    r0          = residual;
                }
            }

            for (int t = 0; t < maxIterations; t++)
            {
                for (int i = 0; i < rows; i++)
                {
                    rTr += residual[i] * residual[i];
                }

                for (int i = 0; i < rows; i++)
                {
                    for (int j = 0; j < columns; j++)
                    {
                        dTAd += A[i, j] * d[i] * d[j];
                    }
                }

                double alpha = rTr / dTAd;

                double[] rn = new double[columns];
                for (int i = 0; i < columns; i++)
                {
                    x[i] += alpha * d[i];
                    rn[i] = residual[i];
                    for (int j = 0; j < columns; j++)
                    {
                        rn[i] -= alpha * A[i, j] * d[j];
                    }
                }

                double rnTrn = 0.0;
                double norm  = 0.0;
                var    sum   = 0.0;
                for (int i = 0; i < columns; i++)
                {
                    rnTrn += rn[i] * rn[i];
                    norm  += rn[i] * rn[i];
                    norm   = Math.Sqrt(norm);
                    sum   += r0[i] * r0[i];
                    sum    = Math.Sqrt(sum);
                }
                res = norm / sum;

                if (res < 1e-10)
                {
                    break;
                }

                double beta = rnTrn / rTr;
                for (int i = 0; i < columns; i++)
                {
                    d[i]        = beta * d[i] + rn[i];
                    residual[i] = rn[i];
                }

                iterations = t;
            }

            return(x, iterations, res);
        }
        public (double[], int, double) SD(double[,] A, double[] b, double accuracy, int maxIterations)
        {
            CompressedSparseRows csr = new CompressedSparseRows(A);

            int rows    = csr.Rows;
            int columns = csr.Columns;

            int[]    rowoffsets = csr.RowOffsets;
            int[]    colindices = csr.ColIndices;
            double[] values     = csr.Values;

            double[] x          = new double[columns];
            double[] residual   = new double[b.Length];
            double[] r0         = new double[b.Length];
            double[] d          = new double[b.Length];
            double   rTr        = 0.0;
            double   rTAr       = 0.0;
            int      iterations = 0;

            double norm = 0.0;

            for (int i = 0; i < rows; i++)
            {
                var sum = 0.0;
                for (int j = 0; j < columns; j++)
                {
                    sum        += A[i, j] * x[j];
                    d[i]        = b[i] - sum;
                    residual[i] = d[i];
                    r0          = residual;
                }
            }

            for (int t = 0; t < maxIterations; t++)
            {
                for (int i = 0; i < rows; i++)
                {
                    rTr += residual[i] * residual[i];
                }

                for (int i = 0; i < rows; i++)
                {
                    for (int j = 0; j < columns; j++)
                    {
                        rTAr += A[i, j] * residual[i] * residual[j];
                    }
                }

                double alpha = rTr / rTAr;

                double[] rn = new double[columns];
                for (int i = 0; i < columns; i++)
                {
                    x[i] += alpha * residual[i];
                    rn[i] = residual[i];
                    for (int j = 0; j < columns; j++)
                    {
                        rn[i] -= alpha * A[i, j] * residual[j];
                    }
                }

                var product = 0.0;
                for (int i = 0; i < columns; i++)
                {
                    for (int j = 0; j < columns; j++)
                    {
                        product += x[j] * A[i, j] * x[j];
                    }
                }

                norm = Math.Sqrt(product);

                if (norm < 1e-10)
                {
                    break;
                }

                for (int i = 0; i < columns; i++)
                {
                    residual[i] = rn[i];
                }

                iterations = t;
            }

            return(x, iterations, norm);
        }
예제 #4
0
        public void TestStorage()
        {
            double[,] matrix2 =
            {
                { 9, 0, 3, 0 },
                { 0, 8, 0, 0 },
                { 0, 2, 6, 0 },
                { 1, 0, 0, 5 },
            };
            double[] vector2 = { 1, 2, 3, 4 };

            //CSR
            CompressedSparseRows csr = new CompressedSparseRows(matrix2);

            double[] values     = csr.Values;
            int[]    colindices = csr.ColIndices;
            int[]    rowoffsets = csr.RowOffsets;

            double[] actualValues     = { 9, 3, 8, 2, 6, 1, 5 };
            int[]    actualColindices = { 0, 2, 1, 1, 2, 0, 3 };
            int[]    actualRowoffsets = { 0, 2, 3, 5, 7 };
            double[] actualProduct2   = { 21, 16, 22, 21 };
            Assert.Equal(values, actualValues);
            Assert.Equal(colindices, actualColindices);
            Assert.Equal(rowoffsets, actualRowoffsets);

            //COOrdinateListRowMajor
            COOrdinateListRowMajor rowCOO = new COOrdinateListRowMajor(matrix2);

            double[] valuesCOOrow     = rowCOO.Values;
            int[]    colindicesCOOrow = rowCOO.ColumnsArray;
            int[]    rowoffsetsCOOrow = rowCOO.RowsArray;

            double[] actualValuesCOOrow     = { 9, 3, 8, 2, 6, 1, 5 };
            int[]    actualColindicesCOOrow = { 0, 2, 1, 1, 2, 0, 3 };
            int[]    actualRowoffsetsCOOrow = { 0, 0, 1, 2, 2, 3, 3 };

            Assert.Equal(valuesCOOrow, actualValuesCOOrow);
            Assert.Equal(colindicesCOOrow, actualColindicesCOOrow);
            Assert.Equal(rowoffsetsCOOrow, actualRowoffsetsCOOrow);

            //COOrdinateListColumnMajor
            COOrdinateListColumnMajor columnCOO = new COOrdinateListColumnMajor(matrix2);

            double[] valuesCOOcolumn     = columnCOO.Values;
            int[]    colindicesCOOcolumn = columnCOO.ColumnsArray;
            int[]    rowoffsetsCOOcolumn = columnCOO.RowsArray;

            double[] actualValuesCOOcolumn     = { 9, 1, 8, 2, 3, 6, 5 };
            int[]    actualColindicesCOOcolumn = { 0, 0, 1, 1, 2, 2, 3 };
            int[]    actualRowoffsetsCOOcolumn = { 0, 3, 1, 2, 0, 2, 3 };
            Assert.Equal(valuesCOOcolumn, actualValuesCOOcolumn);
            Assert.Equal(colindicesCOOcolumn, actualColindicesCOOcolumn);
            Assert.Equal(rowoffsetsCOOcolumn, actualRowoffsetsCOOcolumn);


            //Banded Storage
            double[,] bandedmatrix =
            {
                { 21,  1,  0,  4,  0 },
                {  1, 22,  2,  0,  0 },
                {  0,  2, 23,  1,  3 },
                {  4,  0,  1, 24,  2 },
                {  0,  0,  3,  2, 25 },
            };
            BandedStorage bandedStore  = new BandedStorage();
            var           bandedValues = bandedStore.BandedStorageMethod(bandedmatrix);

            double[,] actualBandedValues =
            {
                { 21, 22, 23, 24, 25 },
                {  1,  2,  1,  2,  0 },
                {  0,  0,  3,  0,  0 },
                {  4,  0,  0,  0,  0 },
            };
            Assert.Equal(bandedValues, actualBandedValues);

            //PackedStorageLower
            double[,] lowerTriangleMatrix =
            {
                {  1,  0,  0,  0,  0 },
                {  2,  3,  0,  0,  0 },
                {  4,  5,  6,  0,  0 },
                {  7,  8,  9, 10,  0 },
                { 11, 12, 13, 14, 15 }
            };
            double[,] upperTriangleMatrix =
            {
                { 1, 2, 3 },
                { 0, 5, 7 },
                { 0, 0, 8 }
            };

            PackedStorageLower packedLow = new PackedStorageLower();
            var lowerColumn = packedLow.ColumnMajorLayout(lowerTriangleMatrix);
            var lowerRow    = packedLow.RowMajorLayout(lowerTriangleMatrix);

            PackedStorageUpper packedUp = new PackedStorageUpper();
            var upperColumn             = packedUp.ColumnMajorLayout(upperTriangleMatrix);
            var upperRow = packedUp.RowMajorLayout(upperTriangleMatrix);

            double[] actualPackedLowerColumn =
            {
                1, 2, 4, 7, 11, 3, 5, 8, 12, 6, 9, 13, 10, 14, 15
            };
            double[] actualPackedLowerRow =
            {
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
            };

            double[] actualPackedUpperColumn =
            {
                1, 2, 5, 3, 7, 8
            };
            double[] actualPackedUpperRow =
            {
                1, 2, 3, 5, 7, 8
            };

            Assert.Equal(lowerColumn, actualPackedLowerColumn);
            Assert.Equal(lowerRow, actualPackedLowerRow);
            Assert.Equal(upperColumn, actualPackedUpperColumn);
            Assert.Equal(upperRow, actualPackedUpperRow);


            // SkyLine Storage
            double[,] skymatrix =
            { { 21,  1,  0,  4,  0 },
              {  1, 22,  2,  0,  0 },
              {  0,  2, 23,  1,  3 },
              {  4,  0,  1, 24,  2 },
              {  0,  0,  3,  2, 25 }, };
            Skyline skyline     = new Skyline(skymatrix, 2, 4);
            var     valuesArray = skyline.ValuesArray;

            int[]  diagOffsets     = skyline.DiagOffsets;
            int[]  actualDiagonals = { 0, 1, 3, 5, 9, 13 };
            double value1          = skyline.Value;

            double[] actualValuesArray = { 21, 22, 1, 23, 2, 24, 1, 0, 4, 25, 2, 3 };
            double   actualValue1      = 3;

            Assert.Equal(diagOffsets, actualDiagonals);
            Assert.Equal(valuesArray, actualValuesArray);
            Assert.Equal(value1, actualValue1);
        }