Example #1
0
        public void AddTest()
        {
            int[,] firstData = new int[3, 3]
                             {
                                 {1, 0, 3},
                                 {0, 2, 5},
                                 {4, 1, 5},
                             };
            int[,] secondData = new int[3, 3]
                              {
                                 {1, 5, 3},
                                 {0, 2, 8},
                                 {4, 2, 5},
                             };

            CRSMatrix firstMatrix = new CRSMatrix(firstData);

            CRSMatrix secondMatrix = new CRSMatrix(secondData);

            CRSMatrix result = firstMatrix + secondMatrix;

            int[,] resultData = new int[3, 3]
                              {
                                 {2, 5, 6},
                                 {0, 4, 13},
                                 {8, 3, 10},
                              };

            Assert.That(result, Is.EqualTo(resultData));
        }
Example #2
0
        public void AddExeptionTest()
        {
            int[,] firstData = new int[2, 3]
                             {
                                 {1, 0, 3},
                                 {0, 2, 5},
                             };
            int[,] secondData = new int[3, 3]
                              {
                                 {1, 5, 3},
                                 {0, 2, 8},
                                 {4, 2, 5},
                             };

            CRSMatrix firstMatrix = new CRSMatrix(firstData);

            CRSMatrix secondMatrix = new CRSMatrix(secondData);

            CRSMatrix result;

            Assert.Throws<NotProperMatrixSizesExeption>(delegate { result = firstMatrix + secondMatrix; });
        }
Example #3
0
        public void FirstMultTest()
        {
            int[,] firstData = new int[2, 2]
                             {
                                 {1, 0},
                                 {0, 0}
                             };
            int[,] secondData = new int[2, 2]
                              {
                                  {0, 1},
                                  {0, 0}
                              };

            CRSMatrix firstMatrix = new CRSMatrix(firstData);

            CRSMatrix secondMatrix = new CRSMatrix(secondData);

            CRSMatrix result = firstMatrix * secondMatrix;

            Assert.That(result[0,0], Is.EqualTo(0));
            Assert.That(result[0,1], Is.EqualTo(1));
            Assert.That(result.values.Count, Is.EqualTo(1));
        }
Example #4
0
        public void ToStringTest()
        {
            int[,] firstData = new int[1, 2]
                             {
                                 {1, 3}
                             };

            CRSMatrix firstMatrix = new CRSMatrix(firstData);

            Assert.That(firstMatrix.ToString(), Is.EqualTo("1 3 \n"));
        }
Example #5
0
        public void MultScTest()
        {
            int[,] firstData = new int[3, 3]
                              {
                                 {2, 1, 3},
                                 {0, 4, 5},
                                 {8, 3, 7},
                              };

            CRSMatrix matrix = new CRSMatrix(firstData);

            CRSMatrix result = matrix * 2;

            int[,] resultData = new int[3, 3]
                              {
                                 {4, 2, 6},
                                 {0, 8, 10},
                                 {16, 6, 14},
                              };

            Assert.That(result, Is.EqualTo(resultData));
        }
Example #6
0
        public void SecondMultTest()
        {
            int[,] firstData = new int[2, 3]
                             {
                                 {1, 0, 3},
                                 {0, 2, 5}
                             };
            int[,] secondData = new int[3, 2]
                              {
                                  {0, 1},
                                  {2, 3},
                                  {0, 1}
                              };

            CRSMatrix firstMatrix = new CRSMatrix(firstData);

            CRSMatrix secondMatrix = new CRSMatrix(secondData);

            CRSMatrix result = firstMatrix * secondMatrix;

            int[,] resultData = new int[2, 2]
                              {
                                  {0, 4},
                                  {4, 11}
                              };

            Assert.That(result, Is.EqualTo(resultData));
        }
Example #7
0
        public void IndexerTest()
        {
            int[,] firstData = new int[2, 2]
                             {
                                 {0, 0},
                                 {0, 2}
                             };

            CRSMatrix matrix = new CRSMatrix(firstData);

            Assert.That(matrix[0,0], Is.EqualTo(0));
            Assert.That(matrix[1,1], Is.EqualTo(2));
        }
Example #8
0
        /// <summary>
        /// Adds another instance of CRSMatrix and produce a result of type CRSMatrix
        /// </summary>
        /// <param name="matrix1"></param>
        /// <param name="matrix2"></param>
        /// <returns></returns>
        public static CRSMatrix operator +(CRSMatrix matrix1, CRSMatrix matrix2)
        {
            if (matrix1.NumberOfCols != matrix2.NumberOfCols ||
                matrix1.NumberOfRows != matrix2.NumberOfRows)
            {
                throw new NotProperMatrixSizesExeption();
            }

            var returnData = new CRSMatrix(matrix1.NumberOfRows, matrix1.NumberOfCols);

            for (int i = 0; i < matrix1.NumberOfRows; i++)
            {
                for (int j = 0; j < matrix1.NumberOfCols; j++)
                {
                    returnData[i, j] = matrix1[i, j] + matrix2[i, j];
                }
            }

            return returnData;
        }
Example #9
0
        /// <summary>
        /// Multiplies matrix and scalar
        /// </summary>
        /// <param name="matrix"></param>
        /// <param name="scalar"></param>
        /// <returns></returns>
        public static CRSMatrix operator *(CRSMatrix matrix, int scalar)
        {
            var result = new CRSMatrix(matrix);
            for (int i = 0; i < matrix.values.Count; i++)
            {
                result.values[i] *= scalar;
            }

            return result;
        }
Example #10
0
        /// <summary>
        /// Multiplies two instances of CRSMatrix
        /// </summary>
        /// <param name="matrix1"></param>
        /// <param name="matrix2"></param>
        /// <returns></returns>
        public static CRSMatrix operator *(CRSMatrix matrix1, CRSMatrix matrix2)
        {
            if (matrix1.NumberOfCols != matrix2.NumberOfRows)
            {
                throw new NotProperMatrixSizesExeption();
            }

            var result = new CRSMatrix(matrix1.NumberOfRows, matrix2.NumberOfCols);

            for (int j = 0; j < matrix2.NumberOfCols; j++)
            {
                for (int i = 0; i < matrix1.NumberOfRows; i++)
                {
                    if (matrix1.rows[i] == -1)
                    {
                        continue;
                    }

                    int nextValue = matrix1.values.Count;
                    for (int k = i + 1; k < matrix1.rows.Count; k++)
                    {
                        if (matrix1.rows[k] != -1)
                        {
                            nextValue = matrix1.rows[k];
                            break;
                        }
                    }

                    for (int k = matrix1.rows[i]; k < nextValue; k++)
                    {
                        result[i, j] += matrix1.values[k] * matrix2[matrix1.cols[k], j];
                    }
                }
            }

            return result;
        }
Example #11
0
 /// <summary>
 /// initializes new instance of CRSMatrix class that
 /// contain copied elements from another instance of CRSMatrix class
 /// </summary>
 /// <param name="init"></param>
 public CRSMatrix(CRSMatrix init)
 {
     this.NumberOfCols = init.NumberOfCols;
     this.NumberOfRows = init.NumberOfRows;
     this.values = new List<int>(init.values);
     this.rows = new List<int>(init.rows);
     this.cols = new List<int>(init.cols);
 }