🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The tanh function in Python's math module is used to compute the hyperbolic tangent of a given number. This function is essential in various fields such as mathematics, physics, engineering, and computer science where hyperbolic functions are often required.
Table of Contents
- Introduction
- Importing the
mathModule tanhFunction Syntax- Examples
- Basic Usage
- Handling Negative Numbers
- Handling Edge Cases
- Real-World Use Case
- Conclusion
- Reference
Introduction
The tanh function in Python's math module allows you to compute the hyperbolic tangent of a given number. The hyperbolic tangent function is a fundamental hyperbolic function that describes the ratio of the hyperbolic sine to the hyperbolic cosine of a given angle.
Importing the math Module
Before using the tanh function, you need to import the math module.
import math
tanh Function Syntax
The syntax for the tanh function is as follows:
math.tanh(x)
Parameters:
x: A numeric value representing the angle in radians.
Returns:
- The hyperbolic tangent of
x.
Examples
Basic Usage
To demonstrate the basic usage of tanh, we will compute the hyperbolic tangent of a few values.
Example
import math
# Hyperbolic tangent of 0
result = math.tanh(0)
print(result) # Output: 0.0
# Hyperbolic tangent of 1
result = math.tanh(1)
print(result) # Output: 0.7615941559557649
# Hyperbolic tangent of -1
result = math.tanh(-1)
print(result) # Output: -0.7615941559557649
Output:
0.0
0.7615941559557649
-0.7615941559557649
Handling Negative Numbers
This example demonstrates how tanh handles negative numbers by computing the hyperbolic tangent of a negative value.
Example
import math
# Hyperbolic tangent of -2
result = math.tanh(-2)
print(result) # Output: -0.9640275800758169
# Hyperbolic tangent of -3.5
result = math.tanh(-3.5)
print(result) # Output: -0.9981778976111987
Output:
-0.9640275800758169
-0.9981778976111987
Handling Edge Cases
This example demonstrates how tanh handles special cases such as zero and very large values.
Example
import math
# Hyperbolic tangent of 0
result = math.tanh(0)
print(result) # Output: 0.0
# Hyperbolic tangent of a very large positive number
large_value = 100
result = math.tanh(large_value)
print(f"Hyperbolic tangent of {large_value}: {result}") # Output: 1.0
# Hyperbolic tangent of a very large negative number
large_negative_value = -100
result = math.tanh(large_negative_value)
print(f"Hyperbolic tangent of {large_negative_value}: {result}") # Output: -1.0
Output:
0.0
Hyperbolic tangent of 100: 1.0
Hyperbolic tangent of -100: -1.0
Real-World Use Case
Machine Learning: Activation Function
In machine learning, the tanh function can be used as an activation function for neural networks, which helps in mapping the input to an output in the range of -1 to 1.
Example
import math
# Function to apply tanh activation
def tanh_activation(x):
return math.tanh(x)
# Input values for the activation function
inputs = [-2, -1, 0, 1, 2]
# Applying tanh activation function to the inputs
outputs = [tanh_activation(x) for x in inputs]
print(f"Activation outputs: {outputs}")
Output:
Activation outputs: [-0.9640275800758169, -0.7615941559557649, 0.0, 0.7615941559557649, 0.9640275800758169]
Conclusion
The tanh function in Python's math module is used for computing the hyperbolic tangent of a given number. This function is useful in various numerical and data processing applications, particularly those involving hyperbolic functions in fields like mathematics, physics, and engineering. Proper usage of this function can enhance the accuracy and efficiency of your computations.
Reference
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment