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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
|
/* testExtract.c */
#include "../InpMtx.h"
#include "../../Drand.h"
/*--------------------------------------------------------------------*/
int
main ( int argc, char *argv[] )
/*
------------------------------------------------------------------
generate a random matrix and test the submatrix extraction method.
the output is a matlab file to test correctness.
created -- 98oct15, cca
--------------------------------------------------------------------
*/
{
int coordType, dataType, ii, msglvl, ncolA, ncol1, ncol2,
nitem, nrowA, nrow1, nrow2, rc, seed, symmetryflag ;
int *colids, *rowids, *temp ;
InpMtx *A ;
IV *cols1IV, *cols2IV, *rows1IV, *rows2IV ;
FILE *msgFile ;
if ( argc != 14 ) {
fprintf(stdout,
"\n\n %% usage : %s msglvl msgFile dataType symmetryflag coordType "
"\n %% nrowA ncolA nitem nrow1 nrow2 ncol1 ncol2 seed"
"\n %% msglvl -- message level"
"\n %% msgFile -- message file"
"\n %% dataType -- type of matrix entries"
"\n %% 1 -- real"
"\n %% 2 -- complex"
"\n %% symmetryflag -- type of matrix symmetry"
"\n %% 0 -- symmetric"
"\n %% 1 -- hermitian"
"\n %% 2 -- nonsymmetric"
"\n %% coordType -- coordinate Type"
"\n %% 1 -- by rows"
"\n %% 2 -- by columns"
"\n %% 3 -- by chevrons"
"\n %% nrowA -- number of rows in A"
"\n %% ncolA -- number of columns in A"
"\n %% nitem -- number of items to be loaded into A"
"\n %% nrow1 -- number of rows in the first block"
"\n %% nrow2 -- number of rows in the second block"
"\n %% ncol1 -- number of columns in the first block"
"\n %% ncol2 -- number of columns in the second block"
"\n %% seed -- random number seed"
"\n", argv[0]) ;
return(0) ;
}
msglvl = atoi(argv[1]) ;
if ( strcmp(argv[2], "stdout") == 0 ) {
msgFile = stdout ;
} else if ( (msgFile = fopen(argv[2], "a")) == NULL ) {
fprintf(stderr, "\n fatal error in %s"
"\n unable to open file %s\n",
argv[0], argv[2]) ;
return(-1) ;
}
dataType = atoi(argv[3]) ;
symmetryflag = atoi(argv[4]) ;
coordType = atoi(argv[5]) ;
nrowA = atoi(argv[6]) ;
ncolA = atoi(argv[7]) ;
nitem = atoi(argv[8]) ;
nrow1 = atoi(argv[9]) ;
nrow2 = atoi(argv[10]) ;
ncol1 = atoi(argv[11]) ;
ncol2 = atoi(argv[12]) ;
seed = atoi(argv[13]) ;
fprintf(msgFile,
"\n %% %s "
"\n %% msglvl -- %d"
"\n %% msgFile -- %s"
"\n %% dataType -- %d"
"\n %% symmetryflag -- %d"
"\n %% coordType -- %d"
"\n %% nrowA -- %d"
"\n %% ncolA -- %d"
"\n %% nitem -- %d"
"\n %% nrow1 -- %d"
"\n %% nrow2 -- %d"
"\n %% ncol1 -- %d"
"\n %% ncol2 -- %d"
"\n %% seed -- %d"
"\n",
argv[0], msglvl, argv[2], dataType, symmetryflag, coordType,
nrowA, ncolA, nitem, nrow1, nrow2, ncol1, ncol2, seed) ;
fflush(msgFile) ;
if ( dataType != SPOOLES_REAL && dataType != SPOOLES_COMPLEX ) {
fprintf(stderr, "\n invalid value %d for dataType\n", dataType) ;
exit(-1) ;
}
if ( symmetryflag != SPOOLES_SYMMETRIC
&& symmetryflag != SPOOLES_HERMITIAN
&& symmetryflag != SPOOLES_NONSYMMETRIC ) {
fprintf(stderr,
"\n invalid value %d for symmetryflag\n", symmetryflag) ;
exit(-1) ;
}
if ( symmetryflag == SPOOLES_HERMITIAN && dataType != SPOOLES_COMPLEX ){
fprintf(stderr,
"\n symmetryflag is hermitian, data type is not complex\n") ;
exit(-1) ;
}
if ( coordType != INPMTX_BY_ROWS
&& coordType != INPMTX_BY_COLUMNS
&& coordType != INPMTX_BY_CHEVRONS ) {
fprintf(stderr,
"\n invalid value %d for coordType\n", coordType) ;
exit(-1) ;
}
if ( nrowA <= 0 || ncolA <= 0 || nitem <= 0 ) {
fprintf(stderr,
"\n invalid value: nrow = %d, ncol = %d, nitem = %d",
nrowA, ncolA, nitem) ;
exit(-1) ;
}
fprintf(msgFile, "\n %% symmetryflag %d, nrowA %d, ncolA %d\n",
symmetryflag, nrowA, ncolA) ;
if ( ( symmetryflag == SPOOLES_SYMMETRIC
|| symmetryflag == SPOOLES_HERMITIAN)
&& nrowA != ncolA ) {
fprintf(stderr,
"\n symmetric matrix, nrowA %d, ncolA %d\n", nrowA, ncolA) ;
exit(-1) ;
}
if ( nrow1 < 0 || nrow2 < 0 || (nrow1 + nrow2 != nrowA) ) {
fprintf(stderr,
"\n invalid value: nrow = %d, nrow1 = %d, nrow2 = %d",
nrowA, nrow1, nrow2) ;
exit(-1) ;
}
if ( ncol1 < 0 || ncol2 < 0 || (ncol1 + ncol2 != ncolA) ) {
fprintf(stderr,
"\n invalid value: ncol = %d, ncol1 = %d, ncol2 = %d",
ncolA, ncol1, ncol2) ;
exit(-1) ;
}
/*
----------------------------
initialize the matrix object
----------------------------
*/
A = InpMtx_new() ;
rc = InpMtx_randomMatrix(A, dataType, coordType, INPMTX_BY_VECTORS,
nrowA, ncolA, symmetryflag, 0, nitem, seed) ;
/*
-------------------------------------------
write the assembled matrix to a matlab file
-------------------------------------------
*/
fprintf(msgFile, "\n A = zeros(%d,%d) ;", nrowA, ncolA) ;
InpMtx_writeForMatlab(A, "A", msgFile) ;
if ( symmetryflag == SPOOLES_SYMMETRIC ) {
fprintf(msgFile,
"\n A = diag(diag(A)) + triu(A,1) + transpose(triu(A,1)) ;") ;
} else if ( symmetryflag == SPOOLES_HERMITIAN ) {
fprintf(msgFile,
"\n A = diag(diag(A)) + triu(A,1) + ctranspose(triu(A,1)) ;") ;
}
/*
----------------------------------
generate row and column id vectors
----------------------------------
*/
temp = IVinit(nrowA, -1) ;
IVramp(nrowA, temp, 0, 1) ;
IVshuffle(nrowA, temp, ++seed) ;
if ( nrow1 > 0 ) {
rows1IV = IV_new() ;
IV_init(rows1IV, nrow1, NULL) ;
IVqsortUp(nrow1, temp) ;
IVcopy(nrow1, IV_entries(rows1IV), temp) ;
} else {
rows1IV = NULL ;
}
if ( nrow2 > 0 ) {
rows2IV = IV_new() ;
IV_init(rows2IV, nrow2, NULL) ;
IVqsortUp(nrow2, temp + nrow1) ;
IVcopy(nrow2, IV_entries(rows2IV), temp + nrow1) ;
} else {
rows2IV = NULL ;
}
IVfree(temp) ;
if ( symmetryflag == SPOOLES_NONSYMMETRIC ) {
temp = IVinit(ncolA, -1) ;
IVramp(ncolA, temp, 0, 1) ;
IVshuffle(ncolA, temp, ++seed) ;
if ( ncol1 > 0 ) {
cols1IV = IV_new() ;
IV_init(cols1IV, ncol1, NULL) ;
IVqsortUp(ncol1, temp) ;
IVcopy(ncol1, IV_entries(cols1IV), temp) ;
} else {
cols1IV = NULL ;
}
if ( ncol2 > 0 ) {
cols2IV = IV_new() ;
IV_init(cols2IV, ncol2, NULL) ;
IVqsortUp(ncol2, temp + ncol1) ;
IVcopy(ncol2, IV_entries(cols2IV), temp + ncol1) ;
} else {
cols2IV = NULL ;
}
IVfree(temp) ;
} else {
if ( ncol1 > 0 ) {
cols1IV = IV_new() ;
IV_init(cols1IV, ncol1, NULL) ;
IV_copy(cols1IV, rows1IV) ;
} else {
cols1IV = NULL ;
}
if ( ncol2 > 0 ) {
cols2IV = IV_new() ;
IV_init(cols2IV, ncol2, NULL) ;
IV_copy(cols2IV, rows2IV) ;
} else {
cols2IV = NULL ;
}
}
if ( nrow1 > 0 ) {
fprintf(msgFile, "\n rows1 = zeros(%d,1) ;", nrow1) ;
IV_writeForMatlab(rows1IV, "rows1", msgFile) ;
}
if ( nrow2 > 0 ) {
fprintf(msgFile, "\n rows2 = zeros(%d,1) ;", nrow2) ;
IV_writeForMatlab(rows2IV, "rows2", msgFile) ;
}
if ( ncol1 > 0 ) {
fprintf(msgFile, "\n cols1 = zeros(%d,1) ;", ncol1) ;
IV_writeForMatlab(cols1IV, "cols1", msgFile) ;
}
if ( ncol2 > 0 ) {
fprintf(msgFile, "\n cols2 = zeros(%d,1) ;", ncol2) ;
IV_writeForMatlab(cols2IV, "cols2", msgFile) ;
}
/*
-----------------------
extract the submatrices
-----------------------
*/
if ( nrow1 > 0 ) {
if ( ncol1 > 0 ) {
InpMtx *A11 = InpMtx_new() ;
rc = InpMtx_initFromSubmatrix(A11, A, rows1IV, cols1IV,
symmetryflag, msglvl, msgFile) ;
if ( rc != 1 ) {
fprintf(stderr, "\n error return %d for A11\n", rc) ;
exit(-1) ;
}
fprintf(msgFile, "\n A11 = zeros(%d,%d) ;", nrow1, ncol1) ;
InpMtx_writeForMatlab(A11, "A11", msgFile) ;
if ( symmetryflag == SPOOLES_SYMMETRIC ) {
fprintf(msgFile,
"\n A11 = diag(diag(A11)) + triu(A11,1) + transpose(triu(A11,1)) ;") ;
} else if ( symmetryflag == SPOOLES_HERMITIAN ) {
fprintf(msgFile,
"\n A11 = diag(diag(A11)) + triu(A11,1) + ctranspose(triu(A11,1)) ;") ;
}
fprintf(msgFile,
"\n err11 = max(max(abs(A11 - A(rows1,cols1)))) ;") ;
InpMtx_free(A11) ;
} else {
fprintf(msgFile, "\n err11 = 0 ;") ;
}
if ( ncol2 > 0 ) {
InpMtx *A12 = InpMtx_new() ;
rc = InpMtx_initFromSubmatrix(A12, A, rows1IV, cols2IV,
symmetryflag, msglvl, msgFile) ;
if ( rc != 1 ) {
fprintf(stderr, "\n error return %d for A12\n", rc) ;
exit(-1) ;
}
fprintf(msgFile, "\n A12 = zeros(%d,%d) ;", nrow1, ncol2) ;
InpMtx_writeForMatlab(A12, "A12", msgFile) ;
fprintf(msgFile,
"\n err12 = max(max(abs(A12 - A(rows1,cols2)))) ;") ;
InpMtx_free(A12) ;
} else {
fprintf(msgFile, "\n err12 = 0 ;") ;
}
} else {
fprintf(msgFile, "\n err11 = 0 ;") ;
fprintf(msgFile, "\n err12 = 0 ;") ;
}
if ( nrow2 > 0 ) {
if ( ncol1 > 0 ) {
InpMtx *A21 = InpMtx_new() ;
rc = InpMtx_initFromSubmatrix(A21, A, rows2IV, cols1IV,
symmetryflag, msglvl, msgFile) ;
if ( rc != 1 ) {
fprintf(stderr, "\n error return %d for A21\n", rc) ;
exit(-1) ;
}
fprintf(msgFile, "\n A21 = zeros(%d,%d) ;", nrow2, ncol1) ;
InpMtx_writeForMatlab(A21, "A21", msgFile) ;
fprintf(msgFile,
"\n err21 = max(max(abs(A21 - A(rows2,cols1)))) ;") ;
InpMtx_free(A21) ;
} else {
fprintf(msgFile, "\n err21 = 0 ;") ;
}
if ( ncol2 > 0 ) {
InpMtx *A22 = InpMtx_new() ;
rc = InpMtx_initFromSubmatrix(A22, A, rows2IV, cols2IV,
symmetryflag, msglvl, msgFile) ;
if ( rc != 1 ) {
fprintf(stderr, "\n error return %d for A22\n", rc) ;
exit(-1) ;
}
fprintf(msgFile, "\n A22 = zeros(%d,%d) ;", nrow2, ncol2) ;
InpMtx_writeForMatlab(A22, "A22", msgFile) ;
if ( symmetryflag == SPOOLES_SYMMETRIC ) {
fprintf(msgFile,
"\n A22 = diag(diag(A22)) + triu(A22,1) + transpose(triu(A22,1)) ;") ;
} else if ( symmetryflag == SPOOLES_HERMITIAN ) {
fprintf(msgFile,
"\n A22 = diag(diag(A22)) + triu(A22,1) + ctranspose(triu(A22,1)) ;") ;
}
fprintf(msgFile,
"\n err22 = max(max(abs(A22 - A(rows2,cols2)))) ;") ;
InpMtx_free(A22) ;
} else {
fprintf(msgFile, "\n err22 = 0 ;") ;
}
} else {
fprintf(msgFile, "\n err21 = 0 ;") ;
fprintf(msgFile, "\n err22 = 0 ;") ;
}
fprintf(msgFile, "\n error = [ err11 err12 ; err21 err22 ] ") ;
/*
------------------------
free the working storage
------------------------
*/
InpMtx_free(A) ;
if ( rows1IV != NULL ) { IV_free(rows1IV) ; }
if ( rows2IV != NULL ) { IV_free(rows2IV) ; }
if ( cols1IV != NULL ) { IV_free(cols1IV) ; }
if ( cols2IV != NULL ) { IV_free(cols2IV) ; }
fclose(msgFile) ;
return(1) ; }
/*--------------------------------------------------------------------*/
|