/[ascend]/trunk/base/generic/solver/mtx_perms.h
ViewVC logotype

Contents of /trunk/base/generic/solver/mtx_perms.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1015 - (show annotations) (download) (as text)
Wed Jan 3 05:03:43 2007 UTC (13 years, 1 month ago) by johnpye
File MIME type: text/x-chdr
File size: 19461 byte(s)
Trying doxygen comments once again
1 /* ASCEND modelling environment
2 Copyright (C) 2006 Carnegie Mellon University
3 Copyright (C) 1996 Benjamin Andrew Allan
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19 *//** @file
20 Sparse matrix permutation functions
21
22 Requires:
23 #include "utilities/ascConfig.h"
24 #include "mtx.h"
25 *//*
26 by Benjamin Andrew Allan
27 derived from mtx by Karl Michael Westerberg
28 Created: 5/3/90
29 Last in CVS: $Revision: 1.10 $ $Date: 1998/05/06 17:28:54 $ $Author: ballan $
30 */
31
32 #ifndef ASC_MTX_PERMS_H
33 #define ASC_MTX_PERMS_H
34
35 /** @addtogroup linear
36 * @{
37 */
38
39 /* the following block_perm functions are not yet implemented:
40 * this is the software spec. 5/3/95 baa. */
41
42 extern mtx_block_perm_t mtx_create_block_perm(mtx_matrix_t mtx);
43 /**<
44 *** Returns a token with the permutation/block information of the
45 *** mtx given. The mtx given must be previously output assigned and,
46 *** if it is to be partitioned, should already be partitioned.
47 *** The permutation returned can be used subsequently in various
48 *** ways, but all operations must be on the mtx the data came from.
49 *** If the matrix is reoutput assigned or repartitioned, the data
50 *** in bp becomes (potentially) invalid and should be completely
51 *** updated with mtx_update_block_perm.
52 *** Returns NULL if a bad mtx is detected.<br><br>
53 ***
54 *** Passes calls on slave matrices up to the master matrix.<br><br>
55 ***
56 -$- Checks extra carefully for a bad matrix, and returns NULL if so.
57 **/
58
59 extern int mtx_update_block_perm(mtx_matrix_t mtx,
60 int32 bnum,
61 mtx_block_perm_t bperm);
62 /**<
63 *** Given an mtx, a block number, and an existing bperm, this
64 *** routine updates the bperm permutation information about the
65 *** block bnum in the mtx.
66 *** The bperm updated must come from the mtx the bperm was created
67 *** for. The mtx must be output assigned and, if the mtx was
68 *** partitioned, the partition data must be consistent between the
69 *** bperm and the mtx. The mtx cannot have been resized.<br><br>
70 ***
71 *** Exceptions: If bnum is mtx_ALL_BLOCKS, we check only for the
72 *** mtx identity and output assignment. All previous permutation and
73 *** block information is ignored and replace by current info.
74 *** Calling with mtx_ALL_BLOCKS is substantially more expensive
75 *** than calling with a specific block number unless the block is
76 *** nearly the size of the problem.<br><br>
77 ***
78 *** Passes calls on slave matrices up to the master matrix.<br><br>
79 ***
80 *** Returns 1 from a bad matrix, 2 from a bad bperm, 3 from a mismatch.
81 *** Returns 0 from a successful call.<br><br>
82 ***
83 -$- Does excessive result checking, and then returns 3 if the
84 -$- excessive checks fail.
85 **/
86
87 extern int mtx_restore_block_perm(mtx_matrix_t mtx,
88 int32 bnum,
89 mtx_block_perm_t bperm);
90 /**<
91 *** Given an mtx, a block number, and an existing bperm, this
92 *** routine updates the mtx permutation information for the
93 *** block bnum using the bperm.
94 *** The mtx updated must go with the bperm.
95 *** The mtx must be output assigned and, if the mtx was
96 *** partitioned, the partition data must be consistent between the
97 *** bperm and the mtx. The mtx cannot have been resized.
98 *** The parity of the mtx will be set to that stored with the bperm;
99 *** if you try to do sneaky things, parity may get out of whack.<br><br>
100 ***
101 *** Exceptions: If bnum is mtx_ALL_BLOCKS, we check only for the
102 *** mtx identity and order. All previous permutation and
103 *** block information is ignored and replaced by bperm info.
104 *** The mtx's output_assigned and partition characteristics will
105 *** be restored to their values at the time the bperm was last
106 *** created or updated.<br><br>
107 *** Calling with mtx_ALL_BLOCKS is substantially more expensive
108 *** than calling with a specific block number if all you really
109 *** want updated is a specific block.<br><br>
110 ***
111 *** Passes calls on slave matrices up to the master matrix.<br><br>
112 ***
113 *** Returns 1 from a bad matrix, 2 from a bad bperm, 3 from a mismatch.
114 *** Returns 0 from a successful call.<br><br>
115 ***
116 -$- Does excessive checking, and then returns usual values if the
117 -$- corresponding excessive checks fail.
118 **/
119
120 extern int mtx_destroy_block_perm(mtx_block_perm_t bperm);
121 /**<
122 *** Deallocates all memory associated with the bperm.
123 *** Has nothing to do with the matrix of bperm's origin.
124 *** Returns 1 if anything untoward happens during the
125 *** destruction. Returns 0 otherwise.
126 **/
127
128 extern size_t mtx_block_perm_size(mtx_block_perm_t bperm);
129 /**<
130 *** One for the bean counters. Returns current memory used by
131 *** the mtx_block_perm_t. Bytes as usual.
132 **/
133
134 /* end block_perm functions */
135
136 /*------------------------------------------------------------------------------
137 MTX PERMUTATION AND PERMUTATION INFO ROUTINES
138 */
139
140 extern void mtx_swap_rows(mtx_matrix_t mtx, int32 row1, int32 row2);
141 /**<
142 *** Swaps two rows of the matrix. The association between the
143 *** "original row number" and the row contents is not
144 *** changed, so that some record is kept as to where a given row
145 *** originally came from (see mtx_org_to_row(), etc.).
146 ***
147 *** Passes calls on slave matrices up to the master matrix.
148 -$- Does nothing to a bad matrix.
149 **/
150 ASC_DLLSPEC(void ) mtx_swap_cols(mtx_matrix_t mtx, int32 col1, int32 col2);
151 /**<
152 *** Swaps two columns of the matrix. The association between the
153 *** "original column number" and the column contents is not
154 *** changed, so that some record is kept as to where a given column
155 *** originally came from (see mtx_org_to_row(), etc.).
156 ***
157 *** Passes calls on slave matrices up to the master matrix.
158 -$- Does nothing to a bad matrix.
159 **/
160 extern void mtx_drag(mtx_matrix_t mtx, int32 d1, int32 d2);
161 /**<
162 *** Drag shifts diagonal element d1 to position d2, or vice versa,
163 *** rotating all the intermediate elements as if d1 were swapped
164 *** (row and col) with all the intermediate di.<br><br>
165 *** Drag exists because it is twice the speed of the following
166 *** implementation outside of mtx: <pre>
167 *** while( n1 < n2 ) {
168 *** mtx_swap_rows(mtx,n1,n1+1);
169 *** mtx_swap_cols(mtx,n1,n1+1);
170 *** ++n1;
171 *** }
172 *** while( n1 > n2 ) {
173 *** mtx_swap_rows(mtx,n1,n1-1);
174 *** mtx_swap_cols(mtx,n1,n1-1);
175 *** --n1;
176 *** } </pre>
177 *** If it turns out that a cycle_col or cycle_row (independent of diagonal)
178 *** is wanted, implementation is now trivial.
179 ***
180 *** Passes calls on slave matrices up to the master matrix.
181 -$- Does nothing to a bad matrix.
182 **/
183 extern void mtx_reverse_diagonal(mtx_matrix_t mtx, int32 d1, int32 d2);
184 /**<
185 *** Does a series of symmetric permutations that reverses
186 *** the ordering of the diagonal between (and including) d1 & d2.
187 *** If d2 < d1, does nothing.
188 ***
189 *** Passes calls on slave matrices up to the master matrix.
190 -$- Does nothing to a bad matrix.
191 **/
192
193
194 ASC_DLLSPEC(int32 ) mtx_row_to_org(mtx_matrix_t mtx, int32 row);
195 /**<
196 Converts original row number <--> row number.
197 Passes calls on slave matrices up to the master matrix.
198
199 @return original row number, or -1 from a bad matrix.
200 */
201
202 ASC_DLLSPEC(int32 ) mtx_col_to_org(mtx_matrix_t mtx, int32 col);
203 /**<
204 Converts original column number <--> column number.
205 Passes calls on slave matrices up to the master matrix.
206
207 @return original column numner, or -1 from a bad matrix.
208 */
209
210 ASC_DLLSPEC(int32 ) mtx_org_to_row(mtx_matrix_t mtx, int32 orgrow);
211 /**<
212 Converts original row number <--> row number.
213 Passes calls on slave matrices up to the master matrix.
214
215 @return row number, or -1 from a bad matrix.
216 */
217
218 ASC_DLLSPEC(int32 ) mtx_org_to_col(mtx_matrix_t mtx, int32 orgcol);
219 /**<
220 Converts original column number <--> column number.
221 Passes calls on slave matrices up to the master matrix.
222
223 @return column number, or -1 in case of a bad matrix.
224 */
225
226
227 extern boolean mtx_row_parity(mtx_matrix_t mtx);
228 /**<
229 *** Returns the parity (even=FALSE,odd=TRUE) of the permutation which
230 *** carries original row to current row numbers.
231 *** Passes calls on slave matrices up to the master matrix.
232 -$- Returns -1 from a bad matrix.
233 **/
234 extern boolean mtx_col_parity(mtx_matrix_t mtx);
235 /**<
236 *** Returns the parity (even=FALSE,odd=TRUE) of the permutation which
237 *** carries original column to current column numbers.
238 *** Passes calls on slave matrices up to the master matrix.
239 -$- Returns -1 from a bad matrix.
240 **/
241
242 /*------------------------------------------------------------------------------
243 MTX STRUCTURAL MANIPULATION AND INFO ROUTINES
244 */
245
246 extern int mtx_output_assign_region(mtx_matrix_t mtx,
247 mtx_region_t *region,
248 int *orphaned_rows);
249 /**<
250 *** This function is very similar to its sister function mtx_output_assign.
251 *** It reorders the matrix to put as many non-zeros on the diagonal as
252 *** possible. It mtx_ENTIRE_MATRIX is sent in as the region, the output
253 *** assignment will affect the entire matrix. Otherwise the output
254 *** assignment will be restricted to the designated region.
255 *** This function returns the symbolic rank of the matrix. If a row can
256 *** not be assigned it is moved to the end of lower edge of the matrix.
257 *** The count of the unassigned aka orphaned rows is returned also.
258 *** Unlike mtx_output_assign nothing else is done to the matrix.<br><br>
259 ***
260 *** Calls on slaves are passed up to the master matrix.
261 ***
262 -$- Does nothing to a bad matrix.
263 **/
264
265 extern void mtx_output_assign(mtx_matrix_t mtx, int32 hirow, int32 hicol);
266 /**<
267 *** Reorders the matrix to put as many non-zeros on the diagonal as
268 *** possible. This function does not assume the validity of a previous
269 *** assignment. mtx_symbolic_rank() may be subsequently called to produce
270 *** the symbolic rank. All assigned rows and columns go into forming
271 *** a single initial block which is of dimension equal to the symbolic
272 *** rank of the matrix.<br><br>
273 ***
274 *** All unassigned rows and columns with incidence are then placed
275 *** against the borders of the nonsingular region while all empty rows
276 *** and columns are moved toward the outer borders of the matrix.
277 *** If hirow and hicol are not improperly small, we will guarantee that all
278 *** original rows r, 0 <= r < hirow, and original columns c,
279 *** 0 <= c < hicol, are placed next to the initial block. That is,
280 *** if that block is contained within the block ((0,0),(hirow,hicol)).
281 *** This is not a particularly interesting property mathematically, but
282 *** it makes things loads easier when writing interfaces that involve
283 *** mtx objects; the first hirow rows and hicol cols of the jacobian will
284 *** correspond to some relation or variable even if they haven't incidence.
285 -$- Does nothing to a bad matrix.<br><br>
286 *** NOTE:
287 *** When used on an incidence matrix, the fixed vars and unincluded
288 *** relations which don't generate elements may end up anywhere between
289 *** the initial block and the ends of the matrix if hirow and hicol are
290 *** not specified correctly.<br><br>
291 ***
292 *** Calls on slaves are passed up to the master matrix.
293 ***
294 *** Note that mtx_output_assign invalidates any data saved
295 *** with the mtx_*_block_perm functions.
296 **/
297
298 ASC_DLLSPEC(boolean ) mtx_output_assigned(mtx_matrix_t mtx);
299 /**<
300 *** Determines if the matrix has been previously output assigned.
301 *** Calls on slaves are passed up to the master matrix.
302 ***
303 -$- Returns FALSE on a bad matrix.
304 **/
305
306 ASC_DLLSPEC(int32 ) mtx_symbolic_rank(mtx_matrix_t mtx);
307 /**<
308 *** Returns the symbolic rank determined by a previous call to
309 *** mtx_output_assign().
310 *** Calls on slaves are passed up to the master matrix.
311 ***
312 -$- Returns -2 on bad matrix or -1 on unassigned one.
313 **/
314
315 extern void mtx_set_symbolic_rank(mtx_matrix_t mtx, int32 rank);
316 /**<
317 *** Sets symbolic rank of mtx. This is used in a hack
318 *** and is not intended for general use.
319 **/
320
321 extern boolean mtx_make_col_independent(mtx_matrix_t mtx,
322 int32 col,
323 mtx_range_t *rng);
324 /**<
325 *** Removes col from the basis in place of one of the columns in rng.
326 *** I.e. redoes the mtx_output_assign so as to not use col, if
327 *** possible, using one of the cols in rng instead.
328 *** Clears any prior partitioning data.
329 *** Note that mtx_make_col_independent invalidates any data saved
330 *** with the mtx_*_block_perm functions.<br><br>
331 ***
332 *** Calls on slaves are passed up to the master matrix.
333 **/
334
335 extern void mtx_org_permute(mtx_matrix_t mtx, mtx_region_t * region);
336 /**<
337 *** Given a region, repermutes rows and columns within it to the ordering
338 *** where mtx_row_to_org(mtx,i) < mtx_row_to_org(mtx,i+1) for all i and
339 *** where mtx_col_to_org(mtx,j) < mtx_row_to_org(mtx,j+1) for all j
340 *** within the region. At present this is implemented as a qsort and
341 *** may be a little pricey.<br><br>
342 ***
343 *** mtx_org_permute(mtx,mtx_ENTIRE_MATRIX); is equivalent to
344 *** mtx_reset_perm(mtx);
345 **/
346
347 extern int32 mtx_full_diagonal(mtx_matrix_t mtx, mtx_range_t *rng, int noisy);
348 /**<
349 *** This function checks the diagonal for holes. If symbolic_rank is
350 *** set, and rng->high < rank, returns immediately.
351 *** Returns number of holes detected in diagonal in rng given.
352 *** If noisy != 0, writes the hole locations to g_mtxerr.
353 *** On detectably bad input, returns -1.
354 *** Worst Cost: O(nnz) where nnz = incidence count sum over rows in rng.
355 *** Usual Cost: O(n) where n = a small factor *(rng->high - rng->low +1)
356 *** Does not pass calls up to master.
357 **/
358
359 extern int32 mtx_transpose(mtx_matrix_t mtx);
360 /**<
361 *** Transposes everything about the matrix. The user is
362 *** responsible for keeping track of the change in the semantics
363 *** this implies if the matrix is being used in a nonlinear context.<br><br>
364 ***
365 *** Transposing a matrix also transposes all associated slave/master
366 *** matrices.
367 *** To check if a matrix has been transposed, use mtx_isa_transpose.<br><br>
368 *** Cost O(Nnz).
369 *** 0 order matrices cannot be transposed.
370 -$- Returns mtx_NONE on a bad or 0 order matrix.
371 **/
372
373 extern int32 mtx_isa_transpose(mtx_matrix_t mtx);
374 /**<
375 *** Returns 1 if the matrix is transposed from another and 0 if not.
376 *** Calling mtx_transpose twice yields a mtx which responds with 0.
377 -$- Returns mtx_NONE on a bad or 0 order matrix.
378 **/
379
380 /*------------------------------------------------------------------------------
381 START OF SOME NEW BLOCK MATRIX ROUTINES.
382
383 At the moment the block structure is intimately tied up with the matrix.
384 */
385
386 extern mtx_block_t *mtx_block_partition(mtx_matrix_t mtx, mtx_region_t *reg);
387 /**<
388 *** Partitions the single block of a previously output assigned matrix
389 *** into smaller blocks if possible. This function unlike its sister
390 *** mtx_partition, takes in, and returns information explicitly, rather
391 *** than assuming it to be a property of the matrix. The result will
392 *** be NULL, if there are no blocks. At the moment works only on
393 *** a square matrix. The range reg->row is assumed to represent the
394 *** extreme points of a square region. The caller owns the result and
395 *** must be deallocate it.
396 *** If the matrix given (or its master) is not assigned, this will
397 *** verify (at some expense) that the diagonal is full and whine
398 *** if not. It will attempt to partition anyway assuming a diagonal.<br><br>
399 ***
400 *** Calls on slaves are passed up to the master matrix.
401 **/
402
403
404 /*------------------------------------------------------------------------------
405 THE ORIGINAL BLOCK MANIPULATION ROUTINES.
406 */
407
408 extern void mtx_partition(mtx_matrix_t mtx);
409 /**<
410 *** Takes an output assigned matrix and does a permutation to
411 *** block-lower-triangular form of the square region from
412 *** 0,0 to symbolic_rank-1, symbolic_rank-1.<br><br>
413 ***
414 *** Calls on slaves are passed up to the master matrix.
415 **/
416
417 extern void mtx_ut_partition(mtx_matrix_t mtx);
418 /**<
419 *** Takes an output assigned matrix and does a permutation to
420 *** block-UPPER-triangular form of the square region from
421 *** 0,0 to symbolic_rank-1, symbolic_rank-1.<br><br>
422 ***
423 *** Calls on slaves are passed up to the master matrix.
424 **/
425
426 extern boolean mtx_check_blocks(mtx_matrix_t mtx);
427 /**<
428 *** Checks whether or not the block data are consistent with the
429 *** non-zero structure of the matrix. If not, the block data
430 *** are destroyed. Matrix must be previously output assigned,
431 *** if not (or bad matrix) nothing is done.<br><br>
432 ***
433 *** Calls on slaves are passed up to the master matrix.
434 **/
435
436 extern int32 mtx_number_of_blocks(mtx_matrix_t mtx);
437 /**<
438 *** Returns the number of blocks in the matrix. Matrix must be
439 *** previously output assigned.
440 ***
441 *** Calls on slaves are passed up to the master matrix.
442 ***
443 -$- Returns mtx_NONE if matrix is bad which is liable to lead to core
444 -$- dumping if the user is not checking for a bad return.
445 **/
446
447 extern int32 mtx_block(mtx_matrix_t mtx,
448 int32 block_number,
449 mtx_region_t *block);
450 /**<
451 *** Sets block to the "block_number"-th block (indexed 0 to nblocks-1) in
452 *** the matrix. Matrix must be previously output assigned.
453 *** Returns mtx_NONE if not previously output assigned
454 *** or if requested block does not exist, or if no blocks exist,
455 *** otherwise returns 0.
456 *** If mtx_NONE is returned, block will contain the region
457 *** ((0,order-1),(0,order-1)) where order is the mtx_order.<br><br>
458 ***
459 *** Calls on slaves are passed up to the master matrix.
460 **/
461
462 ASC_DLLSPEC(int32 ) mtx_block_containing_row(mtx_matrix_t mtx,
463 int32 row,
464 mtx_region_t *block);
465 /**<
466 *** Sets block to the block which contains the given row and
467 *** returns the block number. If the given row doesn't belong
468 *** to any block, mtx_NONE is returned. Matrix must be previously output
469 *** assigned or a 0 is returned.<br><br>
470 ***
471 *** Calls on slaves are passed up to the master matrix.
472 **/
473 extern int32 mtx_block_containing_col(mtx_matrix_t mtx,
474 int32 col,
475 mtx_region_t *block);
476 /**<
477 *** Sets block to the block which contains the given column and
478 *** returns the block number. If the given column doesn't belong
479 *** to any block, mtx_NONE is returned. Matrix must be previously output
480 *** assigned or a 0 is returned.<br><br>
481 ***
482 *** Calls on slaves are passed up to the master matrix.
483 **/
484
485 /** @} */
486
487 #endif /* ASC_MTX_PERMS_H */

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22