1+ """
2+ balance!(A::AbstractMatrix{T}; radix=convert(T, 2)) where T
3+
4+ A pure Julia implementation of the GEBAL algorithm.
5+ Balances a matrix to improve eigenvalue accuracy.
6+ """
7+ function balance! (A:: AbstractMatrix{T} ; radix= convert (T, 2 )) where T
8+ n = LinearAlgebra. checksquare (A)
9+
10+ scale = ones (real (T), n)
11+ low = 1
12+ high = n
13+ converged = false
14+
15+ # --- Step 1: Permutation (Search for isolated eigenvalues) ---
16+ # (Simplified for brevity; most performance gain comes from scaling)
17+ # In a full GEBAL, we would swap rows/cols to move zeros to the corners.
18+
19+ # --- Step 2: Scaling (The Ward Algorithm) ---
20+ while ! converged
21+ converged = true
22+ for i in low: high
23+ # Calculate row and column norms (excluding diagonal)
24+ row_norm = 0.0
25+ col_norm = 0.0
26+ for j in low: high
27+ if i == j continue end
28+ row_norm += abs (A[i, j])
29+ col_norm += abs (A[j, i])
30+ end
31+
32+ # Avoid division by zero
33+ if row_norm == 0 || col_norm == 0
34+ continue
35+ end
36+
37+ # Iterative scaling to bring row and col norms closer
38+ g = row_norm / radix
39+ f = 1.0
40+ s = col_norm + row_norm
41+
42+ # While column norm is significantly smaller than row norm
43+ while col_norm < g
44+ f *= radix
45+ col_norm *= radix^ 2
46+ end
47+
48+ # While column norm is significantly larger than row norm
49+ g = row_norm * radix
50+ while col_norm >= g
51+ f /= radix
52+ col_norm /= radix^ 2
53+ end
54+
55+ # Apply scaling if it's significant
56+ if (col_norm + row_norm) / f < 0.95 * s
57+ converged = false
58+ g = 1.0 / f
59+ scale[i] *= f
60+ # Apply to rows
61+ for j in 1 : n
62+ A[i, j] *= g
63+ end
64+ # Apply to columns
65+ for j in 1 : n
66+ A[j, i] *= f
67+ end
68+ end
69+ end
70+ end
71+
72+ return A, scale
73+ end
74+
75+
76+ function permute_matrix! (A:: AbstractMatrix{T} ) where T
77+ n = size (A, 1 )
78+ low = 1
79+ high = n
80+
81+ # Simple implementation of the permutation search
82+ # We look for rows/cols that are essentially isolated
83+
84+ # Search from the bottom up (high) and top down (low)
85+ changed = true
86+ while changed
87+ changed = false
88+
89+ # Look for a column with only one non-zero element
90+ for j in high: - 1 : low
91+ col = A[low: high, j]
92+ if count (! iszero, col) == 1
93+ # Permute this column to the 'high' position
94+ swap_cols! (A, j, high)
95+ swap_rows! (A, j, high)
96+ high -= 1
97+ changed = true
98+ end
99+ end
100+
101+ # Look for a row with only one non-zero element
102+ for i in low: high
103+ row = A[i, low: high]
104+ if count (! iszero, row) == 1
105+ # Permute this row to the 'low' position
106+ swap_cols! (A, i, low)
107+ swap_rows! (A, i, low)
108+ low += 1
109+ changed = true
110+ end
111+ end
112+ end
113+ return low, high
114+ end
115+
116+ function swap_rows! (A, i, j)
117+ for k in axes (A, 2 )
118+ A[i, k], A[j, k] = A[j, k], A[i, k]
119+ end
120+ end
121+ function swap_cols! (A, i, j)
122+ for k in axes (A, 1 )
123+ A[k, i], A[k, j] = A[k, j], A[k, i]
124+ end
125+ end
0 commit comments