3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
10 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12 * Copyright (c) 2001-2004, The GROMACS development team,
13 * check out http://www.gromacs.org for more information.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * If you want to redistribute modifications, please consider that
21 * scientific software is very special. Version control is crucial -
22 * bugs must be traceable. We will be happy to consider code for
23 * inclusion in the official distribution, but derived work must not
24 * be called official GROMACS. Details are found in the README & COPYING
25 * files - if they are missing, get the official version at www.gromacs.org.
27 * To help us fund GROMACS development, we humbly ask that you cite
28 * the papers on the package - you can find them in the top README file.
30 * For more info, check our website at http://www.gromacs.org
33 * Green Red Orange Magenta Azure Cyan Skyblue
39 #ifndef _SPARSEMATRIX_H_
40 #define _SPARSEMATRIX_H_
43 #include "types/simple.h"
49 /*! \brief Sparse matrix storage format
51 * This structure specifies a storage format for a sparse matrix.
52 * The memory requirements are only proportional to the number
53 * of nonzero elements, and it provides a reasonably fast way to
54 * perform matrix-vector multiplications.
56 * The data format is very similar to a neighborlist. It is optimized
57 * for fast access, but it is difficult to add entries. If you are
58 * constructing a matrix you should either do it in exactly the order
59 * specified here, or use some other more flexible intermediate structure.
61 * The index array is of size nrow+1. All non-zero matrix elements
62 * on row i are stored in positions index[i] through index[i+1]-1 in
63 * the arrays column and value. The column array contains the column
64 * index for each entry, in ascending order, and the corresponding
65 * position in the value array contains the floating point matrix element.
67 * index[nrow] should be equal to the total number of elements stored.
69 * Thus, to find the value of matrix element [5,4] you should loop
70 * over positions index[5] to index[6]-1 in column until you either find
71 * the value 4, or a higher value (meaning the element was zero).
73 * It is fairly easy to construct the matrix on-the-fly if you can do
77 * If compressed_symmetric is set to TRUE, you should only store EITHER the upper OR
78 * lower triangle (and the diagonal), and the other half is assumed to be
79 * symmetric. Otherwise, if compressed_symmetric==FALSE, no symmetry is implied and all
80 * elements should be stored.
82 * The symmetry compression saves us a factor 2 both in storage and
83 * matrix multiplication CPU-time, which can be very useful for huge eigenproblems.
85 * If you are unsure, just set compressed_symmetric to FALSE and list all elements. If
86 * you enable it but still list all elements (both upper and lower triangle) you will be sorry...
88 * Internally, the sparse data is stored as a separate list for each row, where the list
89 * element is a structure with a column and (floating-point) data value. This makes it
90 * possible, although not completely transparent, to update values in random access order.
91 * The drawback is that the structure will allocate nrow memory regions.
92 * The matrix data could be stored in a single contiguous array with indices for each row,
93 * but then we could only insert elements at the end without copying the entire matrix.
97 * In other words: Not perfect, but it works.
101 gmx_sparsematrix_entry
105 } gmx_sparsematrix_entry_t
;
110 bool compressed_symmetric
; /*!< Store half elements and assume symmetry. */
111 int nrow
; /*!< Number of rows in matrix */
112 int * ndata
; /*!< Number of entries on each row (list) */
113 int * nalloc
; /*!< Allocated entry list length for each row */
114 gmx_sparsematrix_entry_t
** data
; /*!< data[i] is a list with entries on row i */
119 /*! \Allocate a new sparse matrix structure
121 * The number of rows is used to allocate the index array entry. Obviously you
122 * can reallocate these later yourself if necessary - this is a
123 * convenience routine.
125 * By default, the compressed_symmetric flag in the structure will
126 * be FALSE. Set it to TRUE manually if you are only storing either the
127 * upper or lower half of the matrix.
130 gmx_sparsematrix_init (int nrow
);
133 /*! \brief Release all resources used by a sparse matrix structure
135 * All arrays in the structure will be freed, and the structure itself.
138 gmx_sparsematrix_destroy (gmx_sparsematrix_t
* A
);
141 /*! \brief Print sparse matrix to a stream.
143 * Mainly used for debugging. Be warned that the real sparse matrices used
144 * in Gromacs runs can be HUGE (think 100,000 rows).
147 gmx_sparsematrix_print (FILE * stream
,
148 gmx_sparsematrix_t
* A
);
150 /* Adds value at row,col. If the value did not exist
151 * previously it is added, otherwise it is incremented with difference.
153 * The column sort order might change, so you need to run fix_sparsematrix
154 * once you are done changing the matrix.
157 gmx_sparsematrix_value (gmx_sparsematrix_t
* A
,
162 /* Adds value at row,col. If the value did not exist
163 * previously it is added, otherwise it is incremented with difference.
165 * The column sort order might change, so you need to run fix_sparsematrix
166 * once you are done changing the matrix.
169 gmx_sparsematrix_increment_value(gmx_sparsematrix_t
* A
,
176 /*! \brief Sort elements in each column and remove zeros.
178 * Sparse matrix access is faster when the elements are stored in
179 * increasing column order in each row. In some cases previously non-zero
180 * elements will be zero after adding more data, and this routine also removes
181 * those entries to reduce the storage requirements.
183 * It never hurts to run this routine if you have been updating the matrix...
186 gmx_sparsematrix_compress (gmx_sparsematrix_t
* A
);
190 /*! \brief Sparse matrix vector multiplication
192 * Calculate y = A * x for a sparse matrix A.
195 gmx_sparsematrix_vector_multiply(gmx_sparsematrix_t
* A
,