1 /* glpios04.c (operations on sparse vectors) */
3 /***********************************************************************
4 * This code is part of GLPK (GNU Linear Programming Kit).
6 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
7 * 2009, 2010 Andrew Makhorin, Department for Applied Informatics,
8 * Moscow Aviation Institute, Moscow, Russia. All rights reserved.
9 * E-mail: <mao@gnu.org>.
11 * GLPK is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * GLPK is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
19 * License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
23 ***********************************************************************/
27 /***********************************************************************
30 * ios_create_vec - create sparse vector
35 * IOSVEC *ios_create_vec(int n);
39 * The routine ios_create_vec creates a sparse vector of dimension n,
40 * which initially is a null vector.
44 * The routine returns a pointer to the vector created. */
46 IOSVEC *ios_create_vec(int n)
49 v = xmalloc(sizeof(IOSVEC));
52 v->pos = xcalloc(1+n, sizeof(int));
53 memset(&v->pos[1], 0, n * sizeof(int));
54 v->ind = xcalloc(1+n, sizeof(int));
55 v->val = xcalloc(1+n, sizeof(double));
59 /***********************************************************************
62 * ios_check_vec - check that sparse vector has correct representation
67 * void ios_check_vec(IOSVEC *v);
71 * The routine ios_check_vec checks that a sparse vector specified by
72 * the parameter v has correct representation.
76 * Complexity of this operation is O(n). */
78 void ios_check_vec(IOSVEC *v)
82 for (j = v->n; j >= 1; j--)
84 xassert(0 <= k && k <= v->nnz);
86 { xassert(v->ind[k] == j);
90 xassert(v->nnz == nnz);
94 /***********************************************************************
97 * ios_get_vj - retrieve component of sparse vector
101 * #include "glpios.h"
102 * double ios_get_vj(IOSVEC *v, int j);
106 * The routine ios_get_vj returns j-th component of a sparse vector
107 * specified by the parameter v. */
109 double ios_get_vj(IOSVEC *v, int j)
111 xassert(1 <= j && j <= v->n);
113 xassert(0 <= k && k <= v->nnz);
114 return (k == 0 ? 0.0 : v->val[k]);
117 /***********************************************************************
120 * ios_set_vj - set/change component of sparse vector
124 * #include "glpios.h"
125 * void ios_set_vj(IOSVEC *v, int j, double val);
129 * The routine ios_set_vj assigns val to j-th component of a sparse
130 * vector specified by the parameter v. */
132 void ios_set_vj(IOSVEC *v, int j, double val)
134 xassert(1 <= j && j <= v->n);
138 { /* remove j-th component */
141 { v->pos[v->ind[v->nnz]] = k;
142 v->ind[k] = v->ind[v->nnz];
143 v->val[k] = v->val[v->nnz];
150 { /* create j-th component */
160 /***********************************************************************
163 * ios_clear_vec - set all components of sparse vector to zero
167 * #include "glpios.h"
168 * void ios_clear_vec(IOSVEC *v);
172 * The routine ios_clear_vec sets all components of a sparse vector
173 * specified by the parameter v to zero. */
175 void ios_clear_vec(IOSVEC *v)
177 for (k = 1; k <= v->nnz; k++)
178 v->pos[v->ind[k]] = 0;
183 /***********************************************************************
186 * ios_clean_vec - remove zero or small components from sparse vector
190 * #include "glpios.h"
191 * void ios_clean_vec(IOSVEC *v, double eps);
195 * The routine ios_clean_vec removes zero components and components
196 * whose magnitude is less than eps from a sparse vector specified by
197 * the parameter v. If eps is 0.0, only zero components are removed. */
199 void ios_clean_vec(IOSVEC *v, double eps)
202 for (k = 1; k <= v->nnz; k++)
203 { if (fabs(v->val[k]) == 0.0 || fabs(v->val[k]) < eps)
204 { /* remove component */
205 v->pos[v->ind[k]] = 0;
208 { /* keep component */
210 v->pos[v->ind[k]] = nnz;
211 v->ind[nnz] = v->ind[k];
212 v->val[nnz] = v->val[k];
219 /***********************************************************************
222 * ios_copy_vec - copy sparse vector (x := y)
226 * #include "glpios.h"
227 * void ios_copy_vec(IOSVEC *x, IOSVEC *y);
231 * The routine ios_copy_vec copies a sparse vector specified by the
232 * parameter y to a sparse vector specified by the parameter x. */
234 void ios_copy_vec(IOSVEC *x, IOSVEC *y)
237 xassert(x->n == y->n);
240 memcpy(&x->ind[1], &y->ind[1], x->nnz * sizeof(int));
241 memcpy(&x->val[1], &y->val[1], x->nnz * sizeof(double));
242 for (j = 1; j <= x->nnz; j++)
243 x->pos[x->ind[j]] = j;
247 /***********************************************************************
250 * ios_linear_comb - compute linear combination (x := x + a * y)
254 * #include "glpios.h"
255 * void ios_linear_comb(IOSVEC *x, double a, IOSVEC *y);
259 * The routine ios_linear_comb computes the linear combination
263 * where x and y are sparse vectors, a is a scalar. */
265 void ios_linear_comb(IOSVEC *x, double a, IOSVEC *y)
269 xassert(x->n == y->n);
270 for (k = 1; k <= y->nnz; k++)
272 xj = ios_get_vj(x, j);
274 ios_set_vj(x, j, xj + a * yj);
279 /***********************************************************************
282 * ios_delete_vec - delete sparse vector
286 * #include "glpios.h"
287 * void ios_delete_vec(IOSVEC *v);
291 * The routine ios_delete_vec deletes a sparse vector specified by the
292 * parameter v freeing all the memory allocated to this object. */
294 void ios_delete_vec(IOSVEC *v)
295 { /* delete sparse vector */