linalg.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. '''
  2. This module provides some basic 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 .consts import DEBUGGING, EPS, REALMAX, REALMIN
  9. from .present import present
  10. # We use naive implementations of matrix multiplication and other routines for two
  11. # reasons:
  12. # 1. When Fortran is compiled in debug mode, and Python is using these routines, we
  13. # can get bit for bit identical results as compared to Fortran. This is helpful
  14. # for comparing the two implementations. It will be particularly helpful when porting
  15. # the other implementations like LINCOA, etc.
  16. # 2. On some problems this algorithm is very sensitive to errors in finite precision
  17. # arithmetic. Switching to naive implementation will slow down the algorithm, but
  18. # may be more stable.
  19. USE_NAIVE_MATH = False
  20. def inprod(x, y):
  21. if not USE_NAIVE_MATH:
  22. return np.dot(x, y)
  23. result = 0
  24. for i in range(len(x)):
  25. result += x[i] * y[i]
  26. return result
  27. def matprod12(x, y):
  28. result = np.zeros(y.shape[1])
  29. for i in range(y.shape[1]):
  30. result[i] = inprod(x, y[:, i])
  31. return result
  32. def matprod21(x, y):
  33. result = np.zeros(x.shape[0])
  34. for i in range(x.shape[1]):
  35. result += x[:, i] * y[i]
  36. return result
  37. def matprod22(x, y):
  38. result = np.zeros((x.shape[0], y.shape[1]))
  39. for i in range(y.shape[1]):
  40. for j in range(x.shape[1]):
  41. result[:, j] += x[:, i] * y[i, j]
  42. return result
  43. def matprod(x, y):
  44. if not USE_NAIVE_MATH:
  45. return x@y
  46. if len(x.shape) == 1 and len(y.shape) == 1:
  47. return inprod(x, y)
  48. elif len(x.shape) == 1 and len(y.shape) == 2:
  49. return matprod12(x, y)
  50. elif len(x.shape) == 2 and len(y.shape) == 1:
  51. return matprod21(x, y)
  52. elif len(x.shape) == 2 and len(y.shape) == 2:
  53. return matprod22(x, y)
  54. else:
  55. raise ValueError(f'Invalid shapes for x and y: {x.shape} and {y.shape}')
  56. def outprod(x, y):
  57. if not USE_NAIVE_MATH:
  58. return np.outer(x, y)
  59. result = np.zeros((len(x), len(y)))
  60. for i in range(len(x)):
  61. result[:, i] = x * y[i]
  62. return result
  63. def lsqr(A, b, Q, Rdiag):
  64. if not USE_NAIVE_MATH:
  65. return np.linalg.lstsq(A, b, rcond=None)[0]
  66. m = A.shape[0]
  67. n = A.shape[1]
  68. rank = min(m, n)
  69. x = np.zeros(n)
  70. y = b.copy()
  71. for i in range(rank - 1, -1, -1):
  72. yq = inprod(y, Q[:, i])
  73. yqa = inprod(np.abs(y), np.abs(Q[:, i]))
  74. if isminor(yq, yqa):
  75. x[i] = 0
  76. else:
  77. x[i] = yq / Rdiag[i]
  78. y = y - x[i] * A[:, i]
  79. return x
  80. def hypot(x1, x2):
  81. if not USE_NAIVE_MATH:
  82. return np.hypot(x1, x2)
  83. if not np.isfinite(x1):
  84. r = abs(x1)
  85. elif not np.isfinite(x2):
  86. r = abs(x2)
  87. else:
  88. y = abs(np.array([x1, x2]))
  89. y = np.array([min(y), max(y)])
  90. if y[0] > np.sqrt(REALMIN) and y[1] < np.sqrt(REALMAX/2.1):
  91. r = np.sqrt(sum(y*y))
  92. elif y[1] > 0:
  93. r = y[1] * np.sqrt((y[0]/y[1])*(y[0]/y[1]) + 1)
  94. else:
  95. r = 0
  96. return r
  97. def norm(x):
  98. if not USE_NAIVE_MATH:
  99. return np.linalg.norm(x)
  100. # NOTE: Avoid np.pow! And exponentiation in general!
  101. # It appears that in Fortran, x*x and x**2 are the same, but in Python they are not!
  102. # Try it with x = 5 - 1e-15
  103. result = np.sqrt(sum([xi*xi for xi in x]))
  104. return result
  105. def istril(A, tol=0):
  106. return primasum(abs(A) - np.tril(abs(A))) <= tol
  107. def istriu(A, tol=0):
  108. return primasum(abs(A) - np.triu(abs(A))) <= tol
  109. def inv(A):
  110. if not USE_NAIVE_MATH:
  111. return np.linalg.inv(A)
  112. A = A.copy()
  113. n = A.shape[0]
  114. if istril(A):
  115. # This case is invoked in COBYLA.
  116. R = A.T
  117. B = np.zeros((n, n))
  118. for i in range(n):
  119. B[i, i] = 1 / R[i, i]
  120. B[:i, i] = -matprod(B[:i, :i], R[:i, i]) / R[i, i]
  121. return B.T
  122. elif istriu(A):
  123. B = np.zeros((n, n))
  124. for i in range(n):
  125. B[i, i] = 1 / A[i, i]
  126. B[:i, i] = -matprod(B[:i, :i], A[:i, i]) / A[i, i]
  127. else:
  128. # This is NOT the best algorithm for the inverse, but since the QR subroutine is available ...
  129. Q, R, P = qr(A)
  130. R = R.T
  131. B = np.zeros((n, n))
  132. for i in range(n - 1, -1, -1):
  133. B[:, i] = (Q[:, i] - matprod(B[:, i + 1:n], R[i + 1:n, i])) / R[i, i]
  134. InvP = np.zeros(n, dtype=int)
  135. InvP[P] = np.linspace(0, n-1, n)
  136. B = B[:, InvP].T
  137. return B
  138. def qr(A):
  139. m = A.shape[0]
  140. n = A.shape[1]
  141. Q = np.eye(m)
  142. T = A.T
  143. P = np.linspace(0, n-1, n, dtype=int)
  144. for j in range(n):
  145. k = np.argmax(primasum(primapow2(T[j:n+1, j:m+1]), axis=1), axis=0)
  146. if k > 0 and k <= n - j - 1:
  147. k += j
  148. P[j], P[k] = P[k], P[j]
  149. T[[j, k], :] = T[[k, j], :]
  150. for i in range(m-1, j, -1):
  151. G = planerot(T[j, [j, i]]).T
  152. T[j, [j, i]] = np.append(hypot(T[j, j], T[j, i]), 0)
  153. T[j + 1:n + 1, [j, i]] = matprod(T[j + 1:n + 1, [j, i]], G)
  154. Q[:, [j, i]] = matprod(Q[:, [j, i]], G)
  155. R = T.T
  156. return Q, R, P
  157. def primasum(x, axis=None):
  158. '''
  159. According to its documentation, np.sum will sometimes do partial pairwise summation.
  160. For our purposes, when comparing, we want don't want to do anything fancy, and we
  161. just want to add things up one at a time.
  162. '''
  163. if not USE_NAIVE_MATH:
  164. return np.sum(x, axis=axis)
  165. if axis is None:
  166. if x.ndim == 2:
  167. # Sum columns first, then sum the result
  168. return sum(primasum(x, axis=0))
  169. else:
  170. return sum(x)
  171. elif axis == 0:
  172. result = np.zeros(x.shape[1])
  173. for i in range(x.shape[1]):
  174. result[i] = sum(x[:, i])
  175. return result
  176. elif axis == 1:
  177. result = np.zeros(x.shape[0])
  178. for i in range(x.shape[0]):
  179. result[i] = sum(x[i, :])
  180. return result
  181. def primapow2(x):
  182. '''
  183. Believe it or now, x**2 is not always the same as x*x in Python. In Fortran they
  184. appear to be identical. Here's a quick one-line to find an example on your system
  185. (well, two liner after importing numpy):
  186. list(filter(lambda x: x[1], [(x:=np.random.random(), x**2 - x*x != 0) for _ in range(10000)]))
  187. '''
  188. return x*x
  189. def planerot(x):
  190. '''
  191. As in MATLAB, planerot(x) returns a 2x2 Givens matrix G for x in R2 so that Y=G@x has Y[1] = 0.
  192. Roughly speaking, G = np.array([[x[0]/R, x[1]/R], [-x[1]/R, x[0]/R]]), where R = np.linalg.norm(x).
  193. 0. We need to take care of the possibilities of R=0, Inf, NaN, and over/underflow.
  194. 1. The G defined above is continuous with respect to X except at 0. Following this definition,
  195. G = np.array([[np.sign(x[0]), 0], [0, np.sign(x[0])]]) if x[1] == 0,
  196. G = np.array([[0, np.sign(x[1])], [np.sign(x[1]), 0]]) if x[0] == 0
  197. Yet some implementations ignore the signs, leading to discontinuity and numerical instability.
  198. 2. Difference from MATLAB: if x contains NaN of consists of only Inf, MATLAB returns a NaN matrix,
  199. but we return an identity matrix or a matrix of +/-np.sqrt(2). We intend to keep G always orthogonal.
  200. '''
  201. # Preconditions
  202. if DEBUGGING:
  203. assert len(x) == 2, "x must be a 2-vector"
  204. # ==================
  205. # Calculation starts
  206. # ==================
  207. # Define C = X(1) / R and S = X(2) / R with R = HYPOT(X(1), X(2)). Handle Inf/NaN, over/underflow.
  208. if (any(np.isnan(x))):
  209. # In this case, MATLAB sets G to NaN(2, 2). We refrain from doing so to keep G orthogonal.
  210. c = 1
  211. s = 0
  212. elif (all(np.isinf(x))):
  213. # In this case, MATLAB sets G to NaN(2, 2). We refrain from doing so to keep G orthogonal.
  214. c = 1 / np.sqrt(2) * np.sign(x[0])
  215. s = 1 / np.sqrt(2) * np.sign(x[1])
  216. elif (abs(x[0]) <= 0 and abs(x[1]) <= 0): # X(1) == 0 == X(2).
  217. c = 1
  218. s = 0
  219. elif (abs(x[1]) <= EPS * abs(x[0])):
  220. # N.B.:
  221. # 0. With <= instead of <, this case covers X(1) == 0 == X(2), which is treated above separately
  222. # to avoid the confusing SIGN(., 0) (see 1).
  223. # 1. SIGN(A, 0) = ABS(A) in Fortran but sign(0) = 0 in MATLAB, Python, Julia, and R#
  224. # 2. Taking SIGN(X(1)) into account ensures the continuity of G with respect to X except at 0.
  225. c = np.sign(x[0])
  226. s = 0
  227. elif (abs(x[0]) <= EPS * abs(x[1])):
  228. # N.B.: SIGN(A, X) = ABS(A) * sign of X /= A * sign of X # Therefore, it is WRONG to define G
  229. # as SIGN(RESHAPE([ZERO, -ONE, ONE, ZERO], [2, 2]), X(2)). This mistake was committed on
  230. # 20211206 and took a whole day to debug! NEVER use SIGN on arrays unless you are really sure.
  231. c = 0
  232. s = np.sign(x[1])
  233. else:
  234. # Here is the normal case. It implements the Givens rotation in a stable & continuous way as in:
  235. # Bindel, D., Demmel, J., Kahan, W., and Marques, O. (2002). On computing Givens rotations
  236. # reliably and efficiently. ACM Transactions on Mathematical Software (TOMS), 28(2), 206-238.
  237. # N.B.: 1. Modern compilers compute SQRT(REALMIN) and SQRT(REALMAX/2.1) at compilation time.
  238. # 2. The direct calculation without involving T and U seems to work better; use it if possible.
  239. if (all(np.logical_and(np.sqrt(REALMIN) < np.abs(x), np.abs(x) < np.sqrt(REALMAX / 2.1)))):
  240. # Do NOT use HYPOTENUSE here; the best implementation for one may be suboptimal for the other
  241. r = norm(x)
  242. c = x[0] / r
  243. s = x[1] / r
  244. elif (abs(x[0]) > abs(x[1])):
  245. t = x[1] / x[0]
  246. u = max(1, abs(t), np.sqrt(1 + t*t)) # MAXVAL: precaution against rounding error.
  247. u *= np.sign(x[0]) ##MATLAB: u = sign(x(1))*sqrt(1 + t**2)
  248. c = 1 / u
  249. s = t / u
  250. else:
  251. t = x[0] / x[1]
  252. u = max([1, abs(t), np.sqrt(1 + t*t)]) # MAXVAL: precaution against rounding error.
  253. u *= np.sign(x[1]) ##MATLAB: u = sign(x(2))*sqrt(1 + t**2)
  254. c = t / u
  255. s = 1 / u
  256. G = np.array([[c, s], [-s, c]]) # MATLAB: G = [c, s; -s, c]
  257. #====================#
  258. # Calculation ends #
  259. #====================#
  260. # Postconditions
  261. if DEBUGGING:
  262. assert G.shape == (2,2)
  263. assert np.all(np.isfinite(G))
  264. assert abs(G[0, 0] - G[1, 1]) + abs(G[0, 1] + G[1, 0]) <= 0
  265. tol = np.maximum(1.0E-10, np.minimum(1.0E-1, 1.0E6 * EPS))
  266. assert isorth(G, tol)
  267. if all(np.logical_and(np.isfinite(x), np.abs(x) < np.sqrt(REALMAX / 2.1))):
  268. r = np.linalg.norm(x)
  269. assert max(abs(G@x - [r, 0])) <= max(tol, tol * r), 'G @ X = [||X||, 0]'
  270. return G
  271. def isminor(x, ref):
  272. '''
  273. This function tests whether x is minor compared to ref. It is used by Powell, e.g., in COBYLA.
  274. In precise arithmetic, isminor(x, ref) is true if and only if x == 0; in floating point
  275. arithmetic, isminor(x, ref) is true if x is 0 or its nonzero value can be attributed to
  276. computer rounding errors according to ref.
  277. Larger sensitivity means the function is more strict/precise, the value 0.1 being due to Powell.
  278. For example:
  279. isminor(1e-20, 1e300) -> True, because in floating point arithmetic 1e-20 cannot be added to
  280. 1e300 without being rounded to 1e300.
  281. isminor(1e300, 1e-20) -> False, because in floating point arithmetic adding 1e300 to 1e-20
  282. dominates the latter number.
  283. isminor(3, 4) -> False, because 3 can be added to 4 without being rounded off
  284. '''
  285. sensitivity = 0.1
  286. refa = abs(ref) + sensitivity * abs(x)
  287. refb = abs(ref) + 2 * sensitivity * abs(x)
  288. return np.logical_or(abs(ref) >= refa, refa >= refb)
  289. def isinv(A, B, tol=None):
  290. '''
  291. This procedure tests whether A = B^{-1} up to the tolerance TOL.
  292. '''
  293. # Sizes
  294. n = np.size(A, 0)
  295. # Preconditions
  296. if DEBUGGING:
  297. assert np.size(A, 0) == np.size(A, 1)
  298. assert np.size(B, 0) == np.size(B, 1)
  299. assert np.size(A, 0) == np.size(B, 0)
  300. if present(tol):
  301. assert tol >= 0
  302. #====================#
  303. # Calculation starts #
  304. #====================#
  305. tol = tol if present(tol) else np.minimum(1e-3, 1e2 * EPS * np.maximum(np.size(A, 0), np.size(A, 1)))
  306. tol = np.max([tol, tol * np.max(abs(A)), tol * np.max(abs(B))])
  307. is_inv = ((abs(matprod(A, B)) - np.eye(n)) <= tol).all() or ((abs(matprod(B, A) - np.eye(n))) <= tol).all()
  308. #===================#
  309. # Calculation ends #
  310. #===================#
  311. return is_inv
  312. def isorth(A, tol=None):
  313. '''
  314. This function tests whether the matrix A has orthonormal columns up to the tolerance TOL.
  315. '''
  316. # Preconditions
  317. if DEBUGGING:
  318. if present(tol):
  319. assert tol >= 0
  320. #====================#
  321. # Calculation starts #
  322. #====================#
  323. num_vars = np.size(A, 1)
  324. if num_vars > np.size(A, 0):
  325. is_orth = False
  326. elif (np.isnan(primasum(abs(A)))):
  327. is_orth = False
  328. else:
  329. if present(tol):
  330. is_orth = (abs(matprod(A.T, A) - np.eye(num_vars)) <= np.maximum(tol, tol * np.max(abs(A)))).all()
  331. else:
  332. is_orth = (abs(matprod(A.T, A) - np.eye(num_vars)) <= 0).all()
  333. #====================#
  334. # Calculation ends #
  335. #====================#
  336. return is_orth
  337. def get_arrays_tol(*arrays):
  338. """
  339. Get a relative tolerance for a set of arrays. Borrowed from COBYQA
  340. Parameters
  341. ----------
  342. *arrays: tuple
  343. Set of `numpy.ndarray` to get the tolerance for.
  344. Returns
  345. -------
  346. float
  347. Relative tolerance for the set of arrays.
  348. Raises
  349. ------
  350. ValueError
  351. If no array is provided.
  352. """
  353. if len(arrays) == 0:
  354. raise ValueError("At least one array must be provided.")
  355. size = max(array.size for array in arrays)
  356. weight = max(
  357. np.max(np.abs(array[np.isfinite(array)]), initial=1.0)
  358. for array in arrays
  359. )
  360. return 10.0 * EPS * max(size, 1.0) * weight