Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- namespace ConsoleApplication3
- {
- public class Matrix
- {
- public float[,] matrix = null;
- public int CountColumn { get; private set; }
- public int CountRow { get; private set; }
- public Matrix(int x = 1, int y = 1, bool autoGeneration = true, int startGen = 1)
- {
- if (autoGeneration) generator(x, y, startGen);
- else
- matrix = new float[x, y];
- CountColumn = y;
- CountRow = x;
- }
- public float this[int x, int y]
- {
- get { return matrix[x, y]; }
- set { matrix[x, y] = value; }
- }
- public static Matrix operator *(Matrix x1, Matrix x2)
- {
- if (x1.CountColumn != x2.CountRow) throw new ArgumentException("Число столбцов матрицы А не равно числу строк матрицы В.");
- Matrix ret = new Matrix(x1.CountRow, x2.CountColumn, false);
- for (int i = 0; i < x1.CountRow; i++)
- for (int j = 0; j < x2.CountColumn; j++)
- for (int k = 0; k < x2.CountRow; k++)
- ret[i, j] += x1[i, k] * x2[k, j];
- return ret;
- }
- //возведение в степень
- public static Matrix operator ^ (Matrix x1, int pow)
- {
- Matrix matr = new Matrix(x1.CountRow, x1.CountColumn);
- matr = x1;
- for (int z = 1; z < pow; z++)
- {
- Matrix bufer = new Matrix(x1.CountRow, x1.CountColumn);
- for (int i = 0; i < x1.CountRow; i++)
- {
- for (int j = 0; j < x1.CountRow; j++)
- {
- float sum = 0;
- for (int r = 0; r < x1.CountRow; r++)
- sum += matr[i, r] * x1[r, j];
- bufer[i, j] = sum;
- }
- }
- matr = bufer;
- }
- return matr;
- }
- public static Matrix operator *(Matrix x1, int constNum)
- {
- Matrix ret = new Matrix(x1.CountRow, x1.CountColumn, false);
- for (int i = 0; i < x1.CountColumn * x1.CountRow; i++)
- {
- int X = i / x1.CountColumn;
- int Y = i - x1.CountColumn * X;
- ret[X, Y] = x1[X, Y] * constNum;
- }
- return ret;
- }
- public static Matrix operator /(Matrix x1, Matrix x2)
- {
- if (x1.CountColumn != x2.CountRow) throw new ArgumentException("Число столбцов матрицы А не равно числу строк матрицы В.");
- Matrix ret = new Matrix(x1.CountRow, x2.CountColumn, false);
- ret = x1 * x2.Inverse();
- return ret;
- }
- public float[] GetRow(int row)
- {
- if (row >= CountRow) throw new IndexOutOfRangeException("Индекс строки не принадлежит массиву.");
- float[] ret = new float[CountColumn];
- for (int i = 0; i < CountColumn; i++)
- ret[i] = matrix[row, i];
- return ret;
- }
- public void SetRow(float[] values, int row)
- {
- if (row >= CountRow) throw new IndexOutOfRangeException("Индекс строки не принадлежит массиву.");
- for (int i = 0; i < (CountColumn > values.Length ? values.Length : CountColumn); i++)
- matrix[row, i] = values[i];
- }
- public Matrix Inverse()
- {
- if (CountRow != CountColumn) throw new ArgumentException("Обратная матрица существует только для квадратных, невырожденных, матриц.");
- Matrix ret = new Matrix(CountRow, CountColumn, false);
- ret.matrix = (float[,])this.matrix.Clone();
- float determinant = ret.Determinant();
- if (determinant == 0) return ret; //Если определитель == 0 - матрица вырожденная
- for (int i = 0; i < CountRow; i++)
- {
- for (int t = 0; t < CountColumn; t++)
- {
- Matrix tmp = ret.Exclude(i, t);
- ret[t, i] = tmp.Determinant() / determinant;
- }
- }
- return ret;
- }
- public float Determinant()
- {
- if (CountColumn != CountRow) throw new ArgumentException("Вычисление определителя возможно только для квадратных матриц.");
- Matrix _matrix = new Matrix(CountRow, CountColumn, false);
- _matrix.matrix = (float[,])this.matrix.Clone();
- float det = 1;
- int order = CountRow;
- for (int i = 0; i < order - 1; i++)
- {
- float[] masterRow = _matrix.GetRow(i);
- det *= masterRow[i];
- if (det == 0) return 0;
- for (int t = i + 1; t < order; t++)
- {
- float[] slaveRow = _matrix.GetRow(t);
- float[] tmp = MulArrayConst(masterRow, slaveRow[i] / masterRow[i]);
- float[] source = _matrix.GetRow(t);
- _matrix.SetRow(SubArray(source, tmp), t);
- }
- }
- det *= _matrix[order - 1, order - 1];
- return det;
- }
- public Matrix Exclude(int row, int column)
- {
- if (row > CountRow || column > CountColumn) throw new IndexOutOfRangeException("Строка или столбец не принадлежат матрице.");
- Matrix ret = new Matrix(CountRow - 1, CountColumn - 1, false);
- ret.matrix = (float[,])this.matrix.Clone();
- int offsetX = 0;
- for (int i = 0; i < CountRow; i++)
- {
- int offsetY = 0;
- if (i == row) { offsetX++; continue; }
- for (int t = 0; t < CountColumn; t++)
- {
- if (t == column) { offsetY++; continue; }
- ret[i - offsetX, t - offsetY] = this[i, t];
- }
- }
- return ret;
- }
- public override string ToString()
- {
- StringBuilder ret = new StringBuilder();
- if (matrix == null) return ret.ToString();
- for (int i = 0; i < CountRow; i++)
- {
- for (int t = 0; t < CountColumn; t++)
- {
- ret.Append(matrix[i, t]);
- ret.Append("\t");
- }
- ret.Append("\n");
- }
- return ret.ToString();
- }
- float[] SubArray(float[] A, float[] B)
- {
- float[] ret = (float[])A.Clone();
- for (int i = 0; i < (A.Length > B.Length ? A.Length : B.Length); i++)
- ret[i] -= B[i];
- return ret;
- }
- float[] MulArrayConst(float[] array, float number)
- {
- float[] ret = (float[])array.Clone();
- for (int i = 0; i < ret.Length; i++)
- ret[i] *= number;
- return ret;
- }
- void generator(int x, int y, int startGen)
- {
- matrix = new float[x, y];
- Random rnd = new Random(startGen);
- for (int i = 0; i < x * y; i++)
- {
- int X = i / y;
- int Y = i - y * X;
- matrix[X, Y] = rnd.Next(-10, 10);
- }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Matrix m1 = new Matrix(3, 3, true, 22);
- Matrix m2 = new Matrix(3, 3, true, 28);
- Console.WriteLine("{0}\n{1}", m1, m2);
- Console.WriteLine("Произведение\n",m1 * m2);
- Console.WriteLine("Деление\n", m1 / m2);
- Console.WriteLine("Умножение на 4\n", m1 * 4);
- Console.WriteLine("Возведение в 3 степень\n", m1 ^ 3);
- Console.WriteLine("Произведение с присвоением\n", m1 *= m2);
- Console.WriteLine("деление с присвоением\n", m1 /= m2);
- Console.WriteLine("Детерминант\n", m1.Determinant());
- Console.WriteLine(m1.Inverse());
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement