Exponential function in python

Exponential function in Python

In this post, you will learn how to find the exponent using the Python programming language.

An exponent refers to the number of times a number is multiplied by itself. For example, 3 4 means we are multiplying 3 four times.

An exponential function is a mathematical function that is used in many real-world situations. It is mainly used to find the exponential decay or exponential growth or to calculate bacterial growth or decay, population growth or decline, or to compute investments, model populations and so on.

Python exp() function

Python provides a number method, exp() to calculate the exponential value with the base set to e. The function accepts an exponent value as input. The syntax of this function is —

math.exp(exponent)

Exponent function example

The given example demonstrates the usage of exp() method.

import math print("math.exp(-35.13) : ", math.exp(-35.13)) print("math.exp(98.13) : ", math.exp(98.13)) print("math.exp(-6.89) : ",math.exp(-6.89)) print("math.exp(82.29) : ", math.exp(82.29)) print("math.exp(math.pi) : ", math.exp(math.pi))
math.exp(-35.13) : 5.53649421850571e-16 math.exp(98.13) : 4.1430245728472585e+42 math.exp(-6.89) : 0.0010179138409954387 math.exp(82.29) : 5.471330076079942e+35 math.exp(math.pi) : 23.140692632779267

Источник

Calculating The Exponential Value in Python

Python Certification Course: Master the essentials

In mathematics, a number’s exponential value results from that number being multiplied by itself a certain number of times. The base is the integer multiplied by itself, and the exponent is the number of times it will be multiplied.

Читайте также:  Reading file in java as string

Introduction

In this article, we will learn about calculating the exponential value in Python using different ways, but first, let’s understand its mathematical concept.

You may have seen exponential value in mathematics in the form of A^x, having a simple meaning of multiplying A for x times.

Here, A is a base value, and x is an exponent value.

In this example, 5 is a base value, and 3 is an exponent value, so we multiplied 5 for three times.

value of e

There are multiple ways to calculate the exponential value in Python. So let’s go through them one by one.

Using loops

The first method for calculating exponential value in python is using loops.

Loops will help us execute the block of code, again and again, to take its benefit for calculating the exponential value in Python.

Here we took the base value as 5 and the exponent as 3.

We took the result variable and initialized the base value to it for making logic.

Here the range of the for loop is set from 0 to 2 (i.e. exponent – 1) to iterate through the loop two times.

So the time complexity for the above example becomes O(n) and space complexity is O(1).

Here we iterate through the loop many times to calculate the final value. But we have more straightforward methods for calculating the exponential value in Python.

Exponentiation operator (**)

In Python, we have an exponentiation operator, which is one of the ways to calculate the exponential value of the given base and exponent values.

We use the (**) double asterisk/exponentiation operator between the base and exponent values.

In the above example, we took base 2 and exponent as 16. Here, 2 gets multiplied 16 times.

It is the simplest method for calculating the exponential value in Python.

We can use floating-point values as well while calculating the exponential values.

Using exponentiations operator

When we use any operand as a floating-point number, we get the result in the floating-point number as shown in the following code snippet.

So these are some methods for calculating exponential values in Python. There are various pros and cons for the different methods explained above, so use them as per your requirements.

pow() function

We have a huge variety of built-in functions in Python, and pow() is one of them, which helps us calculate the exponential value.

In the pow() function, we can pass the base and exponent values. These values can be of different data types, including integers, float, and complex.

pow(base, exponent) is equivalent to the base ** exponent.

But in this pow() function, three parameters are also allowed. The first two arguments are base and exponent, but we can give the third argument, which will calculate the modulus of the calculated exponential value.

The time complexity of calculating the exponential value by squaring is O(Log(exponent)).

Although Python doesn’t use the method of squaring but still shows complexity due to exponential increase with big values.

Let’s consider some of the examples with 3 parameters.

The first three examples have three arguments in the above examples, and the 4th only with two arguments.

It is advisable to use pow(5,3,2) instead of pow(5,3)%2 because the efficiency is more here to calculate the modulo of the exponential value.

We can perform these operations for negative numbers as well:

When using a negative number in the pow() function, we should take care of some things while using a negative number.

As the pow() function first converts its argument into float and then calculates the power, we see some return type differences.

