SciPy - cosm() Function



The scipy.linalg.cosm function calculates the cosine of a square matrix . This is identical to the cosine function for numbers, but it applies to matrices. The resulting matrix depicts 's cosine transformation.

It helps solve problems in physics, engineering, and differential equations, especially for systems with periodic behavior.

By applying their power series expansion to matrices, the matrix cosine is comparable to the cosine function for scalars.It is frequently applied to linear dynamical systems.

Syntax

Following is the syntax of the SciPy cosm() method

scipy.linalg.cosm(A)

Parameters

This method accepts s sqaure matrix for which the matrix cosine is to be computed.

Return Value

The cosm(A) returns a cosA(ndarray), where it represents the computed matrix cosine, having the same shape as A.

Example 1

This is the basic example of cosm() method demonstrates the cosine of a identity matrix.

In this example, we use cosm to confirm that the cosine of simple matrices ghd mathametical properties we anticipate.

import numpy as np
from scipy.linalg import cosm

A = np.eye(2)  # Identity matrix
cos_A = cosm(A)
print(cos_A)       

When we run above program, it produces following result

[[0.54030231 0.        ]
 [0.         0.54030231]]

Example 2: Cosine of a Diagonal Matrix

This example demonstrates the use of the matrix exponential to solve a diagonal matrix.

import numpy as np
from scipy.linalg import cosm

A = np.array([[1, 0],
              [0, 2]])
cos_A = cosm(A)
print(cos_A) 

When we run above program, it produces following result −

[[-0.41614684  0.        ]
 [ 0.         -0.65364362]]

Example 3: Cosine of a General Matrix

In this example, A represents a rotation matrix. The cosine operation results in a scaled identity matrix.

import numpy as np
from scipy.linalg import cosm

A = np.array([[0, 1],
              [-1, 0]])
cos_A = cosm(A)
print(cos_A)

Following is an output of the above code −

[[ 0.54030231  0.        ]
 [ 0.         0.54030231]]
scipy_linalg.htm
Advertisements