SciPy - matrix_balance() Function



The matrix_balance() method modifies a matrix to more evenly scale its rows and columns. This has an impact on the precision of computations by reducing large differences in the matrix's values. It useful when we need to solve equations or find eigenvalues.

When we balance a matrix, we make it simpler to use in calculations. This step helps reduce mistakes, speeds up repeated methods, and leads to more reliable outcomes when working with tricky math problems.

Syntax

The syntax for the Scipy matrix_balance() method is as follows −

.matrix_balance(A, permute=True, scale=True, separate=False, overwrite_a=False)

Parameters

This method accepts the following parameters −

  • A (array_like n,n)− The square matrix to be balanced.

  • permute (bool, optional) − If True (default), a permutation is applied to the matrix to make it closer to upper triangular form.

  • scale (bool, optional − If True (default), the rows and columns of the matrix are rescaled to equalize their norms as much as possible.

  • separate (bool, optional) − If True, the method returns the separate permutation and scaling factors.

  • overwrite_a (bool, optional) − If True, the input matrix A may be overwritten to save memory.

Return Value

This method returns the following depending on the value of seperate parameter.

  • balanced (ndarray) − The balanced matrix.

  • perm (ndarray), scale (ndarray) − If separate=True, perm is the permutation array and scale contains the scaling factors.

  • info (dict, optional) − May contain additional information about the balancing process.

Example 1: Balancing a 2x2 Matrix

Matrix balancing method is used to modify the scale and structure of the matrix, so we can improve numerical ability and accuracy of computations.

Below code is the example of how to use matrix_balance() method to balances a 2x2 matrix, displaying both the original and balanced versions. This method helps users to understand how matrix balancing can stabilize numerical calculations in real-world scenarios.

import scipy.linalg
import numpy as np

# Define a simple 2x2 matrix
A = np.array([[10, 1],
              [1, 0.001]])

# Balance the matrix
balanced = scipy.linalg.matrix_balance(A)
print("Original Matrix:", A)
print("\nBalanced Matrix:", balanced)

When we run above program, it produces following result −

Original Matrix: [[1.e+01 1.e+00]
 [1.e+00 1.e-03]]

Balanced Matrix: (array([[1.e+01, 1.e+00],
       [1.e+00, 1.e-03]]), array([[1., 0.],
       [0., 1.]]))

Example 2: Permutation and Scaling Factors

A matrix may be scaled and permuted for balancing purposes. The below example shows how to perform this procedure in SciPy and retrieve these factors independently.

Here the scaling factors and permutation are obtained by using scipy.linalg.matrix_balance method with the separate=True parameter on a 2x2 matrix. This helps users to understand how such elements stabilize the matrices in exact numerical computation.

import scipy.linalg
import numpy as np

# Define a simple 2x2 matrix
A = np.array([[8, 1],
              [7, 0.006]])

perm, scale = scipy.linalg.matrix_balance(A, separate=True)
print("\nPermutation Array:", perm)
print("\nScaling Factors:", scale)

Following is an output of the above code

Permutation Array: [[8.0e+00 2.0e+00]
 [3.5e+00 6.0e-03]]

Scaling Factors: (array([1., 2.]), array([0, 1]))

Example 3: Balancing a 3x3 Matrix and Analyzing Ratios

Calculations involving eigenvalues and linear algebra benefit by balancing a matrix, in which the structure of the matrix is changed to make the numerical stability better. This example shows how column-to-row sum ratios are affected by balancing.

The code demonstrates how to use matrix_balance() method to balance a 3x3 matrix and compares the column-to-row sum ratios before and after balancing.

import numpy as np
from scipy import linalg
x = np.array([[2, 50, 0.1], 
              [8, 1, 100], 
              [0.01, 20, 5]])
y, permscale = linalg.matrix_balance(x)

# Ratios of column sums to row sums for the original matrix
original_ratios = np.abs(x).sum(axis=0) / np.abs(x).sum(axis=1)

# Ratios of column sums to row sums for the balanced matrix
balanced_ratios = np.abs(y).sum(axis=0) / np.abs(y).sum(axis=1)

print("Original Matrix Ratios:", original_ratios)
print("\nBalanced Matrix Ratios:", balanced_ratios)
print("\nPermutation and Scaling Factors:", permscale)

Output of the above code is as follows −

Original Matrix Ratios: [0.19213052 0.65137615 4.20231907]

Balanced Matrix Ratios: [0.66753006 0.98507463 1.22169183]

Permutation and Scaling Factors: [[4. 0. 0.]
 [0. 2. 0.]
 [0. 0. 1.]]
scipy_linalg.htm
Advertisements