Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication1
- {
- class Matrix
- {
- public double[,] matice;
- public Matrix(double[,] matice)
- {
- this.matice = matice;
- }
- private int RowSize
- {
- get { return matice.GetLength(0); }
- }
- private int ColumnSize
- {
- get { return matice.GetLength(1); }
- }
- // operatory +-*
- public static Matrix operator +(Matrix a, Matrix b)
- {
- if (a.RowSize != b.RowSize || a.ColumnSize != b.ColumnSize)
- {
- throw new Exception("Nenašlo");
- }
- double[,] result = new double[a.RowSize, a.ColumnSize];
- for (int i = 0; i < a.RowSize; i++)
- {
- for (int j = 0; j < a.ColumnSize; j++)
- {
- result[i, j] = a.matice[i, j] + b.matice[i, j];
- }
- }
- return new Matrix(result);
- }
- public static Matrix operator -(Matrix a, Matrix b)
- {
- if (a.RowSize != b.RowSize || a.ColumnSize != b.ColumnSize)
- {
- throw new Exception("Nenašlo");
- }
- double[,] result = new double[a.RowSize, a.ColumnSize];
- for (int i = 0; i < a.RowSize; i++)
- {
- for (int j = 0; j < a.ColumnSize; j++)
- {
- result[i, j] = a.matice[i, j] - b.matice[i, j];
- }
- }
- return new Matrix(result);
- }
- public static Matrix operator -(Matrix a)
- {
- double[,] result = new double[a.RowSize, a.ColumnSize];
- for (int i = 0; i < a.RowSize; i++)
- {
- for (int j = 0; j < a.ColumnSize; j++)
- {
- result[i, j] = -a.matice[i, j];
- }
- }
- return new Matrix(result);
- }
- public static Matrix operator *(Matrix a, Matrix b)
- {
- if (a.ColumnSize != b.RowSize)
- {
- throw new Exception("Nenašlo");
- }
- double sum = 0;
- double[,] result = new double[a.RowSize, a.ColumnSize];
- for (int i = 0; i < a.RowSize; i++)
- {
- for (int j = 0; j < b.ColumnSize; j++)
- {
- for (int sumI = 0; sumI<a.ColumnSize; sumI++) {
- sum += a.matice[i, sumI] * b.matice[sumI, j];
- }
- result[i,j] = sum;
- sum = 0;
- }
- }
- return new Matrix(result);
- }
- // logické operace
- public static bool operator ==(Matrix a, Matrix b) {
- if (a.RowSize != b.RowSize || a.ColumnSize != b.ColumnSize)
- {
- return false;
- }
- for (int i = 0; i < a.RowSize; i++) {
- for (int j = 0; j < a.ColumnSize; j++) {
- if(a.matice[i,j] != b.matice[i,j]){
- return false;
- }
- }
- }
- return true;
- }
- public static bool operator !=(Matrix a,Matrix b){
- return!(a==b);
- }
- public override string ToString()
- { string result= "";
- for (int i = 0; i < RowSize; i++) {
- for (int j = 0; j <ColumnSize; j++) {
- result += string.Format("{0,10:F2}",matice[i,j]);
- }
- result += Environment.NewLine;}
- return result;
- }
- public double Determinant()
- {
- if (ColumnSize != RowSize) {
- throw new Exception("NOT a square matrix");
- }
- if (ColumnSize == 1)
- { return matice[0, 0]; }
- else if (ColumnSize == 2) {
- return matice[0, 0] * matice[1, 1] - matice[1, 0] * matice[0, 1];
- }
- else if (ColumnSize == 3)
- {
- return matice[0, 0] * matice[1, 1] * matice[2, 2]
- + matice[0, 1] * matice[1, 2] * matice[2, 0]
- + matice[0, 2] * matice[1, 0] * matice[2, 1]
- - matice[0, 2] * matice[1, 1] * matice[2, 0]
- - matice[0, 0] * matice[1, 2] * matice[2, 1]
- - matice[0, 1] * matice[1, 0] * matice[2, 2];
- }
- else { throw new Exception("Neumim vetsi ne 3"); }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement