How to Compute the Inverse Cosine and Inverse Hyperbolic Cosine in PyTorch
In this article, we will see how to compute the inverse Cosine and Inverse Hyperbolic Cosine in Pytorch.
torch.acos()
torch.acos() is used to find the inverse cosine of elements in a given tensor. We can apply this function on real as well as complex tensors.
Syntax: torch.acos(input_tensor)
Parameter:
It will take a tensor which can be real or complex.
Return:
inverse cosine values in a tensor
Example 1:
In this example, we will create a 3D tensor with three rows and three columns and return the inverse cosine values.
Python3
# importing torch import torch # create tensor t1 = torch.tensor([[ 1 , 2 , 3 ], [ 5 , 6 , 7 ], [ 9 , 10 , 11 ]]) # printing the tensor print (t1) #get the inverse cosine values print (torch.acos(t1)) |
Output:
tensor([[ 1, 2, 3], [ 5, 6, 7], [ 9, 10, 11]]) tensor([[0., nan, nan], [nan, nan, nan], [nan, nan, nan]])
Example 2:
In this example, we will create a 1D complex tensor with real and imaginary parts with float type and return the inverse cosine values.
Python3
# import the torch module import torch # create real and img with float type real = torch.tensor([ 78.2 , 23.2 ], dtype = torch.float32) img = torch.tensor([ 32 , 41 ], dtype = torch.float32) # create the complex number t1 = torch. complex (real, img) print (t1) # get the inverse cosine values of the # complex tensor. print (torch.acos(t1)) |
Output:
tensor([78.2000+32.j, 23.2000+41.j]) tensor([0.3884-5.1298j, 1.0560-4.5457j])
torch.acosh()
torch.acosh() is used to find the inverse hyperbolic cosine of elements in a given tensor. We can apply this function on real as well as a complex tensor.
Syntax: torch.acosh(input_tensor)
Parameter:
It will take a tensor which can be real or complex.
Return:
inverse hyperbolic cosine values in a tensor
In this example, we will create a 3D tensor with three rows and three columns and return the inverse hyperbolic cosine values.
Python3
# importing torch import torch # create tensor t1 = torch.tensor([[ 1 , 2 , 3 ], [ 5 , 6 , 7 ], [ 9 , 10 , 11 ]]) # printing the tensor print (t1) # get the inverse hyperbolic cosine values print (torch.acosh(t1)) |
Output:
tensor([[ 1, 2, 3], [ 5, 6, 7], [ 9, 10, 11]]) tensor([[0.0000, 1.3170, 1.7627], [2.2924, 2.4779, 2.6339], [2.8873, 2.9932, 3.0890]])
Example 2:
In this example, we will create a 1D complex tensor with real and imaginary parts with float type and return the inverse hyperbolic cosine values.
Python3
# import the torch module import torch # create real and img with float type real = torch.tensor([ 78.2 , 23.2 ], dtype = torch.float32) img = torch.tensor([ 32 , 41 ], dtype = torch.float32) # create the complex number t1 = torch. complex (real, img) print (t1) # get the inverse hyperbolic # cosine values of the complex tensor. print (torch.acosh(t1)) |
Output:
tensor([78.2000+32.j, 23.2000+41.j]) tensor([5.1298+0.3884j, 4.5457+1.0560j])
Please Login to comment...