Esempio n. 1
0
        public DiagonalMatrix Invert()
        {
            DiagonalMatrix newMatrix = new DiagonalMatrix(Width);

            for (int i = 0; i < newMatrix.Width; i++)
            {
                newMatrix[i] = (Complex)1 / Diagonal[i];
            }
            return(newMatrix);
        }
Esempio n. 2
0
        public static DiagonalMatrix Pow(DiagonalMatrix matrix, double exp)
        {
            DiagonalMatrix newMatrix = new DiagonalMatrix(matrix.Width);

            for (int i = 0; i < newMatrix.Width; i++)
            {
                newMatrix[i] = Complex.Pow(matrix[i], exp);
            }
            return(newMatrix);
        }
Esempio n. 3
0
        public static DiagonalMatrix operator -(DiagonalMatrix a)
        {
            DiagonalMatrix result = new DiagonalMatrix(a.Width);

            for (int i = 0; i < result.Width; i++)
            {
                result[i] = -a[i];
            }
            return(result);
        }
Esempio n. 4
0
        public static DiagonalMatrix operator *(DiagonalMatrix a, Complex b)
        {
            DiagonalMatrix product = new DiagonalMatrix(a.Height);

            for (int i = 0; i < product.Width; i++)
            {
                product[i] = a[i] * b;
            }
            return(product);
        }
Esempio n. 5
0
        public static DiagonalMatrix Pow(DiagonalMatrix matrix, Complex exp)
        {
            if (exp.Imaginary == 0)
            {
                return(Pow(matrix, exp.Real));
            }
            DiagonalMatrix newMatrix = new DiagonalMatrix(matrix.Width);

            for (int i = 0; i < newMatrix.Width; i++)
            {
                newMatrix[i] = Complex.Pow(matrix[i], exp);
            }
            return(newMatrix);
        }
Esempio n. 6
0
        public static DiagonalMatrix operator *(DiagonalMatrix a, DiagonalMatrix b)
        {
            if (a.Width != b.Height)
            {
                return(null);
            }

            DiagonalMatrix product = new DiagonalMatrix(a.Height);

            for (int i = 0; i < product.Width; i++)
            {
                product[i] = a[i] * b[i];
            }
            return(product);
        }
Esempio n. 7
0
        /// <summary>
        /// Throws exception if not a square matrix.
        /// </summary>
        /// <returns></returns>
        public Polynomial FindCharacteristicPolynomial()
        {
            if (Width != Height)
            {
                throw new Exception("Must be square matrix.");
            }
            //Method can be found at: https://en.wikipedia.org/wiki/Faddeev%E2%80%93LeVerrier_algorithm
            Complex[] c = new Complex[Width + 1];
            c[0] = (Complex)1;
            var I = DiagonalMatrix.IdentityMatrix(Width);
            var M = (Matrix)I;

            for (int i = 1; i < c.Length; i++)
            {
                //Console.WriteLine();
                //Console.WriteLine(M);
                M    = this * M;
                c[i] = -1.0 / i * M.Trace();

                //Console.WriteLine(M);
                //Console.WriteLine(c[i]);

                if (i + 1 < c.Length)
                {
                    M += c[i] * I;
                }
            }

            Polynomial poly = new Polynomial(c.Length);

            for (int i = 0; i < c.Length; i++)
            {
                poly[i] = c[c.Length - i - 1];
            }
            return(poly);
        }
Esempio n. 8
0
        static void Main(string[] args)
        {
            DateTime t0 = DateTime.Now;

            Console.WriteLine("Start Time: {0}", t0);
            List <string> str = new List <string>();

            File.WriteAllLines("Test.txt",
                               PolynomialGeneratorX.GenerateAndBuild("eccentricemery", PolynomialGeneratorX.InputTypes.Primes, TextFormat.PlainText, 'x', "f", 47, true)
                               );
            DateTime t1 = DateTime.Now;

            Console.WriteLine("End Time: {0}", t1);
            Console.WriteLine("Total Time: {0}", t1 - t0);
            Console.WriteLine("Press enter to exit.");
            Console.ReadLine();
            return;

            //Matrix m = new Matrix(3, 3);

            //m[0, 0] = (Complex)3;
            //m[1, 0] = (Complex)1;
            //m[2, 0] = (Complex)5;

            //m[0, 1] = (Complex)3;
            //m[1, 1] = (Complex)3;
            //m[2, 1] = (Complex)1;

            //m[0, 2] = (Complex)4;
            //m[1, 2] = (Complex)6;
            //m[2, 2] = (Complex)4;

            Matrix m   = new Matrix(3, 3);
            Random ran = new Random(465165313);

            for (int i = 0; i < m.Width; i++)
            {
                for (int j = 0; j < m.Height; j++)
                {
                    m[i, j] = new Complex(ran.Next() / 100.0, ran.Next() / 100.0 * 0);
                }
            }


            Console.WriteLine(m);
            Polynomial poly = m.FindCharacteristicPolynomial();

            Console.WriteLine("f(x) = {0}", poly);
            ////poly[0] = (Complex)(3);
            ////poly[1] = (Complex)(47);
            ////poly[2] = (Complex)(182);
            ////poly[3] = (Complex)(327);
            ////poly[4] = (Complex)(1027);
            foreach (var root in poly.FindAllRoots())
            {
                Console.WriteLine("Root: {0}, Approx: {1}", root, poly.F(root));
            }
            Console.ReadLine();
            Console.WriteLine("SBS^(-1) = ");
            m.Diagonalize(out Matrix S, out DiagonalMatrix B);
            var S1 = S.Invert();

            //S.Round(5);
            //S1.Round(5);
            //B.Round(5);
            string[] M1 = S.ToStringArray(), M2 = ((Matrix)B).ToStringArray(), M3 = S1.ToStringArray();
            for (int i = 0; i < B.Height; i++)
            {
                Console.WriteLine(M1[i] + M2[i] + M3[i]);
            }
            Console.WriteLine("=");
            Console.WriteLine(S * B * S1);
            Console.WriteLine("M^20 = ");
            Console.WriteLine(S * DiagonalMatrix.Pow(B, 20) * S1);
            Matrix m20 = (Matrix)DiagonalMatrix.IdentityMatrix(2);

            for (int i = 0; i < 20; i++)
            {
                m20 *= m;
            }
            Console.WriteLine("?=");
            Console.WriteLine(m20);
            Console.WriteLine("(M^(1/3))^3 = ");
            var cubeRt = S * DiagonalMatrix.Pow(B, 1.0 / 3) * S1;

            Console.WriteLine(cubeRt * cubeRt * cubeRt);
            Console.WriteLine("END");
            Console.ReadLine();
            //Matrix m = GetSquareMatrixFromUser();
            //Console.WriteLine("\nInput Matrix: \n" + m);
            //Matrix inverse = m.Invert();
            //if (inverse == null)
            //    Console.WriteLine("Matrix is not invertable");
            //else
            //{
            //    Console.WriteLine("\nInverse Matrix: \n" + inverse);
            //    Matrix product = inverse * m;
            //    Console.WriteLine("\nProduct: \n" + product);
            //    product.Round(10);
            //    Console.WriteLine("\nProduct (rounded to 10 decimel places): \n" + product);
            //    Console.WriteLine("\nPress Enter to exit");
            //    Console.ReadLine();
            //}
        }