Комплексное число. Возведение в степень и извлечение корня - C#

Узнай цену своей работы

Формулировка задачи:

Всем привет! Проблема в следующем: не могу понять как в C# правильно возводить комплексное число в степень, а также брать от него произвольный корень. Прошу строго не судить и помочь разобраться с вопросом. ( реализация идет через классы)
Листинг программы
  1. namespace ConsoleApplication3
  2. {
  3. class Program
  4. {
  5. private static Complex z;
  6. private static Complex z1;
  7.  
  8. protected static Double ReadRealNumber(String message)
  9. {
  10. Double result;
  11. do
  12. {
  13. Console.Write(message);
  14. } while (!Double.TryParse(Console.ReadLine(), out result));
  15. return result;
  16. }
  17.  
  18. protected static Complex ReadComplexNumber(String message)
  19. {
  20. Console.WriteLine(message);
  21. var real = ReadRealNumber("Действительная: ");
  22. var imaginer = ReadRealNumber("Мнимая: ");
  23. return new Complex(real, imaginer);
  24. }
  25.  
  26. protected static Double ReadN(String message)
  27. {
  28. Double result;
  29. do
  30. {
  31. Console.Write(message);
  32. } while (!Double.TryParse(Console.ReadLine(), out result));
  33. return result;
  34. }
  35.  
  36. static void Main(string[] args)
  37. {
  38. var firstNumber = ReadComplexNumber("Введите первое комплексное число (z1): ");
  39. var secondNumber = ReadComplexNumber("Введите второе комплексное число (z2): ");
  40. var degree = ReadN("Введите степень для первого комплексного числа (n): ");
  41. var additionalResult = firstNumber + secondNumber;
  42. Console.WriteLine("z1 + z2 = {0}", additionalResult);
  43. var additionalResult1 = firstNumber - secondNumber;
  44. Console.WriteLine("z1 - z2 = {0}", additionalResult1);
  45. var additionalResult2 = firstNumber * secondNumber;
  46. Console.WriteLine("z1*z2 = {0}", additionalResult2);
  47. var additionalResult3 = firstNumber / secondNumber;
  48. Console.WriteLine("z1/z2 = {0}", additionalResult3);
  49. var additionalResult4 = Math.Pow(firstNumber, degree);
  50. Console.WriteLine("z^n = {0}", additionalResult4);
  51. // z = new Complex();
  52. //z1 = new Complex();
  53. Console.ReadKey();
Листинг программы
  1. namespace ConsoleApplication3
  2. {
  3. class Complex : ICloneable
  4. {
  5. public Double Real;
  6. public Double Image;
  7. public Double Z;
  8. public Double n;
  9. public Complex(Double re = 0, Double im = 0)
  10. {
  11. Real = re;
  12. Image = im;
  13. }
  14. public static Complex Sum(Complex arg0, Complex arg1)
  15. {
  16. //return new Complex
  17. //{
  18. // Real = arg0.Real + arg1.Real,
  19. // Image = arg0.Image + arg1.Image
  20. //};
  21. return new Complex(
  22. re: arg0.Real + arg1.Real,
  23. im: arg0.Image + arg1.Image
  24. );
  25. }
  26. public static Complex operator +(Double arg0, Complex arg1)
  27. {
  28. return new Complex(arg0) + arg1;
  29. }
  30. public static Complex operator +(Complex arg0, Double arg1)
  31. {
  32. return arg0 + new Complex(arg1);
  33. }
  34. public static Complex operator +(Complex arg0, Complex arg1)
  35. {
  36. return new Complex
  37. {
  38. Real = arg0.Real + arg1.Real,
  39. Image = arg0.Image + arg1.Image
  40. };
  41. }
  42. public static Complex operator-(Complex arg0, Complex arg1)
  43. {
  44. return new Complex
  45. {
  46. Real = arg0.Real - arg1.Real,
  47. Image = arg0.Image - arg1.Image
  48. };
  49. }
  50. public static Complex operator *(Complex arg0, Complex arg1)
  51. {
  52. return new Complex
  53. {
  54. Real = arg0.Real * arg1.Real - arg0.Image * arg1.Image,
  55. Image = arg1.Real * arg0.Image + arg1.Image * arg0.Real
  56. };
  57. }
  58. public static Complex operator /(Complex arg0, Complex arg1)
  59. {
  60. return new Complex
  61. {
  62. Real = (arg0.Real*arg1.Real + arg0.Image*arg1.Image)/arg1.Real*arg1.Real + arg1.Image*arg1.Image,
  63. Image = (arg1.Real*arg0.Image - arg1.Image*arg0.Real)/arg1.Real*arg1.Real + arg1.Image*arg1.Image
  64. };
  65. }
  66. public static Complex operator ^ (Complex arg0, Double n)
  67. {
  68. return new Complex
  69. {
  70. Z = Math.Pow((arg0.Real+arg0.Image),n)
  71. };
  72. }
  73. public object Clone()
  74. {
  75. return new Complex { Real = Real, Image = Image };
  76. }
  77. public override string ToString()
  78. {
  79. return String.Format("{0}{1}{2}", Real, (Image < 0 ? " - i" : " + i"), Math.Abs(Image));
  80. }
  81. }
  82. }

Решение задачи: «Комплексное число. Возведение в степень и извлечение корня»

textual
Листинг программы
  1.  public class Complex
  2.     {
  3.         double re;
  4.         double im;
  5.  
  6.         /// <summary>
  7.         /// Contains the real part of a complex number.
  8.         /// </summary>
  9.         public double Re
  10.         {
  11.             get { return re; }
  12.             set { re = value; }
  13.         }
  14.  
  15.         /// <summary>
  16.         /// Contains the imaginary part of a complex number.
  17.         /// </summary>
  18.         public double Im
  19.         {
  20.             get { return im; }
  21.             set { im = value; }
  22.         }
  23.  
  24.         /// <summary>
  25.         /// Imaginary unit.
  26.         /// </summary>
  27.         public static Complex I
  28.         {
  29.             get
  30.             {
  31.                 return new Complex(0, 1);
  32.             }
  33.         }
  34.        
  35.         /// <summary>
  36.         /// Complex number zero.
  37.         /// </summary>
  38.         public static Complex Zero
  39.         {
  40.             get
  41.             {
  42.                 return new Complex(0, 0);
  43.             }
  44.         }
  45.  
  46.         /// <summary>
  47.         /// Complex number one.
  48.         /// </summary>
  49.         public static Complex One
  50.         {
  51.             get
  52.             {
  53.                 return new Complex(1, 0);
  54.             }
  55.         }
  56.  
  57.         #region Constructors
  58.  
  59.         /// <summary>
  60.         /// Inits complex number as (0, 0).
  61.         /// </summary>
  62.         public Complex()
  63.         {
  64.             Re = 0;
  65.             Im = 0;
  66.         }
  67.  
  68.         /// <summary>
  69.         /// Inits complex number with imaginary part = 0.
  70.         /// </summary>
  71.         /// <param name="real_part"></param>
  72.         public Complex(double real_part)
  73.         {
  74.             Re = real_part;
  75.             Im = 0;
  76.         }
  77.  
  78.         /// <summary>
  79.         /// Inits complex number.
  80.         /// </summary>
  81.         /// <param name="imaginary_part"></param>
  82.         /// <param name="real_part"></param>
  83.         public Complex(double real_part, double imaginary_part)
  84.         {
  85.             Re = real_part;
  86.             Im = imaginary_part;
  87.         }
  88.  
  89.         /// <summary>
  90.         /// Inits complex number from string like "a+bi".
  91.         /// </summary>
  92.         /// <param name="s"></param>
  93.         public Complex(string s)
  94.         {
  95.             throw new NotImplementedException();
  96.         }
  97.  
  98.         public static Match Test(string s)
  99.         {
  100.            
  101.             string dp = "([0-9]+[.]?[0-9]*|[.][0-9]+)";
  102.             string dm = "[-]?" + dp;
  103.  
  104.             Regex r = new Regex("^(?<RePart>(" + dm + ")[-+](?<ImPart>(" + dp + "))[i])$");
  105.  
  106.             return r.Match(s);
  107.         }
  108.  
  109.         #endregion
  110.  
  111.         #region Operators
  112.  
  113.         public static Complex operator +(Complex a, Complex b)
  114.         {
  115.             //if (a == null) return b;
  116.             //else if (b == null) return a;
  117.             //else
  118.                 return new Complex(a.Re + b.Re, a.Im + b.Im);
  119.         }
  120.  
  121.         public static Complex operator +(Complex a, double b)
  122.         {
  123.             return new Complex(a.Re + b, a.Im);
  124.         }
  125.  
  126.         public static Complex operator +(double a, Complex b)
  127.         {
  128.             return new Complex(a + b.Re, b.Im);
  129.         }
  130.  
  131.         public static Complex operator -(Complex a, Complex b)
  132.         {
  133.             return new Complex(a.Re - b.Re, a.Im - b.Im);
  134.         }
  135.  
  136.         public static Complex operator -(Complex a, double b)
  137.         {
  138.             return new Complex(a.Re - b, a.Im);
  139.         }
  140.  
  141.         public static Complex operator -(double a, Complex b)
  142.         {
  143.             return new Complex(a - b.Re, -b.Im);
  144.         }
  145.  
  146.         public static Complex operator -(Complex a)
  147.         {
  148.             return new Complex(-a.Re, -a.Im);
  149.         }
  150.  
  151.         public static Complex operator *(Complex a, Complex b)
  152.         {
  153.             return new Complex(a.Re * b.Re - a.Im * b.Im,
  154.                 a.Im * b.Re + a.Re * b.Im);
  155.         }
  156.  
  157.         public static Complex operator *(Complex a, double d)
  158.         {
  159.             return new Complex(d * a.Re, d * a.Im);
  160.         }
  161.  
  162.         public static Complex operator *(double d, Complex a)
  163.         {
  164.             return new Complex(d * a.Re, d * a.Im);
  165.         }          
  166.  
  167.         public static Complex operator /(Complex a, Complex b)
  168.         {
  169.             return a * Conj(b) * (1 / (Abs(b) * Abs(b)));
  170.         }
  171.  
  172.         public static Complex operator /(Complex a, double b)
  173.         {
  174.             return a * (1 / b);
  175.         }
  176.  
  177.         public static Complex operator /(double a, Complex b)
  178.         {
  179.             return a * Conj(b) * (1 / (Abs(b) * Abs(b)));
  180.         }
  181.  
  182.         public static bool operator ==(Complex a, Complex b)
  183.         {
  184.             return (a.Re == b.Re && a.Im == b.Im);
  185.         }
  186.  
  187.         public static bool operator ==(Complex a, double b)
  188.         {
  189.             return a == new Complex(b);
  190.         }
  191.  
  192.         public static bool operator ==(double a, Complex b)
  193.         {
  194.             return new Complex(a) == b;
  195.         }
  196.  
  197.         public static bool operator !=(Complex a, Complex b)
  198.         {
  199.             return !(a == b);
  200.         }
  201.  
  202.         public static bool operator !=(Complex a, double b)
  203.         {
  204.             return !(a == b);
  205.         }
  206.  
  207.         public static bool operator !=(double a, Complex b)
  208.         {
  209.             return !(a == b);
  210.         }
  211.  
  212.         #endregion
  213.  
  214.         #region Static funcs & overrides
  215.  
  216.         /// <summary>
  217.         /// Calcs the absolute value of a complex number.
  218.         /// </summary>
  219.         /// <param name="a"></param>
  220.         /// <returns></returns>
  221.         public static double Abs(Complex a)
  222.         {
  223.             return Math.Sqrt(a.Im * a.Im + a.Re * a.Re);
  224.         }
  225.  
  226.         /// <summary>
  227.         /// Inverts a.
  228.         /// </summary>
  229.         /// <param name="a"></param>
  230.         /// <returns></returns>
  231.         public static Complex Inv(Complex a)
  232.         {
  233.             return new Complex(a.Re / (a.Re * a.Re + a.Im * a.Im),
  234.                 -a.Im / (a.Re * a.Re + a.Im * a.Im));
  235.         }
  236.  
  237.         /// <summary>
  238.         /// Tangent of a.
  239.         /// </summary>
  240.         /// <param name="a"></param>
  241.         /// <returns></returns>
  242.         public static Complex Tan(Complex a)
  243.         {
  244.             return Sin(a) / Cos(a);
  245.         }
  246.  
  247.         /// <summary>
  248.         /// Hyperbolic cosine of a.
  249.         /// </summary>
  250.         /// <param name="a"></param>
  251.         /// <returns></returns>
  252.         public static Complex Cosh(Complex a)
  253.         {
  254.             return (Exp(a) + Exp(-a)) / 2;
  255.         }
  256.  
  257.         /// <summary>
  258.         /// Hyperbolic sine of a.
  259.         /// </summary>
  260.         /// <param name="a"></param>
  261.         /// <returns></returns>
  262.         public static Complex Sinh(Complex a)
  263.         {
  264.             return (Exp(a) - Exp(-a)) / 2;
  265.         }
  266.  
  267.         /// <summary>
  268.         /// Hyperbolic tangent of a.
  269.         /// </summary>
  270.         /// <param name="a"></param>
  271.         /// <returns></returns>
  272.         public static Complex Tanh(Complex a)
  273.         {
  274.             return (Exp(2 * a) - 1) / (Exp(2 * a) + 1);
  275.         }
  276.  
  277.         /// <summary>
  278.         /// Hyperbolic cotangent of a.
  279.         /// </summary>
  280.         /// <param name="a"></param>
  281.         /// <returns></returns>
  282.         public static Complex Coth(Complex a)
  283.         {
  284.             return (Exp(2 * a) + 1) / (Exp(2 * a) - 1);
  285.         }
  286.  
  287.         /// <summary>
  288.         /// Hyperbolic secant of a.
  289.         /// </summary>
  290.         /// <param name="a"></param>
  291.         /// <returns></returns>
  292.         public static Complex Sech(Complex a)
  293.         {
  294.             return Inv(Cosh(a));
  295.         }
  296.  
  297.         /// <summary>
  298.         /// Hyperbolic cosecant of a.
  299.         /// </summary>
  300.         /// <param name="a"></param>
  301.         /// <returns></returns>
  302.         public static Complex Csch(Complex a)
  303.         {
  304.             return Inv(Sinh(a));
  305.         }
  306.  
  307.         /// <summary>
  308.         /// Cotangent of a.
  309.         /// </summary>
  310.         /// <param name="a"></param>
  311.         /// <returns></returns>
  312.         public static Complex Cot(Complex a)
  313.         {
  314.             return Cos(a) / Sin(a);
  315.         }
  316.  
  317.         /// <summary>
  318.         /// Computes the conjugation of a complex number.
  319.         /// </summary>
  320.         /// <param name="a"></param>
  321.         /// <returns></returns>
  322.         public static Complex Conj(Complex a)
  323.         {
  324.             return new Complex(a.Re, -a.Im);
  325.         }
  326.  
  327.         /// <summary>
  328.         /// Complex square root.
  329.         /// </summary>
  330.         /// <param name="d"></param>
  331.         /// <returns></returns>
  332.         public static Complex Sqrt(double d)
  333.         {
  334.             if (d >= 0)
  335.                 return new Complex(Math.Sqrt(d));
  336.             else
  337.                 return new Complex(0, Math.Sqrt(-d));
  338.         }
  339.  
  340.         /// <summary>
  341.         /// Complex square root.
  342.         /// </summary>
  343.         /// <param name="a"></param>
  344.         /// <returns></returns>  
  345.         public static Complex Sqrt(Complex a)
  346.         {
  347.             return Pow(a, .5);
  348.         }
  349.  
  350.         /// <summary>
  351.         /// Complex exponential function.
  352.         /// </summary>
  353.         /// <param name="a"></param>
  354.         /// <returns></returns>
  355.         public static Complex Exp(Complex a)
  356.         {
  357.             return new Complex(Math.Exp(a.Re) * Math.Cos(a.Im), Math.Exp(a.Re) * Math.Sin(a.Im));
  358.         }
  359.  
  360.         /// <summary>
  361.         /// Main value of the complex logarithm.
  362.         /// </summary>
  363.         /// <param name="a"></param>
  364.         /// <returns></returns>
  365.         public static Complex Log(Complex a)
  366.         {
  367.            // Log[|w|]+I*(Arg[w]+2*Pi*k)            
  368.            
  369.             return new Complex(Math.Log(Abs(a)), Arg(a));
  370.         }
  371.  
  372.         /// <summary>
  373.         /// Argument of the complex number.
  374.         /// </summary>
  375.         /// <param name="a"></param>
  376.         /// <returns></returns>
  377.         public static double Arg(Complex a)
  378.         {
  379.             if (a.Re < 0)
  380.             {
  381.                 if (a.Im < 0)
  382.                     return Math.Atan(a.Im / a.Re) - Math.PI;
  383.                 else
  384.                     return Math.PI - Math.Atan(-a.Im / a.Re);
  385.             }
  386.             else
  387.                 return Math.Atan(a.Im / a.Re);
  388.  
  389.         }
  390.  
  391.         /// <summary>
  392.         /// Complex cosine.
  393.         /// </summary>
  394.         /// <param name="a"></param>
  395.         /// <returns></returns>
  396.         public static Complex Cos(Complex a)
  397.         {
  398.             return .5 * (Exp(Complex.I * a) + Exp(-Complex.I * a));
  399.         }
  400.  
  401.         /// <summary>
  402.         /// Complex sine.
  403.         /// </summary>
  404.         /// <param name="a"></param>
  405.         /// <returns></returns>
  406.         public static Complex Sin(Complex a)
  407.         {
  408.             return (Exp(Complex.I * a) - Exp(-Complex.I * a)) / (2*Complex.I);
  409.         }
  410.  
  411.         public static Complex Pow(Complex a, Complex b)
  412.         {
  413.             return Exp(b * Log(a));
  414.         }
  415.  
  416.         public static Complex Pow(double a, Complex b)
  417.         {
  418.             return Exp(b * Math.Log(a));
  419.         }
  420.  
  421.         public static Complex Pow(Complex a, double b)
  422.         {
  423.             return Exp(b * Log(a));
  424.         }
  425.  
  426.         public override string ToString()
  427.         {
  428.             if (this == Complex.Zero) return "0";
  429.  
  430.             string re, im, sign;
  431.  
  432.             if (this.Im < 0)
  433.             {
  434.                 if (this.Re == 0)
  435.                     sign = "-";
  436.                 else
  437.                     sign = " - ";
  438.             }
  439.             else if (this.Im > 0 && this.Re != 0) sign = " + ";
  440.             else sign = "";
  441.  
  442.             if (this.Re == 0) re = "";
  443.             else re = this.Re.ToString();            
  444.  
  445.             if (this.Im == 0) im = "";
  446.             else if (this.Im == -1 || this.Im == 1) im = "i";            
  447.             else im = Math.Abs(this.Im).ToString() + "i";
  448.  
  449.             return re + sign + im;
  450.         }
  451.  
  452.         public string ToString(string format)
  453.         {
  454.             if (this == Complex.Zero) return "0";
  455.             else if (double.IsInfinity(this.Re) || double.IsInfinity(this.Im)) return "oo";
  456.             else if (double.IsNaN(this.Re) || double.IsNaN(this.Im)) return "?";
  457.  
  458.             string re, im, sign;
  459.  
  460.             if (this.Im < 0)
  461.             {
  462.                 if (this.Re == 0)
  463.                     sign = "-";
  464.                 else
  465.                     sign = " - ";
  466.             }
  467.             else if (this.Im > 0 && this.Re != 0) sign = " + ";
  468.             else sign = "";
  469.  
  470.             if (this.Re == 0) re = "";
  471.             else re = this.Re.ToString(format);
  472.  
  473.             if (this.Im == 0) im = "";
  474.             else if (this.Im == -1 || this.Im == 1) im = "i";
  475.             else im = Math.Abs(this.Im).ToString(format) + "i";
  476.  
  477.             return re + sign + im;
  478.         }
  479.  
  480.         public override bool Equals(object obj)
  481.         {
  482.             return obj.ToString() == this.ToString();
  483.         }
  484.  
  485.         public override int GetHashCode()
  486.         {
  487.             return -1;
  488.         }
  489.  
  490.         #endregion
  491.  
  492.         #region Dynamics
  493.  
  494.         public bool IsReal()
  495.         {
  496.             return (this.Im == 0);
  497.         }
  498.  
  499.         public bool IsImaginary()
  500.         {
  501.             return (this.Re == 0);
  502.         }
  503.         #endregion
  504.     }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

11   голосов , оценка 3.455 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы