Class RealBalancer
Instances of this class may be used to balance real dense matrices. Balancing a matrix involves computing a diagonal similarity transformation to "balance" the rows and columns of the matrix. This balancing is achieved by attempting to scale the entries of the matrix by similarity transformations such that the 1-norms of corresponding rows and columns have the similar 1-norms. Rows and columns may also be permuted during balancing if requested.
Balancing is often used as a preprocessing step to improve the conditioning of eigenvalue problems. Because the balancing transformation is a similarity transformation, the eigenvalues are preserved. Further, when permutations are done during balancing it is possible to isolate decoupled eigenvalues.
The similarity transformation of a square matrix \( A \) into the balanced matrix \( B \) can be described as: \[ \begin{align*} B &= T^{-1} A T \\ &= D^{-1} P^{-1} A P D. \end{align*} \] Solving for \( A \), balancing may be viewed as the following decomposition: \[ \begin{align*} A &= T B T^{-1} \\ &= P D B D^{-1} P^{-1}. \end{align*} \] Where \( P \) is a permutation matrix, and \( D \) is a diagonal scaling matrix.
When permutations are used during balancing we obtain a specific form. First, \[ \begin{align*} P^{-1}AP = \begin{bmatrix} T_1 & X & Y \\ \mathbf{0} & B_1 & Z \\ \mathbf{0} & \mathbf{0} & T_2 \end{bmatrix} \end{align*} \] Where \( T_{1} \) and \( T_{2} \) are upper triangular matrices whose eigenvalues lie along the diagonal. These are also eigenvalues of \( A \). Then, if scaling is applied we obtain: \[ D^{-1}P^{-1}APD = \begin{bmatrix} T_1 & XD_1 & Y \\ \mathbf{0} & D_1^{-1}B_1D_1 & D_1^{-1}Z \\ \mathbf{0} & \mathbf{0} & T_2 \end{bmatrix} \] where \( D_{1} \) is a diagonal matrix such that, \[ D = \begin{bmatrix} I_1 & \mathbf{0} & \mathbf{0} \\ \mathbf{0} & D_1 & \mathbf{0} \\ \mathbf{0} & \mathbf{0} & I_2 \end{bmatrix} \] Where \( I_{1} \) and \( I_{2} \) are identity matrices with equivalent shapes to \( T_{1} \) and \( T_{2} \).
Once balancing has been applied, one need only compute the eigenvalues of \( B_{1} \) and combine them with the diagonal entries of \( T_{1} \) and \( T_{2} \) to obtain all eigenvalues of \( A \).
The code in this class if heavily based on LAPACK's reference implementations of xGEBAL (v 3.12.1).
- See Also:
-
Field Summary
Fields inherited from class org.flag4j.linalg.decompositions.balance.Balancer
balancedMatrix, doPermutations, doScaling, iHigh, iLow, inPlace, scalePerm, sizeFields inherited from class org.flag4j.linalg.decompositions.Decomposition
hasDecomposed -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a real balancer which will perform both the permutations and scaling steps out-of-place.RealBalancer(boolean doPermutations, boolean doScaling) Constructs a real balancer optionally performing the permutation and scaling steps out-of-place.RealBalancer(boolean doPermutations, boolean doScaling, boolean inPlace) Constructs a real balancer optionally performing the permutation and scaling steps in/out-of-place. -
Method Summary
Modifier and TypeMethodDescriptionapplyLeftTransform(Matrix src) Efficiently left multiplies \( PD \) to the providedsrcmatrix.Efficiently right multiplies \( D^{-1}P^{-1} \) to the providedsrcmatrix.protected booleanisZero(int idx) Checks if a value withinBalancer.balancedMatrixis zero.protected voidswapCols(int colIdx1, int colIdx2, int start, int stop) Swaps two columns, over a specified range, within theBalancer.balancedMatrixmatrix.protected voidswapRows(int rowIdx1, int rowIdx2, int start, int stop) Swaps two rows, over a specified range, within theBalancer.balancedMatrixmatrix.protected doublevectorMaxAbs(int start, int n, int stride) Computes the maximum absolute value of a vector withnelements fromBalancer.balancedMatrix's 1D data array starting at indexstartand spaced bystride.protected doublevectorNorm(int start, int n, int stride) Computes the \( \ell ^{2} \) norm of a vector withnelements fromBalancer.balancedMatrix's 1D data array starting at indexstartand spaced bystride.protected voidvectorScale(double factor, int start, int n, int stride) Scales a vector withnelements fromBalancer.balancedMatrix's 1D data array starting at indexstartand spaced bystride.Methods inherited from class org.flag4j.linalg.decompositions.balance.Balancer
decompose, doIterativePermutations, doIterativeScaling, ensureHasBalanced, getB, getBSubMatrix, getD, getD, getDInv, getIHigh, getILow, getP, getPInv, getScalePerm, getT, getTInvMethods inherited from class org.flag4j.linalg.decompositions.Decomposition
ensureHasDecomposed
-
Constructor Details
-
RealBalancer
public RealBalancer()Constructs a real balancer which will perform both the permutations and scaling steps out-of-place.
To specify if permutations or scaling should be or should not be performed, use
RealBalancer(boolean, boolean). To specify if the balancing should be done in-place, useRealBalancer(boolean, boolean, boolean). -
RealBalancer
public RealBalancer(boolean doPermutations, boolean doScaling) Constructs a real balancer optionally performing the permutation and scaling steps out-of-place.
To specify if the balancing should be done in-place, use
RealBalancer(boolean, boolean, boolean).- Parameters:
doPermutations- Flag indicating if the permutation step should be performed during balancing.- If
true: the permutation step will be performed. - If
false: the permutation step will not be performed.
- If
doScaling- Flag indicating if the scaling step should be performed during balancing.- If
true: the scaling step will be performed. - If
false: the scaling step will not be performed.
- If
-
RealBalancer
public RealBalancer(boolean doPermutations, boolean doScaling, boolean inPlace) Constructs a real balancer optionally performing the permutation and scaling steps in/out-of-place.
- Parameters:
doPermutations- Flag indicating if the permutation step should be performed during balancing.- If
true: the permutation step will be performed. - If
false: the permutation step will not be performed.
- If
doScaling- Flag indicating if the scaling step should be performed during balancing.- If
true: the scaling step will be performed. - If
false: the scaling step will not be performed.
- If
inPlace- Flag indicating if the balancing should be done in or out-of-place.- If
true: balancing will be done in-place and the source matrix will be overwritten. - If
false: balancing will be done out-of-place.
- If
-
-
Method Details
-
swapRows
protected void swapRows(int rowIdx1, int rowIdx2, int start, int stop) Swaps two rows, over a specified range, within theBalancer.balancedMatrixmatrix.- Specified by:
swapRowsin classBalancer<Matrix>- Parameters:
rowIdx1- Index of the first row to swap.rowIdx2- Index of the second row to swap.start- Index of the column specifying the start of the range for the row swap (inclusive).stop- Index of the column specifying the end of the range for the row swap (exclusive).
-
swapCols
protected void swapCols(int colIdx1, int colIdx2, int start, int stop) Swaps two columns, over a specified range, within theBalancer.balancedMatrixmatrix.- Specified by:
swapColsin classBalancer<Matrix>- Parameters:
colIdx1- Index of the first column to swap.colIdx2- Index of the second column to swap.start- Index of the row specifying the start of the range for the column swap (inclusive).stop- Index of the row specifying the end of the range for the column swap (exclusive).
-
isZero
protected boolean isZero(int idx) Checks if a value withinBalancer.balancedMatrixis zero.- Specified by:
isZeroin classBalancer<Matrix>- Parameters:
idx- Index of value within flat dataBalancer.balancedMatrixto check if it is zero.
-
vectorNorm
protected double vectorNorm(int start, int n, int stride) Computes the \( \ell ^{2} \) norm of a vector withnelements fromBalancer.balancedMatrix's 1D data array starting at indexstartand spaced bystride.- Specified by:
vectorNormin classBalancer<Matrix>- Parameters:
start- Starting index withinBalancer.balancedMatrix's 1D data array to compute norm of.n- The number of elements in the vector to compute norm of.stride- The spacing between each element withinBalancer.balancedMatrix's 1D data array to norm of.- Returns:
- The norm of the vector containing the specified elements from
Balancer.balancedMatrix's 1D data array.
-
vectorMaxAbs
protected double vectorMaxAbs(int start, int n, int stride) Computes the maximum absolute value of a vector withnelements fromBalancer.balancedMatrix's 1D data array starting at indexstartand spaced bystride.- Specified by:
vectorMaxAbsin classBalancer<Matrix>- Parameters:
start- Starting index withinBalancer.balancedMatrix's 1D data array to compute maximum absolute value of.n- The number of elements in the vector to compute maximum absolute value of.stride- The spacing between each element withinBalancer.balancedMatrix's 1D data array to compute maximum absolute value of.- Returns:
- The maximum absolute value of the vector containing the specified elements from
Balancer.balancedMatrix's 1D data array.
-
vectorScale
protected void vectorScale(double factor, int start, int n, int stride) Scales a vector withnelements fromBalancer.balancedMatrix's 1D data array starting at indexstartand spaced bystride. This operation must be done in-place.- Specified by:
vectorScalein classBalancer<Matrix>- Parameters:
factor- Factor to scale elements by.start- Starting index withinBalancer.balancedMatrix's 1D data array begin scaling.n- The number of elements to scale.stride- The spacing between each element withinBalancer.balancedMatrix's 1D data array to scale.
-
applyLeftTransform
Efficiently left multiplies \( PD \) to the providedsrcmatrix.- Specified by:
applyLeftTransformin classBalancer<Matrix>- Parameters:
src- Matrix to apply transform to.- Returns:
- The result of left multiplying \( PD \) to the
srcmatrix.
-
applyRightTransform
Efficiently right multiplies \( D^{-1}P^{-1} \) to the providedsrcmatrix.- Specified by:
applyRightTransformin classBalancer<Matrix>- Parameters:
src- Matrix to apply transform to.- Returns:
- The result of right multiplying \( D^{-1}P^{-1} \) to the
srcmatrix.
-