Advertisement
maxrusmos

Untitled

Apr 15th, 2020
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.29 KB | None | 0 0
  1. using System;
  2. using EventCustomOp;
  3.  
  4. namespace ComplexOp {
  5. public delegate void Handler(EventCustom obj);
  6. public class ComplexNum: IComparable, IComparable<ComplexNum>{
  7. public static event Handler DivisionZero;
  8.  
  9. public int CompareTo(ComplexNum A) {
  10. var ca1 = ComplexAbs(this);
  11. var ca2 = ComplexAbs(A);
  12. if (ca1 > ca2) {
  13. return 1;
  14. } else if (ca1 < ca2){
  15. return -1;
  16. } else {
  17. return 0;
  18. }
  19. }
  20.  
  21. public int CompareTo(object obj) {
  22. if (!(obj is ComplexNum)) {
  23. throw new ArgumentException("Не могу сравнить значения", nameof(obj));
  24. }
  25. return CompareTo((ComplexNum)obj);
  26. }
  27.  
  28. private const double Eps = 1E-10;
  29. private double _real, _imaginary;
  30.  
  31. public ComplexNum() {
  32. _real = 0.0;
  33. _imaginary = 0.0;
  34. }
  35.  
  36. public ComplexNum(double real, double imaginary) {
  37. _real = real;
  38. _imaginary = imaginary;
  39. }
  40.  
  41. public double Real => _real;
  42. public double Imaginary => _imaginary;
  43.  
  44.  
  45. public ComplexNum(int bottomBorder, int topBorder) {
  46. if (bottomBorder > topBorder) {
  47. (bottomBorder, topBorder) = (topBorder, bottomBorder);
  48. }
  49. Random rnd = new Random();
  50. _real = bottomBorder + (rnd.NextDouble() * (topBorder - bottomBorder));
  51. _imaginary = bottomBorder + (rnd.NextDouble() * (topBorder - bottomBorder));
  52. }
  53.  
  54. //сумма
  55. public static ComplexNum ComplexSum(ComplexNum A, ComplexNum B) {
  56. var resultComplex = new ComplexNum(A._real + B._real, A._imaginary + B._imaginary);
  57. return resultComplex;
  58. }
  59.  
  60. public static ComplexNum operator +(ComplexNum A, ComplexNum B) {
  61. return ComplexSum(A, B);
  62. }
  63.  
  64. //разность
  65. public static ComplexNum ComplexDifference(ComplexNum A, ComplexNum B) {
  66. var resultComplex = new ComplexNum(A._real - B._real, A._imaginary - B._imaginary);
  67. return resultComplex;
  68. }
  69.  
  70. public static ComplexNum operator -(ComplexNum A, ComplexNum B) {
  71. return ComplexDifference(A, B);
  72. }
  73.  
  74. //произведение
  75. public static ComplexNum ComplexComposition(ComplexNum A, ComplexNum B) {
  76. var resultComplex = new ComplexNum();
  77. resultComplex._real = A._real * B._real - A._imaginary * B._imaginary;
  78. resultComplex._imaginary = A._real * B._imaginary + A._imaginary * B._real;
  79. return resultComplex;
  80. }
  81.  
  82. public static ComplexNum operator *(ComplexNum A, ComplexNum B) {
  83. return ComplexComposition(A, B);
  84. }
  85.  
  86. //произведение с действительным числом
  87. public static ComplexNum ComplexCompositionNum(ComplexNum A, dynamic num) {
  88. var resultComplex = new ComplexNum();
  89. resultComplex._real = A._real * num;
  90. resultComplex._imaginary = A._imaginary;
  91. return resultComplex;
  92. }
  93.  
  94. public static ComplexNum operator *(ComplexNum A, dynamic num) {
  95. return ComplexCompositionNum(A, num);
  96. }
  97.  
  98. //частное
  99. public static ComplexNum ComplexDivision(ComplexNum A, ComplexNum B) {
  100. var resultComplex = new ComplexNum();
  101. if (Math.Abs(B._real * B._real + B._imaginary * B._imaginary) < Eps || Math.Abs(B._real * B._real + B._imaginary * B._imaginary) < Eps) {
  102. var obj = new EventCustom(A, B);
  103. DivisionZero(obj);
  104. }
  105. resultComplex._real = (A._real * B._real + A._imaginary * B._imaginary) / (B._real * B._real + B._imaginary * B._imaginary);
  106. resultComplex._imaginary = (B._real * A._imaginary - A._real * B._imaginary) / (B._real * B._real + B._imaginary * B._imaginary);
  107. return resultComplex;
  108. }
  109.  
  110. public static ComplexNum operator /(ComplexNum A, ComplexNum B) {
  111. return ComplexDivision(A, B);
  112. }
  113.  
  114. //модуль
  115. public static double ComplexAbs(ComplexNum A) {
  116. return Math.Sqrt(Math.Pow(A._real, 2) + Math.Pow(A._imaginary, 2));
  117. }
  118.  
  119. //степень
  120. public static ComplexNum ComplexPow(ComplexNum A, int power) {
  121. if (power < 0) {
  122. throw new ArgumentException("Отрицательная степень", nameof(power));
  123. }
  124. double fi_cos = Math.Acos(A._real / ComplexAbs(A));
  125. double fi_sin = Math.Asin(A._imaginary / ComplexAbs(A));
  126. var resultComplex = new ComplexNum(Math.Pow(ComplexAbs(A), power) * Math.Cos(power * fi_cos),
  127. Math.Pow(ComplexAbs(A), power) * Math.Sin(power * fi_sin));
  128. return resultComplex;
  129. }
  130.  
  131. //аргумент
  132. public static double ComplexArgument(ComplexNum A) {
  133. var arg = 0.0;
  134. if (A._real > 0 && A._imaginary >= 0) {
  135. arg = Math.Atan(A._imaginary / A._real);
  136. }
  137. if (A._real < 0 && A._imaginary >= 0) {
  138. arg = Math.Atan(A._imaginary / A._real) + Math.PI;
  139. }
  140. if (A._real < 0 && A._imaginary < 0) {
  141. arg = Math.Atan(A._imaginary / A._real) - Math.PI;
  142. }
  143. if (A._real == 0 && A._imaginary > 0) {
  144. arg = Math.PI / 2;
  145. }
  146. if (A._real == 0 && A._imaginary < 0) {
  147. arg = Math.PI * 3 / 2;
  148. }
  149. return arg;
  150. }
  151.  
  152. //корень
  153. public static ComplexNum[] ComplexSqrtN(ComplexNum A, int sqrtValue) {
  154. if (sqrtValue < 0) {
  155. throw new ArgumentException("Корень отрицательной степени", nameof(sqrtValue));
  156. }
  157. var resulComplexArr = new ComplexNum[sqrtValue];
  158. var complexTmpAbs = new ComplexNum(Math.Pow(ComplexAbs(A), (1.0 / sqrtValue)), 0.0);
  159. for (int k = 0; k < sqrtValue; k++) {
  160. var complexTrigonometryNum = new ComplexNum(Math.Cos((ComplexArgument(A) + 2 * Math.PI * k) / Convert.ToDouble(sqrtValue)),
  161. Math.Sin((ComplexArgument(A) + 2 * Math.PI * k) / Convert.ToDouble(sqrtValue)));
  162. resulComplexArr[k] = complexTmpAbs * complexTrigonometryNum;
  163. if (Math.Abs(resulComplexArr[k]._real) < Eps) {
  164. resulComplexArr[k]._real = 0.0;
  165. }
  166. if (Math.Abs(resulComplexArr[k]._imaginary) < Eps) {
  167. resulComplexArr[k]._imaginary = 0.0;
  168. }
  169. }
  170. return resulComplexArr;
  171. }
  172.  
  173. //вывод
  174. public override string ToString() {
  175. return "Complex(" + _real + " " + _imaginary + ")";
  176. }
  177. }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement