Python cmath.log() Method with Examples

cmath.log() Method in Python:

The logarithm of a complex value is returned by the cmath.log() method.

This method takes a single argument and returns the natural logarithm of that argument with base e.

This method takes two arguments and returns the logarithm of the first argument (x) multiplied by the base of the second argument (base).

Syntax:

cmath.log(x, base)

Parameters

x: This is Required. Specifies the value for which the logarithm should be calculated.

  • If the value is 0 or a negative number, a ValueError is returned.
  • If the value is not a number, a TypeError is returned.

base: This is Optional. The logarithmic base that should be used. ‘e’ is the default.

Return Value:

Returns a complex value that represents the natural logarithm of a number or the base logarithm of a number.

Changelog: The base parameter has been added.

Examples:

Example1:

Input:

Given Complex Number = 3+4j

Output:

The given complex number's (3+4j) logarithm value = 
(1.6094379124341003+0.9272952180016122j)

Example2:

Input:

Given realpart = 5
Given imaginary part = 2
Given base value = 4

Output:

The given complex number's (5+2j) logarithm value for the given base{ 4.0 } = 
(1.214495248781893+0.27447733164331733j)

Note: The above input format is for dynamic input.

cmath.log() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

1)With a single argument

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the complex number as static input and store it in a variable.
  • Pass the given complex number as an argument to the cmath.log() method that returns the given complex number’s logarithm value.
  • Store it in another variable.
  • Print the logarithm value of the given complex number.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the complex number as static input and store it in a variable.
complexnumb = 3+4j
# Pass the given complex number as an argument to the cmath.log() method that
# returns the the given complex number's logarithm value.
# Store it in another variable.
rslt = cmath.log(complexnumb)
# Print the logarithm value of the given complex number.
print("The given complex number's", complexnumb,
      "logarithm value = ")
print(rslt)

Output:

The given complex number's (3+4j) logarithm value = 
(1.6094379124341003+0.9272952180016122j)
2)With two arguments

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the complex number as static input and store it in a variable.
  • Give the base value as static input and store it in another variable.
  • Pass the given complex number, base value as the arguments to the cmath.log() method that returns the given complex number’s logarithm value for the given base.
  • Store it in another variable.
  • Print the logarithm value of the given complex number for the given base.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the complex number as static input and store it in a variable.
complexnumb = 4
# Give the base value as static input and store it in another variable.
gvn_basevalu = 8
# Pass the given complex number, base value as the arguments to the cmath.log()
# method that returns the given complex number's logarithm value for
# the given base.
# Store it in another variable.
rslt = cmath.log(complexnumb, gvn_basevalu)
# Print the logarithm value of the given complex number for the given base.
print("The given complex number's", complexnumb,
      "logarithm value for the given base{", gvn_basevalu, "} = ")
print(rslt)

Output:

The given complex number's 4 logarithm value for the given base{ 8 } = 
(0.6666666666666667+0j)

Method #2: Using Built-in Functions (User Input)

1)With a single argument

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the real part and imaginary part of the complex number as user input using map(), int(), split().
  • Store it in two variables.
  • Using a complex() function convert those two variables into a complex number and store them in a variable.
  • Pass the given complex number as an argument to the cmath.log() method that returns the given complex number’s logarithm value.
  • Store it in another variable.
  • Print the logarithm value of the given complex number.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the real part and imaginary part of the complex number as user input
# using map(), int(), split().
# Store it in two variables.
realnumb, imaginarynumb = map(int, input(
    'Enter real part and complex part of the complex number = ').split())
# Using a complex() function convert those two variables into a complex number.
complexnumb = complex(realnumb, imaginarynumb)

# Pass the given complex number as an argument to the cmath.log() method that
# returns the the given complex number's logarithm value.
# Store it in another variable.
rslt = cmath.log(complexnumb)
# Print the logarithm value of the given complex number.
print("The given complex number's", complexnumb,
      "logarithm value = ")
print(rslt)

Output:

Enter real part and complex part of the complex number = 5 2
The given complex number's (5+2j) logarithm value = 
(1.6836479149932368+0.3805063771123649j)
2)With two arguments

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the real part and imaginary part of the complex number as user input using map(), int(), split().
  • Store it in two variables.
  • Using a complex() function convert those two variables into a complex number and store them in a variable.
  • Give the base value as user input using the float(input()) function and store it in another variable.
  • Pass the given complex number, base value as the arguments to the cmath.log() method that returns the given complex number’s logarithm value for the given base.
  • Store it in another variable.
  • Print the logarithm value of the given complex number for the given base.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the real part and imaginary part of the complex number as user input
# using map(), int(), split().
# Store it in two variables.
realnumb, imaginarynumb = map(int, input(
    'Enter real part and complex part of the complex number = ').split())
# Using a complex() function convert those two variables into a complex number.
complexnumb = complex(realnumb, imaginarynumb)

# Give the base value as user input using the float(input()) function and
# store it in another variable.
gvn_basevalu = float(input("Enter some random number = "))
# Pass the given complex number, base value as the arguments to the cmath.log()
# method that returns the given complex number's logarithm value for
# the given base.
# Store it in another variable.
rslt = cmath.log(complexnumb, gvn_basevalu)
# Print the logarithm value of the given complex number for the given base.
print("The given complex number's", complexnumb,
      "logarithm value for the given base{", gvn_basevalu, "} = ")
print(rslt)

Output:

Enter real part and complex part of the complex number = 5 2
Enter some random number = 4
The given complex number's (5+2j) logarithm value for the given base{ 4.0 } = 
(1.214495248781893+0.27447733164331733j)