Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Cmath Module in Python

Cmath Module in Python

? cmath Module in Python

The cmath module in Python provides mathematical functions for working with complex numbers. Unlike the math module, which is designed for real numbers, cmath supports complex numbers and their operations.


? Complex Numbers in Python

A complex number is a number in the form of a + bi, where:

  • a is the real part

  • b is the imaginary part (and i is the imaginary unit).

Python supports complex numbers natively using the j or J suffix to denote the imaginary part.

z = 3 + 4j  # Complex numberprint(type(z))  # <class 'complex'>

? Functions in cmath Module

1?? cmath.sqrt(x) - Square Root

Returns the square root of x, where x can be a complex number.

import cmathz = 4 + 9jresult = cmath.sqrt(z)print(result)  # Output: (2.0+2.0j)

2?? cmath.phase(z) - Phase (Angle) of a Complex Number

Returns the phase (angle) of the complex number z in radians.

z = 1 + 1jphase = cmath.phase(z)print(phase)  # Output: 0.7853981633974483 (? ?/4 radians)

3?? cmath.polar(z) - Polar Coordinates

Converts a complex number z to polar coordinates, returning a tuple (r, ?) where r is the magnitude (absolute value) and ? is the phase.

z = 3 + 4jpolar = cmath.polar(z)print(polar)  # Output: (5.0, 0.9272952180016122) => (magnitude, phase)

4?? cmath.rect(r, phi) - Rectangular Coordinates

Converts polar coordinates (r, ?) back to rectangular form (complex number).

r = 5phi = 0.9272952180016122  # Phase anglerect = cmath.rect(r, phi)print(rect)  # Output: (3+4j)

5?? cmath.exp(z) - Exponential of a Complex Number

Returns e^z, where z is a complex number.

z = 1 + 1jexp_result = cmath.exp(z)print(exp_result)  # Output: (1.4686939399158851+2.2873552871788423j)

6?? cmath.log(z, base) - Natural Logarithm of a Complex Number

Returns the natural logarithm (log base e) of z. You can also specify a different base.

z = 1 + 1jlog_result = cmath.log(z)print(log_result)  # Output: (0.34657359027997264+0.7853981633974483j)# Logarithm with a different base (e.g., base 10)log_base_10 = cmath.log(z, 10)print(log_base_10)  # Output: (0.15490196076090552+0.34113236659763993j)

7?? cmath.sin(z) - Sine of a Complex Number

Computes the sine of a complex number.

z = 1 + 1jsin_result = cmath.sin(z)print(sin_result)  # Output: (1.2984575804191915+0.6349592128240046j)

8?? cmath.cos(z) - Cosine of a Complex Number

Computes the cosine of a complex number.

z = 1 + 1jcos_result = cmath.cos(z)print(cos_result)  # Output: (1.2984575804191915-0.6349592128240046j)

9?? cmath.tan(z) - Tangent of a Complex Number

Computes the tangent of a complex number.

z = 1 + 1jtan_result = cmath.tan(z)print(tan_result)  # Output: (1.3333333333333333+0.0j)

? cmath.conj(z) - Conjugate of a Complex Number

Returns the complex conjugate of z.

z = 3 + 4jconj_result = cmath.conj(z)print(conj_result)  # Output: (3-4j)

1??1?? cmath.isclose(a, b) - Check if Two Complex Numbers are Close

Compares two complex numbers to see if they are approximately equal.

z1 = 1 + 1jz2 = 1 + 1.0000001jis_close = cmath.isclose(z1, z2)print(is_close)  # Output: True

? Summary of Common cmath Functions

  • sqrt(): Square root of a complex number.

  • phase(): Returns the phase (angle) of a complex number.

  • polar(): Converts a complex number to polar coordinates.

  • rect(): Converts polar coordinates back to rectangular (complex number).

  • exp(): Exponential of a complex number.

  • log(): Logarithm of a complex number.

  • sin(), cos(), tan(): Trigonometric functions for complex numbers.

  • conj(): Complex conjugate.

  • isclose(): Check if two complex numbers are close to each other.


Let me know if you need further examples or explanations! ?

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql