使用scimath在Python中计算以n为底的对数

使用scimath在Python中计算以n为底的对数

要使用scimath计算以n为底的对数,在Python的Numpy中使用scimath.logn()方法。该方法返回x值的以n为底的对数。如果x是标量,则输出也是标量,否则返回一个数组。

如果x包含负数输入,答案在复数域中计算并返回。第一个参数n是取对数的整数底数,第二个参数x是需要计算以n为底的对数的值。

步骤

首先,导入所需的库 –

import numpy as np

使用array()方法创建numpy数组−

arr = np.array([-4, -8, 8])

显示数组 –

print("Our Array...\n",arr)

检查尺寸 −

print("\nDimensions of our Array...\n",arr.ndim)

获取数据类型−

print("\nDatatype of our Array object...\n",arr.dtype)

获取形状−

print("\nShape of our Array object...\n",arr.shape)

使用Python NumPy中的scimath.logn()方法计算以n为底数的对数。 该方法返回x值的以n为底数的对数。如果x是一个标量,则返回标量;否则返回一个数组 −

print("\nResult (logn)...\n",np.emath.logn(2, arr))

示例

import numpy as np

# Creating a numpy array using the array() method
arr = np.array([-4, -8, 8])

# Display the array
print("Our Array...\n",arr)

# Check the Dimensions
print("\nDimensions of our Array...\n",arr.ndim)

# Get the Datatype
print("\nDatatype of our Array object...\n",arr.dtype)

# Get the Shape
print("\nShape of our Array object...\n",arr.shape)

# To compute the logarithm base n with scimath, use the scimath.logn() method in Python Numpy
# The method returns the log base n of the x value(s). If x was a scalar, so is out, otherwise an array is returned.
print("\nResult (logn)...\n",np.emath.logn(2, arr))

输出

Our Array...
[-4 -8 8]

Dimensions of our Array...
1

Datatype of our Array object...
int64

Shape of our Array object...
(3,)

Result (logn)...
[2.+4.53236014j 3.+4.53236014j 3.+0.j ]

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程