Base Exponent Return Value
Non-Negative Non-Negative Integer
Non-Negative Negative Float
Negative Non-Negative Integer
Negative Negative Float

The pow() function can give the different errors in different situations, for, eg.

math.pow() function

Apart from the built-in pow() function, we have the math.pow() function comes from the Python math module containing some useful mathematical functions.

This math.pow() function can also calculate the exponential value in Python.

The math.pow() shows the time complexity of O(1) due to the floating-point exponentiation, which is better than the built-in pow() function, But due to this, it sacrifices the precision of the result.

The math.pow() can take only two arguments, unlike the three arguments in the built-in pow() function.

The math.pow() function always returns a float value, whereas in the pow() function, we get int values most of the time.

math.exp() function

We have the function called exp() in the math module, which uses the value of e as the base.

The value of e is approximately 2.71828 in mathematics.

The value of e in Python is 2.718281828459045

This value of e is used as the base value, and the exponent value is given as an argument.

To implement this, we have to import the math module.

These different techniques have different time complexities alongside their pros and cons.

Method Time Complexity Space Complexity
Looping Technique O(n) O(1)
** Operator O(Log(exponent)) O(1)
pow() function O(Log(exponent)) O(1)
math.pow() function O(1) O(1)

Conclusion

In this article, we saw the exponential values and how to calculate them using different techniques in Python.

  1. Exponential value is the multiplication of base value exponent times.
  2. In the looping technique, we need to write so much code and think for logic.
  3. The exponentiation operator, which ** denotes, is one of the simplest ways to find the exponential value.
  4. The pow() is one of the inbuilt functions, which takes 2 to 3 arguments. It helps us to find the exponential value when two arguments are passed, and if we pass the third argument, then the modulus of the exponential value gets calculated.
  5. The math.pow() function comes from the math module, and it is the fastest way to calculate the exponential value with time complexity O(1).
  6. The exp() function finds the exponential value with the base e and exponent, which we passed as an argument.

Источник

Python math.exp() Method

num: The function takes only one argument num, which we want to find exponential.

Return Value

The math.exp() method returns a floating type number by calculating e**n (e^n). This function returns a TypeError if the given input is not a number.

Example 1: How to Use math.exp() Method in Python

# Program to show the working of exp import math # Initializing the values # int type x = 16 # float type y = 10.6 # negative num z = -6 print("Value of e^x: ", math.exp(x)) print("Value of e^y: ", math.exp(y)) print("Value of e^z: ", math.exp(z))
Value of e^x: 8886110.520507872 Value of e^y: 40134.83743087578 Value of e^z: 0.0024787521766663585

In this program, we have imported math libraries and initialized the value of different data types in x, y, and z. Then we have printed values of e**x, e**y, and e**z. We can see that all the printed values are in float data type.

Example 2: TypeError: must be real number, not str

# Program2 to show working of exp import math # When the given number is not a number n = "546" print("Value of e^n: ", math.exp(n))
TypeError: must be real number, not str 

In this program, we have initialized the value of n a string. As the value of n is not a number, we got a TypeError.

Example 3

import math Tup = (1.21, 19.26, 13.05, -40.95, 0.45) Lis = [-11.21, 3.64, -9.59, -4.15, 5.97] print('Python EXP() Function on Positive Number = %.2f' % math.exp(1)) print('Python EXP() Function on Negative Number = %.2f' % math.exp(-1)) print('Python EXP() Function on Tuple Item = %.2f' % math.exp(Tup[2])) print('Python EXP() Function on List Item = %.4f' % math.exp(Lis[2])) print('Python EXP() Function on Multiple Number = %.4f' % math.exp(11 + 19 - 15.64)) print('Python EXP() Function on String Number = ', math.exp('Python'))
Python EXP() Function on Positive Number = 2.72 Python EXP() Function on Negative Number = 0.37 Python EXP() Function on Tuple Item = 465096.41 Python EXP() Function on List Item = 0.0001 Python EXP() Function on Multiple Number = 1723728.0946 Traceback (most recent call last): File "app.py", line 14, in print('Python EXP() Function on String Number = ', math.exp('Python')) TypeError: must be real number, not str

Источник

Оцените статью