Functions.cxx
1 // Copyright (C) 2001-2011 INRIA, Vivien Mallet
2 // Author(s): Marc DuruflĂ©, Marc Fragu, Vivien Mallet
3 //
4 // This file is part of the linear-algebra library Seldon,
5 // http://seldon.sourceforge.net/.
6 //
7 // Seldon is free software; you can redistribute it and/or modify it under the
8 // terms of the GNU Lesser General Public License as published by the Free
9 // Software Foundation; either version 2.1 of the License, or (at your option)
10 // any later version.
11 //
12 // Seldon is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
15 // more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with Seldon. If not, see http://www.gnu.org/licenses/.
19 
20 
21 #ifndef SELDON_FILE_FUNCTIONS_CXX
22 
23 #include "Functions.hxx"
24 
25 #include "../computation/basic_functions/Functions_Vector.cxx"
26 #include <list>
27 
28 /*
29  Functions defined in this file:
30  (storage RowSparse, ColSparse, RowSymSparse, ColSymSparse, and dense)
31 
32  X = A(i, :)
33  GetRow(A, i, X)
34 
35  X = A(:, j)
36  GetCol(A, j, X)
37 
38  A(i, :) = X
39  SetRow(X, i, A)
40 
41  A(:, j) = X
42  SetCol(X, j, A)
43 
44 
45  (dense storages only)
46 
47  A = A(row_perm, col_perm)
48  ApplyPermutation(A, row_perm, col_perm)
49  ApplyPermutation(A, row_perm, col_perm, starting_index)
50 
51  A(row_perm, col_perm) = A
52  ApplyInversePermutation(A, row_perm, col_perm)
53  ApplyInversePermutation(A, row_perm, col_perm, starting_index)
54 
55  A = Drow * A * Dcol
56  ScaleMatrix(A, Drow, Dcol)
57 
58  A = Drow * A
59  ScaleLeftMatrix(A, Drow)
60 
61  A = A * Dcol
62  ScaleRightMatrix(A, Dcol)
63 
64  Implementations of ApplyPermutation, ApplyInversePermutation,
65  ScaleMatrix, ScaleLeftMatrix and ScaleRightMatrix for sparse storages
66  are present in file matrix_sparse/Permutation_ScalingMatrix.cxx
67 */
68 
69 namespace Seldon
70 {
71 
73 
79  template <class T0, class Allocator0, class T1, class Allocator1>
82  {
83 #ifdef SELDON_CHECK_BOUNDS
84  int m = M.GetM();
85  if (i < 0 || i >= m)
86  throw WrongIndex("GetRow()",
87  string("Index should be in [0, ") + to_str(m - 1)
88  + "], but is equal to " + to_str(i) + ".");
89 #endif
90 
91  long* ptr = M.GetPtr();
92  int* ind = M.GetInd();
93  T0* data = M.GetData();
94  int size_row = ptr[i+1] - ptr[i];
95 
96  X.Reallocate(size_row);
97  long shift = ptr[i];
98  for (int j = 0; j < size_row; j++)
99  {
100  X.Index(j) = ind[shift + j];
101  X.Value(j) = data[shift + j];
102  }
103  }
104 
105 
107 
113  template <class T0, class Allocator0, class T1, class Allocator1>
116  {
117 #ifdef SELDON_CHECK_BOUNDS
118  int m = M.GetM();
119  if (i < 0 || i >= m)
120  throw WrongIndex("GetRow()",
121  string("Index should be in [0, ") + to_str(m - 1)
122  + "], but is equal to " + to_str(i) + ".");
123 #endif
124 
125  long* ptr = M.GetPtr();
126  int* ind = M.GetInd();
127  T0* data = M.GetData();
128  list<pair<int, T0> > vec;
129  for (int j = 0; j < M.GetN(); j++)
130  for (long k = ptr[j]; k < ptr[j+1]; k++)
131  if (ind[k] == i)
132  vec.push_back(make_pair(j, data[k]));
133 
134  typename list<pair<int, T0> >::iterator it;
135  X.Reallocate(vec.size());
136  int j = 0;
137  for (it = vec.begin(); it != vec.end(); it++)
138  {
139  X.Index(j) = it->first;
140  X.Value(j) = it->second;
141  j++;
142  }
143  }
144 
145 
147 
153  template <class T0, class Allocator0, class T1, class Allocator1>
156  {
157 #ifdef SELDON_CHECK_BOUNDS
158  int m = M.GetM();
159  if (i < 0 || i >= m)
160  throw WrongIndex("GetRow()",
161  string("Index should be in [0, ") + to_str(m - 1)
162  + "], but is equal to " + to_str(i) + ".");
163 #endif
164 
165  long* ptr = M.GetPtr();
166  int* ind = M.GetInd();
167  T0* data = M.GetData();
168  list<pair<int, T0> > vec;
169  for (int j = 0; j < i; j++)
170  for (long k = ptr[j]; k < ptr[j+1]; k++)
171  if (ind[k] == i)
172  vec.push_back(make_pair(j, data[k]));
173 
174  for (long k = ptr[i]; k < ptr[i+1]; k++)
175  vec.push_back(make_pair(ind[k], data[k]));
176 
177  typename list<pair<int, T0> >::iterator it;
178  X.Reallocate(vec.size());
179  int j = 0;
180  for (it = vec.begin(); it != vec.end(); it++)
181  {
182  X.Index(j) = it->first;
183  X.Value(j) = it->second;
184  j++;
185  }
186  }
187 
188 
190 
196  template <class T0, class Allocator0, class T1, class Allocator1>
199  {
200 #ifdef SELDON_CHECK_BOUNDS
201  int m = M.GetM();
202  if (i < 0 || i >= m)
203  throw WrongIndex("GetRow()",
204  string("Index should be in [0, ") + to_str(m - 1)
205  + "], but is equal to " + to_str(i) + ".");
206 #endif
207 
208  long* ptr = M.GetPtr();
209  int* ind = M.GetInd();
210  T0* data = M.GetData();
211  list<pair<int, T0> > vec;
212  for (long k = ptr[i]; k < ptr[i+1]; k++)
213  vec.push_back(make_pair(ind[k], data[k]));
214 
215  for (int j = i+1; j < M.GetM(); j++)
216  for (long k = ptr[j]; k < ptr[j+1]; k++)
217  if (ind[k] == i)
218  vec.push_back(make_pair(j, data[k]));
219 
220  typename list<pair<int, T0> >::iterator it;
221  X.Reallocate(vec.size());
222  int j = 0;
223  for (it = vec.begin(); it != vec.end(); it++)
224  {
225  X.Index(j) = it->first;
226  X.Value(j) = it->second;
227  j++;
228  }
229  }
230 
231 
233 
239  template <class T0, class Prop0, class Storage0, class Allocator0,
240  class T1, class Storage1, class Allocator1>
243  {
244  if (Storage0::Sparse)
245  throw WrongArgument("GetRow", "Function intended to dense matrices");
246 
247  X.Reallocate(M.GetN());
248  for (int j = 0; j < M.GetN(); j++)
249  X(j) = M(i, j);
250  }
251 
252 
254 
260  template <class T0, class Allocator0, class T1, class Allocator1>
263  {
264 #ifdef SELDON_CHECK_BOUNDS
265  int n = M.GetN();
266  if (j < 0 || j >= n)
267  throw WrongIndex("GetCol()",
268  string("Index should be in [0, ") + to_str(n - 1)
269  + "], but is equal to " + to_str(j) + ".");
270 #endif
271 
272  long* ptr = M.GetPtr();
273  int* ind = M.GetInd();
274  T0* data = M.GetData();
275  int size_col = ptr[j+1] - ptr[j];
276 
277  X.Reallocate(size_col);
278  long shift = ptr[j];
279  for (int i = 0; i < size_col; i++)
280  {
281  X.Index(i) = ind[shift + i];
282  X.Value(i) = data[shift + i];
283  }
284  }
285 
286 
288 
294  template <class T0, class Allocator0, class T1, class Allocator1>
297  {
298 #ifdef SELDON_CHECK_BOUNDS
299  int n = M.GetN();
300  if (j < 0 || j >= n)
301  throw WrongIndex("GetCol()",
302  string("Index should be in [0, ") + to_str(n - 1)
303  + "], but is equal to " + to_str(j) + ".");
304 #endif
305 
306  long* ptr = M.GetPtr();
307  int* ind = M.GetInd();
308  T0* data = M.GetData();
309  int m = M.GetM();
310 
311  list<pair<int, T0> > vec;
312  for (int i = 0; i < m; i++)
313  for (long k = ptr[i]; k < ptr[i+1]; k++)
314  if (ind[k] == j)
315  vec.push_back(make_pair(i, data[k]));
316 
317  typename list<pair<int, T0> >::iterator it;
318  X.Reallocate(vec.size());
319  int i = 0;
320  for (it = vec.begin(); it != vec.end(); it++)
321  {
322  X.Index(i) = it->first;
323  X.Value(i) = it->second;
324  i++;
325  }
326  }
327 
328 
330 
336  template <class T0, class Allocator0, class T1, class Allocator1>
339  {
340  // symmetric matrix row = col
341  GetRow(M, j, X);
342  }
343 
344 
346 
352  template <class T0, class Allocator0, class T1, class Allocator1>
355  {
356  // symmetric matrix row = col
357  GetRow(M, j, X);
358  }
359 
360 
362 
367  template <class T0, class Prop0, class Allocator0,
368  class T1, class Allocator1>
371  {
372  int a, b;
373  M.GetProcessorRowRange(a, b);
374  for (int i = a; i < b; i++)
375  X.SetBuffer(i, M(i, j));
376  X.Flush();
377  }
378 
379 
381 
386  template <class T0, class Prop0, class Allocator0,
387  class T1, class Allocator1>
390  {
391  int a, b;
392  M.GetProcessorRowRange(a, b);
393  for (int i = a; i < b; i++)
394  X.SetBuffer(i, M(i, j));
395  X.Flush();
396  }
397 
398 
400 
406  template <class T0, class Prop0, class Storage0, class Allocator0,
407  class T1, class Storage1, class Allocator1>
410  {
411  if (Storage0::Sparse)
412  throw WrongArgument("GetCol", "Function intended to dense matrices");
413 
414  X.Reallocate(M.GetM());
415  for (int i = 0; i < M.GetM(); i++)
416  X(i) = M(i, j);
417  }
418 
419 
421 
428  template <class T0, class Prop0, class Storage0, class Allocator0,
429  class T1, class Prop1, class Storage1, class Allocator1>
431  int begin, int end,
433  {
434  M_out.Reallocate(M_in.GetM(), end - begin);
435  for (int i = 0; i < M_in.GetM(); i++)
436  for (int j = begin, k = 0; j < end; j++, k++)
437  M_out(i, k) = M_in(i, j);
438  }
439 
440 
442 
448  template <class T0, class Prop0, class Storage0, class Allocator0,
449  class T1, class Storage1, class Allocator1>
450  void SetRow(const Vector<T1, Storage1, Allocator1>& X,
452  {
453  if (Storage0::Sparse)
454  throw WrongArgument("SetRow", "Function intended to dense matrices");
455 
456  for (int j = 0; j < M.GetN(); j++)
457  M.Set(i, j, X(j));
458  }
459 
460 
462 
467  template <class T0, class Prop0, class Allocator0,
468  class T1, class Allocator1>
469  void SetRow(const Vector<T1, PETScSeq, Allocator1>& X,
471  {
472  for (int j = 0; j < M.GetN(); j++)
473  M.SetBuffer(i, j, X(j));
474  M.Flush();
475  }
476 
477 
479 
484  template <class T0, class Prop0, class Allocator0,
485  class T1, class Allocator1>
486  void SetRow(const Vector<T1, PETScPar, Allocator1>& X,
488  {
489  for (int j = 0; j < M.GetN(); j++)
490  M.SetBuffer(i, j, X(j));
491  M.Flush();
492  }
493 
494 
496 
502  template <class T0, class Allocator0, class T1, class Allocator1>
505  {
506  int m = M.GetM();
507  int n = M.GetN();
508  long nnz = M.GetDataSize();
509  int Nx = X.GetSize();
510 
511 #ifdef SELDON_CHECK_BOUNDS
512  if (i < 0 || i >= m)
513  throw WrongIndex("SetRow(Vector, int, Matrix<RowSparse>)",
514  string("Index should be in [0, ") + to_str(m - 1)
515  + "], but is equal to " + to_str(i) + ".");
516 #endif
517 
518  long *ptr_vector = M.GetPtr();
519  long ptr_i0 = ptr_vector[i], ptr_i1 = ptr_vector[i + 1];
520  int row_size_difference = Nx - ptr_i1 + ptr_i0;
521 
522  if (row_size_difference == 0)
523  {
524  for (int k = 0; k < Nx; k++)
525  M.GetInd()[k + ptr_i0] = X.Index(k);
526  for (int k = 0; k < Nx; k++)
527  M.GetData()[k + ptr_i0] = X.Value(k);
528  return;
529  }
530 
532  new_ind_vector(nnz + row_size_difference);
533  for (long k = 0; k < ptr_i0; k++)
534  new_ind_vector(k) = M.GetInd()[k];
535  for (int k = 0; k < Nx; k++)
536  new_ind_vector(k + ptr_i0) = X.Index(k);
537  for (long k = 0; k < nnz - ptr_i1; k++)
538  new_ind_vector(k + ptr_i0 + Nx) = M.GetInd()[k + ptr_i1];
539 
541  new_data_vector(nnz + row_size_difference);
542  for (long k = 0; k < ptr_i0; k++)
543  new_data_vector(k) = M.GetData()[k];
544  for (int k = 0; k < Nx; k++)
545  new_data_vector(k + ptr_i0) = X.Value(k);
546  for (long k = 0; k < nnz - ptr_i1; k++)
547  new_data_vector(k + ptr_i0 + Nx) = M.GetData()[k + ptr_i1];
548 
549  Vector<long> new_ptr_vector(m + 1);
550  for (int j = 0; j < i + 1; j++)
551  new_ptr_vector(j) = ptr_vector[j];
552  for (int j = i + 1; j < m+1; j++)
553  new_ptr_vector(j) = ptr_vector[j] + row_size_difference;
554 
555  M.SetData(m, n, new_data_vector, new_ptr_vector, new_ind_vector);
556  }
557 
558 
560 
566  template <class T0, class Allocator0, class T1, class Allocator1>
569  {
570  int m = M.GetM();
571  int n = M.GetN();
572  long nnz = M.GetDataSize();
573 
574  long* ptr = M.GetPtr();
575  int* ind = M.GetInd();
576  T0* data = M.GetData();
577 
578 #ifdef SELDON_CHECK_BOUNDS
579  if (i < 0 || i >= m)
580  throw WrongIndex("SetRow(Vector, int, Matrix<ColSparse>)",
581  string("Index should be in [0, ") + to_str(m - 1)
582  + "], but is equal to " + to_str(i) + ".");
583 #endif
584 
585  // we retrieve the current row of matrix M
587  GetRow(M, i, row);
588 
589  // counting the new number of new elements
590  long new_nnz = nnz + X.GetM() - row.GetM();
591 
592  // if we have the same pattern for old and new row
593  // we change only the values
594  bool same_pattern = true;
595  if (X.GetM() == row.GetM())
596  {
597  for (int k = 0; k < X.GetM(); k++)
598  if (X.Index(k) != row.Index(k))
599  same_pattern = false;
600  }
601  else
602  same_pattern = false;
603 
604  if (same_pattern)
605  {
606  for (int k = 0; k < X.GetM(); k++)
607  {
608  int j = X.Index(k);
609  for (long k2 = ptr[j]; k2 < ptr[j+1]; k2++)
610  if (ind[k2] == i)
611  data[k2] = X.Value(k);
612  }
613  }
614  else
615  {
616  // the pattern has to be modified, reallocating a new matrix
617  Vector<long> Ptr(n+1); Vector<int> Ind(new_nnz);
619 
620  // loop on first rows
621  int kx = 0, kr = 0;
622  long nb = 0;
623  Ptr(0) = 0;
624  for (int j = 0; j < n; j++)
625  {
626  // trying to find the corresponding index for X and row
627  bool valX = false, val_row = false;
628  while ( (kx < X.GetM()) && (X.Index(kx) < j))
629  kx++;
630 
631  if ( (kx < X.GetM()) && (X.Index(kx) == j))
632  valX = true;
633 
634  while ( (kr < row.GetM()) && (row.Index(kr) < j))
635  kr++;
636 
637  if ( (kr < row.GetM()) && (row.Index(kr) == j))
638  val_row = true;
639 
640  // filling matrix until index i
641  long k = ptr[j];
642  while ((k < ptr[j+1]) && (ind[k] < i))
643  {
644  Ind(nb) = ind[k];
645  Val(nb) = data[k];
646  nb++;
647  k++;
648  }
649 
650  // if there is a value on X for this index, adding it
651  if (valX)
652  {
653  Ind(nb) = i;
654  Val(nb) = X.Value(kx);
655  nb++;
656  kx++;
657  }
658 
659  // if there is a value on row for this index
660  // we skip this value
661  if (val_row)
662  k++;
663 
664  // last values of column
665  while (k < ptr[j+1])
666  {
667  Ind(nb) = ind[k];
668  Val(nb) = data[k];
669  nb++;
670  k++;
671  }
672 
673  Ptr(j+1) = nb;
674  }
675 
676  M.SetData(m, n, Val, Ptr, Ind);
677  }
678  }
679 
680 
682 
688  template <class T0, class Allocator0, class T1, class Allocator1>
691  {
692  int n = M.GetN();
693  long nnz = M.GetDataSize();
694 
695  long* ptr = M.GetPtr();
696  int* ind = M.GetInd();
697  T0* data = M.GetData();
698 
699 #ifdef SELDON_CHECK_BOUNDS
700  if (i < 0 || i >= n)
701  throw WrongIndex("SetRow(Vector, int, Matrix<RowSymSparse>)",
702  string("Index should be in [0, ") + to_str(n - 1)
703  + "], but is equal to " + to_str(i) + ".");
704 #endif
705 
706  // we retrieve the current row of matrix M
708  GetRow(M, i, row);
709 
710  // counting the new number of new elements
711  long new_nnz = nnz + X.GetM() - row.GetM();
712 
713  // if we have the same pattern for old and new row
714  // we change only the values
715  bool same_pattern = true;
716  if (X.GetM() == row.GetM())
717  {
718  for (int k = 0; k < X.GetM(); k++)
719  if (X.Index(k) != row.Index(k))
720  same_pattern = false;
721  }
722  else
723  same_pattern = false;
724 
725  if (same_pattern)
726  {
727  int kdiag = 0;
728  for (int k = 0; k < X.GetM(); k++)
729  {
730  int j = X.Index(k);
731  if (j < i)
732  {
733  for (long k2 = ptr[j]; k2 < ptr[j+1]; k2++)
734  if (ind[k2] == i)
735  data[k2] = X.Value(k);
736 
737  kdiag = k+1;
738  }
739  else
740  {
741  long k2 = ptr[i] + k - kdiag;
742  data[k2] = X.Value(k);
743  }
744  }
745  }
746  else
747  {
748  // the pattern has to be modified, reallocating a new matrix
749  Vector<long> Ptr(n+1); Vector<int> Ind(new_nnz);
751 
752  // loop on first rows
753  int kx = 0, kr = 0;
754  long nb = 0;
755  Ptr(0) = 0;
756  for (int j = 0; j < i; j++)
757  {
758  // trying to find the corresponding index for X and row
759  bool valX = false, val_row = false;
760  while ( (kx < X.GetM()) && (X.Index(kx) < j))
761  kx++;
762 
763  if ( (kx < X.GetM()) && (X.Index(kx) == j))
764  valX = true;
765 
766  while ( (kr < row.GetM()) && (row.Index(kr) < j))
767  kr++;
768 
769  if ( (kr < row.GetM()) && (row.Index(kr) == j))
770  val_row = true;
771 
772  // filling matrix until index i
773  long k = ptr[j];
774  while ((k < ptr[j+1]) && (ind[k] < i))
775  {
776  Ind(nb) = ind[k];
777  Val(nb) = data[k];
778  nb++;
779  k++;
780  }
781 
782  // if there is a value on X for this index, adding it
783  if (valX)
784  {
785  Ind(nb) = i;
786  Val(nb) = X.Value(kx);
787  nb++;
788  kx++;
789  }
790 
791  // if there is a value on row for this index
792  // we go to the next value
793  if (val_row)
794  k++;
795 
796  // last values of row
797  while (k < ptr[j+1])
798  {
799  Ind(nb) = ind[k];
800  Val(nb) = data[k];
801  nb++;
802  k++;
803  }
804 
805  Ptr(j+1) = nb;
806  }
807 
808  // then changing row i
809  while (kx < X.GetM())
810  {
811  Ind(nb) = X.Index(kx);
812  Val(nb) = X.Value(kx);
813  nb++;
814  kx++;
815  }
816 
817  Ptr(i+1) = nb;
818 
819  // then last rows of M
820  for (int j = i+1; j < n; j++)
821  {
822  for (long k = ptr[j]; k < ptr[j+1]; k++)
823  {
824  Ind(nb) = ind[k];
825  Val(nb) = data[k];
826  nb++;
827  }
828 
829  Ptr(j+1) = nb;
830  }
831 
832  M.SetData(n, n, Val, Ptr, Ind);
833  }
834  }
835 
836 
838 
844  template <class T0, class Allocator0, class T1, class Allocator1>
847  {
848  int n = M.GetN();
849  long nnz = M.GetDataSize();
850 
851  long* ptr = M.GetPtr();
852  int* ind = M.GetInd();
853  T0* data = M.GetData();
854 
855 #ifdef SELDON_CHECK_BOUNDS
856  if (i < 0 || i >= n)
857  throw WrongIndex("SetRow(Vector, int, Matrix<RowSymSparse>)",
858  string("Index should be in [0, ") + to_str(n - 1)
859  + "], but is equal to " + to_str(i) + ".");
860 #endif
861 
862  // we retrieve the current row of matrix M
864  GetRow(M, i, row);
865 
866  // counting the new number of new elements
867  long new_nnz = nnz + X.GetM() - row.GetM();
868 
869  // if we have the same pattern for old and new row
870  // we change only the values
871  bool same_pattern = true;
872  if (X.GetM() == row.GetM())
873  {
874  for (int k = 0; k < X.GetM(); k++)
875  if (X.Index(k) != row.Index(k))
876  same_pattern = false;
877  }
878  else
879  same_pattern = false;
880 
881  if (same_pattern)
882  {
883  for (int k = 0; k < X.GetM(); k++)
884  {
885  int j = X.Index(k);
886  if (j <= i)
887  {
888  long k2 = ptr[i] + k;
889  data[k2] = X.Value(k);
890  }
891  else
892  {
893  for (long k2 = ptr[j]; k2 < ptr[j+1]; k2++)
894  if (ind[k2] == i)
895  data[k2] = X.Value(k);
896  }
897  }
898  }
899  else
900  {
901  // the pattern has to be modified, reallocating a new matrix
902  Vector<long> Ptr(n+1); Vector<int> Ind(new_nnz);
904 
905  // first columns of M
906  Ptr(0) = 0;
907  long nb = 0;
908  for (int j = 0; j < i; j++)
909  {
910  for (long k = ptr[j]; k < ptr[j+1]; k++)
911  {
912  Ind(nb) = ind[k];
913  Val(nb) = data[k];
914  nb++;
915  }
916 
917  Ptr(j+1) = nb;
918  }
919 
920  // then changing column i
921  int kx = 0;
922  while ( (kx < X.GetM()) && (X.Index(kx) <= i) )
923  {
924  Ind(nb) = X.Index(kx);
925  Val(nb) = X.Value(kx);
926  nb++;
927  kx++;
928  }
929 
930  Ptr(i+1) = nb;
931 
932  // loop on last columns
933  for (int j = i+1; j < n; j++)
934  {
935  // trying to find the corresponding index for X
936  bool valX = false;
937  while ( (kx < X.GetM()) && (X.Index(kx) < j))
938  kx++;
939 
940  if ( (kx < X.GetM()) && (X.Index(kx) == j))
941  valX = true;
942 
943  // filling matrix until index i
944  long k = ptr[j];
945  while ((k < ptr[j+1]) && (ind[k] < i))
946  {
947  Ind(nb) = ind[k];
948  Val(nb) = data[k];
949  nb++;
950  k++;
951  }
952 
953  // if there is a value on X for this index, adding it
954  if (valX)
955  {
956  Ind(nb) = i;
957  Val(nb) = X.Value(kx);
958  nb++;
959  kx++;
960  }
961 
962  // if there is a value on column j for this index
963  // we go to the next value
964  if ( (k < ptr[j+1]) && (ind[k] == i))
965  k++;
966 
967  // last values of column
968  while (k < ptr[j+1])
969  {
970  Ind(nb) = ind[k];
971  Val(nb) = data[k];
972  nb++;
973  k++;
974  }
975 
976  Ptr(j+1) = nb;
977  }
978 
979  M.SetData(n, n, Val, Ptr, Ind);
980  }
981  }
982 
983 
985 
991  template <class T0, class Prop0, class Allocator0,
992  class T1, class Storage1, class Allocator1>
993  void SetRow(const Vector<T1, Storage1, Allocator1>& X,
995  {
996  for (int j = 0; j <= i; j++)
997  M.Set(i, j, X(j));
998  }
999 
1000 
1002 
1008  template <class T0, class Prop0, class Allocator0,
1009  class T1, class Storage1, class Allocator1>
1010  void SetRow(const Vector<T1, Storage1, Allocator1>& X,
1012  {
1013  for (int j = 0; j <= i; j++)
1014  M.Set(i, j, X(j));
1015  }
1016 
1017 
1019 
1025  template <class T0, class Prop0, class Allocator0,
1026  class T1, class Storage1, class Allocator1>
1027  void SetRow(const Vector<T1, Storage1, Allocator1>& X,
1029  {
1030  for (int j = 0; j <= i; j++)
1031  M.Set(i, j, X(j));
1032  }
1033 
1034 
1036 
1042  template <class T0, class Prop0, class Allocator0,
1043  class T1, class Storage1, class Allocator1>
1044  void SetRow(const Vector<T1, Storage1, Allocator1>& X,
1046  {
1047  for (int j = 0; j <= i; j++)
1048  M.Set(i, j, X(j));
1049  }
1050 
1051 
1053 
1059  template <class T0, class Prop0, class Allocator0,
1060  class T1, class Storage1, class Allocator1>
1061  void SetRow(const Vector<T1, Storage1, Allocator1>& X,
1063  {
1064  for (int j = i; j < M.GetN(); j++)
1065  M.Set(i, j, X(j));
1066  }
1067 
1068 
1070 
1076  template <class T0, class Prop0, class Allocator0,
1077  class T1, class Storage1, class Allocator1>
1078  void SetRow(const Vector<T1, Storage1, Allocator1>& X,
1080  {
1081  for (int j = i; j < M.GetN(); j++)
1082  M.Set(i, j, X(j));
1083  }
1084 
1085 
1087 
1093  template <class T0, class Prop0, class Allocator0,
1094  class T1, class Storage1, class Allocator1>
1095  void SetRow(const Vector<T1, Storage1, Allocator1>& X,
1097  {
1098  for (int j = i; j < M.GetN(); j++)
1099  M.Set(i, j, X(j));
1100  }
1101 
1102 
1104 
1110  template <class T0, class Prop0, class Allocator0,
1111  class T1, class Storage1, class Allocator1>
1112  void SetRow(const Vector<T1, Storage1, Allocator1>& X,
1114  {
1115  for (int j = i; j < M.GetN(); j++)
1116  M.Set(i, j, X(j));
1117  }
1118 
1119 
1121 
1127  template <class T0, class Prop0, class Storage0, class Allocator0,
1128  class T1, class Storage1, class Allocator1>
1129  void SetCol(const Vector<T1, Storage1, Allocator1>& X,
1131  {
1132  if (Storage0::Sparse)
1133  throw WrongArgument("SetCol", "Function intended to dense matrices");
1134 
1135  for (int i = 0; i < M.GetM(); i++)
1136  M.Set(i, j, X(i));
1137  }
1138 
1139 
1141 
1146  template <class T0, class Prop0, class Allocator0,
1147  class T1, class Allocator1>
1148  void SetCol(const Vector<T1, PETScSeq, Allocator1>& X,
1150  {
1151  int a, b;
1152  M.GetProcessorRowRange(a, b);
1153  for (int i = a; i < b; i++)
1154  M.SetBuffer(i, j, X(i));
1155  M.Flush();
1156  }
1157 
1158 
1160 
1165  template <class T0, class Prop0, class Allocator0,
1166  class T1, class Allocator1>
1167  void SetCol(const Vector<T1, PETScPar, Allocator1>& X,
1169  {
1170  int a, b;
1171  M.GetProcessorRowRange(a, b);
1172  for (int i = a; i < b; i++)
1173  M.SetBuffer(i, j, X(i));
1174  M.Flush();
1175  }
1176 
1177 
1179 
1185  template <class T0, class Allocator0,
1186  class T1, class Allocator1>
1187  void SetCol(const Vector<T1, VectFull, Allocator1>& X,
1189  {
1191  for (int k = 0; k < X.GetLength(); k++)
1192  {
1193  T1 value = X(k);
1194  if (value != T1(0.))
1195  X_sparse.AddInteraction(k, value);
1196  }
1197 
1198  SetCol(X_sparse, j, M);
1199  }
1200 
1201 
1203 
1209  template <class T0, class Allocator0, class T1, class Allocator1>
1212  {
1213  int m = M.GetM();
1214  int n = M.GetN();
1215  long nnz = M.GetDataSize();
1216  int Nx = X.GetSize();
1217 
1218 #ifdef SELDON_CHECK_BOUNDS
1219  if (j < 0 || j >= n)
1220  throw WrongIndex("SetCol(Vector, int, Matrix<RowSparse>)",
1221  string("Index should be in [0, ") + to_str(n - 1)
1222  + "], but is equal to " + to_str(j) + ".");
1223 #endif
1224 
1225  // The column to be changed.
1227  GetCol(M, j, column_j);
1228  int Ncolumn_j = column_j.GetSize();
1229  int column_size_difference = Nx - Ncolumn_j;
1230 
1231  // Built a vector indexed with the rows of column_j and X.
1232  Vector<int, VectSparse> column_j_mask;
1233  Vector<int> index_j(Ncolumn_j);
1234  Vector<int> value_j(Ncolumn_j);
1235  for (int p = 0; p < Ncolumn_j; p++)
1236  index_j(p) = column_j.Index(p);
1237  value_j.Fill(-1);
1238  column_j_mask.SetData(value_j, index_j);
1239  value_j.Nullify();
1240  index_j.Nullify();
1241  Vector<int, VectSparse> X_mask;
1242  Vector<int> index_x(Nx);
1243  Vector<int> value_x(Nx);
1244  for (int p = 0; p < Nx; p++)
1245  index_x(p) = X.Index(p);
1246  value_x.Fill(1);
1247  X_mask.SetData(value_x, index_x);
1248  value_x.Nullify();
1249  index_x.Nullify();
1250  X_mask.AddInteractionRow(column_j_mask.GetSize(),
1251  column_j_mask.GetIndex(),
1252  column_j_mask.GetData(), true);
1253 
1254  // Built the new pointer vector.
1255  Vector<long> ptr_vector;
1256  ptr_vector.SetData(m + 1, M.GetPtr());
1257  Vector<long> new_ptr_vector(m + 1);
1258  new_ptr_vector.Zero();
1259  for (int p = 0; p < X_mask.GetM(); p++)
1260  new_ptr_vector(X_mask.Index(p) + 1) = X_mask.Value(p);
1261 
1262  for (int p = 0; p < m; p++)
1263  new_ptr_vector(p + 1) += new_ptr_vector(p);
1264 
1265  new_ptr_vector += ptr_vector;
1266 
1267  // Built the new index and the new data vectors row by row.
1268  Vector<int>
1269  new_ind_vector(nnz + column_size_difference);
1271  new_data_vector(nnz + column_size_difference);
1272 
1273  Vector<T0, VectSparse, Allocator0> working_vector;
1274  int Nworking_vector;
1275 
1276  int line = 0;
1277  for (int interaction = 0; interaction < X_mask.GetM(); interaction++)
1278  {
1279  int ind_x = X_mask.Index(interaction);
1280  for (int k = 0; k < ptr_vector(ind_x) - ptr_vector(line); k++)
1281  new_ind_vector.GetData()[k + new_ptr_vector(line)] =
1282  M.GetInd()[k + ptr_vector(line)];
1283  for (int k = 0; k < ptr_vector(ind_x) - ptr_vector(line); k++)
1284  new_data_vector.GetData()[k + new_ptr_vector(line)] =
1285  M.GetData()[k + ptr_vector(line)];
1286 
1287  int ind_j;
1288  Nworking_vector = ptr_vector(ind_x + 1) - ptr_vector(ind_x);
1289  working_vector.SetData(Nworking_vector,
1290  M.GetData() + ptr_vector(ind_x),
1291  M.GetInd() + ptr_vector(ind_x));
1292  switch(X_mask.Value(interaction))
1293  {
1294  // Collision.
1295  case 0:
1296  working_vector.Get(j) = X(ind_x);
1297  for (int k = 0; k < Nworking_vector; k++)
1298  new_ind_vector.GetData()[k + new_ptr_vector(ind_x)] =
1299  working_vector.GetIndex()[k];
1300  for (int k = 0; k < Nworking_vector; k++)
1301  new_data_vector.GetData()[k + new_ptr_vector(ind_x)] =
1302  working_vector.GetData()[k];
1303  break;
1304 
1305  // Suppression.
1306  case -1:
1307  ind_j = 0;
1308  while (ind_j < Nworking_vector &&
1309  working_vector.Index(ind_j) != j)
1310  ind_j++;
1311 
1312  for (int k = 0; k < ind_j; k++)
1313  new_ind_vector.GetData()[k + new_ptr_vector(ind_x)] =
1314  working_vector.GetIndex()[k];
1315  for (int k = 0; k < Nworking_vector - ind_j - 1; k++)
1316  new_ind_vector.GetData()[k + new_ptr_vector(ind_x) + ind_j] =
1317  working_vector.GetIndex()[k + ind_j + 1];
1318 
1319  for (int k = 0; k < ind_j; k++)
1320  new_data_vector.GetData()[k + new_ptr_vector(ind_x)] =
1321  working_vector.GetData()[k];
1322  for (int k = 0; k < Nworking_vector - ind_j - 1; k++)
1323  new_data_vector.GetData()[k + new_ptr_vector(ind_x) + ind_j] =
1324  working_vector.GetData()[k + ind_j + 1];
1325  break;
1326 
1327  // Addition.
1328  case 1:
1329  ind_j = 0;
1330  while (ind_j < Nworking_vector &&
1331  working_vector.Index(ind_j) < j)
1332  ind_j++;
1333  for (int k = 0; k < ind_j; k++)
1334  new_ind_vector.GetData()[k + new_ptr_vector(ind_x)] =
1335  working_vector.GetIndex()[k];
1336  new_ind_vector.GetData()[new_ptr_vector(ind_x) + ind_j] = j;
1337  for (int k = 0; k < Nworking_vector - ind_j; k++)
1338  new_ind_vector.GetData()[k + new_ptr_vector(ind_x) + ind_j + 1]
1339  = working_vector.GetIndex()[k + ind_j];
1340 
1341  for (int k = 0; k < ind_j; k++)
1342  new_data_vector.GetData()[k + new_ptr_vector(ind_x)] =
1343  working_vector.GetData()[k];
1344  new_data_vector.GetData()[new_ptr_vector(ind_x) + ind_j]
1345  = X(ind_x);
1346  for (int k = 0; k < Nworking_vector - ind_j; k++)
1347  new_data_vector.GetData()[k + new_ptr_vector(ind_x) + ind_j + 1]
1348  = working_vector.GetData()[k + ind_j];
1349  }
1350 
1351  line = ind_x + 1;
1352  working_vector.Nullify();
1353  }
1354  for (int k = 0; k < ptr_vector(m) - ptr_vector(line); k++)
1355  new_ind_vector.GetData()[k + new_ptr_vector(line)] =
1356  M.GetInd()[k + ptr_vector(line)];
1357  for (int k = 0; k < ptr_vector(m) - ptr_vector(line); k++)
1358  new_data_vector.GetData()[k + new_ptr_vector(line)] =
1359  M.GetData()[k + ptr_vector(line)];
1360 
1361  M.SetData(m, n, new_data_vector, new_ptr_vector, new_ind_vector);
1362  ptr_vector.Nullify();
1363  new_data_vector.Nullify();
1364  new_ind_vector.Nullify();
1365  new_ptr_vector.Nullify();
1366  }
1367 
1368 
1370 
1376  template <class T0, class Allocator0, class T1, class Allocator1>
1379  {
1380  int m = M.GetM();
1381  int n = M.GetN();
1382  long nnz = M.GetDataSize();
1383 
1384  long* ptr = M.GetPtr();
1385  int* ind = M.GetInd();
1386  T0* data = M.GetData();
1387 
1388 #ifdef SELDON_CHECK_BOUNDS
1389  if (j < 0 || j >= n)
1390  throw WrongIndex("SetCol(Vector, int, Matrix<ColSparse>)",
1391  string("Index should be in [0, ") + to_str(n - 1)
1392  + "], but is equal to " + to_str(j) + ".");
1393 #endif
1394 
1395  int size_col = ptr[j+1] - ptr[j];
1396  if (size_col == X.GetM())
1397  {
1398  // no need of reallocating matrix M
1399  // changing indexes and values
1400  for (int k = 0; k < X.GetM(); k++)
1401  {
1402  ind[ptr[j] + k] = X.Index(k);
1403  data[ptr[j] + k] = X.Value(k);
1404  }
1405  }
1406  else
1407  {
1408  long new_nnz = nnz + X.GetM() - size_col;
1409  // new matrix
1410  Vector<long> Ptr(n+1); Vector<int> Ind(new_nnz);
1411  Vector<T0, VectFull, Allocator0> Val(new_nnz);
1412  Ptr(0) = 0;
1413  for (int i = 0; i < j; i++)
1414  {
1415  for (long k = ptr[i]; k < ptr[i+1]; k++)
1416  {
1417  Ind(k) = ind[k];
1418  Val(k) = data[k];
1419  }
1420  Ptr(i+1) = ptr[i+1];
1421  }
1422 
1423  new_nnz = Ptr(j);
1424  for (int k = 0; k < X.GetM(); k++)
1425  {
1426  Ind(new_nnz) = X.Index(k);
1427  Val(new_nnz) = X.Value(k);
1428  new_nnz++;
1429  }
1430 
1431  Ptr(j+1) = new_nnz;
1432  for (int i = j+1; i < n; i++)
1433  {
1434  for (long k = ptr[i]; k < ptr[i+1]; k++)
1435  {
1436  Ind(new_nnz) = ind[k];
1437  Val(new_nnz) = data[k];
1438  new_nnz++;
1439  }
1440  Ptr(i+1) = new_nnz;
1441  }
1442 
1443  M.SetData(m, n, Val, Ptr, Ind);
1444  }
1445  }
1446 
1447 
1449 
1455  template <class T0, class Allocator0, class T1, class Allocator1>
1458  {
1459  // symmetric matrix, row = col
1460  SetRow(X, j, M);
1461  }
1462 
1463 
1465 
1471  template <class T0, class Allocator0, class T1, class Allocator1>
1474  {
1475  // symmetric matrix, row = col
1476  SetRow(X, j, M);
1477  }
1478 
1479 
1481 
1487  template <class T0, class Prop0, class Allocator0,
1488  class T1, class Storage1, class Allocator1>
1489  void SetCol(const Vector<T1, Storage1, Allocator1>& X,
1491  {
1492  for (int i = j; i < M.GetM(); i++)
1493  M.Set(i, j, X(i));
1494  }
1495 
1496 
1498 
1504  template <class T0, class Prop0, class Allocator0,
1505  class T1, class Storage1, class Allocator1>
1506  void SetCol(const Vector<T1, Storage1, Allocator1>& X,
1508  {
1509  for (int i = j; i < M.GetM(); i++)
1510  M.Set(i, j, X(i));
1511  }
1512 
1513 
1515 
1521  template <class T0, class Prop0, class Allocator0,
1522  class T1, class Storage1, class Allocator1>
1523  void SetCol(const Vector<T1, Storage1, Allocator1>& X,
1525  {
1526  for (int i = j; i < M.GetM(); i++)
1527  M.Set(i, j, X(i));
1528  }
1529 
1530 
1532 
1538  template <class T0, class Prop0, class Allocator0,
1539  class T1, class Storage1, class Allocator1>
1540  void SetCol(const Vector<T1, Storage1, Allocator1>& X,
1542  {
1543  for (int i = j; i < M.GetM(); i++)
1544  M.Set(i, j, X(i));
1545  }
1546 
1547 
1549 
1555  template <class T0, class Prop0, class Allocator0,
1556  class T1, class Storage1, class Allocator1>
1557  void SetCol(const Vector<T1, Storage1, Allocator1>& X,
1559  {
1560  for (int i = 0; i <= j; i++)
1561  M.Set(i, j, X(i));
1562  }
1563 
1564 
1566 
1572  template <class T0, class Prop0, class Allocator0,
1573  class T1, class Storage1, class Allocator1>
1574  void SetCol(const Vector<T1, Storage1, Allocator1>& X,
1576  {
1577  for (int i = 0; i <= j; i++)
1578  M.Set(i, j, X(i));
1579  }
1580 
1581 
1583 
1589  template <class T0, class Prop0, class Allocator0,
1590  class T1, class Storage1, class Allocator1>
1591  void SetCol(const Vector<T1, Storage1, Allocator1>& X,
1593  {
1594  for (int i = 0; i <= j; i++)
1595  M.Set(i, j, X(i));
1596  }
1597 
1598 
1600 
1606  template <class T0, class Prop0, class Allocator0,
1607  class T1, class Storage1, class Allocator1>
1608  void SetCol(const Vector<T1, Storage1, Allocator1>& X,
1610  {
1611  for (int i = 0; i <= j; i++)
1612  M.Set(i, j, X(i));
1613  }
1614 
1615 
1617 
1620  template<class T, class Prop, class Allocator>
1621  void ApplyPermutation(Matrix<T, Prop, RowMajor, Allocator>& A,
1622  const Vector<int>& row_perm,
1623  const Vector<int>& col_perm,
1624  int starting_index)
1625  {
1626  Matrix<T, Prop, RowMajor, Allocator> A_copy; A_copy = A;
1627 
1628  for (int i = 0; i < A.GetM(); i++)
1629  for (int j = 0; j < A.GetN(); j++)
1630  A(i, j) = A_copy(row_perm(i) - starting_index,
1631  col_perm(j) - starting_index);
1632  }
1633 
1634 
1636 
1639  template<class T, class Prop, class Allocator>
1640  void ApplyPermutation(Matrix<T, Prop, ColMajor, Allocator>& A,
1641  const Vector<int>& row_perm,
1642  const Vector<int>& col_perm,
1643  int starting_index)
1644  {
1645  Matrix<T, Prop, ColMajor, Allocator> A_copy; A_copy = A;
1646 
1647  for (int j = 0; j < A.GetN(); j++)
1648  for (int i = 0; i < A.GetM(); i++)
1649  A(i, j) = A_copy(row_perm(i) - starting_index,
1650  col_perm(j) - starting_index);
1651  }
1652 
1653 
1655 
1658  template<class T, class Prop, class Allocator>
1660  const Vector<int>& row_perm,
1661  const Vector<int>& col_perm,
1662  int starting_index)
1663  {
1664  Matrix<T, Prop, RowSymPacked, Allocator> A_copy; A_copy = A;
1665 
1666  for (int i = 0; i < A.GetM(); i++)
1667  for (int j = i; j < A.GetN(); j++)
1668  A(i, j) = A_copy(row_perm(i) - starting_index,
1669  row_perm(j) - starting_index);
1670  }
1671 
1672 
1674 
1677  template<class T, class Prop, class Allocator>
1679  const Vector<int>& row_perm,
1680  const Vector<int>& col_perm,
1681  int starting_index)
1682  {
1683  Matrix<T, Prop, ColSymPacked, Allocator> A_copy; A_copy = A;
1684 
1685  for (int j = 0; j < A.GetN(); j++)
1686  for (int i = 0; i <= j; i++)
1687  A(i, j) = A_copy(row_perm(i) - starting_index,
1688  row_perm(j) - starting_index);
1689  }
1690 
1691 
1693 
1696  template<class T, class Prop, class Allocator>
1697  void ApplyPermutation(Matrix<T, Prop, RowSym, Allocator>& A,
1698  const Vector<int>& row_perm,
1699  const Vector<int>& col_perm,
1700  int starting_index)
1701  {
1702  Matrix<T, Prop, RowSym, Allocator> A_copy; A_copy = A;
1703 
1704  for (int i = 0; i < A.GetM(); i++)
1705  for (int j = i; j < A.GetN(); j++)
1706  A.Val(i, j) = A_copy(row_perm(i) - starting_index,
1707  row_perm(j) - starting_index);
1708  }
1709 
1710 
1712 
1715  template<class T, class Prop, class Allocator>
1716  void ApplyPermutation(Matrix<T, Prop, ColSym, Allocator>& A,
1717  const Vector<int>& row_perm,
1718  const Vector<int>& col_perm,
1719  int starting_index)
1720  {
1721  Matrix<T, Prop, ColSym, Allocator> A_copy; A_copy = A;
1722 
1723  for (int j = 0; j < A.GetN(); j++)
1724  for (int i = 0; i <= j; i++)
1725  A.Val(i, j) = A_copy(row_perm(i) - starting_index,
1726  row_perm(j) - starting_index);
1727  }
1728 
1729 
1731 
1734  template<class T, class Prop, class Allocator>
1736  const Vector<int>& row_perm,
1737  const Vector<int>& col_perm,
1738  int starting_index)
1739  {
1740  Matrix<T, Prop, RowHermPacked, Allocator> A_copy; A_copy = A;
1741 
1742  for (int i = 0; i < A.GetM(); i++)
1743  for (int j = i; j < A.GetN(); j++)
1744  A.Val(i, j) = A_copy(row_perm(i) - starting_index,
1745  row_perm(j) - starting_index);
1746  }
1747 
1748 
1750 
1753  template<class T, class Prop, class Allocator>
1755  const Vector<int>& row_perm,
1756  const Vector<int>& col_perm,
1757  int starting_index)
1758  {
1759  Matrix<T, Prop, ColHermPacked, Allocator> A_copy; A_copy = A;
1760 
1761  for (int j = 0; j < A.GetN(); j++)
1762  for (int i = 0; i <= j; i++)
1763  A.Val(i, j) = A_copy(row_perm(i) - starting_index,
1764  row_perm(j) - starting_index);
1765  }
1766 
1767 
1769 
1772  template<class T, class Prop, class Allocator>
1773  void ApplyPermutation(Matrix<T, Prop, RowHerm, Allocator>& A,
1774  const Vector<int>& row_perm,
1775  const Vector<int>& col_perm,
1776  int starting_index)
1777  {
1778  Matrix<T, Prop, RowHerm, Allocator> A_copy; A_copy = A;
1779 
1780  for (int i = 0; i < A.GetM(); i++)
1781  for (int j = i; j < A.GetN(); j++)
1782  A.Val(i, j) = A_copy(row_perm(i) - starting_index,
1783  row_perm(j) - starting_index);
1784  }
1785 
1786 
1788 
1791  template<class T, class Prop, class Allocator>
1792  void ApplyPermutation(Matrix<T, Prop, ColHerm, Allocator>& A,
1793  const Vector<int>& row_perm,
1794  const Vector<int>& col_perm,
1795  int starting_index)
1796  {
1797  Matrix<T, Prop, ColHerm, Allocator> A_copy; A_copy = A;
1798 
1799  for (int j = 0; j < A.GetN(); j++)
1800  for (int i = 0; i <= j; i++)
1801  A.Val(i, j) = A_copy(row_perm(i) - starting_index,
1802  row_perm(j) - starting_index);
1803  }
1804 
1805 
1807 
1812  template<class T, class Prop, class Allocator>
1813  void ApplyInversePermutation(Matrix<T, Prop, RowMajor, Allocator>& A,
1814  const Vector<int>& row_perm,
1815  const Vector<int>& col_perm,
1816  int starting_index)
1817  {
1818  Matrix<T, Prop, RowMajor, Allocator> A_copy; A_copy = A;
1819 
1820  for (int i = 0; i < A.GetM(); i++)
1821  for (int j = 0; j < A.GetN(); j++)
1822  A(row_perm(i) - starting_index, col_perm(j) - starting_index)
1823  = A_copy(i, j);
1824  }
1825 
1826 
1828 
1833  template<class T, class Prop, class Allocator>
1834  void ApplyInversePermutation(Matrix<T, Prop, ColMajor, Allocator>& A,
1835  const Vector<int>& row_perm,
1836  const Vector<int>& col_perm,
1837  int starting_index)
1838  {
1839  Matrix<T, Prop, ColMajor, Allocator> A_copy; A_copy = A;
1840 
1841  for (int j = 0; j < A.GetN(); j++)
1842  for (int i = 0; i < A.GetM(); i++)
1843  A(row_perm(i) - starting_index, col_perm(j) - starting_index)
1844  = A_copy(i, j);
1845  }
1846 
1847 
1849 
1854  template<class T, class Prop, class Allocator>
1855  void ApplyInversePermutation(Matrix<T, Prop, RowSymPacked, Allocator>& A,
1856  const Vector<int>& row_perm,
1857  const Vector<int>& col_perm,
1858  int starting_index)
1859  {
1860  Matrix<T, Prop, RowSymPacked, Allocator> A_copy; A_copy = A;
1861 
1862  for (int i = 0; i < A.GetM(); i++)
1863  for (int j = i; j < A.GetN(); j++)
1864  A.Set(row_perm(i) - starting_index, row_perm(j) - starting_index,
1865  A_copy(i, j));
1866  }
1867 
1868 
1870 
1875  template<class T, class Prop, class Allocator>
1876  void ApplyInversePermutation(Matrix<T, Prop, ColSymPacked, Allocator>& A,
1877  const Vector<int>& row_perm,
1878  const Vector<int>& col_perm,
1879  int starting_index)
1880  {
1881  Matrix<T, Prop, ColSymPacked, Allocator> A_copy; A_copy = A;
1882 
1883  for (int j = 0; j < A.GetN(); j++)
1884  for (int i = 0; i <= j; i++)
1885  A.Set(row_perm(i) - starting_index, row_perm(j) - starting_index,
1886  A_copy(i, j));
1887  }
1888 
1889 
1891 
1896  template<class T, class Prop, class Allocator>
1897  void ApplyInversePermutation(Matrix<T, Prop, RowSym, Allocator>& A,
1898  const Vector<int>& row_perm,
1899  const Vector<int>& col_perm,
1900  int starting_index)
1901  {
1902  Matrix<T, Prop, RowSym, Allocator> A_copy; A_copy = A;
1903 
1904  for (int i = 0; i < A.GetM(); i++)
1905  for (int j = i; j < A.GetN(); j++)
1906  A.Set(row_perm(i) - starting_index, row_perm(j) - starting_index,
1907  A_copy(i, j));
1908  }
1909 
1910 
1912 
1917  template<class T, class Prop, class Allocator>
1918  void ApplyInversePermutation(Matrix<T, Prop, ColSym, Allocator>& A,
1919  const Vector<int>& row_perm,
1920  const Vector<int>& col_perm,
1921  int starting_index)
1922  {
1923  Matrix<T, Prop, ColSym, Allocator> A_copy; A_copy = A;
1924 
1925  for (int j = 0; j < A.GetN(); j++)
1926  for (int i = 0; i <= j; i++)
1927  A.Set(row_perm(i) - starting_index, row_perm(j) - starting_index,
1928  A_copy(i, j));
1929  }
1930 
1931 
1933 
1938  template<class T, class Prop, class Allocator>
1939  void ApplyInversePermutation(Matrix<T, Prop, RowHermPacked, Allocator>& A,
1940  const Vector<int>& row_perm,
1941  const Vector<int>& col_perm,
1942  int starting_index)
1943  {
1944  Matrix<T, Prop, RowHermPacked, Allocator> A_copy; A_copy = A;
1945 
1946  for (int i = 0; i < A.GetM(); i++)
1947  for (int j = i; j < A.GetN(); j++)
1948  A.Set(row_perm(i) - starting_index, row_perm(j) - starting_index,
1949  A_copy(i, j));
1950  }
1951 
1952 
1954 
1959  template<class T, class Prop, class Allocator>
1960  void ApplyInversePermutation(Matrix<T, Prop, ColHermPacked, Allocator>& A,
1961  const Vector<int>& row_perm,
1962  const Vector<int>& col_perm,
1963  int starting_index)
1964  {
1965  Matrix<T, Prop, ColHermPacked, Allocator> A_copy; A_copy = A;
1966 
1967  for (int j = 0; j < A.GetN(); j++)
1968  for (int i = 0; i <= j; i++)
1969  A.Set(row_perm(i) - starting_index, row_perm(j) - starting_index,
1970  A_copy(i, j));
1971  }
1972 
1973 
1975 
1980  template<class T, class Prop, class Allocator>
1981  void ApplyInversePermutation(Matrix<T, Prop, RowHerm, Allocator>& A,
1982  const Vector<int>& row_perm,
1983  const Vector<int>& col_perm,
1984  int starting_index)
1985  {
1986  Matrix<T, Prop, RowHerm, Allocator> A_copy; A_copy = A;
1987 
1988  for (int i = 0; i < A.GetM(); i++)
1989  for (int j = i; j < A.GetN(); j++)
1990  A.Set(row_perm(i) - starting_index, row_perm(j) - starting_index,
1991  A_copy(i, j));
1992  }
1993 
1994 
1996 
2001  template<class T, class Prop, class Allocator>
2002  void ApplyInversePermutation(Matrix<T, Prop, ColHerm, Allocator>& A,
2003  const Vector<int>& row_perm,
2004  const Vector<int>& col_perm,
2005  int starting_index)
2006  {
2007  Matrix<T, Prop, ColHerm, Allocator> A_copy; A_copy = A;
2008 
2009  for (int j = 0; j < A.GetN(); j++)
2010  for (int i = 0; i <= j; i++)
2011  A.Set(row_perm(i) - starting_index, row_perm(j) - starting_index,
2012  A_copy(i, j));
2013  }
2014 
2015 
2017 
2022  template<class T, class Prop, class Allocator,
2023  class T1, class Allocator1, class T2, class Allocator2>
2027  {
2028  for (int i = 0; i < A.GetM(); i++)
2029  for (int j = 0; j < A.GetN(); j++)
2030  A(i, j) *= Drow(i)*Dcol(j);
2031  }
2032 
2033 
2035 
2040  template<class T, class Prop, class Allocator,
2041  class T1, class Allocator1, class T2, class Allocator2>
2045  {
2046  for (int j = 0; j < A.GetN(); j++)
2047  for (int i = 0; i < A.GetM(); i++)
2048  A(i, j) *= Drow(i)*Dcol(j);
2049  }
2050 
2051 
2053 
2058  template<class T, class Prop, class Allocator,
2059  class T1, class Allocator1, class T2, class Allocator2>
2063  {
2064  for (int i = 0; i < A.GetM(); i++)
2065  for (int j = i; j < A.GetN(); j++)
2066  A(i, j) *= Drow(i)*Drow(j);
2067  }
2068 
2069 
2071 
2076  template<class T, class Prop, class Allocator,
2077  class T1, class Allocator1, class T2, class Allocator2>
2081  {
2082  for (int j = 0; j < A.GetN(); j++)
2083  for (int i = 0; i <= j; i++)
2084  A(i, j) *= Drow(i)*Drow(j);
2085  }
2086 
2087 
2089 
2094  template<class T, class Prop, class Allocator,
2095  class T1, class Allocator1, class T2, class Allocator2>
2099  {
2100  for (int i = 0; i < A.GetM(); i++)
2101  for (int j = i; j < A.GetN(); j++)
2102  A.Val(i, j) *= Drow(i)*Drow(j);
2103  }
2104 
2105 
2107 
2112  template<class T, class Prop, class Allocator,
2113  class T1, class Allocator1, class T2, class Allocator2>
2117  {
2118  for (int j = 0; j < A.GetN(); j++)
2119  for (int i = 0; i <= j; i++)
2120  A.Val(i, j) *= Drow(i)*Drow(j);
2121  }
2122 
2123 
2125 
2130  template<class T, class Prop, class Allocator,
2131  class T1, class Allocator1, class T2, class Allocator2>
2135  {
2136  for (int i = 0; i < A.GetM(); i++)
2137  for (int j = i; j < A.GetN(); j++)
2138  A.Val(i, j) *= Drow(i)*Drow(j);
2139  }
2140 
2141 
2143 
2148  template<class T, class Prop, class Allocator,
2149  class T1, class Allocator1, class T2, class Allocator2>
2153  {
2154  for (int j = 0; j < A.GetN(); j++)
2155  for (int i = 0; i <= j; i++)
2156  A.Val(i, j) *= Drow(i)*Drow(j);
2157  }
2158 
2159 
2161 
2166  template<class T, class Prop, class Allocator,
2167  class T1, class Allocator1, class T2, class Allocator2>
2171  {
2172  for (int i = 0; i < A.GetM(); i++)
2173  for (int j = i; j < A.GetN(); j++)
2174  A.Val(i, j) *= Drow(i)*Drow(j);
2175  }
2176 
2177 
2179 
2184  template<class T, class Prop, class Allocator,
2185  class T1, class Allocator1, class T2, class Allocator2>
2189  {
2190  for (int j = 0; j < A.GetN(); j++)
2191  for (int i = 0; i <= j; i++)
2192  A.Val(i, j) *= Drow(i)*Drow(j);
2193  }
2194 
2195 
2197 
2202  template<class T, class Prop, class Allocator,
2203  class T1, class Allocator1, class T2, class Allocator2>
2207  {
2208  for (int i = 0; i < A.GetM(); i++)
2209  for (int j = 0; j <= i; j++)
2210  A.Val(i, j) *= Drow(i)*Dcol(j);
2211  }
2212 
2213 
2215 
2220  template<class T, class Prop, class Allocator,
2221  class T1, class Allocator1, class T2, class Allocator2>
2225  {
2226  for (int i = 0; i < A.GetM(); i++)
2227  for (int j = 0; j <= i; j++)
2228  A.Val(i, j) *= Drow(i)*Dcol(j);
2229  }
2230 
2231 
2233 
2238  template<class T, class Prop, class Allocator,
2239  class T1, class Allocator1, class T2, class Allocator2>
2243  {
2244  for (int i = 0; i < A.GetM(); i++)
2245  for (int j = 0; j <= i; j++)
2246  A.Val(i, j) *= Drow(i)*Dcol(j);
2247  }
2248 
2249 
2251 
2256  template<class T, class Prop, class Allocator,
2257  class T1, class Allocator1, class T2, class Allocator2>
2261  {
2262  for (int i = 0; i < A.GetM(); i++)
2263  for (int j = 0; j <= i; j++)
2264  A.Val(i, j) *= Drow(i)*Dcol(j);
2265  }
2266 
2267 
2269 
2274  template<class T, class Prop, class Allocator,
2275  class T1, class Allocator1, class T2, class Allocator2>
2279  {
2280  for (int i = 0; i < A.GetM(); i++)
2281  for (int j = i; j < A.GetM(); j++)
2282  A.Val(i, j) *= Drow(i)*Dcol(j);
2283  }
2284 
2285 
2287 
2292  template<class T, class Prop, class Allocator,
2293  class T1, class Allocator1, class T2, class Allocator2>
2297  {
2298  for (int i = 0; i < A.GetM(); i++)
2299  for (int j = i; j < A.GetN(); j++)
2300  A.Val(i, j) *= Drow(i)*Dcol(j);
2301  }
2302 
2303 
2305 
2310  template<class T, class Prop, class Allocator,
2311  class T1, class Allocator1, class T2, class Allocator2>
2315  {
2316  for (int i = 0; i < A.GetM(); i++)
2317  for (int j = i; j < A.GetM(); j++)
2318  A.Val(i, j) *= Drow(i)*Dcol(j);
2319  }
2320 
2321 
2323 
2328  template<class T, class Prop, class Allocator,
2329  class T1, class Allocator1, class T2, class Allocator2>
2333  {
2334  for (int i = 0; i < A.GetM(); i++)
2335  for (int j = i; j < A.GetN(); j++)
2336  A.Val(i, j) *= Drow(i)*Dcol(j);
2337  }
2338 
2339 
2341 
2345  template<class T, class Prop, class Allocator,
2346  class T1, class Allocator1>
2347  void ScaleLeftMatrix(Matrix<T, Prop, RowMajor, Allocator>& A,
2349  {
2350  for (int i = 0; i < A.GetM(); i++)
2351  for (int j = 0; j < A.GetN(); j++)
2352  A(i, j) *= Drow(i);
2353  }
2354 
2355 
2357 
2361  template<class T, class Prop, class Allocator,
2362  class T1, class Allocator1>
2363  void ScaleLeftMatrix(Matrix<T, Prop, ColMajor, Allocator>& A,
2365  {
2366  for (int i = 0; i < A.GetM(); i++)
2367  for (int j = 0; j < A.GetN(); j++)
2368  A(i, j) *= Drow(i);
2369  }
2370 
2371 
2373 
2377  template<class T, class Prop, class Allocator,
2378  class T1, class Allocator1>
2381  {
2382  for (int i = 0; i < A.GetM(); i++)
2383  for (int j = 0; j <= i; j++)
2384  A.Val(i, j) *= Drow(i);
2385  }
2386 
2387 
2389 
2393  template<class T, class Prop, class Allocator,
2394  class T1, class Allocator1>
2397  {
2398  for (int i = 0; i < A.GetM(); i++)
2399  for (int j = 0; j <= i; j++)
2400  A.Val(i, j) *= Drow(i);
2401  }
2402 
2403 
2405 
2409  template<class T, class Prop, class Allocator,
2410  class T1, class Allocator1>
2413  {
2414  for (int i = 0; i < A.GetM(); i++)
2415  for (int j = 0; j <= i; j++)
2416  A.Val(i, j) *= Drow(i);
2417  }
2418 
2419 
2421 
2425  template<class T, class Prop, class Allocator,
2426  class T1, class Allocator1>
2429  {
2430  for (int i = 0; i < A.GetM(); i++)
2431  for (int j = 0; j <= i; j++)
2432  A.Val(i, j) *= Drow(i);
2433  }
2434 
2435 
2437 
2441  template<class T, class Prop, class Allocator,
2442  class T1, class Allocator1>
2445  {
2446  for (int i = 0; i < A.GetM(); i++)
2447  for (int j = i; j < A.GetN(); j++)
2448  A.Val(i, j) *= Drow(i);
2449  }
2450 
2451 
2453 
2457  template<class T, class Prop, class Allocator,
2458  class T1, class Allocator1>
2461  {
2462  for (int i = 0; i < A.GetM(); i++)
2463  for (int j = i; j < A.GetN(); j++)
2464  A.Val(i, j) *= Drow(i);
2465  }
2466 
2467 
2469 
2473  template<class T, class Prop, class Allocator,
2474  class T1, class Allocator1>
2477  {
2478  for (int i = 0; i < A.GetM(); i++)
2479  for (int j = i; j < A.GetN(); j++)
2480  A.Val(i, j) *= Drow(i);
2481  }
2482 
2483 
2485 
2489  template<class T, class Prop, class Allocator,
2490  class T1, class Allocator1>
2493  {
2494  for (int i = 0; i < A.GetM(); i++)
2495  for (int j = i; j < A.GetN(); j++)
2496  A.Val(i, j) *= Drow(i);
2497  }
2498 
2499 
2501 
2505  template<class T, class Prop, class Allocator,
2506  class T2, class Allocator2>
2507  void ScaleRightMatrix(Matrix<T, Prop, RowMajor, Allocator>& A,
2509  {
2510  for (int i = 0; i < A.GetM(); i++)
2511  for (int j = 0; j < A.GetN(); j++)
2512  A(i, j) *= Dcol(j);
2513  }
2514 
2515 
2517 
2521  template<class T, class Prop, class Allocator,
2522  class T2, class Allocator2>
2523  void ScaleRightMatrix(Matrix<T, Prop, ColMajor, Allocator>& A,
2525  {
2526  for (int i = 0; i < A.GetM(); i++)
2527  for (int j = 0; j < A.GetN(); j++)
2528  A(i, j) *= Dcol(j);
2529  }
2530 
2531 
2533 
2537  template<class T, class Prop, class Allocator,
2538  class T2, class Allocator2>
2541  {
2542  for (int i = 0; i < A.GetM(); i++)
2543  for (int j = 0; j <= i; j++)
2544  A.Val(i, j) *= Dcol(j);
2545  }
2546 
2547 
2549 
2553  template<class T, class Prop, class Allocator,
2554  class T2, class Allocator2>
2557  {
2558  for (int i = 0; i < A.GetM(); i++)
2559  for (int j = 0; j <= i; j++)
2560  A.Val(i, j) *= Dcol(j);
2561  }
2562 
2563 
2565 
2569  template<class T, class Prop, class Allocator,
2570  class T2, class Allocator2>
2573  {
2574  for (int i = 0; i < A.GetM(); i++)
2575  for (int j = 0; j <= i; j++)
2576  A.Val(i, j) *= Dcol(j);
2577  }
2578 
2579 
2581 
2585  template<class T, class Prop, class Allocator,
2586  class T2, class Allocator2>
2589  {
2590  for (int i = 0; i < A.GetM(); i++)
2591  for (int j = 0; j <= i; j++)
2592  A.Val(i, j) *= Dcol(j);
2593  }
2594 
2595 
2597 
2601  template<class T, class Prop, class Allocator,
2602  class T2, class Allocator2>
2605  {
2606  for (int i = 0; i < A.GetM(); i++)
2607  for (int j = i; j < A.GetN(); j++)
2608  A.Val(i, j) *= Dcol(j);
2609  }
2610 
2611 
2613 
2617  template<class T, class Prop, class Allocator,
2618  class T2, class Allocator2>
2621  {
2622  for (int i = 0; i < A.GetM(); i++)
2623  for (int j = i; j < A.GetN(); j++)
2624  A.Val(i, j) *= Dcol(j);
2625  }
2626 
2627 
2629 
2633  template<class T, class Prop, class Allocator,
2634  class T2, class Allocator2>
2637  {
2638  for (int i = 0; i < A.GetM(); i++)
2639  for (int j = i; j < A.GetN(); j++)
2640  A.Val(i, j) *= Dcol(j);
2641  }
2642 
2643 
2645 
2649  template<class T, class Prop, class Allocator,
2650  class T2, class Allocator2>
2653  {
2654  for (int i = 0; i < A.GetM(); i++)
2655  for (int j = i; j < A.GetN(); j++)
2656  A.Val(i, j) *= Dcol(j);
2657  }
2658 
2659 
2661  template<class T, class Prop, class Storage, class Allocator>
2662  typename ClassComplexType<T>::Treal
2664  {
2665  // generic function
2666  typename ClassComplexType<T>::Treal res(0);
2668  long taille = A.GetDataSize();
2669  if (taille > 0)
2670  {
2671  x.SetData(taille, A.GetData());
2672  res = Norm2(x);
2673  x.Nullify();
2674  }
2675 
2676  return res;
2677  }
2678 
2679 } // namespace Seldon.
2680 
2681 #define SELDON_FILE_FUNCTIONS_CXX
2682 #endif
Seldon::Matrix_HermPacked::Set
void Set(int i, int j, const T &val)
Sets an element of the matrix.
Definition: Matrix_HermPackedInline.cxx:254
Seldon::Matrix_Symmetric::Val
const_reference Val(int i, int j) const
Access operator.
Definition: Matrix_SymmetricInline.cxx:151
Seldon::Norm2
ClassComplexType< T >::Treal Norm2(const VectorExpression< T, E > &X)
returns 2-norm of an expression with vectors
Definition: Functions_BaseInline.cxx:153
Seldon::to_str
std::string to_str(const T &input)
Converts most types to string.
Definition: CommonInline.cxx:137
Seldon::Vector
Definition: SeldonHeader.hxx:207
Seldon::Matrix_TriangPacked::Val
reference Val(int i, int j)
Direct access method.
Definition: Matrix_TriangPackedInline.cxx:137
Seldon::WrongIndex
Definition: Errors.hxx:114
Seldon::Matrix_HermPacked::Val
reference Val(int i, int j)
Direct access method.
Definition: Matrix_HermPackedInline.cxx:126
Seldon::Matrix_Triangular::Val
const_reference Val(int i, int j) const
Access operator.
Definition: Matrix_TriangularInline.cxx:144
Seldon::Matrix
Definition: SeldonHeader.hxx:226
Seldon::Matrix_Symmetric::Set
void Set(int i, int j, const T &x)
Sets an element of the matrix.
Definition: Matrix_SymmetricInline.cxx:294
Seldon::Matrix< T, Prop, ColLoTriang, Allocator >
Column-major lower-triangular full-matrix class.
Definition: Matrix_Triangular.hxx:176
Seldon::Matrix< T, Prop, ColHerm, Allocator >
Column-major hermitian full-matrix class.
Definition: Matrix_Hermitian.hxx:167
Seldon::Matrix< T, Prop, RowLoTriang, Allocator >
Row-major lower-triangular full-matrix class.
Definition: Matrix_Triangular.hxx:228
Seldon::Matrix< T, Prop, ColHermPacked, Allocator >
Column-major hermitian packed matrix class.
Definition: Matrix_HermPacked.hxx:160
Seldon::Matrix< T, Prop, ColSym, Allocator >
Column-major symmetric full-matrix class.
Definition: Matrix_Symmetric.hxx:165
Seldon::Matrix< T, Prop, RowHerm, Allocator >
Row-major hermitian full-matrix class.
Definition: Matrix_Hermitian.hxx:194
Seldon::Matrix< T, Prop, RowSym, Allocator >
Row-major symmetric full-matrix class.
Definition: Matrix_Symmetric.hxx:192
Seldon::Matrix< T, Prop, RowUpTriangPacked, Allocator >
Row-major upper-triangular packed matrix class.
Definition: Matrix_TriangPacked.hxx:198
Seldon::Matrix_SymPacked::Set
void Set(int i, int j, const T &x)
Sets an element of the matrix.
Definition: Matrix_SymPackedInline.cxx:292
Seldon::Matrix< T, Prop, RowHermPacked, Allocator >
Row-major hermitian packed matrix class.
Definition: Matrix_HermPacked.hxx:189
Seldon::VirtualMatrix::GetN
int GetN() const
Returns the number of columns.
Definition: Matrix_BaseInline.cxx:80
Seldon::VirtualMatrix::GetM
int GetM() const
Returns the number of rows.
Definition: Matrix_BaseInline.cxx:69
Seldon::Matrix_Hermitian::Set
void Set(int i, int j, const T &x)
Sets an element of the matrix.
Definition: Matrix_HermitianInline.cxx:248
Seldon::Matrix< T, Prop, RowMajor, Allocator >
Row-major full-matrix class.
Definition: Matrix_Pointers.hxx:207
Seldon::Matrix< T, Prop, ColUpTriangPacked, Allocator >
Column-major upper-triangular packed matrix class.
Definition: Matrix_TriangPacked.hxx:146
Seldon::Matrix< T, Prop, RowUpTriang, Allocator >
Row-major upper-triangular full-matrix class.
Definition: Matrix_Triangular.hxx:202
Seldon::WrongArgument
Definition: Errors.hxx:76
Seldon::Matrix< T, Prop, ColUpTriang, Allocator >
Column-major upper-triangular full-matrix class.
Definition: Matrix_Triangular.hxx:150
Seldon::Matrix_Hermitian::Val
const_reference Val(int i, int j) const
Access operator.
Definition: Matrix_HermitianInline.cxx:129
Seldon::Matrix< T, Prop, ColSymPacked, Allocator >
Column-major symmetric packed matrix class.
Definition: Matrix_SymPacked.hxx:162
Seldon::Matrix< T, Prop, RowSymPacked, Allocator >
Row-major symmetric packed matrix class.
Definition: Matrix_SymPacked.hxx:190
Seldon
Seldon namespace.
Definition: Array.cxx:24
Seldon::Matrix< T, Prop, RowLoTriangPacked, Allocator >
Row-major lower-triangular packed matrix class.
Definition: Matrix_TriangPacked.hxx:224
Seldon::Matrix< T, Prop, ColLoTriangPacked, Allocator >
Column-major lower-triangular packed matrix class.
Definition: Matrix_TriangPacked.hxx:172
Seldon::Matrix< T, Prop, ColMajor, Allocator >
Column-major full-matrix class.
Definition: Matrix_Pointers.hxx:176