glpios04.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /* glpios04.c (operations on sparse vectors) */
  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 Andrew Makhorin, Department for Applied Informatics,
  7. * Moscow Aviation Institute, Moscow, Russia. All rights reserved.
  8. * 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. #include "glpios.h"
  24. /***********************************************************************
  25. * NAME
  26. *
  27. * ios_create_vec - create sparse vector
  28. *
  29. * SYNOPSIS
  30. *
  31. * #include "glpios.h"
  32. * IOSVEC *ios_create_vec(int n);
  33. *
  34. * DESCRIPTION
  35. *
  36. * The routine ios_create_vec creates a sparse vector of dimension n,
  37. * which initially is a null vector.
  38. *
  39. * RETURNS
  40. *
  41. * The routine returns a pointer to the vector created. */
  42. IOSVEC *ios_create_vec(int n)
  43. { IOSVEC *v;
  44. xassert(n >= 0);
  45. v = xmalloc(sizeof(IOSVEC));
  46. v->n = n;
  47. v->nnz = 0;
  48. v->pos = xcalloc(1+n, sizeof(int));
  49. memset(&v->pos[1], 0, n * sizeof(int));
  50. v->ind = xcalloc(1+n, sizeof(int));
  51. v->val = xcalloc(1+n, sizeof(double));
  52. return v;
  53. }
  54. /***********************************************************************
  55. * NAME
  56. *
  57. * ios_check_vec - check that sparse vector has correct representation
  58. *
  59. * SYNOPSIS
  60. *
  61. * #include "glpios.h"
  62. * void ios_check_vec(IOSVEC *v);
  63. *
  64. * DESCRIPTION
  65. *
  66. * The routine ios_check_vec checks that a sparse vector specified by
  67. * the parameter v has correct representation.
  68. *
  69. * NOTE
  70. *
  71. * Complexity of this operation is O(n). */
  72. void ios_check_vec(IOSVEC *v)
  73. { int j, k, nnz;
  74. xassert(v->n >= 0);
  75. nnz = 0;
  76. for (j = v->n; j >= 1; j--)
  77. { k = v->pos[j];
  78. xassert(0 <= k && k <= v->nnz);
  79. if (k != 0)
  80. { xassert(v->ind[k] == j);
  81. nnz++;
  82. }
  83. }
  84. xassert(v->nnz == nnz);
  85. return;
  86. }
  87. /***********************************************************************
  88. * NAME
  89. *
  90. * ios_get_vj - retrieve component of sparse vector
  91. *
  92. * SYNOPSIS
  93. *
  94. * #include "glpios.h"
  95. * double ios_get_vj(IOSVEC *v, int j);
  96. *
  97. * RETURNS
  98. *
  99. * The routine ios_get_vj returns j-th component of a sparse vector
  100. * specified by the parameter v. */
  101. double ios_get_vj(IOSVEC *v, int j)
  102. { int k;
  103. xassert(1 <= j && j <= v->n);
  104. k = v->pos[j];
  105. xassert(0 <= k && k <= v->nnz);
  106. return (k == 0 ? 0.0 : v->val[k]);
  107. }
  108. /***********************************************************************
  109. * NAME
  110. *
  111. * ios_set_vj - set/change component of sparse vector
  112. *
  113. * SYNOPSIS
  114. *
  115. * #include "glpios.h"
  116. * void ios_set_vj(IOSVEC *v, int j, double val);
  117. *
  118. * DESCRIPTION
  119. *
  120. * The routine ios_set_vj assigns val to j-th component of a sparse
  121. * vector specified by the parameter v. */
  122. void ios_set_vj(IOSVEC *v, int j, double val)
  123. { int k;
  124. xassert(1 <= j && j <= v->n);
  125. k = v->pos[j];
  126. if (val == 0.0)
  127. { if (k != 0)
  128. { /* remove j-th component */
  129. v->pos[j] = 0;
  130. if (k < v->nnz)
  131. { v->pos[v->ind[v->nnz]] = k;
  132. v->ind[k] = v->ind[v->nnz];
  133. v->val[k] = v->val[v->nnz];
  134. }
  135. v->nnz--;
  136. }
  137. }
  138. else
  139. { if (k == 0)
  140. { /* create j-th component */
  141. k = ++(v->nnz);
  142. v->pos[j] = k;
  143. v->ind[k] = j;
  144. }
  145. v->val[k] = val;
  146. }
  147. return;
  148. }
  149. /***********************************************************************
  150. * NAME
  151. *
  152. * ios_clear_vec - set all components of sparse vector to zero
  153. *
  154. * SYNOPSIS
  155. *
  156. * #include "glpios.h"
  157. * void ios_clear_vec(IOSVEC *v);
  158. *
  159. * DESCRIPTION
  160. *
  161. * The routine ios_clear_vec sets all components of a sparse vector
  162. * specified by the parameter v to zero. */
  163. void ios_clear_vec(IOSVEC *v)
  164. { int k;
  165. for (k = 1; k <= v->nnz; k++)
  166. v->pos[v->ind[k]] = 0;
  167. v->nnz = 0;
  168. return;
  169. }
  170. /***********************************************************************
  171. * NAME
  172. *
  173. * ios_clean_vec - remove zero or small components from sparse vector
  174. *
  175. * SYNOPSIS
  176. *
  177. * #include "glpios.h"
  178. * void ios_clean_vec(IOSVEC *v, double eps);
  179. *
  180. * DESCRIPTION
  181. *
  182. * The routine ios_clean_vec removes zero components and components
  183. * whose magnitude is less than eps from a sparse vector specified by
  184. * the parameter v. If eps is 0.0, only zero components are removed. */
  185. void ios_clean_vec(IOSVEC *v, double eps)
  186. { int k, nnz;
  187. nnz = 0;
  188. for (k = 1; k <= v->nnz; k++)
  189. { if (fabs(v->val[k]) == 0.0 || fabs(v->val[k]) < eps)
  190. { /* remove component */
  191. v->pos[v->ind[k]] = 0;
  192. }
  193. else
  194. { /* keep component */
  195. nnz++;
  196. v->pos[v->ind[k]] = nnz;
  197. v->ind[nnz] = v->ind[k];
  198. v->val[nnz] = v->val[k];
  199. }
  200. }
  201. v->nnz = nnz;
  202. return;
  203. }
  204. /***********************************************************************
  205. * NAME
  206. *
  207. * ios_copy_vec - copy sparse vector (x := y)
  208. *
  209. * SYNOPSIS
  210. *
  211. * #include "glpios.h"
  212. * void ios_copy_vec(IOSVEC *x, IOSVEC *y);
  213. *
  214. * DESCRIPTION
  215. *
  216. * The routine ios_copy_vec copies a sparse vector specified by the
  217. * parameter y to a sparse vector specified by the parameter x. */
  218. void ios_copy_vec(IOSVEC *x, IOSVEC *y)
  219. { int j;
  220. xassert(x != y);
  221. xassert(x->n == y->n);
  222. ios_clear_vec(x);
  223. x->nnz = y->nnz;
  224. memcpy(&x->ind[1], &y->ind[1], x->nnz * sizeof(int));
  225. memcpy(&x->val[1], &y->val[1], x->nnz * sizeof(double));
  226. for (j = 1; j <= x->nnz; j++)
  227. x->pos[x->ind[j]] = j;
  228. return;
  229. }
  230. /***********************************************************************
  231. * NAME
  232. *
  233. * ios_linear_comb - compute linear combination (x := x + a * y)
  234. *
  235. * SYNOPSIS
  236. *
  237. * #include "glpios.h"
  238. * void ios_linear_comb(IOSVEC *x, double a, IOSVEC *y);
  239. *
  240. * DESCRIPTION
  241. *
  242. * The routine ios_linear_comb computes the linear combination
  243. *
  244. * x := x + a * y,
  245. *
  246. * where x and y are sparse vectors, a is a scalar. */
  247. void ios_linear_comb(IOSVEC *x, double a, IOSVEC *y)
  248. { int j, k;
  249. double xj, yj;
  250. xassert(x != y);
  251. xassert(x->n == y->n);
  252. for (k = 1; k <= y->nnz; k++)
  253. { j = y->ind[k];
  254. xj = ios_get_vj(x, j);
  255. yj = y->val[k];
  256. ios_set_vj(x, j, xj + a * yj);
  257. }
  258. return;
  259. }
  260. /***********************************************************************
  261. * NAME
  262. *
  263. * ios_delete_vec - delete sparse vector
  264. *
  265. * SYNOPSIS
  266. *
  267. * #include "glpios.h"
  268. * void ios_delete_vec(IOSVEC *v);
  269. *
  270. * DESCRIPTION
  271. *
  272. * The routine ios_delete_vec deletes a sparse vector specified by the
  273. * parameter v freeing all the memory allocated to this object. */
  274. void ios_delete_vec(IOSVEC *v)
  275. { /* delete sparse vector */
  276. xfree(v->pos);
  277. xfree(v->ind);
  278. xfree(v->val);
  279. xfree(v);
  280. return;
  281. }
  282. /* eof */