{"id":2206,"date":"2020-01-04T08:06:12","date_gmt":"2020-01-04T08:06:12","guid":{"rendered":"https:\/\/www.askpython.com\/?p=2206"},"modified":"2022-08-06T13:09:35","modified_gmt":"2022-08-06T13:09:35","slug":"python-complex-numbers","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/python-complex-numbers","title":{"rendered":"Python Complex Numbers"},"content":{"rendered":"\n<p>A Complex Number is any number of the form <code>a + bj<\/code>, where <code>a<\/code> and <code>b<\/code> are real numbers, and <code>j*j<\/code> = -1.<\/p>\n\n\n\n<p>In Python, there are multiple ways to create such a Complex Number.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Create a Complex Number in Python<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>We can directly use the syntax <code>a + bj<\/code> to create a Complex Number.<\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; a = 4 + 3j\n&gt;&gt;&gt; print(a)\n(4+3j)\n&gt;&gt;&gt; print(type(a))\n&lt;class &#039;complex&#039;&gt;\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\"><li>We can also use the <code>complex<\/code> Class to create a complex number<\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; a = complex(4, 3)\n&gt;&gt;&gt; print(type(a))\n&lt;class &#039;complex&#039;&gt;\n&gt;&gt;&gt; print(a)\n(4+3j)\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Real and Imaginary Parts in Complex Number<\/h3>\n\n\n\n<p>Every complex number (<code>a + bj<\/code>) has a real part (<code>a<\/code>), and an imaginary part (<code>b<\/code>).<\/p>\n\n\n\n<p>To get the real part, use <code>number.real<\/code>, and to get the imaginary part, use <code>number.imag<\/code>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; a\n(4+3j)\n&gt;&gt;&gt; a.real\n4.0\n&gt;&gt;&gt; a.imag\n3.0\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Conjugate of a Complex Number<\/h3>\n\n\n\n<p>The conjugate of a complex number <code>a + bj<\/code> is defined as <code>a - bj<\/code>. We can also use <code>number.conjugate()<\/code>method to get the conjugate.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; a\n(4 + 3j)\n&gt;&gt;&gt; a.conjugate()\n(4-3j)\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Arithmetic Operations on Complex Numbers<\/h2>\n\n\n\n<p>Similar to real numbers, Complex Numbers also can be added, subtracted, multiplied and divided. Let us look at how we could do this in Python.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\na = 1 + 2j\nb = 2 + 4j\nprint(&#039;Addition =&#039;, a + b)\nprint(&#039;Subtraction =&#039;, a - b)\nprint(&#039;Multiplication =&#039;, a * b)\nprint(&#039;Division =&#039;, a \/ b)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nAddition = (3+6j)\nSubtraction = (-1-2j)\nMultiplication = (-6+8j)\nDivision = (2+0j)\n<\/pre><\/div>\n\n\n<p><strong>NOTE<\/strong>: Unlike real numbers, we cannot compare two complex numbers. We can only compare their real and imaginary parts individually, since they are real numbers. The below snippet proves this.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; a\n(4+3j)\n&gt;&gt;&gt; b\n(4+6j)\n&gt;&gt;&gt; a &lt; b\nTraceback (most recent call last):\n  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;\nTypeError: &#039;&lt;&#039; not supported between instances of &#039;complex&#039; and &#039;complex&#039;\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Phase (Argument) of a Complex Number<\/h2>\n\n\n\n<p>We can represent a complex number as a vector consisting of two components in a plane consisting of the <code>real<\/code> and <code>imaginary<\/code> axes. Therefore, the two components of the vector are it&#8217;s real part and it&#8217;s imaginary part.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1280\" height=\"1378\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/complex-number-vector-1.png\" alt=\"Complex Number Vector 1\" class=\"wp-image-2230\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/complex-number-vector-1.png 1280w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/complex-number-vector-1-279x300.png 279w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/complex-number-vector-1-951x1024.png 951w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/complex-number-vector-1-768x827.png 768w\" sizes=\"auto, (max-width: 1280px) 100vw, 1280px\" \/><figcaption>Complex Number Vector<\/figcaption><\/figure><\/div>\n\n\n\n<p>The angle between the vector and the real axis is defined as the <code>argument<\/code> or <code>phase<\/code> of a Complex Number.<\/p>\n\n\n\n<p>It is formally defined as :<\/p>\n\n\n\n<p><em><strong>phase(number) = arctan(imaginary_part \/ real_part)<\/strong><\/em><\/p>\n\n\n\n<p>where the arctan function is the tan inverse mathematical function.<\/p>\n\n\n\n<p>In Python, we can get the phase of a Complex Number using the <code>cmath<\/code> module for complex numbers. We can also use the <code>math.arctan<\/code> function and get the phase from it&#8217;s mathematical definition.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport cmath\nimport math\n\nnum = 4 + 3j\n\n# Using cmath module\np = cmath.phase(num)\nprint(&#039;cmath Module:&#039;, p)\n\n# Using math module\np = math.atan(num.imag\/num.real)\nprint(&#039;Math Module:&#039;, p)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ncmath Module: 0.6435011087932844\nMath Module: 0.6435011087932844\n<\/pre><\/div>\n\n\n<p>Note that this function returns the phase angle in <code>radians<\/code>, so if we need to convert to <code>degrees<\/code>, we can use another library like <code>numpy<\/code>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport cmath\nimport numpy as np\n\nnum = 4 + 3j\n\n# Using cmath module\np = cmath.phase(num)\nprint(&#039;cmath Module in Radians:&#039;, p)\nprint(&#039;Phase in Degrees:&#039;, np.degrees(p))\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ncmath Module in Radians: 0.6435011087932844\nPhase in Degrees: 36.86989764584402\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Rectangular and Polar Coordinates<\/h2>\n\n\n\n<p>A Complex Number can be written in Rectangular Coordinate or Polar Coordinate formats using the <code>cmath.rect()<\/code> and <code>cmath.polar()<\/code> functions.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; import cmath\n&gt;&gt;&gt; a = 3 + 4j\n&gt;&gt;&gt; polar_coordinates = cmath.polar(a)\n&gt;&gt;&gt; print(polar_coordinates)\n(5.0, 0.9272952180016122)\n\n&gt;&gt;&gt; modulus = abs(a)\n&gt;&gt;&gt; phase = cmath.phase(a)\n&gt;&gt;&gt; rect_coordinates = cmath.rect(modulus, phase)\n&gt;&gt;&gt; print(rect_coordinates)\n(3.0000000000000004+3.9999999999999996j)\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Constants in the cmath Module<\/h2>\n\n\n\n<p>There are special constants in the cmath module. Some of them are listed below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(&#039;\u03c0 =&#039;, cmath.pi)\nprint(&#039;e =&#039;, cmath.e)\nprint(&#039;tau =&#039;, cmath.tau)\nprint(&#039;Positive infinity =&#039;, cmath.inf)\nprint(&#039;Positive Complex infinity =&#039;, cmath.infj)\nprint(&#039;NaN =&#039;, cmath.nan)\nprint(&#039;NaN Complex =&#039;, cmath.nanj)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n\u03c0 = 3.141592653589793\ne = 2.718281828459045\ntau = 6.283185307179586\nPositive infinity = inf\nPositive Complex infinity = infj\nNaN = nan\nNaN Complex = nanj\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Trigonometric Functions<\/h2>\n\n\n\n<p>Trigonometric functions for a complex number are also available in the <code>cmath<\/code> module.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport cmath\n\na = 3 + 4j\n\nprint(&#039;Sine:&#039;, cmath.sin(a))\nprint(&#039;Cosine:&#039;, cmath.cos(a))\nprint(&#039;Tangent:&#039;, cmath.tan(a))\n\nprint(&#039;ArcSin:&#039;, cmath.asin(a))\nprint(&#039;ArcCosine:&#039;, cmath.acos(a))\nprint(&#039;ArcTan:&#039;, cmath.atan(a))\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nSine: (3.853738037919377-27.016813258003936j)\nCosine: (-27.034945603074224-3.8511533348117775j)\nTangent: (-0.0001873462046294784+0.999355987381473j)\nArcSin: (0.6339838656391766+2.305509031243477j)\nArcCosine: (0.9368124611557198-2.305509031243477j)\nArcTan: (1.4483069952314644+0.15899719167999918j)\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Hyperbolic Functions<\/h2>\n\n\n\n<p>Similar to Trigonometric functions, Hyperbolic Functions for a complex number are also available in the <code>cmath<\/code> module.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport cmath\n\na = 3 + 4j\n\nprint(&#039;Hyperbolic Sine:&#039;, cmath.sinh(a))\nprint(&#039;Hyperbolic Cosine:&#039;, cmath.cosh(a))\nprint(&#039;Hyperbolic Tangent:&#039;, cmath.tanh(a))\n\nprint(&#039;Inverse Hyperbolic Sine:&#039;, cmath.asinh(a))\nprint(&#039;Inverse Hyperbolic Cosine:&#039;, cmath.acosh(a))\nprint(&#039;Inverse Hyperbolic Tangent:&#039;, cmath.atanh(a))\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nHyperbolic Sine: (-6.5481200409110025-7.61923172032141j)\nHyperbolic Cosine: (-6.580663040551157-7.581552742746545j)\nHyperbolic Tangent: (1.000709536067233+0.00490825806749606j)\nInverse Hyperbolic Sine: (2.2999140408792695+0.9176168533514787j)\nInverse Hyperbolic Cosine: (2.305509031243477+0.9368124611557198j)\nInverse Hyperbolic Tangent: (0.11750090731143388+1.4099210495965755j)\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Exponential and Logarithmic Functions<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport cmath\na = 3 + 4j\nprint(&#039;e^c =&#039;, cmath.exp(a))\nprint(&#039;log2(c) =&#039;, cmath.log(a, 2))\nprint(&#039;log10(c) =&#039;, cmath.log10(a))\nprint(&#039;sqrt(c) =&#039;, cmath.sqrt(a))\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ne^c = (-13.128783081462158-15.200784463067954j)\nlog2(c) = (2.321928094887362+1.3378042124509761j)\nlog10(c) = (0.6989700043360187+0.4027191962733731j)\nsqrt(c) = (2+1j)\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Miscellaneous Functions<\/h2>\n\n\n\n<p>There are some miscellaneous functions to check if a complex number is finite, infinite or <code>nan<\/code>. There is also a function to check if two complex numbers are close.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; print(cmath.isfinite(2 + 2j))\nTrue\n\n&gt;&gt;&gt; print(cmath.isfinite(cmath.inf + 2j))\nFalse\n\n&gt;&gt;&gt; print(cmath.isinf(2 + 2j))\nFalse\n\n&gt;&gt;&gt; print(cmath.isinf(cmath.inf + 2j))\nTrue\n\n&gt;&gt;&gt; print(cmath.isinf(cmath.nan + 2j))\nFalse\n\n&gt;&gt;&gt; print(cmath.isnan(2 + 2j))\nFalse\n\n&gt;&gt;&gt; print(cmath.isnan(cmath.inf + 2j))\nFalse\n\n&gt;&gt;&gt; print(cmath.isnan(cmath.nan + 2j))\nTrue\n\n&gt;&gt;&gt; print(cmath.isclose(2+2j, 2.01+1.9j, rel_tol=0.05))\nTrue\n\n&gt;&gt;&gt; print(cmath.isclose(2+2j, 2.01+1.9j, abs_tol=0.005))\nFalse\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>We learned about the Complex Numbers module, and various functions associated with the <code>cmath<\/code> module.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>JournaDev article on cmath module<\/li><li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Complex_number\" class=\"rank-math-link\" target=\"_blank\" rel=\"noopener\">Wikipedia article on Complex Numbers<\/a><\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n","protected":false},"excerpt":{"rendered":"<p>A Complex Number is any number of the form a + bj, where a and b are real numbers, and j*j = -1. In Python, there are multiple ways to create such a Complex Number. Create a Complex Number in Python We can directly use the syntax a + bj to create a Complex Number. [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2206","post","type-post","status-publish","format-standard","hentry","category-python"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/2206","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=2206"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/2206\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=2206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=2206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=2206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}