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 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()); } } }
run
|
edit
|
history
|
help
0
Linked List with basic functions.
hackton
ggg
SKYIES
factorial ncr + unique list
remove duplicates
codeWall
NationalCode
Student Cables
30272 Program Ex3