Beispiel #1
0
        /// <summary>
        /// Skalarmultiplikation
        /// </summary>
        /// <param name="lambda">Skalar</param>
        /// <param name="a">Matrix</param>
        /// <returns>Ergebnismatrix</returns>
        public static MyMatrix operator *(int lambda, MyMatrix a)
        {
            MyMatrix erg = new MyMatrix();

            for (int z = 0; z < a.data.GetLength(0); z++)
            {
                for (int s = 0; s < a.data.GetLength(1); s++)
                {
                    erg[z, s] = a.data[z, s] * lambda;
                }
            }
            return(erg);
        }
Beispiel #2
0
        //вычитание
        public static MyMatrix operator -(MyMatrix matrix1, MyMatrix matrix2)
        {
            MyMatrix NewMatrix = new MyMatrix();

            for (int i = 0; i < matrix1.V; i++)
            {
                for (int j = 0; j < matrix1.G; j++)
                {
                    NewMatrix.a[j, i] = matrix1.a[i, j] - matrix2.a[i, j];
                }
            }
            return(NewMatrix);
        }
Beispiel #3
0
        public static MyMatrix operator -(MyMatrix a, MyMatrix b)
        {
            MyMatrix erg = new MyMatrix();

            for (int z = 0; z < a.data.GetLength(0); z++)
            {
                for (int s = 0; s < a.data.GetLength(1); s++)
                {
                    erg[z, s] = a.data[z, s] - b.data[z, s];
                }
            }
            return(erg);
        }
Beispiel #4
0
        //транспонирование
        public MyMatrix Trans()
        {
            MyMatrix NewMatrix = new MyMatrix();

            for (int i = 0; i < NewMatrix.V; i++)
            {
                for (int j = 0; j < NewMatrix.G; j++)
                {
                    NewMatrix.a[i, j] = a[j, i];
                }
            }
            return(NewMatrix);
        }
            public static MyMatrix <T> operator +(MyMatrix <T> a, MyMatrix <T> b)
            {
                MyMatrix <T> result = new MyMatrix <T>();

                for (int i = 0; i < Configuration.SIZE; i++)
                {
                    for (int j = 0; j < Configuration.SIZE; j++)
                    {
                        result[i, j] = (dynamic)a[i, j] + b[i, j];
                    }
                }
                return(result);
            }
            public static MyMatrix <T> operator /(MyMatrix <T> matrixA, MyMatrix <T> matrixB)
            {
                MyMatrix <T> result = new MyMatrix <T>();

                for (int i = 0; i < Configuration.SIZE; i++)
                {
                    for (int j = 0; j < Configuration.SIZE; j++)
                    {
                        result[i] += (dynamic)matrixA[i, j] * matrixB[j];
                    }
                }
                return(result);
            }
Beispiel #7
0
        private double[,] GetTransponedArray(MyMatrix matrix)
        {
            var transponedMatrix = new double[matrix.Width, matrix.Height];

            for (int i = 0; i < matrix.Height; i++)
            {
                for (int j = 0; j < matrix.Width; j++)
                {
                    transponedMatrix[i, j] = matrix.getElement(j, i);
                }
            }
            return(transponedMatrix);
        }
Beispiel #8
0
        //сложение
        public static MyMatrix operator +(MyMatrix matrix1, MyMatrix matrix2)
        {
            MyMatrix NewMatrix = new MyMatrix();

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    NewMatrix.a[i, j] = matrix1.a[i, j] + matrix2.a[i, j];
                }
            }
            return(NewMatrix);
        }
Beispiel #9
0
        private static double MatrixNorm <T1, T2>(MyMatrix <T1> matrix, MyMatrix <T2> refMatrix) where T1 : new() where T2 : new()
        {
            var sum = 0.0;

            for (var i = 0; i < matrix.Rows; i++)
            {
                for (var j = 0; j < matrix.Cols; j++)
                {
                    sum += (refMatrix[i, j] - (dynamic)matrix[i, j]) * (refMatrix[i, j] - (dynamic)matrix[i, j]);
                }
            }

            return(Math.Sqrt(sum));
        }
