Advertisement
Guest User

Untitled

a guest
Oct 4th, 2015
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.26 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace ConsoleApplication5
  9. {
  10.     public class SomeClass
  11.     {
  12.         public class Vector : IEnumerable, IEquatable<Vector>
  13.         {
  14.             public List<int> vector = new List<int>();
  15.             public int this[int index]
  16.             {
  17.                 set
  18.                 {
  19.                     vector[index] = value;
  20.                 }
  21.  
  22.                 get
  23.                 {
  24.                     return vector[index];
  25.                 }
  26.             }
  27.             private class VectorEnum : IEnumerator
  28.             {
  29.                 public List<int> vector;
  30.                 int position = -1;
  31.                 public VectorEnum(List<int> vector)
  32.                 {
  33.                     this.vector = vector;
  34.                 }
  35.  
  36.                 public bool MoveNext()
  37.                 {
  38.                     position++;
  39.                     return (position < vector.Count);
  40.                 }
  41.  
  42.                 public void Reset()
  43.                 {
  44.                     position = -1;
  45.                 }
  46.  
  47.                 object IEnumerator.Current
  48.                 {
  49.                     get { return Current; }
  50.                 }
  51.  
  52.  
  53.                 public int Current
  54.                 {
  55.                     get { return vector[position]; }
  56.                 }
  57.             }
  58.  
  59.  
  60.             public IEnumerator GetEnumerator()
  61.             {
  62.                 return new VectorEnum(vector);
  63.             }
  64.  
  65.  
  66.             public override String ToString()
  67.             {
  68.                 return string.Join(",", vector.ToArray());
  69.  
  70.             }
  71.  
  72.             public Vector(params int[] values)
  73.             {
  74.                 foreach (int a in values)
  75.                 {
  76.                     vector.Add(a);
  77.                 }
  78.             }
  79.  
  80.             public void Add(params int[] values)
  81.             {
  82.                 foreach (int a in values)
  83.                 {
  84.                     vector.Add(a);
  85.                 }
  86.             }
  87.             public int Count
  88.             {
  89.                 get { return vector.Count; }
  90.             }
  91.  
  92.             public bool Search(int a)
  93.             {
  94.                 foreach (int value in vector)
  95.                     if (value == a)
  96.                         return true;
  97.                 return false;
  98.             }
  99.             public bool Equals(Vector a)
  100.             {
  101.                 if (a.Count != this.Count)
  102.                     return false;
  103.                 for (int i = 0; i < a.Count; i++)
  104.                 {
  105.                     if (a[i] == this[i])
  106.                         continue;
  107.                     else
  108.                         return false;
  109.                 }
  110.                 return true;
  111.             }
  112.  
  113.  
  114.         }
  115.  
  116.         public class Matrix : IEnumerable, IEquatable<Matrix>
  117.         {
  118.             List<Vector> matrix = new List<Vector>();
  119.             public int Length
  120.             {
  121.                 get;
  122.                 set;
  123.             }
  124.             public int Height
  125.             {
  126.                 get
  127.                 {
  128.                     return matrix.Count;
  129.                 }
  130.             }
  131.             public Vector this[int index]
  132.             {
  133.                 set
  134.                 {
  135.                     matrix[index] = value;
  136.                 }
  137.  
  138.                 get
  139.                 {
  140.                     return matrix[index];
  141.                 }
  142.             }
  143.             public int this[int index, int index2]
  144.             {
  145.                 set
  146.                 {
  147.                     matrix[index][index2] = value;
  148.                 }
  149.  
  150.                 get
  151.                 {
  152.                     return matrix[index][index2];
  153.                 }
  154.             }
  155.             public int Count
  156.             {
  157.                 get { return matrix.Count; }
  158.             }
  159.  
  160.             public Matrix(params Vector[] vectors)
  161.             {
  162.                 try {
  163.                     this.Length = vectors[0].Count;
  164.                 }
  165.                 catch {
  166.                     this.Length = 0;
  167.                 }
  168.                     foreach (Vector vector in vectors)
  169.                         if (vector.Count == this.Length)
  170.                             matrix.Add(vector);
  171.                         else throw new ArgumentException("Matrix is not MxN");
  172.                
  173.                
  174.             }
  175.  
  176.             public Matrix(int a, int b)
  177.             {
  178.                 this.Length = a;
  179.                 for (int i = 0; i < a; i++)
  180.                 {
  181.                     matrix.Add(new Vector());
  182.                 }
  183.                 for (int i = 0; i < a; i++)
  184.                 {
  185.                     for (int j = 0; j < b; j++)
  186.                     {
  187.                         matrix[i].Add(0);
  188.                     }
  189.                 }
  190.             }
  191.  
  192.             public void Add(params Vector[] vectors)
  193.             {
  194.  
  195.                 foreach (Vector vector in vectors)
  196.                 {
  197.                     if (vector.Count == this.Length)
  198.                         matrix.Add(vector);
  199.                     else
  200.                         throw new ArgumentException("Vector length is greater or lesser than matrix length");
  201.                 }
  202.             }
  203.  
  204.             public override String ToString()
  205.             {
  206.                 String a = "";
  207.                 foreach (Vector vector in matrix)
  208.                 {
  209.                     a = a + vector.ToString() + "\n";
  210.  
  211.                 }
  212.                 return a;
  213.             }
  214.             public bool Search(Vector a)
  215.             {
  216.                 int flag = 1;
  217.                 foreach (Vector b in matrix)
  218.                 {
  219.                     if (a.Count != b.Count)
  220.                         continue;
  221.                     flag = 0;
  222.                     for (int i = 0; i < a.Count; i++)
  223.                     {
  224.                         if (a[i] != b[i])
  225.                             return false;
  226.                         return true;
  227.                     }
  228.                 }
  229.                 if (flag == 0)
  230.                     return true;
  231.                 else
  232.                     return false;
  233.             }
  234.  
  235.             public bool Equals(Matrix a)
  236.             {
  237.                 if (a.Count != this.Count)
  238.                     return false;
  239.                 for (int i = 0; i < a.Count; i++)
  240.                 {
  241.                     if (a[i].Equals(this[i]))
  242.                         continue;
  243.                     else
  244.                         return false;
  245.                 }
  246.                 return true;
  247.             }
  248.             public bool isSquare()
  249.             {
  250.                 if (this.Height == this.Length)
  251.                     return true;
  252.                 return false;
  253.  
  254.             }
  255.             public bool isEqualSize(Matrix a)
  256.             {
  257.                 if ((this.Height == a.Height) && (this.Length == a.Length))
  258.                     return true;
  259.                 return false;
  260.  
  261.             }
  262.             public Matrix MultiplyByNumber(int a)
  263.             {
  264.                 int vecCount = 0;
  265.                 Matrix rezult = new Matrix();
  266.                 foreach (Vector vec in matrix)
  267.                 {
  268.                     rezult.Add(new Vector());
  269.                     for (int i = 0; i < vec.Count; i++)
  270.                         rezult[vecCount].Add(0);
  271.                     for (int i = 0; i < vec.Count; i++)
  272.                         rezult[vecCount][i] = a * vec[i];
  273.                     vecCount += 1;
  274.                 }
  275.                 return rezult;
  276.             }
  277.  
  278.             public Matrix AddMatrix(Matrix a)
  279.             {
  280.                 Matrix rezult = new Matrix();
  281.                 int vecCount = 0;
  282.                 if (this.isEqualSize(a))
  283.                 {
  284.                     foreach (Vector vec in matrix)
  285.                     {
  286.                         rezult.Add(new Vector());
  287.                         for (int i = 0; i < vec.Count; i++)
  288.                             rezult[vecCount].Add(0);
  289.                         for (int i = 0; i < vec.Count; i++)
  290.                             rezult[vecCount][i] = a[vecCount][i] + vec[i];
  291.                         vecCount += 1;
  292.                     }
  293.                 }
  294.                 else throw new ArgumentException("Matrices must have equal size");
  295.                 return rezult;
  296.             }
  297.             public Matrix MultiplyByMatrix(Matrix a)
  298.             {
  299.                 Matrix rezult = new Matrix(this.Height, a.Length);
  300.                 if (this.Length == a.Height)
  301.                 {
  302.                     int sum = 0;
  303.                     for (int c = 0; c < this.Height; c++)
  304.                     {
  305.                         for (int d = 0; d < a.Length; d++)
  306.                         {
  307.                             for (int k = 0; k < a.Height; k++)
  308.                             {
  309.                                 sum = sum + this[c][k] * a[k][d];
  310.                             }
  311.  
  312.                             rezult[c][d] = sum;
  313.                             sum = 0;
  314.                         }
  315.                     }
  316.                     return rezult;
  317.                 }
  318.                 else throw new ArgumentException("Number of columns in M1 must be equal to the number of rows in D");
  319.             }
  320.  
  321.             public Matrix Transpose()
  322.             {
  323.                 int t;
  324.                
  325.                 Matrix a = new Matrix(this.Length,this.Height);
  326.                 for (int c = 0; c < this.Height; c++)
  327.                     for( int d = 0 ; d < this.Length ; d++ )
  328.                          a[d][c] = matrix[c][d];
  329.                
  330.                 return a;
  331.             }
  332.  
  333.             public static Matrix operator *(Matrix mat, int num)
  334.             {
  335.                 return mat.MultiplyByNumber(num);
  336.             }
  337.             public static Matrix operator *(Matrix mat, Matrix mat2)
  338.             {
  339.                 return mat.MultiplyByMatrix(mat2);
  340.             }
  341.             public static Matrix operator +(Matrix mat, Matrix mat2)
  342.             {
  343.                 return mat.AddMatrix(mat2);
  344.             }
  345.  
  346.             private class MatrixEnum : IEnumerator
  347.             {
  348.                 public List<Vector> matrix;
  349.                 int position = -1;
  350.                 public MatrixEnum(List<Vector> matrix)
  351.                 {
  352.                     this.matrix = matrix;
  353.                 }
  354.                 public bool MoveNext()
  355.                 {
  356.                     position++;
  357.                     return (position < matrix.Count);
  358.                 }
  359.                 public void Reset()
  360.                 {
  361.                     position = -1;
  362.                 }
  363.                 object IEnumerator.Current
  364.                 {
  365.                     get { return Current; }
  366.                 }
  367.                 public Vector Current
  368.                 {
  369.                     get { return matrix[position]; }
  370.                 }
  371.             }
  372.             public IEnumerator GetEnumerator()
  373.             {
  374.                 return new MatrixEnum(matrix);
  375.             }
  376.  
  377.         }
  378.         public static int Main()
  379.         {
  380.             Matrix mat = new Matrix(
  381.                 new Vector(1, 2, 3),
  382.                 new Vector(4, 5, 6),
  383.                 new Vector(2, 3, 4));
  384.  
  385.             Console.WriteLine(mat);
  386.             Console.WriteLine(mat + mat);
  387.             Console.WriteLine(mat * 3);
  388.  
  389.             Matrix mat1 = new Matrix(
  390.                 new Vector(1, 2, 0),
  391.                 new Vector(3, 1, -1));
  392.             Matrix mat2 = new Matrix(
  393.                 new Vector(1),
  394.                 new Vector(2),
  395.                 new Vector(3));
  396.  
  397.             Console.WriteLine(mat2.Transpose());
  398.             Console.WriteLine(mat);
  399.  
  400.             Matrix mat3 = new Matrix(
  401.                 new Vector(1, 2),
  402.                 new Vector(3, 1));
  403.             Console.WriteLine(mat3.isSquare());
  404.             Console.WriteLine(mat1.isSquare());
  405.             Console.WriteLine(mat3.Search(new Vector(1, 2)));
  406.             Console.WriteLine(mat3.Search(new Vector(3)));
  407.  
  408.             Matrix mat4 = new Matrix(
  409.                 new Vector(1, 2),
  410.                 new Vector(3, 1));
  411.             Console.WriteLine(mat3.Equals(mat4));
  412.             Console.WriteLine(mat3.Equals(mat1));
  413.             Console.ReadLine();
  414.             return 0;
  415.         }
  416.     }
  417. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement