コード例 #1
0
        private void FillCells(ISquareMatrix matrix, ICell currentCell, ICell nextCell, ref int currentValue)
        {
            while (true)
            {
                matrix.Field[currentCell.Row, currentCell.Col] = currentValue;

                bool matrixBoundaryReached = this.CheckIfBoundaryReached(matrix.Field, currentCell.Row, currentCell.Col);

                if (!matrixBoundaryReached)
                {
                    break;
                }

                bool steppedOutside = this.CheckIfSteppedOutsideOfMatrix(matrix, currentCell, nextCell);

                while (steppedOutside)
                {
                    this.ChangeDirection(nextCell);
                    steppedOutside = this.CheckIfSteppedOutsideOfMatrix(matrix, currentCell, nextCell);
                }

                currentCell.Row += nextCell.Row;
                currentCell.Col += nextCell.Col;
                currentValue++;
            }
        }
コード例 #2
0
 private static void Transpose <T>(ISquareMatrix <T> matrix)
 {
     for (int row = 0; row < matrix.GetSize() - 1; row++)
     {
         for (int col = row + 1; col < matrix.GetSize(); col++)
         {
             Swap(ref matrix.At(row, col), ref matrix.At(col, row));
         }
     }
 }
コード例 #3
0
 static void Print(ISquareMatrix <int> matrix)
 {
     for (int i = 0; i < matrix.Length; ++i)
     {
         for (int j = 0; j < matrix.Length; ++j)
         {
             Console.Write(matrix[i, j] + " ");
         }
         Console.WriteLine();
     }
 }
コード例 #4
0
ファイル: MatrixUtils.cs プロジェクト: worinium/SharpMath
        internal static double LaplaceExpansion(this ISquareMatrix matrix)
        {
            double determinant = 0;

            for (uint i = 0; i < matrix.Dimension; ++i)
            {
                determinant += matrix[i, 0] * matrix.GetCofactor(i, 0);
            }
            // The sigma sign is equal to a for-loop with recursion.
            return(determinant);
        }
コード例 #5
0
    public static void Rotate90DegreesClockwise <T>(ISquareMatrix <T> matrix)
    {
        Transpose(matrix);

        // Mirror left to right
        for (int row = 0; row < matrix.GetSize(); row++)
        {
            for (int col = 0; col < matrix.GetSize() / 2; col++)
            {
                Swap(ref matrix.At(row, col), ref matrix.At(row, matrix.GetSize() - 1 - col));
            }
        }
    }
コード例 #6
0
        public void MatrixHelperExceptionTest()
        {
            var matrixleft = new SquareMatrix <int>(new int[9] {
                3, 4, 0, 0, 4, 0, 0, 0, 15
            });
            var matrixright = new SymmetricMatrix <int>(new int[3] {
                1, 1, 1
            });
            var newMatrix = new SquareMatrix <int>(3);
            ISquareMatrix <int> result = newMatrix;

            MatrixHelper.Add <int>((ISquareMatrix <int>)matrixleft, (ISquareMatrix <int>)matrixright, result);
        }
コード例 #7
0
        public static bool IsNaM(this ISquareMatrix m)
        {
            int len = m.Length;

            for (int i = 0; i < len; i++)
            {
                if (double.IsNaN(m[i]))
                {
                    return(true);
                }
            }
            return(false);
        }
コード例 #8
0
    public static void Rotate90DegreesCounterclockwise <T>(ISquareMatrix <T> matrix)
    {
        Transpose(matrix);

        // Mirror top to bottom
        for (int col = 0; col < matrix.GetSize(); col++)
        {
            for (int row = 0; row < matrix.GetSize() / 2; row++)
            {
                Swap(ref matrix.At(row, col), ref matrix.At(matrix.GetSize() - 1 - row, col));
            }
        }
    }
コード例 #9
0
        private void FillMatrixRandomValue(ISquareMatrix obj)
        {
            var rnd = new Random();

            for (var i = 0; i < obj.Rank; i++)
            {
                for (var j = 0; j < obj.Rank; j++)
                {
                    obj[i, j] = rnd.Next(100);
                }
            }
            Console.WriteLine(obj.ToString());
        }