Beispiel #10
0
        private double[,] MultA(double[,] ar1)
        {
            MyMatrix matrix1 = new MyMatrix(ar1.GetLength(1), ar1.GetLength(0));

            matrix1.Zapoln(firstDgv);
            if (!string.IsNullOrEmpty(textBox3.Text))
            {
                int num = Convert.ToInt32(textBox3.Text);
                return(matrix1.Multiplayer(matrix1, num));
            }
            else
            {
                return(matrix1.Multiplayer(matrix1, 1));
            }
        }
Beispiel #11
0
        private double[,] MultB(double[,] ar2)
        {
            MyMatrix matrix2 = new MyMatrix(ar2.GetLength(1), ar2.GetLength(0));

            matrix2.Zapoln(secondDgv);
            if (!string.IsNullOrEmpty(textBox4.Text))
            {
                int num = Convert.ToInt32(textBox4.Text);
                return(matrix2.Multiplayer(matrix2, num));
            }
            else
            {
                return(matrix2.Multiplayer(matrix2, 1));
            }
        }
Beispiel #12
0
        public MyMatrix <float> FloatMatrixFromFractionMatrix(MyMatrix <Fraction> m)
        {
            var values = new float[m.Rows, m.Cols];

            for (var i = 0; i < m.Rows; i++)
            {
                for (var j = 0; j < m.Cols; j++)
                {
                    values[i, j] = (float)m.Matrix[i, j].Numerator /
                                   (float)m.Matrix[i, j].Denominator;
                }
            }

            return(new MyMatrix <float>(values));
        }
Beispiel #13
0
        public MyMatrix <double> DoubleMatrixFromFractionMatrix(MyMatrix <Fraction> m)
        {
            var values = new double[m.Rows, m.Cols];

            for (var i = 0; i < m.Rows; i++)
            {
                for (var j = 0; j < m.Cols; j++)
                {
                    values[i, j] = (double)m.Matrix[i, j].Numerator /
                                   (double)m.Matrix[i, j].Denominator;
                }
            }

            return(new MyMatrix <double>(values));
        }
Beispiel #14
0
        /// <summary>
        /// Matrixmultiplikation
        /// </summary>
        /// <param name="mya">Die eine Matrix</param>
        /// <param name="myb">Die andere Matrix</param>
        /// <returns>Ergebnismatrix</returns>
        public static MyMatrix operator *(MyMatrix mya, MyMatrix myb)
        {
            MyMatrix erg = new MyMatrix();

            for (int a = 0; a < mya.data.GetLength(0); a++)
            {
                for (int b = 0; b < myb.data.GetLength(1); b++)
                {
                    for (int c = 0; c < myb.data.GetLength(0); c++)
                    {
                        erg[a, b] += mya[a, c] * myb[c, b];
                    }
                }
            }
            return(erg);
        }
Beispiel #15
0
        static void Main(string[] args)
        {
            MyMatrix test = new MyMatrix(6, 6);

            test.FillRand();
            test.ShowMatrix();
            Console.WriteLine("---------------------------");
            //test.ShowPartMatrix(2, 2);
            //test.DeleteColumn();
            //test.AddColumn();
            //test.ShowMatrix();
            Console.WriteLine(test.MaxValMatrix());
            Console.WriteLine(test.MinValMatrix());
            Console.WriteLine(test.SumAllElemMatrix());
            Console.WriteLine(test.AvgValMatrix());
            Console.WriteLine("---------------------------");
            test.OddValsMatrix();
        }
Beispiel #16
0
        //умножение
        public static MyMatrix operator *(MyMatrix matrix1, MyMatrix matrix2)
        {
            MyMatrix NewMatrix = new MyMatrix();
            int      n         = matrix1.V;
            int      m         = matrix1.G;

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < matrix2.G; j++)
                {
                    for (int k = 0; k < m; k++)
                    {
                        NewMatrix.a[j, i] += (matrix1.a[i, k] * matrix2.a[k, j]);
                    }
                }
            }
            return(NewMatrix);
        }
 public static void partial(MyMatrix <T> a)
 {
     for (int i = 0; i < Configuration.SIZE; i++)
     {
         for (int k = i + 1; k < Configuration.SIZE; k++)
         {
             if (Math.Abs((dynamic)a[i, i]) < Math.Abs((dynamic)a[k, i]))
             {
                 for (int j = 0; j <= Configuration.SIZE; j++)
                 {
                     double temp = (dynamic)a[i, j];
                     a[i, j] = a[k, j];
                     a[k, j] = (dynamic)temp;
                 }
             }
         }
     }
 }
Beispiel #18
0
        //умножение
        public static MyMatrix operator *(MyMatrix matrix1, MyMatrix matrix2)
        {
            MyMatrix NewMatrix = new MyMatrix();

            for (int i = 0; i < 3; i++)
            {
                for (int k = 0; k < 3; k++)
                {
                    //int a = 0;
                    for (int j = 0; j < 3; j++)
                    {
                        //a += matrix1.a[j,k] * matrix2.a[i, j];
                        NewMatrix.a[i, k] += matrix1.a[j, k] * matrix2.a[i, j];
                    }
                    //NewMatrix.a[i, k] = a;
                }
            }
            return(NewMatrix);
        }
Beispiel #19
0
        public static void DoubleArray(int SIZE)
        {
            double[] numbers     = new double[Configuration.BIGSIZE];
            var      fileNumbers = File.ReadLines("matrix.txt").Select(double.Parse);
            int      index       = 0;

            foreach (var number in fileNumbers)
            {
                numbers[index] = number;
                index++;
            }
            MyMatrix <double> A = new MyMatrix <double>();
            MyMatrix <double> B = new MyMatrix <double>();
            MyMatrix <double> X = new MyMatrix <double>();
            int k = 0;

            for (int i = 0; i < SIZE; i++)
            {
                for (int j = 0; j < SIZE; j++)
                {
                    {
                        double value = numbers[k];
                        A[i, j] = value;
                        B[i, j] = value;
                        if (k % SIZE == 0)
                        {
                            A[i, SIZE] = value;
                            X[i]       = value;
                        }
                    }
                    k++;
                }
            }
            var watch = System.Diagnostics.Stopwatch.StartNew();

            X = MyMatrix <double> .Gauss(SIZE, A);

            watch.Stop();
            double elapsedMs = watch.ElapsedMilliseconds;

            Console.WriteLine((elapsedMs / 1000));
        }
Beispiel #20
0
        static void Main(string[] args)
        {
            int sizeOfMatrix;

            Console.Write("Size of matrix: ");

            sizeOfMatrix = Convert.ToInt32(Console.ReadLine());          //reading and converting size of matrix from console input
            MyMatrix <int> intMatrix = new MyMatrix <int>(sizeOfMatrix); //initialization of new matrix

            intMatrix.elementChangedOrNot += DisplayMessage;             //subscribing method to event

            intMatrix.MatrixOutput();                                    //call method for outputting matrix
            intMatrix.IsDiagonal();                                      //call method for checking diagonal of matrix

            intMatrix[2, 2] = 0;                                         //access to matrix elements through an indexer
            intMatrix[1, 3] = 0;                                         //
            intMatrix[3, 3] = 1;                                         //

            intMatrix.MatrixOutput();
        }
        public void LoadMatrices()
        {
            // fraction
            _sfrA = _handler.LoadFractionMatrix(IO.PrefixFraction + IO.FileA, false).Item1;
            _sfrB = _handler.LoadFractionMatrix(IO.PrefixFraction + IO.FileB, false).Item1;
            _sfrC = _handler.LoadFractionMatrix(IO.PrefixFraction + IO.FileC, false).Item1;
            _sfrX = _handler.LoadFractionVector(IO.PrefixFraction + IO.FileX, false).Item1;

            // float
            _sfA = _handler.LoadFloatMatrix(IO.PrefixFloat + IO.FileA, false).Item1;
            _sfB = _handler.LoadFloatMatrix(IO.PrefixFloat + IO.FileB, false).Item1;
            _sfC = _handler.LoadFloatMatrix(IO.PrefixFloat + IO.FileC, false).Item1;
            _sfX = _handler.LoadFloatVector(IO.PrefixFloat + IO.FileX, false).Item1;

            // double
            _sdA = _handler.LoadDoubleMatrix(IO.PrefixDouble + IO.FileA, false).Item1;
            _sdB = _handler.LoadDoubleMatrix(IO.PrefixDouble + IO.FileB, false).Item1;
            _sdC = _handler.LoadDoubleMatrix(IO.PrefixDouble + IO.FileC, false).Item1;
            _sdX = _handler.LoadDoubleVector(IO.PrefixDouble + IO.FileX, false).Item1;
        }
Beispiel #22
0
        public static void MyArray(double SIZE)
        {
            string[] numbers = new string[100];

            MyMatrix <Fraction> kolekcja = new MyMatrix <Fraction>();
            int k = 0;

            for (int i = 0; i < SIZE; i++)
            {
                for (int j = 0; j < SIZE; j++)
                {
                    {
                        Fraction value = new Fraction(numbers[k]);
                        kolekcja.AddMatrix(value, i, j);
                    }
                    k++;
                }
            }
            // Console.WriteLine(kolekcja.Suma());
        }
Beispiel #23
0
        public static void MyArray(int SIZE)
        {
            string[] numbers     = new string[Configuration.BIGSIZE];
            var      fileNumbers = File.ReadLines("matrix.txt");
            int      index       = 0;

            foreach (var number in fileNumbers)
            {
                numbers[index] = number;
                index++;
            }
            MyMatrix <Fraction> MyMatrix = new MyMatrix <Fraction>();
            MyMatrix <Fraction> A        = new MyMatrix <Fraction>();
            MyMatrix <Fraction> B        = new MyMatrix <Fraction>();
            MyMatrix <Fraction> X        = new MyMatrix <Fraction>();
            int k = 0;

            for (int i = 0; i < SIZE; i++)
            {
                for (int j = 0; j < SIZE; j++)
                {
                    {
                        Fraction value = new Fraction(numbers[k]);
                        A[i, j] = value;
                        B[i, j] = value;
                        if (k % SIZE == 0)
                        {
                            A[i, SIZE] = value;
                            X[i]       = value;
                        }
                    }
                    k++;
                }
            }
            var watch = System.Diagnostics.Stopwatch.StartNew();

            watch.Stop();
            double elapsedMs = watch.ElapsedMilliseconds;

            Console.WriteLine(elapsedMs / 1000);
        }
Beispiel #24
0
        static void Main(string[] args)
        {
            double[][] m = MyMatrix.MatrixCreate(3, 3);

            m[0][0] = 1;
            m[0][1] = 2;
            m[0][2] = 3;

            m[1][0] = 0;
            m[1][1] = 77;
            m[1][2] = 3;

            m[2][0] = 11;
            m[2][1] = 0;
            m[2][2] = 14;

            var inv = MyMatrix.MatrixInverse(m);

            Console.WriteLine(MyMatrix.MatrixAsString(m));
            Console.WriteLine("Inverse = " + MyMatrix.MatrixAsString(inv));
        }
Beispiel #25
0
        public static string GetFormattedMatrix <T>(MyMatrix <T> matrix) where T : new()
        {
            var sb = new StringBuilder();

            for (var i = 0; i < matrix.Rows; i++)
            {
                for (var j = 0; j < matrix.Cols; j++)
                {
                    sb.Append(matrix[i, j]);
                    if (j < matrix.Cols - 1)
                    {
                        sb.Append(" ");
                    }
                }

                if (i < matrix.Rows - 1)
                {
                    sb.AppendLine();
                }
            }
            return(sb.ToString());
        }
