GMM_ToolsT.cc 33.1 KB
Newer Older
Henrik Zimmer's avatar
Henrik Zimmer committed
1
2
/*===========================================================================*\
 *                                                                           *
Henrik Zimmer's avatar
Henrik Zimmer committed
3
 *                               CoMISo                                      *
Henrik Zimmer's avatar
Henrik Zimmer committed
4
 *      Copyright (C) 2008-2009 by Computer Graphics Group, RWTH Aachen      *
Henrik Zimmer's avatar
Henrik Zimmer committed
5
 *                           www.rwth-graphics.de                            *
Henrik Zimmer's avatar
Henrik Zimmer committed
6
 *                                                                           *
Henrik Zimmer's avatar
Henrik Zimmer committed
7
8
 *---------------------------------------------------------------------------* 
 *  This file is part of CoMISo.                                             *
Henrik Zimmer's avatar
Henrik Zimmer committed
9
 *                                                                           *
Henrik Zimmer's avatar
Henrik Zimmer committed
10
11
12
13
 *  CoMISo is free software: you can redistribute it and/or modify           *
 *  it under the terms of the GNU General Public License as published by     *
 *  the Free Software Foundation, either version 3 of the License, or        *
 *  (at your option) any later version.                                      *
Henrik Zimmer's avatar
Henrik Zimmer committed
14
15
16
17
 *                                                                           *
 *  CoMISo is distributed in the hope that it will be useful,                *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
Henrik Zimmer's avatar
Henrik Zimmer committed
18
 *  GNU General Public License for more details.                             *
Henrik Zimmer's avatar
Henrik Zimmer committed
19
 *                                                                           *
Henrik Zimmer's avatar
Henrik Zimmer committed
20
21
 *  You should have received a copy of the GNU General Public License        *
 *  along with CoMISo.  If not, see <http://www.gnu.org/licenses/>.          *
Henrik Zimmer's avatar
Henrik Zimmer committed
22
 *                                                                           *
Henrik Zimmer's avatar
Henrik Zimmer committed
23
\*===========================================================================*/ 
Henrik Zimmer's avatar
Henrik Zimmer committed
24
25


Henrik Zimmer's avatar
Henrik Zimmer committed
26

27
28
29
30
31
32
//=============================================================================
//
//  CLASS GMM_Tools - IMPLEMENTATION
//
//=============================================================================

David Bommes's avatar
David Bommes committed
33
#define COMISO_GMM_TOOLS_C
34
35
36
37
38
39
40

//== INCLUDES =================================================================

#include "GMM_Tools.hh"
#define GMM_USES_LAPACK
#include <gmm/gmm_lapack_interface.h>
#include <queue>
Henrik Zimmer's avatar
Henrik Zimmer committed
41
#include <CoMISo/Utils/VSToolsT.hh>
42
43
44
45


//== NAMESPACES ===============================================================

David Bommes's avatar
David Bommes committed
46
namespace COMISO_GMM
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
{

//== IMPLEMENTATION ==========================================================


//-----------------------------------------------------------------------------


// too many if-statements, seems slower in most cases
// use eliminate_csc_vars2
template<class ScalarT, class VectorT, class RealT, class IntegerT>
void eliminate_csc_vars(
    const std::vector<IntegerT>&     _evar,
    const std::vector<ScalarT>&      _eval,
    typename gmm::csc_matrix<RealT>&  _A,
    VectorT&                          _x,
    VectorT&                          _rhs )
{
  typedef unsigned int uint;
  typedef typename gmm::csc_matrix<RealT> MatrixT;

Andreas Fabri's avatar
Andreas Fabri committed
68
69
  gmm::size_type nc = _A.nc;
  gmm::size_type nr = _A.nr;
70

Andreas Fabri's avatar
Andreas Fabri committed
71
  gmm::size_type n_new = nc - _evar.size();
72
73

  // modify rhs
Andreas Fabri's avatar
Andreas Fabri committed
74
  for ( gmm::size_type k=0; k<_evar.size(); ++k )
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  {
    IntegerT i = _evar[k];

    // number of elements in this column
    uint n_elem = _A.jc[i+1]-_A.jc[i];
    uint r_idx  = 0;
    RealT entry = 0.0;
    for( uint i_elem = _A.jc[i]; i_elem < _A.jc[i+1]; ++i_elem)
    {
      r_idx = _A.ir[i_elem];
      entry = _A.pr[i_elem];
      _rhs[r_idx] -= _eval[k]*( entry );
    }
  }

  // sort vector
  std::vector<IntegerT> evar( _evar );
  std::sort( evar.begin(), evar.end() );
  evar.push_back( std::numeric_limits<IntegerT>::max() );

  // build subindex set and update rhs
  std::vector<size_t> si( n_new );
  int cur_evar_idx=0;
Andreas Fabri's avatar
Andreas Fabri committed
98
  for ( gmm::size_type i=0; i<nc; ++i )
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
  {
    unsigned int next_i = evar[cur_evar_idx];
    if ( i != next_i )
    {
      _rhs[i-cur_evar_idx] = _rhs[i];
      _x  [i-cur_evar_idx] = _x  [i];
      si  [i-cur_evar_idx] = i;
    }
    else
      ++cur_evar_idx;
  }

  // delete last elements
  _rhs.resize( n_new );
  _x.resize( n_new );

  // csc erasing rows and columns
  uint offset(0);
  uint next_evar_row(0);
  uint next_evar_col(0);
  uint last_jc = _A.jc[0];
  uint col_offset(0);
  uint offset_update(0);
Andreas Fabri's avatar
Andreas Fabri committed
122
  uint last_evar_idx= static_cast<uint>(evar.size())-2;
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

  for( uint c = 0; c < nc; ++c)
  {
    uint el_span( _A.jc[c+1]-last_jc);

    offset_update=0;
    if( c != (uint) evar[next_evar_col] )
    {
      next_evar_row=0;
      for( uint e = last_jc; e < _A.jc[c+1]; ++e)
      {
        while( _A.ir[e] > (uint) evar[next_evar_row])
        {
          ++next_evar_row;
          ++offset_update;
        }
        _A.pr[e-offset] = _A.pr[e];
        _A.ir[e-offset] = _A.ir[e]-offset_update;
        if( _A.ir[e] == (uint)evar[next_evar_row])
        {
          ++offset;
        }
      }
      last_jc = _A.jc[c+1];
      _A.jc[c+1-col_offset] = _A.jc[c+1]-offset;
    }
    else
    {
      ++col_offset;
      offset+=el_span;
      ++next_evar_col;
      last_jc = _A.jc[c+1];
    }
  }
  _A.nc = nc - evar.size()+1;
  _A.nr = nr - evar.size()+1;
}


//-----------------------------------------------------------------------------


template<class ScalarT, class VectorT, class RealT, class IntegerT>
void eliminate_csc_vars2(
    const std::vector<IntegerT>&     _evar,
    const std::vector<ScalarT>&      _eval,
    typename gmm::csc_matrix<RealT>&  _A,
    VectorT&                          _x,
    VectorT&                          _rhs )
{
  typedef unsigned int uint;
  typedef typename gmm::csc_matrix<RealT> MatrixT;

Andreas Fabri's avatar
Andreas Fabri committed
176
177
  gmm::size_type nc = _A.nc;
  gmm::size_type nr = _A.nr;
178

Andreas Fabri's avatar
Andreas Fabri committed
179
  gmm::size_type n_new = nc - _evar.size();
180
181

  // modify rhs
Andreas Fabri's avatar
Andreas Fabri committed
182
  for ( std::size_t k=0; k<_evar.size(); ++k )
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
  {
    IntegerT i = _evar[k];

    // number of elements in this column
    //    uint n_elem = _A.jc[i+1]-_A.jc[i];
    uint r_idx  = 0;
    RealT entry = 0.0;
    for( uint i_elem = _A.jc[i]; i_elem < _A.jc[i+1]; ++i_elem)
    {
      r_idx = _A.ir[i_elem];
      entry = _A.pr[i_elem];
      _rhs[r_idx] -= _eval[k]*( entry );
    }
  }

  // sort vector
  std::vector<IntegerT> evar( _evar );
  std::sort( evar.begin(), evar.end() );
  evar.push_back( std::numeric_limits<IntegerT>::max() );

  // build subindex set and update rhs
  std::vector<size_t> si( n_new );
  int cur_evar_idx=0;
  for ( unsigned int i=0; i<nc; ++i )
  {

    unsigned int next_i = evar[cur_evar_idx];

    if ( i != next_i )
    {

      _rhs[i-cur_evar_idx] = _rhs[i];
      _x  [i-cur_evar_idx] = _x  [i];
      si  [i-cur_evar_idx] = i;

    }
    else
    {
      ++cur_evar_idx;
    }
  }

  // delete last elements
  _rhs.resize( n_new );
  _x.resize( n_new );

  std::vector< int > new_row_idx_map( nr, -1);
  // build re-indexing map
  // -1 means deleted
  {
    int next_evar_idx(0);
    int offset(0);
    for( int i = 0; i < (int)nr; ++i)
    {
      if( i == (int)evar[next_evar_idx])
      {
        ++next_evar_idx;
        ++offset;
      }
      else
      {
        new_row_idx_map[i] = i-offset;
      }
    }
  }

  // csc erasing rows and columns
  int read(0), write(0), evar_col(0), last_jc(0);
  for( unsigned int c = 0; c < nc; ++c)
  {
    if( c == (unsigned int)evar[evar_col] )
    {
      ++evar_col;
      read += _A.jc[c+1]-last_jc;
    }
    else
    {
      while( read < (int)_A.jc[c+1] )
      {
        int new_idx = new_row_idx_map[_A.ir[read]];
        if( new_idx != -1)
        {
          _A.pr[write] = _A.pr[read];
          _A.ir[write] = new_idx;
          ++write;
        }
        ++read;
      }
   }
    last_jc = _A.jc[c+1];

    _A.jc[c+1-evar_col] = write;
  }

  _A.nc = nc - evar.size()+1;
  _A.nr = nr - evar.size()+1;
}


//-----------------------------------------------------------------------------


template<class MatrixT, class REALT, class INTT>
void get_ccs_symmetric_data( const MatrixT&      _mat,
                             const char          _uplo,
                             std::vector<REALT>& _values,
                             std::vector<INTT>&  _rowind,
                             std::vector<INTT>&  _colptr )
{
Max Lyon's avatar
Max Lyon committed
292
293
   gmm::size_type m = gmm::mat_nrows( _mat );
   gmm::size_type n = gmm::mat_ncols( _mat );
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308

   gmm::csc_matrix<REALT> csc_mat( m,n );
   gmm::copy( _mat, csc_mat );

   _values.resize( 0 );
   _rowind.resize( 0 );
   _colptr.resize( 0 );

   INTT iv( 0 );

   switch ( _uplo )
   {
      case 'l':
      case 'L':
         // for all columns
David Bommes's avatar
   
David Bommes committed
309
         for ( unsigned int i=0; i<n; ++i )
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
         {
            _colptr.push_back( iv );
            for ( unsigned int j=( csc_mat.jc )[i]; j<( csc_mat.jc )[i+1]; ++j )
               if (( csc_mat.ir )[j] >= i )
               {
                  ++iv;
                  _values.push_back(( csc_mat.pr )[j] );
                  _rowind.push_back(( csc_mat.ir )[j] );
               }
         }
         _colptr.push_back( iv );
         break;

      case 'u':
      case 'U':
         // for all columns
David Bommes's avatar
   
David Bommes committed
326
         for ( unsigned int i=0; i<n; ++i )
327
328
329
330
331
332
333
334
335
336
337
338
339
         {
            _colptr.push_back( iv );
            for ( unsigned int j=( csc_mat.jc )[i]; j<( csc_mat.jc )[i+1]; ++j )
               if (( csc_mat.ir )[j] <= i )
               {
                  ++iv;
                  _values.push_back(( csc_mat.pr )[j] );
                  _rowind.push_back(( csc_mat.ir )[j] );
               }
         }
         _colptr.push_back( iv );
         break;

David Bommes's avatar
David Bommes committed
340
341
342
      case 'c':
      case 'C':
         // for all columns
David Bommes's avatar
   
David Bommes committed
343
         for ( unsigned int i=0; i<n; ++i )
David Bommes's avatar
David Bommes committed
344
345
346
347
348
349
350
351
352
353
354
355
356
         {
            _colptr.push_back( iv );
            for ( unsigned int j=( csc_mat.jc )[i]; j<( csc_mat.jc )[i+1]; ++j )
	      //               if (( csc_mat.ir )[j] <= i )
               {
                  ++iv;
                  _values.push_back(( csc_mat.pr )[j] );
                  _rowind.push_back(( csc_mat.ir )[j] );
               }
         }
         _colptr.push_back( iv );
         break;

357
      default:
David Bommes's avatar
David Bommes committed
358
         std::cerr << "ERROR: parameter uplo must bei either 'U' or 'L' or 'C'!!!\n";
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
         break;
   }
}


//-----------------------------------------------------------------------------


template<class ScalarT, class VectorT, class SVT>
void eliminate_var( const unsigned int _j,
                    const ScalarT      _value_j,
                    gmm::col_matrix<SVT>& _A,
                    VectorT&      _x,
                    VectorT&      _rhs )
{
   //ACG::StopWatch sw1;

   //sw1.start();

   typedef typename gmm::linalg_traits< gmm::col_matrix< SVT > >::const_sub_col_type ColT;
   typedef typename gmm::linalg_traits<ColT>::const_iterator CIter;

   unsigned int m = gmm::mat_nrows( _A );
   unsigned int n = gmm::mat_ncols( _A );


   // update rhs
   ColT col = mat_const_col( _A, _j );
   CIter it  = gmm::vect_const_begin( col );
   CIter ite = gmm::vect_const_end( col );

   for ( ; it!=ite; ++it )
   {
      _rhs[it.index()] -= _value_j*( *it );
   }

   _rhs.erase( _rhs.begin() + _j );
   _x.erase( _x.begin() + _j );

   //std::cerr << " FIRST BLOCK " << sw1.stop()/1000.0 << std::endl;
   /*
   for(unsigned int i=_j; i<m-1; ++i)
   {
     _rhs[i] = _rhs[i+1];
     _x  [i] = _x  [i+1];
   }

   // delete last element
   _rhs.resize( _rhs.size()-1);
   _x.resize( _x.size()-1);
   */

   /*
   MatrixT A_temp( m, n);
   gmm::copy(_A, A_temp);
   gmm::clear(_A);

   for(unsigned int i=0; i<m; ++i)
   {
     // skip _j'th column
     if( i==_j) continue;

     ColT col = mat_const_col(A_temp, i);

     CIter it  = gmm::vect_const_begin(col);
     CIter ite = gmm::vect_const_end(col);
     
     // compute new index
     unsigned int i_new = i;
     if( i>_j) --i_new;

     for(; it!=ite; ++it)
     {
       unsigned int j_new = it.index();
       if( j_new == _j) continue;
       if( j_new > _j) --j_new;
       _A(j_new,i_new) = *it;
       //       if( it.index() != j_new || i_new != i)
       // 	_A(it.index(), i) = 0.0;

     }
   }

   gmm::resize( _A, m-1, n-1);
   */
   //sw1.start();

   typedef typename gmm::linalg_traits<SVT>::const_iterator SIter;

   for ( unsigned int i=0; i<m; ++i )
   {
      // skip _j'th column
      if ( i==_j ) continue;

      // compute new index
      unsigned int i_new = i;
      if ( i>_j ) --i_new;

      SVT* cur_col = &( _A.col( i ) );
      if ( i == i_new )
      {
         cur_col = new SVT( _A.col( i ) );
         //gmm::copy( _A.col(i), *cur_col);
      }

      SIter it  = gmm::vect_const_begin( *cur_col );
      SIter ite = gmm::vect_const_end( *cur_col );

      //mat_col(_A, i_new).clear();
      SVT& new_col = _A.col( i_new );
      new_col.clear();

      for ( ; it!=ite; ++it )
      {
         unsigned int j_new = it.index();
         if ( j_new == _j ) continue;
         if ( j_new > _j ) --j_new;
         //_A(j_new,i_new) = *it;
         new_col[j_new] = *it;
         //       if( it.index() != j_new || i_new != i)
         // 	_A(it.index(), i) = 0.0;

      }
      if ( i == i_new )
      {
         delete cur_col;
      }
   }
   //std::cerr << " BLOCK TWO " << sw1.stop()/1000.0 << std::endl;
   //sw1.start();

   gmm::resize( _A, m-1, n-1 );
   //std::cerr << "RESIZE " << sw1.stop()/1000.0 << std::endl;
}

//-----------------------------------------------------------------------------


template<class ScalarT, class VectorT, class RealT>
void eliminate_var( const unsigned int                _i,
                    const ScalarT                     _xi,
                    typename gmm::csc_matrix<RealT>&  _A,
                    VectorT&                          _x,
                    VectorT&                          _rhs )
{
   unsigned int n = _A.nc;

   unsigned int iv_old( 0 );
   unsigned int iv_new( 0 );

   // for all columns
   for ( unsigned int j=0; j<n; ++j )
   {
      // update x and rhs
      if ( j > _i )
      {
         _rhs[j-1] = _rhs[j];
         _x  [j-1] = _x  [j];
      }


      if ( j == _i )
      {
         // update rhs
         for ( unsigned int i=_A.jc[j]; i<_A.jc[j+1]; ++i )
         {
            _rhs[_A.ir[iv_old]] -= _xi*_A.pr[iv_old];
            ++iv_old;
         }
      }
      else
      {
         // store index to update colptr
         unsigned int iv_new_save = iv_new;

         for ( unsigned int i=_A.jc[j]; i<_A.jc[j+1]; ++i )
         {
            if ( _A.ir[iv_old] < _i )
            {
               _A.ir[iv_new] = _A.ir[iv_old];
               _A.pr[iv_new] = _A.pr[iv_old];
               ++iv_new;
            }
            else if ( _A.ir[iv_old] > _i )
            {
               _A.ir[iv_new] = _A.ir[iv_old]-1;
               _A.pr[iv_new] = _A.pr[iv_old];
               ++iv_new;
            }
            ++iv_old;
         }

         if ( j<_i )
            _A.jc[j] = iv_new_save;
         else
            if ( j>_i )
               _A.jc[j-1] = iv_new_save;
      }
   }

   // store index to end
   _A.jc[n-1] = iv_new;

   // resize matrix and vectors
   _A.nc = n-1;
   _A.nr = n-1;
   _x.resize( _x.size()-1 );
   _rhs.resize( _rhs.size()-1 );
}


//-----------------------------------------------------------------------------


template<class IntegerT, class ScalarT, class VectorT, class MatrixT>
void eliminate_vars( const std::vector<IntegerT>&     _evar,
                     const std::vector<ScalarT>&      _eval,
                     MatrixT&     _A,
                     VectorT&     _x,
                     VectorT&     _rhs )
{
  std::cerr << __FUNCTION__ << std::endl;
  typedef typename gmm::linalg_traits<MatrixT>::const_sub_col_type ColT;
  typedef typename gmm::linalg_traits<ColT>::const_iterator CIter;

  //  unsigned int m = gmm::mat_nrows( _A);
  unsigned int n = gmm::mat_ncols( _A );

  unsigned int n_new = n - _evar.size();

  // modify rhs
  for ( unsigned int k=0; k<_evar.size(); ++k )
  {
    IntegerT i = _evar[k];

    ColT col = mat_const_col( _A, i );

    CIter it  = gmm::vect_const_begin( col );
    CIter ite = gmm::vect_const_end( col );

    for ( ; it!=ite; ++it )
    {
      _rhs[it.index()] -= _eval[k]*( *it );
    }
  }

  // sort vector
  std::vector<IntegerT> evar( _evar );
  std::sort( evar.begin(), evar.end() );
  evar.push_back( std::numeric_limits<int>::max() );

  // build subindex set and update rhs
  std::vector<size_t> si( n_new );
  int cur_evar_idx=0;
  for ( unsigned int i=0; i<n; ++i )
  {
    unsigned int next_i = evar[cur_evar_idx];

    if ( i != next_i )
    {
      _rhs[i-cur_evar_idx] = _rhs[i];
      _x  [i-cur_evar_idx] = _x  [i];
      si  [i-cur_evar_idx] = i;
    }
    else
    {
      ++cur_evar_idx;
    }
  }

  // delete last element
  _rhs.resize( n_new );
  _x.resize( n_new );

  // copy and resize
  // ToDo: doesn't work without temp matrix ... (more efficient possibility?)
  //MatrixT A_temp( n_new, n_new );
  //gmm::copy( gmm::sub_matrix( _A, gmm::sub_index( si ), gmm::sub_index( si ) ), A_temp );
  //gmm::resize( _A, n_new, n_new );
  //gmm::copy( A_temp, _A );









  // to remove last "virtual" element
  evar.resize(evar.size()-1);


  //std::cerr << "THIS IS THE VECTOR OF COLUMNS THAT SHALL DISAPPEAR!s" << evar << std::endl;

  typename std::vector< IntegerT >::iterator col_it  = evar.begin();
  typename std::vector< IntegerT >::iterator col_end = evar.end();

  int next_i = *col_it;
  for( int i = *col_it; i < (int)_A.ncols(); ++i)
  {
    if( col_it != col_end && i == *col_it)
    {
      ++col_it;
    }
    else
    {
      _A.col(next_i) = _A.col(i);
      ++next_i;
    }
  }

  gmm::resize(_A, gmm::mat_nrows(_A), gmm::mat_ncols(_A)-evar.size());
  gmm::row_matrix< gmm::wsvector<double> > Arow( gmm::mat_nrows(_A), gmm::mat_ncols(_A));
  gmm::copy(_A, Arow);
  col_it  = evar.begin();
  col_end = evar.end();


  int next_ii = *col_it;
  for( int i = *col_it; i < (int)_A.nrows(); ++i)
  {
    if( col_it != col_end && i == *col_it)
    {
      ++col_it;
    }
    else
    {
      Arow.row(next_ii) = Arow.row(i);
      ++next_ii;
    }
  }

  gmm::resize( _A, gmm::mat_ncols(Arow), gmm::mat_ncols(Arow));
  gmm::resize( Arow, gmm::mat_ncols(Arow), gmm::mat_ncols(Arow));
  gmm::copy(   Arow, _A);




  //alternative (probably not working anymore)
  /*
     MatrixT Atmp( gmm::mat_nrows(_A) - evar.size(), gmm::mat_ncols(_A) - evar.size());

     evar.push_back(INT_MAX);
     unsigned int next_evar_row=0, next_evar_col=0;
     unsigned int col_offset = 0;
     for( unsigned int c = 0; c < mat_ncols(_A); ++c)
     {
     if( c != evar[next_evar_col] )
     {
     CIter it  = gmm::vect_const_begin( mat_const_col( _A, c ) );
     CIter ite = gmm::vect_const_end( mat_const_col( _A, c) );

     unsigned int row_offset = 0;
     next_evar_row=0;
     for( ; it != ite; ++it)
     {
     while( it.index() > evar[next_evar_row] )
     {
     ++row_offset;
     ++next_evar_row;
     }
     if( it.index() != evar[next_evar_row])
     {
     Atmp( it.index() - row_offset, c - col_offset) = *it;
     }
     }
     }
     else
     {
     ++col_offset;
     ++next_evar_col;
     }
     }
     gmm::resize( _A, gmm::mat_nrows(Atmp), gmm::mat_ncols(Atmp));
     gmm::copy( Atmp, _A);
   */

}


//-----------------------------------------------------------------------------


template<class IntegerT, class IntegerT2>
void eliminate_vars_idx( const std::vector<IntegerT >&     _evar,
                         std::vector<IntegerT2>&     _idx,
David Bommes's avatar
David Bommes committed
747
748
                         IntegerT2                   _dummy, 
			 IntegerT2                   _range )
749
750
751
752
753
754
{
   // sort input
   std::vector<IntegerT> evar( _evar );
   std::sort( evar.begin(), evar.end() );

   // precompute update
David Bommes's avatar
David Bommes committed
755
756
   IntegerT2 range = _range;
   if( range == -1 )
Andreas Fabri's avatar
Andreas Fabri committed
757
     range = static_cast<int>(_idx.size()); // AF: what can an IntegerT2 be?
David Bommes's avatar
David Bommes committed
758
   std::vector<int> update_map( range );
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812

   typename std::vector<IntegerT>::iterator cur_var = evar.begin();
   unsigned int deleted_between = 0;

   for ( unsigned int i=0; i<update_map.size(); ++i )
   {
     if ( cur_var != evar.end() && (IntegerT)i == *cur_var )
      {
         // mark as deleted
         update_map[i] = _dummy;

         ++deleted_between;
         ++cur_var;
      }
      else
      {
         update_map[i] = i-deleted_between;
      }
   }

   for ( unsigned int i=0; i<_idx.size(); ++i )
   {
     if ( (IntegerT2)_idx[i] != _dummy )
      {
         _idx[i] = update_map[_idx[i]];
      }
   }
}


//-----------------------------------------------------------------------------


template<class IntegerT, class IntegerT2>
void eliminate_var_idx( const IntegerT           _evar,
                        std::vector<IntegerT2>&  _idx,
                        IntegerT2                _dummy )
{
   for ( unsigned int i=0; i<_idx.size(); ++i )
   {
      if ( _idx[i] != _dummy )
      {
         if ( _idx[i] == ( IntegerT2 )_evar )
            _idx[i] = _dummy;
         else
         {
            if ( _idx[i] > ( IntegerT2 )_evar )
               --_idx[i];
         }
      }
   }
}


David Bommes's avatar
David Bommes committed
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828

//-----------------------------------------------------------------------------


template<class ScalarT, class VectorT, class RealT>
void fix_var_csc_symmetric( const unsigned int                _i,
			    const ScalarT                     _xi,
			    typename gmm::csc_matrix<RealT>&  _A,
			    VectorT&                          _x,
			    VectorT&                          _rhs )
{
// GMM CSC FORMAT
//     T *pr;        // values.
//     IND_TYPE *ir; // row indices.
//     IND_TYPE *jc; // column repartition on pr and ir.

Andreas Fabri's avatar
Andreas Fabri committed
829
  gmm::size_type n = _A.nc;
David Bommes's avatar
David Bommes committed
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855

  // update x
  _x[_i]   = _xi;

  std::vector<unsigned int> idx; idx.reserve(16);

  // clear i-th column and collect nonzeros
  for( unsigned int iv=_A.jc[_i]; iv<_A.jc[_i+1]; ++iv)
  {
    if(_A.ir[iv] == _i)
    {
      _A.pr[iv] = 1.0;
      _rhs[_i]  = _xi;
    }
    else
    {
      // update rhs
      _rhs[_A.ir[iv]] -= _A.pr[iv]*_xi;
      // clear entry
      _A.pr[iv] = 0;
      // store index
      idx.push_back( _A.ir[iv]);
    }

  }

Andreas Fabri's avatar
Andreas Fabri committed
856
  for(std::size_t i=0; i<idx.size(); ++i)
David Bommes's avatar
David Bommes committed
857
858
859
860
861
862
863
864
865
866
867
868
869
870
  {
    unsigned int col = idx[i];

    for( unsigned int j=_A.jc[col]; j<_A.jc[col+1]; ++j)
      if(_A.ir[j] == _i)
      {
	_A.pr[j] = 0.0;
	// move to next
	break;
      }
  }
}


871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
//-----------------------------------------------------------------------------


template<class MatrixT>
void regularize_hack( MatrixT& _mat, double _v )
{
   unsigned int m = gmm::mat_nrows( _mat );
   unsigned int n = gmm::mat_ncols( _mat );

   if ( m!=n )
      std::cerr << "ERROR: regularizing works only for square matrices...\n";
   else
   {
      double sum = 0.0;
      for ( unsigned int i=0; i<n; ++i )
         sum += _mat( i,i );

      double v_hack = _v*sum/double( n );

      for ( unsigned int i=0; i<n; ++i )
         _mat( i,i ) += v_hack;

   }
}


//-----------------------------------------------------------------------------


template<class MatrixT, class VectorT>
int gauss_seidel_local( MatrixT& _A, VectorT& _x, VectorT& _rhs, std::vector<unsigned int> _idxs, int _max_iter, double _tolerance )
{
   typedef typename gmm::linalg_traits<MatrixT>::const_sub_col_type ColT;
   typedef typename gmm::linalg_traits<ColT>::const_iterator CIter;

   double t2 = _tolerance*_tolerance;

David Bommes's avatar
David Bommes committed
908
   // static variables to prevent memory allocation in each step
909
910
911
   std::vector<unsigned int> i_temp;
   std::queue<unsigned int>  q;

David Bommes's avatar
David Bommes committed
912
913
914
915
//    // clear old data
//    i_temp.clear();
//    q.clear();

916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
   for ( unsigned int i=0; i<_idxs.size(); ++i )
      q.push( _idxs[i] );

   int it_count = 0;

   while ( !q.empty() && it_count < _max_iter )
   {
      ++it_count;
      unsigned int cur_i = q.front();
      q.pop();
      i_temp.clear();

      ColT col = mat_const_col( _A, cur_i );

      CIter it  = gmm::vect_const_begin( col );
      CIter ite = gmm::vect_const_end( col );

      double res_i   = -_rhs[cur_i];
      double x_i_new = _rhs[cur_i];
David Bommes's avatar
David Bommes committed
935
      double diag    = 1.0;
936
937
938
939
      for ( ; it!=ite; ++it )
      {
         res_i   += ( *it ) * _x[it.index()];
         x_i_new -= ( *it ) * _x[it.index()];
David Bommes's avatar
David Bommes committed
940
941
942
943
	 if( it.index() != cur_i)
	   i_temp.push_back( it.index() );
	 else
	   diag = *it;
944
945
      }

David Bommes's avatar
David Bommes committed
946
947
      // compare relative residuum normalized by diagonal entry
      if ( res_i*res_i/diag > t2 )
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
      {
         _x[cur_i] += x_i_new/_A( cur_i, cur_i );

         for ( unsigned int j=0; j<i_temp.size(); ++j )
            q.push( i_temp[j] );
      }
   }

   return it_count;
}

//-----------------------------------------------------------------------------

template<class MatrixT, class VectorT>
double residuum_norm( MatrixT& _A, VectorT& _x, VectorT& _rhs )
{
   if ( gmm::mat_ncols( _A ) != _x.size() )
David Bommes's avatar
David Bommes committed
965
966
967
      std::cerr << "DIM ERROR (residuum_norm): " << gmm::mat_ncols( _A ) << " vs " << _x.size() << std::endl;
   if ( gmm::mat_nrows( _A) !=_rhs.size() )
      std::cerr << "DIM ERROR 2 (residuum_norm): " << gmm::mat_nrows( _A) << " vs " << _rhs.size() << std::endl;
968
969

   // temp vectors
David Bommes's avatar
David Bommes committed
970
971
   VectorT Ax( _rhs.size());
   VectorT res( _rhs.size() );
972
973
974
975
976
977
978
979
980
981
982

   gmm::mult( _A,_x, Ax );
   gmm::add( Ax, gmm::scaled( _rhs, -1.0 ), res );

   return gmm::vect_norm2( res );
}


//-----------------------------------------------------------------------------


David Bommes's avatar
David Bommes committed
983
984
985
template<class MatrixT, class MatrixT2, class VectorT>
void factored_to_quadratic( MatrixT& _F, MatrixT2& _Q, VectorT& _rhs)
{
Andreas Fabri's avatar
Andreas Fabri committed
986
987
  gmm::size_type m = gmm::mat_nrows(_F);
  gmm::size_type n = gmm::mat_ncols(_F);
David Bommes's avatar
David Bommes committed
988
989
990
991
992

  // resize result matrix and vector
  gmm::resize(_Q, n-1, n-1);
  gmm::resize(_rhs, n);

993
994
995
//  // set up transposed
//  MatrixT Ft(n,m);
//  gmm::copy(gmm::transposed(_F), Ft);
David Bommes's avatar
David Bommes committed
996
997
998

  // compute quadratic matrix
  MatrixT Q(n,n);
999
1000
//  gmm::mult(Ft,_F,Q);
  gmm::mult(gmm::transposed(_F),_F,Q);