コード例 #10
0
 private bool CompareMatrix(ISquareMatrix <int> matrixLeft, ISquareMatrix <int> matrixRight)
 {
     for (int i = 0; i < 3; ++i)
     {
         for (int j = 0; j < 3; ++j)
         {
             if (matrixLeft[i, j] != matrixRight[i, j])
             {
                 return(false);
             }
         }
     }
     return(true);
 }
コード例 #11
0
ファイル: MatrixUtils.cs プロジェクト: worinium/SharpMath
        //public static TOut GetCore<TOut>(this ISquareMatrix matrix) where TOut : IVector, new()
        //{
        //    if (!matrix.Determinant.IsApproximatelyEqualTo(0))
        //        throw new InvalidOperationException($"Cannot calculate core of {nameof(matrix)} as its determinant is not 0.");

        //    var vector = new TOut();
        //    if (matrix.Dimension != vector.Dimension)
        //        throw new InvalidOperationException($"Type parameter TOut is not an adequate IVector - type as its dimension does not fit the one of the resulting vector. The dimension must be {matrix.Dimension}.");

        //    var equations = new List<LinearEquation>((int)matrix.ColumnCount);
        //    for (uint y = 0; y < matrix.RowCount; ++y)
        //    {
        //        var coefficients = new List<double>((int)matrix.ColumnCount);
        //        for (uint x = 0; x < matrix.ColumnCount; ++x)
        //            coefficients.Add(matrix[y, x]);

        //        equations.Add(new LinearEquation(coefficients.ToArray(), 0));
        //    }

        //    var equationSystem = new LinearEquationSystem(equations);
        //    double[] solutions = equationSystem.Solve();
        //    for (uint i = 0; i < matrix.ColumnCount; ++i)
        //        vector[i] = solutions[i];

        //    return vector;
        //}

        /// <summary>
        ///     Calculates the determinant of the <see cref="ISquareMatrix" />.
        /// </summary>
        /// <param name="matrix">The <see cref="ISquareMatrix" /> whose determinant should be calculated.</param>
        /// <returns>The determinant of the <see cref="ISquareMatrix" />.</returns>
        public static double GetDeterminant(this ISquareMatrix matrix)
        {
            switch (matrix.Dimension)
            {
            case 1:
                return(matrix[0, 0]);

            case 2:
                return(matrix[0, 0] * matrix[1, 1] - matrix[0, 1] * matrix[1, 0]);

            default:
                return(LaplaceExpansion(matrix));
            }
        }
コード例 #12
0
        public static T Mul <T>(this ISquareMatrix lhs, T rhs) where T : IVector
        {
            int dim = rhs.Dimension;

            for (int i = 0; i < dim; i++)
            {
                double dot = 0;
                for (int j = 0; j < dim; j++)
                {
                    dot += lhs[j + i] * rhs[j];
                }
                rhs[i] = dot;
            }
            return(rhs);
        }
コード例 #13
0
        private bool CheckIfSteppedOutsideOfMatrix(ISquareMatrix matrix, ICell currentCell, ICell nextCell)
        {
            bool steppedOusideRight  = currentCell.Row + nextCell.Row >= matrix.Size;
            bool steppedOusideLeft   = currentCell.Row + nextCell.Row < 0;
            bool steppedOusideBottom = currentCell.Col + nextCell.Col >= matrix.Size;
            bool steppedOutsideTop   = currentCell.Col + nextCell.Col < 0;
            bool steppedOuside       = steppedOusideRight || steppedOusideLeft || steppedOusideBottom || steppedOutsideTop;
            bool isPopulated         = false;

            if (!steppedOuside)
            {
                isPopulated = matrix.Field[currentCell.Row + nextCell.Row, currentCell.Col + nextCell.Col] != 0;
            }

            return(steppedOuside || isPopulated);
        }
コード例 #14
0
        private ISquareMatrix Adder(ISquareMatrix added, AdderArgument type)
        {
            if (Rank != added.Rank)
            {
                throw new ArgumentException();
            }
            var result = GetZeroMatrix();

            for (var i = 0; i < Rank; i++)
            {
                for (var j = 0; j < Rank; j++)
                {
                    result[i, j] = this[i, j] + added[i, j] * (int)type;
                }
            }
            return(result);
        }
