comparison liboctave/array/CSparse.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 93e272018df2
children d63878346099
comparison
equal deleted inserted replaced
17690:79d6af38b96f 17691:8a54a481ecb5
340 idx_arg.resize (dim_vector (nr, nc == 0 ? 0 : 1), 0); 340 idx_arg.resize (dim_vector (nr, nc == 0 ? 0 : 1), 0);
341 341
342 if (nr == 0 || nc == 0 || dim >= dv.length ()) 342 if (nr == 0 || nc == 0 || dim >= dv.length ())
343 return SparseComplexMatrix (nr, nc == 0 ? 0 : 1); 343 return SparseComplexMatrix (nr, nc == 0 ? 0 : 1);
344 344
345 345 OCTAVE_LOCAL_BUFFER (octave_idx_type, found, nr);
346 idx_arg.resize (dim_vector (nr, 1), 0); 346
347 347 for (octave_idx_type i = 0; i < nr; i++)
348 for (octave_idx_type i = cidx (0); i < cidx (1); i++) 348 found [i] = 0;
349 idx_arg.elem (ridx (i)) = -1;
350 349
351 for (octave_idx_type j = 0; j < nc; j++) 350 for (octave_idx_type j = 0; j < nc; j++)
352 for (octave_idx_type i = 0; i < nr; i++) 351 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++)
353 { 352 if (found [ridx (i)] == -j)
354 if (idx_arg.elem (i) != -1) 353 found [ridx (i)] = -j - 1;
355 continue; 354
356 bool found = false; 355 for (octave_idx_type i = 0; i < nr; i++)
357 for (octave_idx_type k = cidx (j); k < cidx (j+1); k++) 356 if (found [i] > -nc && found [i] < 0)
358 if (ridx (k) == i) 357 idx_arg.elem (i) = -found [i];
359 {
360 found = true;
361 break;
362 }
363
364 if (!found)
365 idx_arg.elem (i) = j;
366
367 }
368 358
369 for (octave_idx_type j = 0; j < nc; j++) 359 for (octave_idx_type j = 0; j < nc; j++)
370 { 360 {
371 for (octave_idx_type i = cidx (j); i < cidx (j+1); i++) 361 for (octave_idx_type i = cidx (j); i < cidx (j+1); i++)
372 { 362 {
507 idx_arg.resize (dim_vector (nr, nc == 0 ? 0 : 1), 0); 497 idx_arg.resize (dim_vector (nr, nc == 0 ? 0 : 1), 0);
508 498
509 if (nr == 0 || nc == 0 || dim >= dv.length ()) 499 if (nr == 0 || nc == 0 || dim >= dv.length ())
510 return SparseComplexMatrix (nr, nc == 0 ? 0 : 1); 500 return SparseComplexMatrix (nr, nc == 0 ? 0 : 1);
511 501
512 for (octave_idx_type i = cidx (0); i < cidx (1); i++) 502 OCTAVE_LOCAL_BUFFER (octave_idx_type, found, nr);
513 idx_arg.elem (ridx (i)) = -1; 503
504 for (octave_idx_type i = 0; i < nr; i++)
505 found [i] = 0;
514 506
515 for (octave_idx_type j = 0; j < nc; j++) 507 for (octave_idx_type j = 0; j < nc; j++)
516 for (octave_idx_type i = 0; i < nr; i++) 508 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++)
517 { 509 if (found [ridx (i)] == -j)
518 if (idx_arg.elem (i) != -1) 510 found [ridx (i)] = -j - 1;
519 continue; 511
520 bool found = false; 512 for (octave_idx_type i = 0; i < nr; i++)
521 for (octave_idx_type k = cidx (j); k < cidx (j+1); k++) 513 if (found [i] > -nc && found [i] < 0)
522 if (ridx (k) == i) 514 idx_arg.elem (i) = -found [i];
523 {
524 found = true;
525 break;
526 }
527
528 if (!found)
529 idx_arg.elem (i) = j;
530
531 }
532 515
533 for (octave_idx_type j = 0; j < nc; j++) 516 for (octave_idx_type j = 0; j < nc; j++)
534 { 517 {
535 for (octave_idx_type i = cidx (j); i < cidx (j+1); i++) 518 for (octave_idx_type i = cidx (j); i < cidx (j+1); i++)
536 { 519 {
580 563
581 /* 564 /*
582 565
583 %!assert (max (max (speye (65536) * 1i)), sparse (1i)) 566 %!assert (max (max (speye (65536) * 1i)), sparse (1i))
584 %!assert (min (min (speye (65536) * 1i)), sparse (0)) 567 %!assert (min (min (speye (65536) * 1i)), sparse (0))
568 %!assert (size (max (sparse (8, 0), [], 1)), [1, 0])
569 %!assert (size (max (sparse (8, 0), [], 2)), [8, 0])
570 %!assert (size (max (sparse (0, 8), [], 1)), [0, 8])
571 %!assert (size (max (sparse (0, 8), [], 2)), [0, 1])
572 %!assert (size (min (sparse (8, 0), [], 1)), [1, 0])
573 %!assert (size (min (sparse (8, 0), [], 2)), [8, 0])
574 %!assert (size (min (sparse (0, 8), [], 1)), [0, 8])
575 %!assert (size (min (sparse (0, 8), [], 2)), [0, 1])
585 576
586 */ 577 */
587 578
588 ComplexRowVector 579 ComplexRowVector
589 SparseComplexMatrix::row (octave_idx_type i) const 580 SparseComplexMatrix::row (octave_idx_type i) const