Пример #1
0
    void Valid(Matrix m)
    {
        MyMatrix temp = new MyMatrix(m);

        for (int i = 0; i < 4; i++)
        {
            for (int k = 0; k < 4; k++)
            {
                Debug_c.Assert(float.IsNaN(temp[i, k]) == false);
            }
        }
    }
Пример #2
0
        public static MyMatrix Clone(this MyMatrix theMatrix)
        {
            MyMatrix theClone = new MyMatrix(theMatrix.rows, theMatrix.columns);
            for (int i = 0; i < theMatrix.rows; ++i)
            {
                for (int j = 0; j < theMatrix.columns; ++j)
                {
                    theClone[i, j] = theMatrix[i, j];
                }
            }

            return theClone;
        }
Пример #3
0
        public void FindIndexInVectorWithNumberTest()
        {
            //Arrange
            int[] a = { 4, 3, 2, 5, 6 };

            const int expectedIndexValue = 1;

            //Act
            var result = MyMatrix <int> .FindIndexWithNumberInVector(a, 3);

            //Assert
            Assert.AreEqual(expectedIndexValue, result);
        }
Пример #4
0
        public static void SumElements(this MyMatrix obj)
        {
            int k = 0;

            for (int x = 0; x < obj.Rows; x++)
            {
                for (int y = 0; y < obj.Cols; y++)
                {
                    k += obj.Matrix[x, y];
                }
            }
            Console.WriteLine("Cумма всех элементов матрицы " + k);
        }
Пример #5
0
 public static void Zeroing(this MyMatrix obj)
 {
     for (int x = 0; x < obj.Rows; x++)
     {
         for (int y = 0; y < obj.Cols; y++)
         {
             if (obj.Matrix[x, y] < 0)
             {
                 obj.Matrix[x, y] = 0;
             }
         }
     }
 }
        public void FindsMax_InColumns_Of_Double_Matrix()
        {
            var m = new MyMatrix <double>(new[, ]
            {
                { 1.0, 2.0 },
                { 3.0, 4.0 },
                { 2.0, 6.0 },
                { 3.5, 4.5 }
            });

            Assert.Equal(3, m.FindMaxInColumn(0));
            Assert.Equal(2, m.FindMaxInColumn(1));
        }
Пример #7
0
        /// <summary>
        /// Converts a screen space point into a corresponding point in world space.
        /// </summary>
        /// <param name="source">The vector to project.</param>
        /// <param name="projection">The projection matrix.</param>
        /// <param name="view">The view matrix.</param>
        /// <param name="world">The world matrix.</param>
        /// <returns>The unprojected Vector.</returns>
        public MyVector3 Unproject(MyVector3 source, MyMatrix projection, MyMatrix view, MyMatrix world)
        {
            MyMatrix matrix;

            MyMatrix.Multiply(ref world, ref view, out matrix);
            MyMatrix.Multiply(ref matrix, ref projection, out matrix);
            MyMatrix.Invert(ref matrix, out matrix);

            MyVector3 vector;

            Unproject(ref source, ref matrix, out vector);
            return(vector);
        }
Пример #8
0
    /// <summary> Create a transposed matrix </summary>
    public MyMatrix Transpose()
    {
        MyMatrix result = new MyMatrix(this.cols, this.rows);

        for (int row = 0; row < this.rows; row++)
        {
            for (int col = 0; col < this.cols; col++)
            {
                result[col, row] = this.data[row, col];
            }
        }
        return(result);
    }
Пример #9
0
    public static MyMatrix ZeroMatrix(int iRows, int iCols)       // Function generates the zero matrix
    {
        MyMatrix matrix = new MyMatrix(iRows, iCols);

        for (int i = 0; i < iRows; i++)
        {
            for (int j = 0; j < iCols; j++)
            {
                matrix[i, j] = 0;
            }
        }
        return(matrix);
    }
Пример #10
0
    private static MyMatrix Multiply(int n, MyMatrix m)                          // Multiplication by constant n
    {
        MyMatrix r = new MyMatrix(m.rows, m.cols);

        for (int i = 0; i < m.rows; i++)
        {
            for (int j = 0; j < m.cols; j++)
            {
                r[i, j] = m[i, j] * n;
            }
        }
        return(r);
    }
