在NumPy中返回複數型別陣列的逐元素平方根
要在Python NumPy中逐元素返回陣列的非負平方根,請使用**numpy.sqrt()**方法。返回的陣列與x形狀相同,包含x中每個元素的正平方根。如果x中的任何元素是複數,則返回複數陣列(並計算負實數的平方根)。如果x中的所有元素都是實數,則y也是實數,負元素返回nan。如果提供了out,則y是對它的引用。如果x是標量,則這是一個標量。
out是結果儲存到的位置。如果提供,則其形狀必須與輸入廣播到的形狀相同。如果不提供或為None,則返回一個新分配的陣列。元組(僅可能作為關鍵字引數)的長度必須等於輸出的數量。
步驟
首先,匯入所需的庫:
import numpy as np
使用array()方法建立一個具有複數型別的陣列:
arr = np.array([-1j, 1e-15, 27.+0.j, 68.-2.j, 49.+0.j])
顯示陣列:
print("Array...
", arr)獲取陣列的型別:
print("
Our Array type...
", arr.dtype)
獲取陣列的維度:
print("
Our Array Dimension...
",arr.ndim)獲取陣列的形狀:
print("
Our Array Shape...
",arr.shape)
要在Python NumPy中逐元素返回陣列的非負平方根,請使用numpy.sqrt()方法:
print("
Result...
",np.sqrt(arr))示例
import numpy as np
# Create an array with complex type using the array() method
arr = np.array([-1j, 1e-15, 27.+0.j, 68.-2.j, 49.+0.j])
# Display the array
print("Array...
", arr)
# Get the type of the array
print("
Our Array type...
", arr.dtype)
# Get the dimensions of the Array
print("
Our Array Dimension...
",arr.ndim)
# Get the shape of the Array
print("
Our Array Shape...
",arr.shape)
# To return the non-negative square-root of an array, element-wise, use the numpy.sqrt() method in Python Numpy
print("
Result...
",np.sqrt(arr))輸出
Array... [-0.0e+00-1.j 1.0e-15+0.j 2.7e+01+0.j 6.8e+01-2.j 4.9e+01+0.j] Our Array type... complex128 Our Array Dimension... 1 Our Array Shape... (5,) Result... [7.07106781e-01-0.70710678j 3.16227766e-08+0.j 5.19615242e+00+0.j 8.24710269e+00-0.1212547j 7.00000000e+00+0.j ]
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP