You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

220 lines
8.6 KiB

  1. /* lux.h (LU-factorization, rational arithmetic) */
  2. /***********************************************************************
  3. * This code is part of GLPK (GNU Linear Programming Kit).
  4. *
  5. * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
  6. * 2009, 2010, 2011, 2013 Andrew Makhorin, Department for Applied
  7. * Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
  8. * reserved. E-mail: <mao@gnu.org>.
  9. *
  10. * GLPK is free software: you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * GLPK is distributed in the hope that it will be useful, but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  18. * License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
  22. ***********************************************************************/
  23. #ifndef LUX_H
  24. #define LUX_H
  25. #include "dmp.h"
  26. #include "glpgmp.h"
  27. /***********************************************************************
  28. * The structure LUX defines LU-factorization of a square matrix A,
  29. * which is the following quartet:
  30. *
  31. * [A] = (F, V, P, Q), (1)
  32. *
  33. * where F and V are such matrices that
  34. *
  35. * A = F * V, (2)
  36. *
  37. * and P and Q are such permutation matrices that the matrix
  38. *
  39. * L = P * F * inv(P) (3)
  40. *
  41. * is lower triangular with unity diagonal, and the matrix
  42. *
  43. * U = P * V * Q (4)
  44. *
  45. * is upper triangular. All the matrices have the order n.
  46. *
  47. * The matrices F and V are stored in row/column-wise sparse format as
  48. * row and column linked lists of non-zero elements. Unity elements on
  49. * the main diagonal of the matrix F are not stored. Pivot elements of
  50. * the matrix V (that correspond to diagonal elements of the matrix U)
  51. * are also missing from the row and column lists and stored separately
  52. * in an ordinary array.
  53. *
  54. * The permutation matrices P and Q are stored as ordinary arrays using
  55. * both row- and column-like formats.
  56. *
  57. * The matrices L and U being completely defined by the matrices F, V,
  58. * P, and Q are not stored explicitly.
  59. *
  60. * It is easy to show that the factorization (1)-(3) is some version of
  61. * LU-factorization. Indeed, from (3) and (4) it follows that:
  62. *
  63. * F = inv(P) * L * P,
  64. *
  65. * V = inv(P) * U * inv(Q),
  66. *
  67. * and substitution into (2) gives:
  68. *
  69. * A = F * V = inv(P) * L * U * inv(Q).
  70. *
  71. * For more details see the program documentation. */
  72. typedef struct LUX LUX;
  73. typedef struct LUXELM LUXELM;
  74. typedef struct LUXWKA LUXWKA;
  75. struct LUX
  76. { /* LU-factorization of a square matrix */
  77. int n;
  78. /* the order of matrices A, F, V, P, Q */
  79. DMP *pool;
  80. /* memory pool for elements of matrices F and V */
  81. LUXELM **F_row; /* LUXELM *F_row[1+n]; */
  82. /* F_row[0] is not used;
  83. F_row[i], 1 <= i <= n, is a pointer to the list of elements in
  84. i-th row of matrix F (diagonal elements are not stored) */
  85. LUXELM **F_col; /* LUXELM *F_col[1+n]; */
  86. /* F_col[0] is not used;
  87. F_col[j], 1 <= j <= n, is a pointer to the list of elements in
  88. j-th column of matrix F (diagonal elements are not stored) */
  89. mpq_t *V_piv; /* mpq_t V_piv[1+n]; */
  90. /* V_piv[0] is not used;
  91. V_piv[p], 1 <= p <= n, is a pivot element v[p,q] corresponding
  92. to a diagonal element u[k,k] of matrix U = P*V*Q (used on k-th
  93. elimination step, k = 1, 2, ..., n) */
  94. LUXELM **V_row; /* LUXELM *V_row[1+n]; */
  95. /* V_row[0] is not used;
  96. V_row[i], 1 <= i <= n, is a pointer to the list of elements in
  97. i-th row of matrix V (except pivot elements) */
  98. LUXELM **V_col; /* LUXELM *V_col[1+n]; */
  99. /* V_col[0] is not used;
  100. V_col[j], 1 <= j <= n, is a pointer to the list of elements in
  101. j-th column of matrix V (except pivot elements) */
  102. int *P_row; /* int P_row[1+n]; */
  103. /* P_row[0] is not used;
  104. P_row[i] = j means that p[i,j] = 1, where p[i,j] is an element
  105. of permutation matrix P */
  106. int *P_col; /* int P_col[1+n]; */
  107. /* P_col[0] is not used;
  108. P_col[j] = i means that p[i,j] = 1, where p[i,j] is an element
  109. of permutation matrix P */
  110. /* if i-th row or column of matrix F is i'-th row or column of
  111. matrix L = P*F*inv(P), or if i-th row of matrix V is i'-th row
  112. of matrix U = P*V*Q, then P_row[i'] = i and P_col[i] = i' */
  113. int *Q_row; /* int Q_row[1+n]; */
  114. /* Q_row[0] is not used;
  115. Q_row[i] = j means that q[i,j] = 1, where q[i,j] is an element
  116. of permutation matrix Q */
  117. int *Q_col; /* int Q_col[1+n]; */
  118. /* Q_col[0] is not used;
  119. Q_col[j] = i means that q[i,j] = 1, where q[i,j] is an element
  120. of permutation matrix Q */
  121. /* if j-th column of matrix V is j'-th column of matrix U = P*V*Q,
  122. then Q_row[j] = j' and Q_col[j'] = j */
  123. int rank;
  124. /* the (exact) rank of matrices A and V */
  125. };
  126. struct LUXELM
  127. { /* element of matrix F or V */
  128. int i;
  129. /* row index, 1 <= i <= m */
  130. int j;
  131. /* column index, 1 <= j <= n */
  132. mpq_t val;
  133. /* numeric (non-zero) element value */
  134. LUXELM *r_prev;
  135. /* pointer to previous element in the same row */
  136. LUXELM *r_next;
  137. /* pointer to next element in the same row */
  138. LUXELM *c_prev;
  139. /* pointer to previous element in the same column */
  140. LUXELM *c_next;
  141. /* pointer to next element in the same column */
  142. };
  143. struct LUXWKA
  144. { /* working area (used only during factorization) */
  145. /* in order to efficiently implement Markowitz strategy and Duff
  146. search technique there are two families {R[0], R[1], ..., R[n]}
  147. and {C[0], C[1], ..., C[n]}; member R[k] is a set of active
  148. rows of matrix V having k non-zeros, and member C[k] is a set
  149. of active columns of matrix V having k non-zeros (in the active
  150. submatrix); each set R[k] and C[k] is implemented as a separate
  151. doubly linked list */
  152. int *R_len; /* int R_len[1+n]; */
  153. /* R_len[0] is not used;
  154. R_len[i], 1 <= i <= n, is the number of non-zero elements in
  155. i-th row of matrix V (that is the length of i-th row) */
  156. int *R_head; /* int R_head[1+n]; */
  157. /* R_head[k], 0 <= k <= n, is the number of a first row, which is
  158. active and whose length is k */
  159. int *R_prev; /* int R_prev[1+n]; */
  160. /* R_prev[0] is not used;
  161. R_prev[i], 1 <= i <= n, is the number of a previous row, which
  162. is active and has the same length as i-th row */
  163. int *R_next; /* int R_next[1+n]; */
  164. /* R_prev[0] is not used;
  165. R_prev[i], 1 <= i <= n, is the number of a next row, which is
  166. active and has the same length as i-th row */
  167. int *C_len; /* int C_len[1+n]; */
  168. /* C_len[0] is not used;
  169. C_len[j], 1 <= j <= n, is the number of non-zero elements in
  170. j-th column of the active submatrix of matrix V (that is the
  171. length of j-th column in the active submatrix) */
  172. int *C_head; /* int C_head[1+n]; */
  173. /* C_head[k], 0 <= k <= n, is the number of a first column, which
  174. is active and whose length is k */
  175. int *C_prev; /* int C_prev[1+n]; */
  176. /* C_prev[0] is not used;
  177. C_prev[j], 1 <= j <= n, is the number of a previous column,
  178. which is active and has the same length as j-th column */
  179. int *C_next; /* int C_next[1+n]; */
  180. /* C_next[0] is not used;
  181. C_next[j], 1 <= j <= n, is the number of a next column, which
  182. is active and has the same length as j-th column */
  183. };
  184. #define lux_create _glp_lux_create
  185. LUX *lux_create(int n);
  186. /* create LU-factorization */
  187. #define lux_decomp _glp_lux_decomp
  188. int lux_decomp(LUX *lux, int (*col)(void *info, int j, int ind[],
  189. mpq_t val[]), void *info);
  190. /* compute LU-factorization */
  191. #define lux_f_solve _glp_lux_f_solve
  192. void lux_f_solve(LUX *lux, int tr, mpq_t x[]);
  193. /* solve system F*x = b or F'*x = b */
  194. #define lux_v_solve _glp_lux_v_solve
  195. void lux_v_solve(LUX *lux, int tr, mpq_t x[]);
  196. /* solve system V*x = b or V'*x = b */
  197. #define lux_solve _glp_lux_solve
  198. void lux_solve(LUX *lux, int tr, mpq_t x[]);
  199. /* solve system A*x = b or A'*x = b */
  200. #define lux_delete _glp_lux_delete
  201. void lux_delete(LUX *lux);
  202. /* delete LU-factorization */
  203. #endif
  204. /* eof */