Пример #11
0
 public void VerifyThatSubtractThrowsException(MyMatrix <MDouble> a, MyMatrix <MDouble> b, MyMatrix <MDouble> res)
 {
     TestContext.WriteLine(a);
     TestContext.WriteLine(" minus ");
     TestContext.WriteLine(b);
     TestContext.WriteLine(" equals ");
     TestContext.WriteLine("Exception!");
     Assert.Throws <ArgumentException>(
         () =>
     {
         var x = a - b;
     });
 }
Пример #12
0
        public void CheckEqualityOfSolvingMethods(int size)
        {
            var templateMatrix = MyMatrix <MDouble> .GetRandomMatrix(size, size);

            var templateVector = MyMatrix <MDouble> .GetRandomMatrix(1, size);

            var solved1 = templateMatrix.SolveLinearEquation(templateVector, LinearSolver.Gauss);
            var solved2 = templateMatrix.SolveLinearEquation(templateVector, LinearSolver.PartialGauss);
            var solved3 = templateMatrix.SolveLinearEquation(templateVector, LinearSolver.FullGauss);

            Assert.AreEqual(solved1, solved2);
            Assert.AreEqual(solved2, solved3);
        }
Пример #13
0
        public static void SumElements <T>(this MyMatrix <T> obj) where T : IComparable
        {
            dynamic k = 0;

            for (int x = 0; x < obj.Rows; x++)
            {
                for (int y = 0; y < obj.Cols; y++)
                {
                    k += obj.Matrix[x, y];
                }
            }
            Console.WriteLine("Cумма всех элементов матрицы " + k);
        }
Пример #14
0
 public static void Zeroing <T>(this MyMatrix <T> obj) where T : IComparable
 {
     for (int x = 0; x < obj.Rows; x++)
     {
         for (int y = 0; y < obj.Cols; y++)
         {
             if ((dynamic)obj.Matrix[x, y] < 0) //Во время компиляции предполагается что элементы с типом dynamic поддерживают любые операции
             {
                 obj.Matrix[x, y] = default(T);
             }
         }
     }
 }
Пример #15
0
        public static MyMatrix Clone(this MyMatrix theMatrix)
        {
            MyMatrix theClone = new MyMatrix(theMatrix.rows, theMatrix.columns);
            for (int i = 0; i < theMatrix.rows; ++i)
            {
                for (int j = 0; j < theMatrix.columns; ++j)
                {
                    theClone[i, j] = theMatrix[i, j];
                }
            }

            return theClone;
        }
Пример #16
0
    public static MyMatrix Transpose(MyMatrix m)              // Matrix transpose, for any rectangular matrix
    {
        MyMatrix t = new MyMatrix(m.cols, m.rows);

        for (int i = 0; i < m.rows; i++)
        {
            for (int j = 0; j < m.cols; j++)
            {
                t[j, i] = m[i, j];
            }
        }
        return(t);
    }
Пример #17
0
        private static void ExecuteMatrixOptions(ref MyMatrix baseMatrix)
        {
            int[] valuePosition;
            bool  executeOptions = true;

            do
            {
                Write("Enter the option: ");
                switch (ReadLine())
                {
                case "1":
                    baseMatrix += GetInputMatrix();
                    WriteLine(baseMatrix.GetFormattedMatrix());
                    break;

                case "2":
                    baseMatrix *= GetInputMatrix();
                    WriteLine(baseMatrix.GetFormattedMatrix());
                    break;

                case "3":
                    baseMatrix.TransponeMe();
                    WriteLine(baseMatrix.GetFormattedMatrix());
                    break;

                case "4":
                    WriteLine(baseMatrix.GetFormattedMatrix());
                    break;

                case "5":
                    valuePosition = GetValuePosition();
                    WriteLine($"Value: {baseMatrix.GetValue(valuePosition[0], valuePosition[1])}");
                    break;

                case "6":
                    valuePosition = GetValuePosition();
                    int value = GetInputNumber();
                    baseMatrix.SetValue(valuePosition[0], valuePosition[1], value);
                    WriteLine(baseMatrix.GetFormattedMatrix());
                    break;

                case "7":
                    baseMatrix = GetInputMatrix();
                    break;

                default:
                    executeOptions = false;
                    break;
                }
            } while (executeOptions);
        }
