Python cmath.phase Function

The cmath.phase function in Python’s cmath module returns the phase (or argument) of a complex number. The phase is the angle between the positive real axis and the line representing the complex number in the complex plane. The result is given in radians. This function is useful in various fields, including electrical engineering, signal processing, and complex analysis.

Table of Contents

  1. Introduction
  2. cmath.phase Function Syntax
  3. Examples
    • Basic Usage
    • Working with Real Numbers
    • Working with Complex Numbers
  4. Real-World Use Case
  5. Conclusion

Introduction

The cmath.phase function computes the phase angle of a complex number. The returned value is a float representing the angle in radians. The phase (or argument) of a complex number is an important concept in mathematics and engineering, especially when dealing with polar coordinates and phasors.

cmath.phase Function Syntax

Here is how you use the cmath.phase function:

import cmath

result = cmath.phase(x)

Parameters:

  • x: A complex number.

Returns:

  • A float representing the phase angle of x in radians.

Examples

Basic Usage

Calculate the phase angle of a complex number.

Example

import cmath

z = 1 + 2j
result = cmath.phase(z)
print(f"phase({z}) = {result} radians")

Output:

phase((1+2j)) = 1.1071487177940904 radians

Working with Real Numbers

Calculate the phase angle of real numbers. Note that the phase of a positive real number is 0 and the phase of a negative real number is π.

Example with Positive Real Number

import cmath

x = 2
result = cmath.phase(x)
print(f"phase({x}) = {result} radians")

Output:

phase(2) = 0.0 radians

Example with Negative Real Number

import cmath

x = -2
result = cmath.phase(x)
print(f"phase({x}) = {result} radians")

Output:

phase(-2) = 3.141592653589793 radians

Working with Complex Numbers

Calculate the phase angle of another complex number.

Example

import cmath

z = -1 - 1j
result = cmath.phase(z)
print(f"phase({z}) = {result} radians")

Output:

phase((-1-1j)) = -2.356194490192345 radians

Real-World Use Case

Signal Processing

In signal processing, you may need to compute the phase angle of a complex signal. The cmath.phase function can be used to determine this.

Example

import cmath

# Example signal value as a complex number
signal_value = 1 + 1j
phase_value = cmath.phase(signal_value)

print(f"The phase of the signal value {signal_value} is {phase_value} radians")

Output:

The phase of the signal value (1+1j) is 0.7853981633974483 radians

Conclusion

The cmath.phase function is used for calculating the phase angle of complex numbers in Python. It returns a float representing the angle in radians, which is useful in various fields, such as signal processing and electrical engineering. By understanding how to use this function, you can effectively work with phase angles and polar coordinates involving complex numbers.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top