Mercurial > hg > octave-lyh
comparison liboctave/CSparse.cc @ 7602:7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
author | David Bateman <dbateman@free.fr> |
---|---|
date | Wed, 19 Mar 2008 10:38:33 -0400 |
parents | b166043585a8 |
children | 36594d5bbe13 |
comparison
equal
deleted
inserted
replaced
7601:8a939b217863 | 7602:7bfaa9611558 |
---|---|
37 #include "dRowVector.h" | 37 #include "dRowVector.h" |
38 | 38 |
39 #include "CSparse.h" | 39 #include "CSparse.h" |
40 #include "boolSparse.h" | 40 #include "boolSparse.h" |
41 #include "dSparse.h" | 41 #include "dSparse.h" |
42 #include "functor.h" | |
42 #include "oct-spparms.h" | 43 #include "oct-spparms.h" |
43 #include "SparseCmplxLU.h" | 44 #include "SparseCmplxLU.h" |
44 #include "oct-sparse.h" | 45 #include "oct-sparse.h" |
45 #include "sparse-util.h" | 46 #include "sparse-util.h" |
46 #include "SparseCmplxCHOL.h" | 47 #include "SparseCmplxCHOL.h" |
7443 } | 7444 } |
7444 | 7445 |
7445 SparseMatrix | 7446 SparseMatrix |
7446 SparseComplexMatrix::map (dmapper fcn) const | 7447 SparseComplexMatrix::map (dmapper fcn) const |
7447 { | 7448 { |
7448 SparseMatrix result; | 7449 return MSparse<Complex>::map<double> (func_ptr (fcn)); |
7449 double f_zero = fcn (0.); | |
7450 | |
7451 if (f_zero != 0.) | |
7452 { | |
7453 octave_idx_type nr = rows (); | |
7454 octave_idx_type nc = cols (); | |
7455 | |
7456 result = SparseMatrix (nr, nc, f_zero); | |
7457 | |
7458 for (octave_idx_type j = 0; j < nc; j++) | |
7459 for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) | |
7460 { | |
7461 OCTAVE_QUIT; | |
7462 /* Use data instead of elem for better performance. */ | |
7463 result.data (ridx (i) + j * nr) = fcn (data(i)); | |
7464 } | |
7465 | |
7466 result.maybe_compress (true); | |
7467 } | |
7468 else | |
7469 { | |
7470 octave_idx_type nz = nnz (); | |
7471 octave_idx_type nr = rows (); | |
7472 octave_idx_type nc = cols (); | |
7473 | |
7474 result = SparseMatrix (nr, nc, nz); | |
7475 octave_idx_type ii = 0; | |
7476 result.cidx (ii) = 0; | |
7477 | |
7478 for (octave_idx_type j = 0; j < nc; j++) | |
7479 { | |
7480 for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) | |
7481 { | |
7482 double val = fcn (data (i)); | |
7483 if (val != 0.0) | |
7484 { | |
7485 result.data (ii) = val; | |
7486 result.ridx (ii++) = ridx (i); | |
7487 } | |
7488 OCTAVE_QUIT; | |
7489 } | |
7490 result.cidx (j+1) = ii; | |
7491 } | |
7492 | |
7493 result.maybe_compress (false); | |
7494 } | |
7495 | |
7496 return result; | |
7497 } | 7450 } |
7498 | 7451 |
7499 SparseComplexMatrix | 7452 SparseComplexMatrix |
7500 SparseComplexMatrix::map (cmapper fcn) const | 7453 SparseComplexMatrix::map (cmapper fcn) const |
7501 { | 7454 { |
7502 SparseComplexMatrix result; | 7455 return MSparse<Complex>::map<Complex> (func_ptr (fcn)); |
7503 Complex f_zero = fcn (0.); | |
7504 | |
7505 if (f_zero != 0.) | |
7506 { | |
7507 octave_idx_type nr = rows (); | |
7508 octave_idx_type nc = cols (); | |
7509 | |
7510 result = SparseComplexMatrix (nr, nc, f_zero); | |
7511 | |
7512 for (octave_idx_type j = 0; j < nc; j++) | |
7513 for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) | |
7514 { | |
7515 OCTAVE_QUIT; | |
7516 /* Use data instead of elem for better performance. */ | |
7517 result.data (ridx (i) + j * nr) = fcn (data(i)); | |
7518 } | |
7519 | |
7520 result.maybe_compress (true); | |
7521 } | |
7522 else | |
7523 { | |
7524 octave_idx_type nz = nnz (); | |
7525 octave_idx_type nr = rows (); | |
7526 octave_idx_type nc = cols (); | |
7527 | |
7528 result = SparseComplexMatrix (nr, nc, nz); | |
7529 Complex zero = Complex (0.0, 0.0); | |
7530 octave_idx_type ii = 0; | |
7531 result.cidx (ii) = 0; | |
7532 | |
7533 for (octave_idx_type j = 0; j < nc; j++) | |
7534 { | |
7535 for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) | |
7536 { | |
7537 Complex val = fcn (data (i)); | |
7538 if (val != zero) | |
7539 { | |
7540 result.data (ii) = val; | |
7541 result.ridx (ii++) = ridx (i); | |
7542 } | |
7543 OCTAVE_QUIT; | |
7544 } | |
7545 result.cidx (j+1) = ii; | |
7546 } | |
7547 | |
7548 result.maybe_compress (false); | |
7549 } | |
7550 | |
7551 return result; | |
7552 } | 7456 } |
7553 | 7457 |
7554 SparseBoolMatrix | 7458 SparseBoolMatrix |
7555 SparseComplexMatrix::map (bmapper fcn) const | 7459 SparseComplexMatrix::map (bmapper fcn) const |
7556 { | 7460 { |
7557 SparseBoolMatrix result; | 7461 return MSparse<Complex>::map<bool> (func_ptr (fcn)); |
7558 bool f_zero = fcn (0.); | |
7559 | |
7560 if (f_zero) | |
7561 { | |
7562 octave_idx_type nr = rows (); | |
7563 octave_idx_type nc = cols (); | |
7564 | |
7565 result = SparseBoolMatrix (nr, nc, f_zero); | |
7566 | |
7567 for (octave_idx_type j = 0; j < nc; j++) | |
7568 for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) | |
7569 { | |
7570 OCTAVE_QUIT; | |
7571 /* Use data instead of elem for better performance. */ | |
7572 result.data (ridx (i) + j * nr) = fcn (data(i)); | |
7573 } | |
7574 | |
7575 result.maybe_compress (true); | |
7576 } | |
7577 else | |
7578 { | |
7579 octave_idx_type nz = nnz (); | |
7580 octave_idx_type nr = rows (); | |
7581 octave_idx_type nc = cols (); | |
7582 | |
7583 result = SparseBoolMatrix (nr, nc, nz); | |
7584 octave_idx_type ii = 0; | |
7585 result.cidx (ii) = 0; | |
7586 | |
7587 for (octave_idx_type j = 0; j < nc; j++) | |
7588 { | |
7589 for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) | |
7590 { | |
7591 bool val = fcn (data (i)); | |
7592 if (val) | |
7593 { | |
7594 result.data (ii) = val; | |
7595 result.ridx (ii++) = ridx (i); | |
7596 } | |
7597 OCTAVE_QUIT; | |
7598 } | |
7599 result.cidx (j+1) = ii; | |
7600 } | |
7601 | |
7602 result.maybe_compress (false); | |
7603 } | |
7604 | |
7605 return result; | |
7606 } | 7462 } |
7607 | 7463 |
7608 std::ostream& | 7464 std::ostream& |
7609 operator << (std::ostream& os, const SparseComplexMatrix& a) | 7465 operator << (std::ostream& os, const SparseComplexMatrix& a) |
7610 { | 7466 { |