コード例 #15
0
ファイル: MatrixUtils.cs プロジェクト: worinium/SharpMath
        /// <summary>
        ///     Calculates the sub <see cref="IMatrix" /> of the current <see cref="IMatrix" /> by removing the specified column
        ///     and row.
        /// </summary>
        /// <param name="matrix">The <see cref="ISquareMatrix" />.</param>
        /// <param name="row">The row that should be removed.</param>
        /// <param name="column">The column that should be removed.</param>
        /// <returns>The calculated sub <see cref="ISquareMatrix" />.</returns>
        public static ISquareMatrix GetSubMatrix(this ISquareMatrix matrix, uint row, uint column)
        {
            ISquareMatrix resultMatrix;

            switch (matrix.Dimension)
            {
            case 2:
                resultMatrix = new Matrix1x1();
                break;

            case 3:
                resultMatrix = new Matrix2x2();
                break;

            case 4:
                resultMatrix = new Matrix3x3();
                break;

            default:
                throw new InvalidOperationException("Cannot build sub matrix of " + nameof(matrix) +
                                                    " as there is no lower dimension defined for it.");
            }

            uint y = 0;

            for (uint cy = 0; cy < matrix.RowCount; cy++)
            {
                if (cy != row)
                {
                    uint x = 0;
                    for (uint cx = 0; cx < matrix.ColumnCount; ++cx)
                    {
                        if (cx != column)
                        {
                            resultMatrix[y, x] = matrix[cy, cx];
                            x++;
                        }
                    }

                    y++;
                }
            }

            return(resultMatrix);
        }
コード例 #16
0
        public void MatrixHelperAddTest()
        {
            var matrixleft = new SquareMatrix <int>(new int[9] {
                3, 4, 0, 0, 4, 0, 0, 0, 15
            });
            var matrixright = new SymmetricMatrix <int>(new int[6] {
                1, 1, 1, 1, 1, 1
            });
            var newMatrix = new SquareMatrix <int>(3);
            ISquareMatrix <int> result = newMatrix;

            MatrixHelper.Add <int>((ISquareMatrix <int>)matrixleft, (ISquareMatrix <int>)matrixright, result);
            var expected = new SquareMatrix <int>(new int[9] {
                4, 5, 1, 1, 5, 1, 1, 1, 16
            });

            Assert.IsTrue(CompareMatrix((ISquareMatrix <int>)expected, result));
        }
