Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Square Integer Matrix
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.Text; namespace Rextester { public class Program { public class SquareIntegerMatrix { private int[,] InternalRep = null; public int Rows = 0; public int Columns = 0; private List<int> Vector = null; private void Zero() { for (int i = 0; i < Rows; i++) { for (int j = 0; j < Columns; j++) { InternalRep[i, j] = 0; } } } private int SignOfElement(int i, int j) { if ((i + j) % 2 == 0) { return 1; } else { return -1; } } //this method determines the sub matrix corresponding to a given element private int[,] CreateSmallerMatrix(int[,] input, int i, int j) { int order = int.Parse(System.Math.Sqrt(input.Length).ToString()); int[,] output = new int[order - 1, order - 1]; int x = 0, y = 0; for (int m = 0; m < order; m++, x++) { if (m != i) { y = 0; for (int n = 0; n < order; n++) { if (n != j) { output[x, y] = input[m, n]; y++; } } } else { x--; } } return output; } public int Determinant() { return Determinant(this.InternalRep); } private int Determinant(int[,] input) { int order = int.Parse(System.Math.Sqrt(input.Length).ToString()); if (order > 2) { int value = 0; for (int j = 0; j < order; j++) { int[,] Temp = CreateSmallerMatrix(input, 0, j); value = value + input[0, j] * (SignOfElement(0, j) * Determinant(Temp)); } return value; } else if (order == 2) { return ((input[0, 0] * input[1, 1]) - (input[1, 0] * input[0, 1])); } else { return (input[0, 0]); } } private void FromVector() { int cnt = 0; for (int i = 0; i < Rows; i++) { for (int j = 0; j < Columns; j++) { InternalRep[i, j] = Vector[cnt++]; } } } public SquareIntegerMatrix(int rows, int columns) { if (rows != columns) { throw new Exception("rows and columns must be equal for square matrix"); } this.Rows = rows; this.Columns = columns; InternalRep = new int[this.Rows, this.Columns]; Zero(); } public SquareIntegerMatrix(int[,] RC) { int rows = RC.GetLength(0); int columns = RC.GetLength(1); if (rows != columns) { throw new Exception("rows and columns must be equal for square matrix"); } this.Rows = rows; this.Columns = columns; InternalRep = RC; } public SquareIntegerMatrix(int rows, int columns, List<int> V) { if (rows != columns) { throw new Exception("rows and columns must be equal for square matrix"); } if (V.Count % rows != 0) { throw new Exception("Vector does not contain even row count"); } Vector = V; this.Rows = rows; this.Columns = columns; InternalRep = new int[this.Rows, this.Columns]; FromVector(); } public static SquareIntegerMatrix operator +(SquareIntegerMatrix a, SquareIntegerMatrix b) { SquareIntegerMatrix ret = new SquareIntegerMatrix(a.Rows, a.Columns); for (int i = 0; i < ret.Rows; i++) { for (int j = 0; j < ret.Columns; j++) { ret.InternalRep[i, j] = a.InternalRep[i, j] + b.InternalRep[i, j]; } } return ret; } public static SquareIntegerMatrix operator *(SquareIntegerMatrix a, SquareIntegerMatrix b) { SquareIntegerMatrix ret = new SquareIntegerMatrix(a.Rows, a.Columns); for (int i = 0; i < ret.Rows; i++) { for (int j = 0; j < ret.Columns; j++) { for (int k = 0; k < ret.Columns; k++) { ret.InternalRep[i, j] += a.InternalRep[i, k] * b.InternalRep[k, j]; } } } return ret; } public int this[int r, int c] { get { if (!(r < Rows && c < Columns)) { throw new Exception("rows and columns out of range of square matrix"); } return (InternalRep[r, c]); } set { InternalRep[r, c] = value; } } public override string ToString() { StringBuilder sb = new StringBuilder(); for (int i = 0; i < Rows; i++) { for (int j = 0; j < Columns; j++) { sb.AppendFormat("{0}\t", InternalRep[i, j]); } sb.Append("\r\n"); } return sb.ToString(); } } public static void Main(string[] args) { List<int> v = new List<int> { 1, 2, 3, 4 }; List<int> v2= new List<int> { 7, 8, 9, 10 }; SquareIntegerMatrix m = new SquareIntegerMatrix(2, 2, v); SquareIntegerMatrix m2 = new SquareIntegerMatrix(2, 2, v2); SquareIntegerMatrix mm = m * m2; Console.WriteLine(mm.ToString()); Console.WriteLine(mm[0, 0]); mm[0, 0] = 97; Console.WriteLine(mm.ToString()); int[,] ir = new int[2, 2]; ir[0, 0] = 1; ir[0, 1] = 2; ir[1,0] = 3; ir[1,1] = 4; SquareIntegerMatrix mmir = new SquareIntegerMatrix(ir); Console.WriteLine(mmir.ToString()); SquareIntegerMatrix mma = m + m2; Console.WriteLine(mma.ToString()); int[,] mat = { {2, 6, 6, 2}, {2, 7, 3, 6}, {1, 5, 0, 1}, {3, 7, 0, 7} }; SquareIntegerMatrix md = new SquareIntegerMatrix(mat); Console.WriteLine(md.ToString()); Console.Write(md.Determinant()); Console.WriteLine(); } } }
run
|
edit
|
history
|
help
0
Area and Parameter of Circle
Square Real Matrix
الحاسوب
Hzzz
SVSVSV
a6
asxasxd
Дамир
sdfghyjuki54efc sacd
Generate Secure GUID