Пример #18
0
        /// <summary>
        /// Projects a 3D vector from object space into screen space.
        /// </summary>
        /// <param name="source">The vector to project.</param>
        /// <param name="matrix">A combined WorldViewProjection matrix.</param>
        /// <param name="vector">The projected vector.</param>
        public void Project(ref MyVector3 source, ref MyMatrix matrix, out MyVector3 vector)
        {
            MyVector3.Transform(ref source, ref matrix, out vector);
            float a = (((source.X * matrix.M14) + (source.Y * matrix.M24)) + (source.Z * matrix.M34)) + matrix.M44;

            if (!MyMathf.IsOne(a))
            {
                vector = (vector / a);
            }

            vector.X = (((vector.X + 1f) * 0.5f) * Width) + X;
            vector.Y = (((-vector.Y + 1f) * 0.5f) * Height) + Y;
            vector.Z = (vector.Z * (MaxDepth - MinDepth)) + MinDepth;
        }
Пример #19
0
 private static void SafeACopytoC(MyMatrix A, int xa, int ya, MyMatrix C, int size)
 {
     for (int i = 0; i < size; i++)         // rows
     {
         for (int j = 0; j < size; j++)     // cols
         {
             C[i, j] = 0;
             if (xa + j < A.cols && ya + i < A.rows)
             {
                 C[i, j] += A[ya + i, xa + j];
             }
         }
     }
 }
Пример #20
0
        public void TestMethodAvgFunction()
        {
            bool     check    = false;
            MyMatrix myMatrix = new MyMatrix();

            int[,] matrix = new int[3, 3] {
                { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }
            };
            if (myMatrix.AvgArr(matrix) == 5)
            {
                check = true;
            }
            Assert.IsTrue(check);
        }
Пример #21
0
        public void OperatorMinus_ShouldReturn_ExpectedMatrix()
        {
            int[,] arr1 = { { 2, 3 }, { 1, 1 } };
            MyMatrix m1 = new MyMatrix(arr1);

            int[,] arr2 = { { 1, 1 }, { 3, 3 } };
            MyMatrix m2 = new MyMatrix(arr2);

            MyMatrix m3 = m1 - m2;

            int[,] excpected = { { 1, 2 }, { -2, -2 } };

            Assert.IsTrue(m3 == new MyMatrix(excpected));
        }
Пример #22
0
        public void OperatorIsEqualTo_ShouldReturn_True()
        {
            int[,] arr1 = { { 2, 3 }, { 1, 1 } };
            MyMatrix m1 = new MyMatrix(arr1);

            int[,] arr2 = { { 1, 1 }, { 3, 3 } };
            MyMatrix m2 = new MyMatrix(arr2);

            MyMatrix m3 = m1 * m2;

            int[,] excpected = { { 11, 11 }, { 4, 4 } };

            Assert.IsTrue(m3 == new MyMatrix(excpected));
        }
Пример #23
0
    public static MyMatrix Transposed(MyMatrix mat)
    {
        MyMatrix newMatrix = new MyMatrix(mat.m_columnCountX, mat.m_rowCountY);

        for (int x = 0; x < newMatrix.m_columnCountX; x++)
        {
            for (int y = 0; y < newMatrix.m_rowCountY; y++)
            {
                newMatrix.m_data[y][x] = mat.m_data[x][y];
            }
        }

        return(newMatrix);
    }
Пример #24
0
        public void OperatorIsNotEqualTo_ShouldReturn_False()
        {
            int[,] arr1 = { { 2, 3 }, { 1, 1 } };
            MyMatrix m1 = new MyMatrix(arr1);

            int[,] arr2 = { { 1, 1 }, { 3, 3 } };
            MyMatrix m2 = new MyMatrix(arr2);

            MyMatrix m3 = m1 * m2;

            int[,] excpected = { { 5, 15 }, { 2, 6 } };

            Assert.IsFalse(m3 == new MyMatrix(excpected));
        }
            public void Append(MyMatrix M, int l)
            {
                //矩阵在右边或下边concatenate
                // 0 for col; 1 for row
                if (l == 0)
                {
                    int c = this.Cols;
                    this.Cols += M.Cols;
                    var newValue = new double[this.Rows, this.Cols];
                    for (int i = 0; i < this.Cols; i++)
                    {
                        for (int j = 0; j < this.Rows; j++)
                        {
                            if (i < c)
                            {
                                newValue[j, i] = Value[j, i];
                            }
                            else
                            {
                                newValue[j, i] = M.Value[j, i - c];
                            }
                        }
                    }
                    Value = null;
                    Value = newValue;
                }
                if (l == 1)
                {
                    int r = this.Rows;
                    this.Rows += M.Rows;
                    var newValue = new double[this.Rows, this.Cols];

                    for (int i = 0; i < this.Cols; i++)
                    {
                        for (int j = 0; j < this.Rows; j++)
                        {
                            if (j < r)
                            {
                                newValue[j, i] = Value[j, i];
                            }
                            else
                            {
                                newValue[j, i] = M.Value[j - r, i];
                            }
                        }
                    }
                    Value = null;
                    Value = newValue;
                }
            }
Пример #26
0
    private MyMatrix GetWeightDecayRate(MyMatrix weights)
    {
        MyMatrix mat = new MyMatrix(weights, false);

        for (int y = 0; y < weights.m_rowCountY; y++)
        {
            for (int x = 0; x < weights.m_columnCountX; x++)
            {
                //mat.m_data[y][x] =
            }
        }

        return(mat);
    }
 public MyMatrix(MyMatrix M)
 {
     //复制构造函数
     this.Rows  = M.Rows;
     this.Cols  = M.Cols;
     this.Value = new double[Rows, Cols];
     for (int i = 0; i < Rows; i++)
     {
         for (int j = 0; j < Cols; j++)
         {
             Value[i, j] = M.Value[i, j];
         }
     }
 }
Пример #28
0
        public void MatrixToStringTest()
        {
            //Arrange
            var matrixArray = _matrixFileReader.ReadIntMatrix();
            var matrix      = new MyMatrix <int>(_matrixFileReader);

            //Act
            var result = matrix.ToString();

            foreach (var value in matrixArray)
            {
                StringAssert.Contains(result, value.ToString());
            }
        }
Пример #29
0
    public static MyMatrix RandomMatrix(int iRows, int iCols, int dispersion)       // Function generates the random matrix
    {
        Random   random = new Random();
        MyMatrix matrix = new MyMatrix(iRows, iCols);

        for (int i = 0; i < iRows; i++)
        {
            for (int j = 0; j < iCols; j++)
            {
                matrix[i, j] = random.Next(-dispersion, dispersion);
            }
        }
        return(matrix);
    }
Пример #30
0
        public void VerifyThatRandomMatrixIsNotNull(int size)
        {
            var x = MyMatrix <MDouble> .GetRandomMatrix(size, size);

            Assert.AreEqual(size, x.Height);
            Assert.AreEqual(size, x.Width);
            for (int i = 0; i < x.Height; i++)
            {
                for (int j = 0; j < x.Width; j++)
                {
                    Assert.NotNull(x[i, j]);
                }
            }
        }
Пример #31
0
    /// <summary> Iteratively solve the coefficients using Gauss-Newton method </summary>
    /// <remarks> http://en.wikipedia.org/wiki/Gauss%E2%80%93Newton_algorithm </remarks>
    public double[] Solve()
    {
        if (this.ys == null)
        {
            throw new InvalidOperationException("Not yet initialized");
        }

        double lastSSE = double.MaxValue;

        double[] errors = new double[this.ys.Length];

        // let C0 be the current coefficient guess, C1 be the better answer we are after
        // let E0 be the error using current guess
        // we have:
        // JacT * Jac * (C1 - C0) = JacT * E0
        //
        MyMatrix     jacobian  = Jacobian();
        MyMatrix     jacobianT = jacobian.Transpose();
        MyMatrix     product   = jacobianT * jacobian;
        SquareMatrix inverse   = SquareMatrix.FromMatrix(product).Inverse();

        for (int iteration = 0; iteration < GaussNewton.MaxIteration; iteration++)
        {
            this.sse = 0;

            for (int i = 0; i < this.ys.Length; i++)
            {
                double y = function(this.coefficients, this.xs[i]);
                errors[i] = this.ys[i] - y;
                sse      += errors[i] * errors[i];
            }

            if (lastSSE - sse < GaussNewton.ConvergeThreshold)
            {
                this.solved = true;
                return(this.coefficients);
            }

            double[] shift = inverse * (jacobianT * errors);

            for (int i = 0; i < this.coefficientCount; i++)
            {
                this.coefficients[i] += shift[i];
            }

            lastSSE = sse;
        }
        throw new InvalidOperationException("No answer can be found");
    }
Пример #32
0
 public static double throwCError(
                      [Parameter("Just any random string ")] string err
                     )
 {
     MyMatrix theMatrix = new MyMatrix(2,2);
     return theMatrix[3,3];
 }