1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
/* permute.c */
#include "../DenseMtx.h"
/*--------------------------------------------------------------------*/
/*
-------------------------------------------
purpose -- to permute the rows of an object
created -- 98may02, cca
-------------------------------------------
*/
void
DenseMtx_permuteRows (
DenseMtx *mtx,
IV *oldToNewIV
) {
A2 a2 ;
int ii, irow, maxnrow, nrow ;
int *oldToNew, *rowind ;
/*
---------------
check the input
---------------
*/
if ( mtx == NULL || oldToNewIV == NULL ) {
fprintf(stderr, "\n fatal error in DenseMtx_permuteRows(%p,%p)"
"\n bad input\n", mtx, oldToNewIV) ;
exit(-1) ;
}
DenseMtx_rowIndices(mtx, &nrow, &rowind) ;
if ( nrow <= 0 ) {
return ;
}
/*
----------------------------------------------
overwrite the old row ids with the new row ids
----------------------------------------------
*/
IV_sizeAndEntries(oldToNewIV, &maxnrow, &oldToNew) ;
for ( ii = 0 ; ii < nrow ; ii++ ) {
irow = rowind[ii] ;
if ( irow < 0 || irow >= maxnrow ) {
fprintf(stderr, "\n fatal error in DenseMtx_permuteRows(%p,%p)"
"\n irow = %d, maxnrow = %d",
mtx, oldToNewIV, irow, maxnrow) ;
exit(-1) ;
}
rowind[ii] = oldToNew[rowind[ii]] ;
}
/*
------------------------------------
now sort the rows in ascending order
------------------------------------
*/
A2_setDefaultFields(&a2) ;
DenseMtx_setA2(mtx, &a2) ;
A2_sortRowsUp(&a2, nrow, rowind) ;
return ; }
/*--------------------------------------------------------------------*/
/*
----------------------------------------------
purpose -- to permute the columns of an object
created -- 98may02, cca
----------------------------------------------
*/
void
DenseMtx_permuteColumns (
DenseMtx *mtx,
IV *oldToNewIV
) {
A2 a2 ;
int ii, jcol, maxncol, ncol ;
int *oldToNew, *colind ;
/*
---------------
check the input
---------------
*/
if ( mtx == NULL || oldToNewIV == NULL ) {
fprintf(stderr, "\n fatal error in DenseMtx_permuteColumns(%p,%p)"
"\n bad input\n", mtx, oldToNewIV) ;
exit(-1) ;
}
DenseMtx_columnIndices(mtx, &ncol, &colind) ;
if ( ncol <= 0 ) {
return ;
}
/*
----------------------------------------------------
overwrite the old column ids with the new column ids
----------------------------------------------------
*/
IV_sizeAndEntries(oldToNewIV, &maxncol, &oldToNew) ;
for ( ii = 0 ; ii < ncol ; ii++ ) {
jcol = colind[ii] ;
if ( jcol < 0 || jcol >= maxncol ) {
fprintf(stderr,
"\n fatal error in DenseMtx_permuteColumns(%p,%p)"
"\n jcol = %d, maxncol = %d",
mtx, oldToNewIV, jcol, maxncol) ;
exit(-1) ;
}
colind[ii] = oldToNew[jcol] ;
}
/*
------------------------------------
now sort the rows in ascending order
------------------------------------
*/
A2_setDefaultFields(&a2) ;
DenseMtx_setA2(mtx, &a2) ;
A2_sortColumnsUp(&a2, ncol, colind) ;
return ; }
/*--------------------------------------------------------------------*/
|