Return the Nuclear Norm of the matrix in Linear Algebra in Python

PythonNumpyServer Side ProgrammingProgramming

To return the Norm of the matrix or vector in Linear Algebra, use the LA.norm() method in Python Numpy. The 1st parameter, x is an input array. If axis is None, x must be 1-D or 2-D, unless ord is None. If both axis and ord are None, the 2-norm of x.ravel will be returned.

The 2nd parameter, ord is the order of the norm. The inf means numpy’s inf object. The default is None. The "nuc" set as a parameter is the Nuclear norm. Both the Frobenius and nuclear norm orders are only defined for matrices

Steps

At first, import the required library −

import numpy as np
from numpy import linalg as LA

Create an array −

arr = np.array([[ -4, -3, -2], [-1, 0, 1], [2, 3, 4] ])

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 return the Norm of the matrix or vector in Linear Algebra, use the LA.norm() method −

print("\nResult...\n",LA.norm(arr, 'nuc'))

Example

import numpy as np
from numpy import linalg as LA

# Create an array
arr = np.array([[ -4, -3, -2],[-1, 0, 1],[2, 3, 4] ])

# 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 return the Norm of the matrix or vector in Linear Algebra, use the LA.norm() method in Python Numpy
print("\nResult...\n",LA.norm(arr, 'nuc'))

Output

Our Array...
   [[-4 -3 -2]
   [-1 0 1]
   [ 2 3 4]]

Dimensions of our Array...
2

Datatype of our Array object...
int64

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

Result...
9.797958971132713
raja
Updated on 09-Mar-2022 05:29:28

Advertisements