
- SciPy - Home
- SciPy - Introduction
- SciPy - Environment Setup
- SciPy - Basic Functionality
- SciPy - Relationship with NumPy
- SciPy Clusters
- SciPy - Clusters
- SciPy - Hierarchical Clustering
- SciPy - K-means Clustering
- SciPy - Distance Metrics
- SciPy Constants
- SciPy - Constants
- SciPy - Mathematical Constants
- SciPy - Physical Constants
- SciPy - Unit Conversion
- SciPy - Astronomical Constants
- SciPy - Fourier Transforms
- SciPy - FFTpack
- SciPy - Discrete Fourier Transform (DFT)
- SciPy - Fast Fourier Transform (FFT)
- SciPy Integration Equations
- SciPy - Integrate Module
- SciPy - Single Integration
- SciPy - Double Integration
- SciPy - Triple Integration
- SciPy - Multiple Integration
- SciPy Differential Equations
- SciPy - Differential Equations
- SciPy - Integration of Stochastic Differential Equations
- SciPy - Integration of Ordinary Differential Equations
- SciPy - Discontinuous Functions
- SciPy - Oscillatory Functions
- SciPy - Partial Differential Equations
- SciPy Interpolation
- SciPy - Interpolate
- SciPy - Linear 1-D Interpolation
- SciPy - Polynomial 1-D Interpolation
- SciPy - Spline 1-D Interpolation
- SciPy - Grid Data Multi-Dimensional Interpolation
- SciPy - RBF Multi-Dimensional Interpolation
- SciPy - Polynomial & Spline Interpolation
- SciPy Curve Fitting
- SciPy - Curve Fitting
- SciPy - Linear Curve Fitting
- SciPy - Non-Linear Curve Fitting
- SciPy - Input & Output
- SciPy - Input & Output
- SciPy - Reading & Writing Files
- SciPy - Working with Different File Formats
- SciPy - Efficient Data Storage with HDF5
- SciPy - Data Serialization
- SciPy Linear Algebra
- SciPy - Linalg
- SciPy - Matrix Creation & Basic Operations
- SciPy - Matrix LU Decomposition
- SciPy - Matrix QU Decomposition
- SciPy - Singular Value Decomposition
- SciPy - Cholesky Decomposition
- SciPy - Solving Linear Systems
- SciPy - Eigenvalues & Eigenvectors
- SciPy Image Processing
- SciPy - Ndimage
- SciPy - Reading & Writing Images
- SciPy - Image Transformation
- SciPy - Filtering & Edge Detection
- SciPy - Top Hat Filters
- SciPy - Morphological Filters
- SciPy - Low Pass Filters
- SciPy - High Pass Filters
- SciPy - Bilateral Filter
- SciPy - Median Filter
- SciPy - Non - Linear Filters in Image Processing
- SciPy - High Boost Filter
- SciPy - Laplacian Filter
- SciPy - Morphological Operations
- SciPy - Image Segmentation
- SciPy - Thresholding in Image Segmentation
- SciPy - Region-Based Segmentation
- SciPy - Connected Component Labeling
- SciPy Optimize
- SciPy - Optimize
- SciPy - Special Matrices & Functions
- SciPy - Unconstrained Optimization
- SciPy - Constrained Optimization
- SciPy - Matrix Norms
- SciPy - Sparse Matrix
- SciPy - Frobenius Norm
- SciPy - Spectral Norm
- SciPy Condition Numbers
- SciPy - Condition Numbers
- SciPy - Linear Least Squares
- SciPy - Non-Linear Least Squares
- SciPy - Finding Roots of Scalar Functions
- SciPy - Finding Roots of Multivariate Functions
- SciPy - Signal Processing
- SciPy - Signal Filtering & Smoothing
- SciPy - Short-Time Fourier Transform
- SciPy - Wavelet Transform
- SciPy - Continuous Wavelet Transform
- SciPy - Discrete Wavelet Transform
- SciPy - Wavelet Packet Transform
- SciPy - Multi-Resolution Analysis
- SciPy - Stationary Wavelet Transform
- SciPy - Statistical Functions
- SciPy - Stats
- SciPy - Descriptive Statistics
- SciPy - Continuous Probability Distributions
- SciPy - Discrete Probability Distributions
- SciPy - Statistical Tests & Inference
- SciPy - Generating Random Samples
- SciPy - Kaplan-Meier Estimator Survival Analysis
- SciPy - Cox Proportional Hazards Model Survival Analysis
- SciPy Spatial Data
- SciPy - Spatial
- SciPy - Special Functions
- SciPy - Special Package
- SciPy Advanced Topics
- SciPy - CSGraph
- SciPy - ODR
- SciPy Useful Resources
- SciPy - Reference
- SciPy - Quick Guide
- SciPy - Cheatsheet
- SciPy - Useful Resources
- SciPy - Discussion
SciPy - logm() Function
To find the matrix logarithm of a square matrix , we use the scipy.linalg.logm function. Since the matrix logarithm is the inverse of the matrix exponential
exp(B) = A if B=logm(A)
In contrast to the natural logarithm for scalars, the matrix logarithm handles the non-commutative characteristics of matrices by using eigenvalue decomposition or other numerical methods.
For invertible matrices with eigenvalues devoid of non-positive real components, the matrix logarithm is defined.
The result satisfies exp(logm(A))=A, but the computation may be inaccurate if A is poorly conditioned or has eigenvalues close to the branch cut of the logarithm.
Syntax
Following is the syntax of the SciPy logm() method
.logm(A, disp=True)
Parameters
This method accepts the following parameters −
A − A sqaure matrix for which the matrix logarithm is to be computed.
disp − It issues warnings if the result might be inaccurate, if True and returns a second value with an error estimate instead of printing warnings, if False.
Return Value
The logm() returns a logA(ndarray), where it represents the matrix logarithm of A and returned only if disp=False, provides the 1-norm of the relative error in the result.
Example 1
This is the basic example of logm() method demonstrates the basic logarithm of a identity matrix.
In this example, we use logm() to confirm that the logarithm of simple matrices behaves as expected.
from scipy.linalg import logm import numpy as np A = np.eye(2) # Identity matrix log_A = logm(A) print(log_A)
When we run above program, it produces following result
[[0. 0.] [0. 0.]]The logarithm of the identity matrix is a zero matrix.
Example 2:Logarithm of a Diagonal Matrix
This example demonstrates the use of the matrix exponential to solve a diagonal matrix.
In this example, we use logm()because it simplifies the computation by directly handling the diagonal structure of A.
import numpy as np from scipy.linalg import logm A = np.array([[2, 0], [0, 3]]) log_A = logm(A) print(log_A)
When we run above program, it produces following result
[[0.69314718 0. ] [0. 1.09861229]]
The logarithm of a diagonal matrix is the logarithm of each diagonal entry.
Example 3: Logarithm of a General Matrix
This example demonstrates the utility of logm() in handling complex cases beyond simple structures.
import numpy as np from scipy.linalg import logm A = np.array([[1, 2], [0, 3]]) log_A = logm(A) print(log_A)
Following is an output of the above code
[[ 0. 1.15443133] [ 0. 1.09861229]]
The approach produces a non-diagonal result after computing the logarithm for non-diagonal matrices.