{"id":25443,"date":"2021-11-10T14:59:51","date_gmt":"2021-11-10T09:29:51","guid":{"rendered":"https:\/\/python-programs.com\/?p=25443"},"modified":"2021-11-14T15:03:01","modified_gmt":"2021-11-14T09:33:01","slug":"python-cmath-rect-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-cmath-rect-method-with-examples\/","title":{"rendered":"Python cmath.rect() Method with Examples"},"content":{"rendered":"
cmath.rect() Method in Python:<\/strong><\/p>\n The cmath.rect() method converts polar coordinates to the complex number’s rectangular form. It generates a complex number that includes phase and modulus.<\/p>\n This method equals r * (math.cos(phi) + math.sin(phi)*1j).<\/p>\n The radius r is the vector’s length, and phi (phase angle) is the angle formed with the real axis.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n r:<\/strong> This is Required. The modulus of a complex number is represented by this symbol.<\/p>\n phi:<\/strong> This is Required. It represents a complex number’s phase.<\/p>\n Return Value:<\/strong><\/p>\n Returns a\u00a0complex value that represents a complex number in its rectangular form.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Similarly, try for the other examples<\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n cmath.rect() Method in Python: The cmath.rect() method converts polar coordinates to the complex number’s rectangular form. It generates a complex number that includes phase and modulus. This method equals r * (math.cos(phi) + math.sin(phi)*1j). The radius r is the vector’s length, and phi (phase angle) is the angle formed with the real axis. Syntax: cmath.rect(r, …<\/p>\ncmath.rect(r, phi)<\/pre>\n
Given modulus = 2.135667\r\nGiven phase = 6.1111116<\/pre>\n
The rectangular form of the complex number = (2.104127071172476-0.3656812864341552j)<\/pre>\n
Given modulus = 5\r\nGiven phase = 2<\/pre>\n
The rectangular form of the complex number = (-2.080734182735712+4.546487134128409j)<\/pre>\n
cmath.rect() Method with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Import cmath module(for complex number operations) using the import keyword.\r\nimport cmath\r\n# Give the modulus of a complex number (r) as static input and store it in a variable.\r\ngvn_moduls_val = 2.135667\r\n# Give the phase of a complex number (phi) as static input and store it\r\n# in another variable.\r\ngvn_phasee_val = 6.1111116\r\n# Pass the given modulus, phase of a complex number as the arguments to the\r\n# cmath.rect() method which returns a complex value that represents the\r\n# complex number in its rectangular form.\r\n# Store it in another variable.\r\nrslt = cmath.rect(gvn_moduls_val, gvn_phasee_val)\r\n# Print a complex value that represents the complex number in its rectangular form.\r\nprint(\"The rectangular form of the complex number = \", rslt)\r\n<\/pre>\n
The rectangular form of the complex number = (2.104127071172476-0.3656812864341552j)<\/pre>\n
import cmath\r\ngvn_moduls_val = 5\r\ngvn_phasee_val = 2\r\nrslt = cmath.rect(gvn_moduls_val, gvn_phasee_val)\r\nprint(\"The rectangular form of the complex number = \", rslt)\r\n<\/pre>\n
The rectangular form of the complex number = (-2.080734182735712+4.546487134128409j)<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Import cmath module(for complex number operations) using the import keyword.\r\nimport cmath\r\n# Give the modulus of a complex number (r) as user input using the float(input()) function\r\n# and store it in a variable.\r\ngvn_moduls_val = float(input(\"Enter some random number = \"))\r\n# Give the phase of a complex number (phi) as user input using the float(input()) function\r\n# and store it in another variable.\r\ngvn_phasee_val = float(input(\"Enter some random number = \"))\r\n# Pass the given modulus, phase of a complex number as the arguments to the\r\n# cmath.rect() method which returns a complex value that represents the\r\n# complex number in its rectangular form.\r\n# Store it in another variable.\r\nrslt = cmath.rect(gvn_moduls_val, gvn_phasee_val)\r\n# Print a complex value that represents the complex number in its rectangular form.\r\nprint(\"The rectangular form of the complex number = \", rslt)\r\n<\/pre>\n
Enter some random number = 1.56\r\nEnter some random number = 2.34546\r\nThe rectangular form of the complex number = (-1.0911821806705588+1.1148638699801174j)<\/pre>\n","protected":false},"excerpt":{"rendered":"