This bugzilla service is closed. All entries have been migrated to https://gitlab.com/libeigen/eigen
View | Details | Raw Unified | Return to bug 1130
Collapse All | Expand All

(-)a/Eigen/src/SparseCore/SparseMatrix.h (+30 lines)
Lines 493-508 class SparseMatrix Link Here
493
        return; 
493
        return; 
494
      m_innerNonZeros = static_cast<StorageIndex*>(std::malloc(m_outerSize * sizeof(StorageIndex)));
494
      m_innerNonZeros = static_cast<StorageIndex*>(std::malloc(m_outerSize * sizeof(StorageIndex)));
495
      for (Index i = 0; i < m_outerSize; i++)
495
      for (Index i = 0; i < m_outerSize; i++)
496
      {
496
      {
497
        m_innerNonZeros[i] = m_outerIndex[i+1] - m_outerIndex[i]; 
497
        m_innerNonZeros[i] = m_outerIndex[i+1] - m_outerIndex[i]; 
498
      }
498
      }
499
    }
499
    }
500
    
500
    
501
    /** Filter outer dimension of matrix */
502
    template <typename T>
503
    void filterOuter(std::vector<T> &toInclude) {
504
      eigen_assert(Index(toInclude.size()) == outerSize() &&
505
		   "filter mask must be the same size as the outer dimension");
506
      uncompress();
507
      Index dx = 0;
508
      for (Index cx=0; cx < outerSize(); ++cx) {
509
	if (!toInclude[cx]) continue;
510
	m_outerIndex[dx] = m_outerIndex[cx];
511
	m_innerNonZeros[dx] = m_innerNonZeros[cx];
512
	++dx;
513
      }
514
      if (dx == outerSize()) return; // didn't filter anything
515
      m_outerIndex[dx] = m_outerIndex[outerSize()];
516
517
      StorageIndex *newOuterIndex = static_cast<StorageIndex*>(std::realloc(m_outerIndex, (dx + 1) * sizeof(StorageIndex)));
518
      if (!newOuterIndex) internal::throw_std_bad_alloc();
519
      m_outerIndex = newOuterIndex;
520
      if (dx == 0) {
521
	std::free(m_innerNonZeros);
522
	m_innerNonZeros = 0;
523
      } else {
524
	StorageIndex *newInnerNonZeros = static_cast<StorageIndex*>(std::realloc(m_innerNonZeros, dx * sizeof(StorageIndex)));
525
	if (!newInnerNonZeros) internal::throw_std_bad_alloc();
526
	m_innerNonZeros = newInnerNonZeros;
527
      }
528
      m_outerSize = dx;
529
    }
530
501
    /** Suppresses all nonzeros which are \b much \b smaller \b than \a reference under the tolerence \a epsilon */
531
    /** Suppresses all nonzeros which are \b much \b smaller \b than \a reference under the tolerence \a epsilon */
502
    void prune(const Scalar& reference, const RealScalar& epsilon = NumTraits<RealScalar>::dummy_precision())
532
    void prune(const Scalar& reference, const RealScalar& epsilon = NumTraits<RealScalar>::dummy_precision())
503
    {
533
    {
504
      prune(default_prunning_func(reference,epsilon));
534
      prune(default_prunning_func(reference,epsilon));
505
    }
535
    }
506
    
536
    
507
    /** Turns the matrix into compressed format, and suppresses all nonzeros which do not satisfy the predicate \a keep.
537
    /** Turns the matrix into compressed format, and suppresses all nonzeros which do not satisfy the predicate \a keep.
508
      * The functor type \a KeepFunc must implement the following function:
538
      * The functor type \a KeepFunc must implement the following function:
(-)a/test/sparse_basic.cpp (+43 lines)
Lines 429-444 template<typename SparseMatrixType> void Link Here
429
          m1.insert(0, m1.cols()-1) = refMat1(0, refMat1.cols()-1) = 1;
429
          m1.insert(0, m1.cols()-1) = refMat1(0, refMat1.cols()-1) = 1;
430
          
430
          
431
        VERIFY_IS_APPROX(m1, refMat1);
431
        VERIFY_IS_APPROX(m1, refMat1);
432
          
432
          
433
          
433
          
434
      }
434
      }
435
  }
435
  }
436
436
437
  // test filterOuter
438
  {
439
    DenseMatrix ref = DenseMatrix::Zero(rows, cols);
440
    SparseMatrixType m1(rows, cols);
441
    for(int k=0; k<std::max(rows*cols/4, Index(1000)); ++k)
442
    {
443
      Index i = internal::random<Index>(0,rows-1);
444
      Index j = internal::random<Index>(0,cols-1);
445
      Scalar v = internal::random<Scalar>();
446
      m1.coeffRef(i,j) = v;
447
      ref.coeffRef(i,j) = v;
448
    }
449
    if(internal::random<Index>(0,10)<5) {
450
      m1.makeCompressed();
451
    }
452
    std::vector<bool> filter(m1.outerSize());
453
    Index retained = 0;
454
    for (int fx=0; fx < m1.outerSize(); ++fx) {
455
      bool inc = internal::random<bool>();
456
      filter[fx] = inc;
457
      retained += inc;
458
    }
459
    m1.filterOuter(filter);
460
    VERIFY(m1.outerSize() == retained);
461
    if (retained == (Index) filter.size()) {
462
      VERIFY_IS_APPROX(m1, ref);
463
    } else {
464
      if (retained == m1.cols()) {
465
	for (int fx=0, dx=0; fx < ref.cols(); ++fx) {
466
	  if (!filter[fx]) continue;
467
	  VERIFY_IS_APPROX(m1.col(dx), ref.col(fx));
468
	  dx++;
469
	}
470
      } else {
471
	for (int fx=0, dx=0; fx < ref.rows(); ++fx) {
472
	  if (!filter[fx]) continue;
473
	  VERIFY_IS_APPROX(m1.row(dx), ref.row(fx));
474
	  dx++;
475
	}
476
      }
477
    }
478
  }
479
437
  // test Identity matrix
480
  // test Identity matrix
438
  {
481
  {
439
    DenseMatrix refMat1 = DenseMatrix::Identity(rows, rows);
482
    DenseMatrix refMat1 = DenseMatrix::Identity(rows, rows);
440
    SparseMatrixType m1(rows, rows);
483
    SparseMatrixType m1(rows, rows);
441
    m1.setIdentity();
484
    m1.setIdentity();
442
    VERIFY_IS_APPROX(m1, refMat1);
485
    VERIFY_IS_APPROX(m1, refMat1);
443
    for(int k=0; k<rows*rows/4; ++k)
486
    for(int k=0; k<rows*rows/4; ++k)
444
    {
487
    {

Return to bug 1130