Mercurial > octave
comparison liboctave/array/dSparse.cc @ 17691:8a54a481ecb5
Massive speed improvement in sparse min/max functions for calls like
'max(s,[],2)' (bug #40268)
* dSparse.cc (SparseMatrix SparseMatrix::max (Array<octave_idx-type>&, int)) :
Remove triple loop by using a local buffer
(SparseMatrix SparseMatrix::min (Array<octave_idx-type>&, int)) : ditto
* CSparse.cc (SparseComplexMatrix SparseComplexMatrix::max (
Array<octave_idx-type>&, int)) : Remove triple loop by using a local buffer
(SparseComplexMatrix SparseComplexMatrix::min (Array<octave_idx-type>&,
int)) : ditto
author | David Bateman <dbateman@free.fr> |
---|---|
date | Fri, 18 Oct 2013 23:44:44 +0200 |
parents | 8031fc73f291 |
children | d63878346099 |
comparison
equal
deleted
inserted
replaced
17690:79d6af38b96f | 17691:8a54a481ecb5 |
---|---|
351 idx_arg.resize (dim_vector (nr, nc == 0 ? 0 : 1), 0); | 351 idx_arg.resize (dim_vector (nr, nc == 0 ? 0 : 1), 0); |
352 | 352 |
353 if (nr == 0 || nc == 0 || dim >= dv.length ()) | 353 if (nr == 0 || nc == 0 || dim >= dv.length ()) |
354 return SparseMatrix (nr, nc == 0 ? 0 : 1); | 354 return SparseMatrix (nr, nc == 0 ? 0 : 1); |
355 | 355 |
356 for (octave_idx_type i = cidx (0); i < cidx (1); i++) | 356 OCTAVE_LOCAL_BUFFER (octave_idx_type, found, nr); |
357 idx_arg.elem (ridx (i)) = -1; | 357 |
358 for (octave_idx_type i = 0; i < nr; i++) | |
359 found [i] = 0; | |
358 | 360 |
359 for (octave_idx_type j = 0; j < nc; j++) | 361 for (octave_idx_type j = 0; j < nc; j++) |
360 for (octave_idx_type i = 0; i < nr; i++) | 362 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
361 { | 363 if (found [ridx (i)] == -j) |
362 if (idx_arg.elem (i) != -1) | 364 found [ridx (i)] = -j - 1; |
363 continue; | 365 |
364 bool found = false; | 366 for (octave_idx_type i = 0; i < nr; i++) |
365 for (octave_idx_type k = cidx (j); k < cidx (j+1); k++) | 367 if (found [i] > -nc && found [i] < 0) |
366 if (ridx (k) == i) | 368 idx_arg.elem (i) = -found [i]; |
367 { | |
368 found = true; | |
369 break; | |
370 } | |
371 | |
372 if (!found) | |
373 idx_arg.elem (i) = j; | |
374 | |
375 } | |
376 | 369 |
377 for (octave_idx_type j = 0; j < nc; j++) | 370 for (octave_idx_type j = 0; j < nc; j++) |
378 { | 371 { |
379 for (octave_idx_type i = cidx (j); i < cidx (j+1); i++) | 372 for (octave_idx_type i = cidx (j); i < cidx (j+1); i++) |
380 { | 373 { |
509 idx_arg.resize (dim_vector (nr, nc == 0 ? 0 : 1), 0); | 502 idx_arg.resize (dim_vector (nr, nc == 0 ? 0 : 1), 0); |
510 | 503 |
511 if (nr == 0 || nc == 0 || dim >= dv.length ()) | 504 if (nr == 0 || nc == 0 || dim >= dv.length ()) |
512 return SparseMatrix (nr, nc == 0 ? 0 : 1); | 505 return SparseMatrix (nr, nc == 0 ? 0 : 1); |
513 | 506 |
514 for (octave_idx_type i = cidx (0); i < cidx (1); i++) | 507 OCTAVE_LOCAL_BUFFER (octave_idx_type, found, nr); |
515 idx_arg.elem (ridx (i)) = -1; | 508 |
509 for (octave_idx_type i = 0; i < nr; i++) | |
510 found [i] = 0; | |
516 | 511 |
517 for (octave_idx_type j = 0; j < nc; j++) | 512 for (octave_idx_type j = 0; j < nc; j++) |
518 for (octave_idx_type i = 0; i < nr; i++) | 513 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
519 { | 514 if (found [ridx (i)] == -j) |
520 if (idx_arg.elem (i) != -1) | 515 found [ridx (i)] = -j - 1; |
521 continue; | 516 |
522 bool found = false; | 517 for (octave_idx_type i = 0; i < nr; i++) |
523 for (octave_idx_type k = cidx (j); k < cidx (j+1); k++) | 518 if (found [i] > -nc && found [i] < 0) |
524 if (ridx (k) == i) | 519 idx_arg.elem (i) = -found [i]; |
525 { | |
526 found = true; | |
527 break; | |
528 } | |
529 | |
530 if (!found) | |
531 idx_arg.elem (i) = j; | |
532 | |
533 } | |
534 | 520 |
535 for (octave_idx_type j = 0; j < nc; j++) | 521 for (octave_idx_type j = 0; j < nc; j++) |
536 { | 522 { |
537 for (octave_idx_type i = cidx (j); i < cidx (j+1); i++) | 523 for (octave_idx_type i = cidx (j); i < cidx (j+1); i++) |
538 { | 524 { |
582 | 568 |
583 /* | 569 /* |
584 | 570 |
585 %!assert (max (max (speye (65536))), sparse (1)) | 571 %!assert (max (max (speye (65536))), sparse (1)) |
586 %!assert (min (min (speye (65536))), sparse (0)) | 572 %!assert (min (min (speye (65536))), sparse (0)) |
573 %!assert (size (max (sparse (8, 0), [], 1)), [1, 0]) | |
574 %!assert (size (max (sparse (8, 0), [], 2)), [8, 0]) | |
575 %!assert (size (max (sparse (0, 8), [], 1)), [0, 8]) | |
576 %!assert (size (max (sparse (0, 8), [], 2)), [0, 1]) | |
577 %!assert (size (min (sparse (8, 0), [], 1)), [1, 0]) | |
578 %!assert (size (min (sparse (8, 0), [], 2)), [8, 0]) | |
579 %!assert (size (min (sparse (0, 8), [], 1)), [0, 8]) | |
580 %!assert (size (min (sparse (0, 8), [], 2)), [0, 1]) | |
587 | 581 |
588 */ | 582 */ |
589 | 583 |
590 RowVector | 584 RowVector |
591 SparseMatrix::row (octave_idx_type i) const | 585 SparseMatrix::row (octave_idx_type i) const |