
- 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 - fractional_matrix_power() Function
The scipy.linalg.fractional_matrix_power() function calculates the fractional power A^t of a square matrix A where t can be any real or complex value. This approach expands the concept of matrix powers to include non-integer exponents, making it an effective tool for advanced linear algebra applications.
To compute the fractional power, the approach uses eigenvalue or Schur decomposition. For t=1, the output is the matrix itself, whereas for t=0.5, it computes the matrix's square root.
This method is flexible enough to work for both real and complex matrices and can easily perform a fractional power on non-diagonalizable matrices, due to its incorporating advanced decomposition techniques.
Input matrix A should be square. The reason is that fractional powers are undefined for non-square matrices. Zero or near-zero eigenvalues in a matrix might lead to numerical instability or errors. In case the fractional power is not well-defined, additional issues might be raised for complex matrices.
This approach could be applied either in combination with expm() − matrix exponential for fractional time evolution in dynamical systems, or combined with logm(), to perform more complicate fractional transformations in linear algebra.
Syntax
The syntax for the Scipy method is as follows −
.fractional_matrix_power(A, t)
Parameters
This method accepts the following parameters −
A (N, N) array_like − Input square matrix (nn), real or complex.
t (float) − Scalar (real or complex), representing the fractional power to compute.
Return Value
X(N, N) array_like − Matrix A^t, of the same shape as A, representing the fractional power of A.
Example 1: Square Root of a 2x2 Matrix Using Fractional Power
The fractional_matrix_power method calculates the fractional powers of a matrix. These are generalized integer matrix powers to noninteger values.
The code takes a input 22 matrix 'A' and applies the fractional_matrix_power() method to calculate A^0.5-the square root of the matrix. The result is a matrix whose square equals the input matrix A, thus properly computing the square root for matrices.
import numpy as np import scipy.linalg from scipy.linalg import fractional_matrix_power # Input: Basic 2x2 matrix A = np.array([[4, 0], [0, 9]]) # Compute the square root of the matrix sqrt_matrix = scipy.linalg.fractional_matrix_power(A, 0.5) print("Matrix Square Root:\n", sqrt_matrix)
When we run above program, it produces following result
Matrix Square Root: [[2. 0.] [0. 3.]]
Example 2: Cube Root of a Diagonal Matrix with Fractional Power
Cube roots and other fractional matrix powers are useful for advanced linear transformations in engineering and science.
In the below code we will craete a 33 diagonal matrix 'A' and use fractional_matrix_power() method to calculate the cube root of the matrix 'A'. This shows how diagonal matrices enable fractional power computations.
import numpy as np from scipy.linalg import fractional_matrix_power # Input: Diagonal matrix A = np.diag([8, 27, 64]) # Compute the cube root of the matrix cube_root_matrix = fractional_matrix_power(A, 1/3) print("Matrix Cube Root:\n", cube_root_matrix)
Following is an output of the above code
Matrix Cube Root: [[2. 0. 0.] [0. 3. 0.] [0. 0. 4.]]
Example 3: Inverse Square Root of a Matrix for Statistical Models
Negative fractional powers, such as A^-0.5, imply the inverse square root, useful for statistical models for the inversion of a matrix.
The below program, uses a 22 positive definite matrix 'A' as an input to the method fractional_matrix_power(). The solution will calculate the inverse square root of the matrix A^-0.5. The output yields a matrix, when multiplied with A^0.5, results in an identity matrix and is required in normalizing covariance matrices.
import numpy as np from scipy.linalg import fractional_matrix_power # Input: Positive definite matrix A = np.array([[4, 0], [0, 9]]) # Compute the inverse square root of the matrix inv_sqrt_matrix = fractional_matrix_power(A, -0.5) print("Inverse Matrix Square Root:\n", inv_sqrt_matrix)
Output of the above code is as follows
Inverse Matrix Square Root: [[0.5 0. ] [0. 0.33333333]]
Example 4: Fourth Root of a Sparse Matrix for Spectral Analysis
In the below code, a 44 sparse matrix 'A' with diagonal elements is input, and A^0.25(fourth root) is computed using fractional_matrix_power(). This is useful for efficient spectral analysis in sparse systems. The result is a 44 matrix with each diagonal member representing the fourth root of its corresponding diagonal element in A.
import numpy as np from scipy.linalg import fractional_matrix_power # Input: Sparse matrix A = np.diag([16, 81, 256, 625]) # Compute the fourth root of the matrix fourth_root_matrix = fractional_matrix_power(A, 0.25) print("Matrix Fourth Root:\n", fourth_root_matrix)
When we run above program, it produces following result
Matrix Fourth Root: [[2. 0. 0. 0.] [0. 3. 0. 0.] [0. 0. 4. 0.] [0. 0. 0. 5.]]
Example 5: Error Handling in Fractional Power
If the input provided is not square matrix, the method fractional_matrix_power() returns a ValueError, which ensures the accuracy of fractional power computations.
The code below uses a 23 non-square matrix as input for the fractional_matrix_power method. As the method accepts only a square matrix, it returns a ValueError. This emphasizes the significance of confirming input dimensions prior to compute fractional power of a matrix.
import numpy as np from scipy.linalg import fractional_matrix_power # Input: Non-square matrix A = np.array([[1, 2, 3], [4, 5, 6]]) try: # Attempt to compute fractional power fractional_power = fractional_matrix_power(A, 0.5) except ValueError as e: print("Error:", e)
When we run above program, it produces following result
Error: expected square array_like input
Example 6: Combining Fractional Power with Matrix Exponential
The fractional_matrix_power() is first applied to a 22 matrix A to compute A^0.5 (its square root). The output is then used in the expm() method to compute the matrix exponential of A^0.5. The output shows how fractional transformations can be combined with exponential dynamics to model fractional time evolution in dynamic systems.
import numpy as np from scipy.linalg import expm, fractional_matrix_power # Input: Basic 2x2 matrix A = np.array([[1, 2], [3, 4]]) # Compute the square root of the matrix sqrt_matrix = fractional_matrix_power(A, 0.5) # Compute the exponential of the square root matrix expm_matrix = expm(sqrt_matrix) print("Matrix Square Root:\n", sqrt_matrix) print("\nExponential of Matrix Square Root:\n", expm_matrix)
When we run above program, it produces following result
Exponential of Matrix Square Root: [[3.04928599+0.43611157j 3.24965891-0.19948925j] [4.87448837-0.29923388j 7.92377436+0.13687769j]]