powalg.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. '''
  2. This module provides some Powell-style linear algebra procedures.
  3. Translated from Zaikun Zhang's modern-Fortran reference implementation in PRIMA.
  4. Dedicated to late Professor M. J. D. Powell FRS (1936--2015).
  5. Python translation by Nickolai Belakovski.
  6. '''
  7. import numpy as np
  8. from .linalg import isminor, planerot, matprod, inprod, hypot
  9. from .consts import DEBUGGING, EPS
  10. def qradd_Rdiag(c, Q, Rdiag, n):
  11. '''
  12. This function updates the QR factorization of an MxN matrix A of full column rank, attempting to
  13. add a new column C to this matrix as the LAST column while maintaining the full-rankness.
  14. Case 1. If C is not in range(A) (theoretically, it implies N < M), then the new matrix is np.hstack([A, C])
  15. Case 2. If C is in range(A), then the new matrix is np.hstack([A[:, :n-1], C])
  16. N.B.:
  17. 0. Instead of R, this subroutine updates Rdiag, which is np.diag(R), with a size at most M and at
  18. least min(m, n+1). The number is min(m, n+1) rather than min(m, n) as n may be augmented by 1 in
  19. the function.
  20. 1. With the two cases specified as above, this function does not need A as an input.
  21. 2. The function changes only Q[:, nsave:m] (nsave is the original value of n) and
  22. R[:, n-1] (n takes the updated value)
  23. 3. Indeed, when C is in range(A), Powell wrote in comments that "set iOUT to the index of the
  24. constraint (here, column of A --- Zaikun) to be deleted, but branch if no suitable index can be
  25. found". The idea is to replace a column of A by C so that the new matrix still has full rank
  26. (such a column must exist unless C = 0). But his code essentially sets iout=n always. Maybe he
  27. found this worked well enough in practice. Meanwhile, Powell's code includes a snippet that can
  28. never be reached, which was probably intended to deal with the case that IOUT != n
  29. '''
  30. m = Q.shape[1]
  31. nsave = n # Needed for debugging (only)
  32. # As in Powell's COBYLA, CQ is set to 0 at the positions with CQ being negligible as per ISMINOR.
  33. # This may not be the best choice if the subroutine is used in other contexts, e.g. LINCOA.
  34. cq = matprod(c, Q)
  35. cqa = matprod(abs(c), abs(Q))
  36. # The line below basically makes an element of cq 0 if adding it to the corresponding element of
  37. # cqa does not change the latter.
  38. cq = np.array([0 if isminor(cqi, cqai) else cqi for cqi, cqai in zip(cq, cqa)])
  39. # Update Q so that the columns of Q[:, n+1:m] are orthogonal to C. This is done by applying a 2D
  40. # Givens rotation to Q[:, [k, k+1]] from the right to zero C' @ Q[:, k+1] out for K=n+1, ... m-1.
  41. # Nothing will be done if n >= m-1
  42. for k in range(m-2, n-1, -1):
  43. if abs(cq[k+1]) > 0:
  44. # Powell wrote cq[k+1] != 0 instead of abs. The two differ if cq[k+1] is NaN.
  45. # If we apply the rotation below when cq[k+1] = 0, then cq[k] will get updated to |cq[k]|.
  46. G = planerot(cq[k:k+2])
  47. Q[:, [k, k+1]] = matprod(Q[:, [k, k+1]], G.T)
  48. cq[k] = hypot(*cq[k:k+2])
  49. # Augment n by 1 if C is not in range(A)
  50. if n < m:
  51. # Powell's condition for the following if: cq[n+1] != 0
  52. if abs(cq[n]) > EPS**2 and not isminor(cq[n], cqa[n]):
  53. n += 1
  54. # Update Rdiag so that Rdiag[n] = cq[n] = np.dot(c, q[:, n]). Note that N may be been augmented.
  55. if n - 1 >= 0 and n - 1 < m: # n >= m should not happen unless the input is wrong
  56. Rdiag[n - 1] = cq[n - 1]
  57. if DEBUGGING:
  58. assert nsave <= n <= min(nsave + 1, m)
  59. assert n <= len(Rdiag) <= m
  60. assert Q.shape == (m, m)
  61. return Q, Rdiag, n
  62. def qrexc_Rdiag(A, Q, Rdiag, i): # Used in COBYLA
  63. '''
  64. This function updates the QR factorization for an MxN matrix A=Q@R so that the updated Q and
  65. R form a QR factorization of [A_0, ..., A_{I-1}, A_{I+1}, ..., A_{N-1}, A_I] which is the matrix
  66. obtained by rearranging columns [I, I+1, ... N-1] of A to [I+1, ..., N-1, I]. Here A is ASSUMED TO
  67. BE OF FULL COLUMN RANK, Q is a matrix whose columns are orthogonal, and R, which is not present,
  68. is an upper triangular matrix whose diagonal entries are nonzero. Q and R need not be square.
  69. N.B.:
  70. 0. Instead of R, this function updates Rdiag, which is np.diag(R), the size being n.
  71. 1. With L = Q.shape[1] = R.shape[0], we have M >= L >= N. Most often L = M or N.
  72. 2. This function changes only Q[:, i:] and Rdiag[i:]
  73. 3. (NDB 20230919) In Python, i is either icon or nact - 2, whereas in FORTRAN it is either icon or nact - 1.
  74. '''
  75. # Sizes
  76. m, n = A.shape
  77. # Preconditions
  78. assert n >= 1 and n <= m
  79. assert i >= 0 and i < n
  80. assert len(Rdiag) == n
  81. assert Q.shape[0] == m and Q.shape[1] >= n and Q.shape[1] <= m
  82. # tol = max(1.0E-8, min(1.0E-1, 1.0E8 * EPS * m + 1))
  83. # assert isorth(Q, tol) # Costly!
  84. if i < 0 or i >= n:
  85. return Q, Rdiag
  86. # Let R be the upper triangular matrix in the QR factorization, namely R = Q.T@A.
  87. # For each k, find the Givens rotation G with G@(R[k:k+2, :]) = [hypt, 0], and update Q[:, k:k+2]
  88. # to Q[:, k:k+2]@(G.T). Then R = Q.T@A is an upper triangular matrix as long as A[:, [k, k+1]] is
  89. # updated to A[:, [k+1, k]]. Indeed, this new upper triangular matrix can be obtained by first
  90. # updating R[[k, k+1], :] to G@(R[[k, k+1], :]) and then exchanging its columns K and K+1; at the same
  91. # time, entries k and k+1 of R's diagonal Rdiag become [hypt, -(Rdiag[k+1]/hypt)*RDiag[k]].
  92. # After this is done for each k = 0, ..., n-2, we obtain the QR factorization of the matrix that
  93. # rearranges columns [i, i+1, ... n-1] of A as [i+1, ..., n-1, i].
  94. # Powell's code, however, is slightly different: before everything, he first exchanged columns k and
  95. # k+1 of Q (as well as rows k and k+1 of R). This makes sure that the entries of the update Rdiag
  96. # are all positive if it is the case for the original Rdiag.
  97. for k in range(i, n-1):
  98. G = planerot([Rdiag[k+1], inprod(Q[:, k], A[:, k+1])])
  99. Q[:, [k, k+1]] = matprod(Q[:, [k+1, k]], (G.T))
  100. # Powell's code updates Rdiag in the following way:
  101. # hypt = np.sqrt(Rdiag[k+1]**2 + np.dot(Q[:, k], A[:, k+1])**2)
  102. # Rdiag[[k, k+1]] = [hypt, (Rdiag[k+1]/hypt)*Rdiag[k]]
  103. # Note that Rdiag[n-1] inherits all rounding in Rdiag[i:n-1] and Q[:, i:n-1] and hence contains
  104. # significant errors. Thus we may modify Powell's code to set only Rdiag[k] = hypt here and then
  105. # calculate Rdiag[n] by an inner product after the loop. Nevertheless, we simple calculate RDiag
  106. # from scratch below.
  107. # Calculate Rdiag(i:n) from scratch
  108. Rdiag[i:n-1] = [inprod(Q[:, k], A[:, k+1]) for k in range(i, n-1)]
  109. Rdiag[n-1] = inprod(Q[:, n-1], A[:, i])
  110. return Q, Rdiag