Beispiel #26
0
        public double Determinant(MyMatrix input)
        {
            if (input.V > 2)
            {
                double value = 0;
                for (int j = 0; j < input.V; j++)
                {
                    MyMatrix Temp = CreateSmallerMatrix(input, 0, j);
                    value = value + input.a[0, j] * (SignOfElement(0, j) * Determinant(Temp));
                }
                return(value);
            }

            else if (input.V == 2)
            {
                return((input.a[0, 0] * input.a[1, 1]) - (input.a[1, 0] * input.a[0, 1]));
            }

            else
            {
                return(input.a[0, 0]);
            }
        }
Beispiel #27
0
        public static string GetFormattedFractionMatrix(MyMatrix <Fraction> matrix)
        {
            var sb = new StringBuilder();

            for (var i = 0; i < matrix.Rows; i++)
            {
                for (var j = 0; j < matrix.Cols; j++)
                {
                    sb.Append(matrix[i, j].Numerator);
                    sb.Append("/");
                    sb.Append(matrix[i, j].Denominator);
                    if (j < matrix.Cols - 1)
                    {
                        sb.Append(" ");
                    }
                }

                if (i < matrix.Rows - 1)
                {
                    sb.AppendLine();
                }
            }
            return(sb.ToString());
        }
        public static void MyArray(int SIZE)
        {
            string[] numbers     = new string[Configuration.BIGSIZE];
            var      fileNumbers = File.ReadLines("matrix.txt");
            int      index       = 0;

            foreach (var number in fileNumbers)
            {
                numbers[index] = number;
                index++;
            }

            MyMatrix <Fraction> MyMatrix = new MyMatrix <Fraction>();
            MyMatrix <Fraction> A        = new MyMatrix <Fraction>();
            MyMatrix <Fraction> B        = new MyMatrix <Fraction>();
            MyMatrix <Fraction> X        = new MyMatrix <Fraction>();
            int k = 0;

            for (int i = 0; i < SIZE; i++)
            {
                for (int j = 0; j < SIZE; j++)
                {
                    {
                        Fraction value = new Fraction(numbers[k]);
                        A[i, j] = value;
                        B[i, j] = value;
                        if (k % SIZE == 0)
                        {
                            //   A[i, SIZE] = value;
                            //  B[i, SIZE] = value;
                            X[i] = value;
                        }
                    }
                    k++;
                }
            }
            MyMatrix <Fraction> C      = new MyMatrix <Fraction>();
            MyMatrix <Fraction> result = new MyMatrix <Fraction>();



            var watch = System.Diagnostics.Stopwatch.StartNew();

            // C = B * A; D = C * C;    //  (A*B)*C
            //  D = A / X;     // C * wektor X (pierwsza kolumna macierzy B)
            // C = A + B; D = C + C; D = D / X;
            C = A;
            //  C = A * B;
            //// mnzoenie przez wektor

            for (int i = 0; i < Configuration.SIZE; i++)
            {
                for (int j = 0; j < Configuration.SIZE; j++)
                {
                    for (int s = 0; s < Configuration.SIZE; s++)
                    {
                        result[i, j] = C[i, 0] * B[0, 0] + C[i, 1] * B[1, 0] + C[i, 2] * B[2, 0] + C[i, 3] * B[3, 0] +
                                       C[i, 4] * B[4, 0] + C[i, 5] * B[5, 0] + C[i, 6] * B[6, 0] + C[i, 7] * B[7, 0] +
                                       C[i, 8] * B[8, 0] + C[i, 9] * B[9, 0];
                    }
                }
            }

            watch.Stop();

            double elapsedMs = watch.ElapsedMilliseconds;

            Console.WriteLine(elapsedMs / 1000);

            /////////////////////////////////////mnozenie  (A+B) * B



            MyMatrix <Fraction> avrage   = new MyMatrix <Fraction>();
            Fraction            dzilenik = new Fraction(1, 10);
            Fraction            srednia  = new Fraction(1, 1);

            for (int i = 0; i < Configuration.SIZE; i++)         // srednia wiersza
            {
                for (int j = 0; j < 1; j++)
                {
                    for (int s = 0; s < Configuration.SIZE; s++)
                    {
                        avrage[i, j] = (result[i, 0] + result[i, 1] + result[i, 2] + result[i, 3] + result[i, 4] + result[i, 5] +
                                        result[i, 6] + result[i, 7] + result[i, 8] + result[i, 9]) * dzilenik;
                    }
                }
            }

            srednia = (avrage[0, 0] + avrage[1, 0] + avrage[2, 0] + avrage[3, 0] + avrage[4, 0] + avrage[5, 0] +
                       avrage[6, 0] + avrage[7, 0] + avrage[8, 0] + avrage[9, 0]) * dzilenik;

            Console.WriteLine("Srednia {0}", srednia);


            for (int i = 0; i < Configuration.SIZE; i++)
            {
                //  for (int j = 0; j < Configuration.SIZE; j++)
                {
                    Console.Write(string.Format("{0} ", result[i, 0]));
                }
                Console.Write(Environment.NewLine + Environment.NewLine);
            }
        }
        public static void DoubleArray(int SIZE)
        {
            double[] numbers     = new double[Configuration.BIGSIZE];
            var      fileNumbers = File.ReadLines("matrix.txt").Select(double.Parse);
            int      index       = 0;

            foreach (var number in fileNumbers)
            {
                numbers[index] = number;
                index++;
            }
            MyMatrix <double> A = new MyMatrix <double>();
            MyMatrix <double> B = new MyMatrix <double>();
            MyMatrix <double> X = new MyMatrix <double>();
            // MyMatrix<double> dupa = new MyMatrix<double>();
            //MyMatrix<double> [,]array = new MyMatrix<double>[4,4];
            // MyMatrix<double>[] test = new MyMatrix<double>[4];
            int k = 0;

            for (int i = 0; i < SIZE; i++)
            {
                for (int j = 0; j < SIZE; j++)
                {
                    {
                        double value = numbers[k];
                        A[i, j] = value;
                        B[i, j] = value;
                        if (k % SIZE == 0)
                        {
                            A[i, SIZE] = value;
                            X[i]       = value;
                        }
                    }
                    k++;
                }
            }
            X = MyMatrix <double> .Gauss(SIZE, A);

            //X = A / X;
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(X[i]);
            }
            MyMatrix <double> D = new MyMatrix <double>();
            MyMatrix <double> C = new MyMatrix <double>();
            //double z = A[0, 0] + B[0, 0];
            //Console.WriteLine(z);

            var    watch  = System.Diagnostics.Stopwatch.StartNew();
            double avrage = 0;

            X = MyMatrix <double> .Gauss(SIZE, A);

            //    C = B * A;     D = C * C;    //  (A*B)*C
            // D = A / B;     // C * wektor X (pierwsza kolumna macierzy B)
            //  C = A + B; D = C + C; D = D / B;

            watch.Stop();
            double elapsedMs = watch.ElapsedMilliseconds;

            Console.WriteLine((elapsedMs / 1000));


            MyMatrix <double> result = new MyMatrix <double>();

            ////////////////////////// srednia wierszy
            for (int i = 0; i < Configuration.SIZE; i++)
            {
                for (int j = 0; j < 1; j++)
                {
                    for (int s = 0; s < Configuration.SIZE; s++)
                    {
                        result[i, j] += D[i, s];
                    }
                }
            }
            /////////////////srednia calej macierzy
            for (int i = 0; i < Configuration.SIZE; i++)
            {
                avrage += result[i, 0];
            }
            avrage = avrage / 10;
            //     Console.WriteLine(avrage);
            for (int i = 0; i < Configuration.SIZE; i++)
            {
                for (int j = 0; j < Configuration.SIZE; j++)
                {
                    //Console.Write(string.Format("{0} ", A[i, j]));
                }
                //  Console.Write(Environment.NewLine + Environment.NewLine);
            }
        }
Beispiel #30
0
        public void WriteFractionMatrixToFile(MyMatrix <Fraction> matrix, string fileName)
        {
            var formattedMatrix = MyMatrixFormatter.GetFormattedFractionMatrix(matrix);

            WriteToFile(fileName, formattedMatrix, matrix.Rows);
        }