コード例 #17
0
ファイル: MatrixUtils.cs プロジェクト: worinium/SharpMath
        //public static T GetInverse<T>(this ISquareMatrix matrix) where T : ISquareMatrix, new()
        //{
        //    if (matrix.Determinant.IsApproximatelyEqualTo(0))
        //        throw new InvalidOperationException("The specified matrix does not have an inverse.");
        //    return GaussJordan(matrix, GetIdentity())
        //}

        /// <summary>
        ///     Determines whether the <see cref="ISquareMatrix" /> is a diagonal <see cref="ISquareMatrix" />, or not.
        /// </summary>
        /// <param name="matrix">The <see cref="ISquareMatrix" />.</param>
        /// <returns>
        ///     <c>true</c>, if the <see cref="ISquareMatrix" /> is a diagonal <see cref="ISquareMatrix" />, otherwise
        ///     <c>false</c>.
        /// </returns>
        public static bool GetIsDiagonal(this ISquareMatrix matrix)
        {
            if (matrix.Dimension == 1)
            {
                return(false);
            }

            for (uint y = 0; y < matrix.RowCount; ++y)
            {
                for (uint x = 0; x < matrix.ColumnCount; ++x)
                {
                    if (y == x && FloatingNumber.AreApproximatelyEqual(matrix[y, x], 0) ||
                        y != x && !FloatingNumber.AreApproximatelyEqual(matrix[y, x], 0))
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
コード例 #18
0
        public ISquareMatrix Multiply(ISquareMatrix factor)
        {
            if (Rank != factor.Rank)
            {
                throw new ArgumentException();
            }
            var result = GetZeroMatrix();

            for (var i = 0; i < Rank; i++)
            {
                for (var j = 0; j < Rank; j++)
                {
                    for (var k = 0; k < Rank; k++)
                    {
                        result[i, j] = this[i, k] * factor[k, j];
                    }
                }
            }
            return(result);
        }
コード例 #19
0
ファイル: MatrixUtils.cs プロジェクト: worinium/SharpMath
        /// <summary>
        ///     Determines whether the <see cref="ISquareMatrix" /> is a triangle <see cref="ISquareMatrix" />, or not.
        /// </summary>
        /// <param name="matrix">The <see cref="ISquareMatrix" />.</param>
        /// <returns>
        ///     <c>true</c>, if the <see cref="ISquareMatrix" /> is a triangle <see cref="ISquareMatrix" />, otherwise
        ///     <c>false</c>.
        /// </returns>
        public static bool GetIsTriangle(this ISquareMatrix matrix)
        {
            if (matrix.Dimension == 1)
            {
                return(false);
            }

            var upperTriangle = new List <double>();
            var lowerTriangle = new List <double>();

            for (uint y = 0; y < matrix.RowCount; ++y)
            {
                var isRightSide = false;
                for (uint x = 0; x < matrix.ColumnCount; ++x)
                {
                    if (y == x)
                    {
                        isRightSide = true;
                        continue;
                    }

                    if (isRightSide)
                    {
                        upperTriangle.Add(matrix[y, x]);
                    }
                    else
                    {
                        lowerTriangle.Add(matrix[y, x]);
                    }
                }
            }

            if (upperTriangle.All(val => val.IsApproximatelyEqualTo(0)) ||
                lowerTriangle.All(val => val.IsApproximatelyEqualTo(0)))
            {
                return(true);
            }
            return(false);
        }
コード例 #20
0
 public static void Add <T>(ISquareMatrix <T> matrixleft, ISquareMatrix <T> matrixright, ISquareMatrix <T> result)
 {
     if (matrixleft == null || matrixright == null || result == null || matrixleft.Length != matrixright.Length ||
         matrixleft.Length != result.Length)
     {
         throw new ArgumentException();
     }
     for (int i = 0; i < matrixleft.Length; ++i)
     {
         for (int j = 0; j < matrixleft.Length; ++j)
         {
             try
             {
                 result[i, j] = (dynamic)matrixleft[i, j] + matrixright[i, j];
             }
             catch (Exception e)
             {
                 throw new ArgumentException("Invalid operation");
             }
         }
     }
 }
コード例 #21
0
 public ISquareMatrix Add(ISquareMatrix matrix)
 {
     return(base.Add(matrix).AsSquare());
 }
コード例 #22
0
 public ISquareMatrix Multiply(ISquareMatrix matrix)
 {
     return(base.Multiply(matrix).AsSquare());
 }
コード例 #23
0
ファイル: MatrixUtils.cs プロジェクト: worinium/SharpMath
 /// <summary>
 ///     Calculates the cofactor of an element at the specified position in the <see cref="ISquareMatrix" />.
 /// </summary>
 /// <param name="matrix">The <see cref="ISquareMatrix" />.</param>
 /// <param name="row">The row of the element.</param>
 /// <param name="column">The column of the element.</param>
 /// <returns>The cofactor of the element in this <see cref="ISquareMatrix" />.</returns>
 public static double GetCofactor(this ISquareMatrix matrix, uint row, uint column)
 {
     return(Math.Pow(-1, row + column) * matrix.GetSubMatrix(row, column).GetDeterminant());
 }
コード例 #24
0
 public ISquareMatrix Sub(ISquareMatrix subtrahend)
 {
     return(Adder(subtrahend, AdderArgument.Sub));
 }
コード例 #25
0
 public ISquareMatrix Add(ISquareMatrix added)
 {
     return(Adder(added, AdderArgument.Add));
 }
コード例 #26
0
 public OSquareMatrix(ISquareMatrix matrix)
 {
     _matrix = matrix;
 }
コード例 #27
0
 public ISquareMatrix Subtract(ISquareMatrix matrix)
 {
     return(base.Subtract(matrix).AsSquare());
 }
コード例 #28
0
 public SquareMatrixFiller(ISquareMatrix matrix, ICell currentCell, ICell nextCell)
 {
     this.Matrix      = matrix;
     this.CurrentCell = currentCell;
     this.NextCell    = nextCell;
 }