python-scipyHow can I use python scipy to calculate the norm of a vector?
Using Python Scipy, the norm of a vector can be calculated using the la.norm
function. This function takes in three parameters, the vector, the order of the norm (default is 2) and an axis (default is None).
Example code
import scipy.linalg as la
vector = [1,2,3,4]
result = la.norm(vector)
print(result)
Output example
5.477225575051661
The code consists of the following parts:
- Importing the
scipy.linalg
module asla
- This imports the module and gives it the aliasla
, which will be used to call thenorm
function. - Creating the vector - This creates a vector of 4 elements.
- Calculating the norm - This uses the
la.norm
function to calculate the norm of the vector. This function takes in three parameters, the vector, the order of the norm (default is 2) and an axis (default is None). - Printing the result - This prints the result of the calculation.
Helpful links
More of Python Scipy
- How do I use Python XlsxWriter to write a NumPy array to an Excel file?
- How do I use Python Numpy to read and write Excel (.xlsx) files?
- How can I check if a certain version of Python is compatible with SciPy?
- How do I use the trapz function in Python SciPy?
- How do I create a 2D array of zeros using Python and NumPy?
- How can I use Python and SciPy to read and write WAV files?
- How do I convert a Python numpy array to a list?
- How do I use the NumPy transpose function in Python?
- How do I use the scipy ttest_ind function in Python?
- How can I use Python and SciPy to implement a quantum Monte Carlo simulation?
See more codes...