fork download
  1. using System;
  2. using System.Text;
  3.  
  4. class CustomMatrix<ELEM, MATRIX>
  5. where MATRIX : CustomMatrix<ELEM, MATRIX>
  6. {
  7. public readonly uint sizeY, sizeX;
  8. public readonly ELEM[,] data;
  9.  
  10. protected CustomMatrix(uint sizeY, uint sizeX)
  11. {
  12. this.sizeY = sizeY;
  13. this.sizeX = sizeX;
  14. data = new ELEM[sizeY, sizeX];
  15. }
  16.  
  17. protected CustomMatrix(uint sizeY, uint sizeX, Func<uint, uint, ELEM> get) :
  18. this(sizeY, sizeX)
  19. {
  20. for (uint y = 0; y < sizeY; y++)
  21. for (uint x = 0; x < sizeX; x++)
  22. data[y, x] = get(y, x);
  23. }
  24.  
  25. public MATRIX Transpose()
  26. {
  27. Func<uint, uint, ELEM> transposer = (y, x) => data[x, y];
  28. return (MATRIX)Activator.CreateInstance(
  29. typeof(MATRIX), new object[]{ sizeX, sizeY, transposer });
  30. }
  31.  
  32. public ELEM this[uint y, uint x]
  33. {
  34. get { return data[y, x]; }
  35. set { data[y, x] = value; }
  36. }
  37.  
  38. public override string ToString()
  39. {
  40. var sb = new StringBuilder();
  41. for (uint y = 0; y < sizeY; y++)
  42. {
  43. if (y > 0) sb.Append("\n");
  44. for (uint x = 0; x < sizeX; x++)
  45. {
  46. if (x > 0) sb.Append(" ");
  47. sb.Append(data[y, x]);
  48. }
  49. }
  50. return sb.ToString();
  51. }
  52. }
  53.  
  54. class Matrix<ELEM> : CustomMatrix<ELEM, Matrix<ELEM>>
  55. {
  56. public Matrix(uint sizeY, uint sizeX) : base(sizeY, sizeX) { }
  57. public Matrix(uint sizeY, uint sizeX, Func<uint, uint, ELEM> get) : base(sizeY, sizeX, get) { }
  58.  
  59. public static explicit operator SquareMatrix<ELEM>(Matrix<ELEM> m)
  60. {
  61. return new SquareMatrix<ELEM>(m.sizeY, m.sizeX, (y, x) => m[y, x]);
  62. }
  63. }
  64.  
  65. class SquareMatrix<ELEM> : CustomMatrix<ELEM, SquareMatrix<ELEM>>
  66. {
  67. public SquareMatrix(uint size) : base(size, size) { }
  68. public SquareMatrix(uint size, Func<uint, uint, ELEM> get) : base(size, size, get) { }
  69. public SquareMatrix(uint sizeY, uint sizeX, Func<uint, uint, ELEM> get) : base(sizeY, sizeX, get)
  70. {
  71. if (sizeY != sizeX) throw new ArgumentException(string.Format("SquareMatrix не может быть размера {0}×{1}.", sizeY, sizeX));
  72. }
  73.  
  74. public static explicit operator Matrix<ELEM>(SquareMatrix<ELEM> m)
  75. {
  76. return new Matrix<ELEM>(m.sizeY, m.sizeX, (y, x) => m[y, x]);
  77. }
  78. }
  79.  
  80. public class MainClass
  81. {
  82. public static void Main()
  83. {
  84. Matrix<double> m = new Matrix<double>(3, 5, (y, x) => 2*y + x);
  85. Console.WriteLine("m: Matrix =\n{0}", m);
  86.  
  87. Matrix<double> m_t = m.Transpose();
  88. Console.WriteLine("\nm.Transpose() =\n{0}", m_t);
  89.  
  90. string m_as_sm_s;
  91. try
  92. {
  93. m_as_sm_s = ((SquareMatrix<double>)m).ToString();
  94. } catch (ArgumentException e)
  95. {
  96. m_as_sm_s = e.Message;
  97. }
  98. Console.WriteLine("\n(SquareMatrix) m =\n{0}", m_as_sm_s);
  99.  
  100. SquareMatrix<double> sm = new SquareMatrix<double>(5, (y, x) => 5 + (int)x - (int)y);
  101. Console.WriteLine("\nsm: SquareMatrix =\n{0}", sm);
  102.  
  103. SquareMatrix<double> sm_t = sm.Transpose();
  104. Console.WriteLine("\nsm.Transpose() =\n{0}", sm_t);
  105.  
  106. Matrix<double> sm_as_m = (Matrix<double>)sm;
  107. Console.WriteLine("\n(Matrix) sm =\n{0}", sm_as_m);
  108.  
  109. SquareMatrix<double> sm_as_m_as_sm = (SquareMatrix<double>)sm_as_m;
  110. Console.WriteLine("\n(SquareMatrix) (Matrix) sm =\n{0}", sm_as_m_as_sm);
  111. }
  112. }
Success #stdin #stdout 0.03s 25412KB
stdin
Standard input is empty
stdout
m: Matrix =
0  1  2  3  4
2  3  4  5  6
4  5  6  7  8

m.Transpose() =
0  2  4
1  3  5
2  4  6
3  5  7
4  6  8

(SquareMatrix) m =
SquareMatrix не может быть размера 3×5.

sm: SquareMatrix =
5  6  7  8  9
4  5  6  7  8
3  4  5  6  7
2  3  4  5  6
1  2  3  4  5

sm.Transpose() =
5  4  3  2  1
6  5  4  3  2
7  6  5  4  3
8  7  6  5  4
9  8  7  6  5

(Matrix) sm =
5  6  7  8  9
4  5  6  7  8
3  4  5  6  7
2  3  4  5  6
1  2  3  4  5

(SquareMatrix) (Matrix) sm =
5  6  7  8  9
4  5  6  7  8
3  4  5  6  7
2  3  4  5  6
1  2  3  4  5