Permutation_ScalingMatrix.cxx
1 // Copyright (C) 2003-2011 Marc DuruflĂ©
2 // Copyright (C) 2001-2011 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_PERMUTATION_SCALING_MATRIX_CXX
22 
23 /*
24  Functions defined in this file:
25 
26  A(I, J) = A
27  ApplyInversePermutation(A, I, J)
28 
29  A = A(I, J)
30  ApplyPermutation(A, I, J)
31 
32  A = Drow * A * Dcol
33  ScaleMatrix(A, Drow, Dcol)
34 
35  A = Drow * A
36  ScaleLeftMatrix(A, Drow)
37 
38  A = A * Dcol
39  ScaleRightMatrix(A, Dcol)
40 */
41 
42 namespace Seldon
43 {
44 
46  // ApplyInversePermutation //
47 
48 
50 
54  template<class T, class Prop, class Allocator>
55  void ApplyInversePermutation(Matrix<T, Prop, RowSparse, Allocator>& A,
56  const Vector<int>& row_perm,
57  const Vector<int>& col_perm)
58  {
59  int i, j, k; long l, nnz;
60  int m = A.GetM();
61  int n = A.GetN();
62  int* ind = A.GetInd();
63  long* ptr = A.GetPtr();
64  T* data = A.GetData();
65 
66  /*** Permutation of the columns ***/
67 
69  // Column indexes of a given row.
70  Vector<int> row_index;
71  for (i = 0; i < m; i++)
72  if ((nnz = ptr[i + 1] - ptr[i]) != 0)
73  {
74  row_index.Reallocate(nnz);
75  row_data.Reallocate(nnz);
76  for (k = 0, l = ptr[i]; k < nnz; k++, l++)
77  {
78  // Applies the permutation on the columns.
79  row_index(k) = col_perm(ind[l]);
80  row_data(k) = data[l];
81  }
82 
83  // The columns must remain in increasing order.
84  Sort(row_index, row_data);
85 
86  // Putting back the data into the array.
87  for (k = 0, l = ptr[i]; k < nnz; k++, l++)
88  {
89  ind[l] = row_index(k);
90  data[l] = row_data(k);
91  }
92  }
93 
94  row_index.Clear();
95  row_data.Clear();
96 
97  /*** Perturbation of the rows ***/
98 
99  // Total number of non-zero elements.
100  nnz = ptr[m];
101 
102  // 'row_perm' is const, so it must be copied.
103  Vector<int> row_permutation(row_perm);
104  // Row indexes in the origin matrix: prev_row_index(i) should be the
105  // location of the i-th row (from the permuted matrix) in the matrix
106  // before permutation.
107  Vector<int> prev_row_index(m);
108  prev_row_index.Fill();
109 
110  Sort(row_permutation, prev_row_index);
111  row_permutation.Clear();
112 
113  // Description of the matrix after permutations.
114  Vector<long> new_ptr(m + 1);
115  Vector<int> new_ind(nnz);
116  Vector<T, VectFull, Allocator> new_data(nnz);
117 
118  long ptr_count = 0; int length;
119  for (i = 0; i < m; i++)
120  {
121  length = ptr[prev_row_index(i) + 1] - ptr[prev_row_index(i)];
122  for (j = 0; j < length; j++)
123  {
124  new_data(ptr_count + j) = data[ptr[prev_row_index(i)] + j];
125  new_ind(ptr_count + j) = ind[ptr[prev_row_index(i)] + j];
126  }
127  new_ptr(i) = ptr_count;
128  ptr_count += length;
129  }
130  new_ptr(m) = ptr_count;
131 
132  A.SetData(m, n, new_data, new_ptr, new_ind);
133  }
134 
135 
137 
141  template<class T, class Prop, class Allocator>
142  void ApplyInversePermutation(Matrix<T, Prop, ColSparse, Allocator>& A,
143  const Vector<int>& row_perm,
144  const Vector<int>& col_perm)
145  {
146  int i, j, k; long l, nnz;
147  int m = A.GetM();
148  int n = A.GetN();
149  int* ind = A.GetInd();
150  long* ptr = A.GetPtr();
151  T* data = A.GetData();
152 
153  /*** Permutation of the rows ***/
154 
156  // Row indexes of a given column.
157  Vector<int> col_index;
158  for (i = 0; i < n; i++)
159  if ((nnz = ptr[i + 1] - ptr[i]) != 0)
160  {
161  col_index.Reallocate(nnz);
162  col_data.Reallocate(nnz);
163  for (k = 0, l = ptr[i]; k < nnz; k++, l++)
164  {
165  // Applies the permutation on the rows.
166  col_index(k) = row_perm(ind[l]);
167  col_data(k) = data[l];
168  }
169 
170  // The rows must remain in increasing order.
171  Sort(col_index, col_data);
172 
173  // Putting back the data into the array.
174  for (k = 0, l = ptr[i]; k < nnz; k++, l++)
175  {
176  ind[l] = col_index(k);
177  data[l] = col_data(k);
178  }
179  }
180  col_index.Clear();
181  col_data.Clear();
182 
183  /*** Perturbation of the columns ***/
184 
185  // Total number of non-zero elements.
186  nnz = ptr[n];
187 
188  // 'col_perm' is const, so it must be copied.
189  Vector<int> col_permutation(col_perm);
190  // Column indexes in the origin matrix: prev_col_index(i) should be the
191  // location of the i-th column (from the permuted matrix) in the matrix
192  // before permutation.
193  Vector<int> prev_col_index(n);
194  prev_col_index.Fill();
195 
196  Sort(col_permutation, prev_col_index);
197  col_permutation.Clear();
198 
199  // Description of the matrix after permutations.
200  Vector<long> new_ptr(n + 1);
201  Vector<int> new_ind(nnz);
202  Vector<T, VectFull, Allocator> new_data(nnz);
203 
204  int ptr_count = 0, length;
205  for (i = 0; i < n; i++)
206  {
207  length = ptr[prev_col_index(i) + 1] - ptr[prev_col_index(i)];
208  for (j = 0; j < length; j++)
209  {
210  new_data(ptr_count + j) = data[ptr[prev_col_index(i)] + j];
211  new_ind(ptr_count + j) = ind[ptr[prev_col_index(i)] + j];
212  }
213  new_ptr(i) = ptr_count;
214  ptr_count += length;
215  }
216  new_ptr(n) = ptr_count;
217 
218  A.SetData(m, n, new_data, new_ptr, new_ind);
219  }
220 
221 
223 
227  template<class T, class Prop, class Allocator>
228  void ApplyInversePermutation(Matrix<T, Prop, RowSymSparse, Allocator>& A,
229  const Vector<int>& row_perm,
230  const Vector<int>& col_perm)
231  {
232  int m = A.GetM(), n = A.GetN();
233  long nnz = A.GetDataSize();
234  Vector<int> IndRow(nnz), IndCol(nnz);
236 
237  long* ptr = A.GetPtr();
238  int* ind = A.GetInd();
239  T* data = A.GetData();
240 
241  // First we convert the matrix in coordinate format and we permute the
242  // indices.
243  // IndRow -> indices of the permuted rows
244  // IndCol -> indices of the permuted columns
245  long k = 0;
246  for (int i = 0; i < m; i++)
247  for (long j = ptr[i]; j < ptr[i+1]; j++)
248  {
249  IndRow(k) = row_perm(i);
250  Val(k) = data[j];
251  IndCol(k) = row_perm(ind[j]);
252  if (IndCol(k) <= IndRow(k))
253  {
254  // We store only the superior part of the symmetric matrix.
255  int ind_tmp = IndRow(k);
256  IndRow(k) = IndCol(k);
257  IndCol(k) = ind_tmp;
258  }
259  k++;
260  }
261 
262  // We sort with respect to row numbers.
263  Sort(nnz, IndRow, IndCol, Val);
264 
265  // then column numbers
266  A.Clear();
267  Vector<long> Ptr(m+1);
268  Ptr(0) = 0; k = 0;
269  for (int i = 0; i < m; i++)
270  {
271  long first_index = k;
272  // We get the size of the column i.
273  while (k < nnz && IndRow(k) <= i)
274  k++;
275 
276  Sort(first_index, k-1, IndCol, Val);
277  Ptr(i+1) = k;
278  }
279 
280  A.SetData(m, n, Val, Ptr, IndCol);
281  }
282 
283 
285 
289  template<class T, class Prop, class Allocator>
290  void ApplyInversePermutation(Matrix<T, Prop, ColSymSparse, Allocator>& A,
291  const Vector<int>& row_perm,
292  const Vector<int>& col_perm)
293  {
294  int m = A.GetM(), n = A.GetN();
295  long nnz = A.GetDataSize();
296  Vector<int> IndRow(nnz), IndCol(nnz);
298 
299  long* ptr = A.GetPtr();
300  int* ind = A.GetInd();
301  T* data = A.GetData();
302 
303  // First we convert the matrix in coordinate format and we permute the
304  // indices.
305  // IndRow -> indices of the permuted rows
306  // IndCol -> indices of the permuted columns
307  long k = 0;
308  for (int i = 0; i < m; i++)
309  for (long j = ptr[i]; j < ptr[i+1]; j++)
310  {
311  IndCol(k) = row_perm(i);
312  Val(k) = data[j];
313  IndRow(k) = row_perm(ind[j]);
314  if (IndCol(k) <= IndRow(k))
315  {
316  // We store only the superior part of the symmetric matrix.
317  int ind_tmp = IndRow(k);
318  IndRow(k) = IndCol(k);
319  IndCol(k) = ind_tmp;
320  }
321  k++;
322  }
323 
324  // We sort with respect to column numbers.
325  Sort(nnz, IndCol, IndRow, Val);
326 
327  // then row numbers
328  A.Clear();
329  Vector<long> Ptr(n+1);
330  Ptr(0) = 0; k = 0;
331  for (int i = 0; i < n; i++)
332  {
333  long first_index = k;
334  // We get the size of the column i.
335  while (k < nnz && IndCol(k) <= i)
336  k++;
337 
338  Sort(first_index, k-1, IndRow, Val);
339  Ptr(i+1) = k;
340  }
341 
342  A.SetData(m, n, Val, Ptr, IndRow);
343  }
344 
345 
347 
351  template<class T, class Prop, class Allocator>
352  void ApplyInversePermutation(Matrix<T, Prop, ArrayRowSparse, Allocator>& A,
353  const IVect& row_perm, const IVect& col_perm)
354  {
355  int m = A.GetM(), n, i, i_, j, i2;
356  IVect ind_tmp, iperm(m), rperm(m);
357  for (i = 0; i < m; i++)
358  {
359  iperm(i) = i;
360  rperm(i) = i;
361  }
362  // A(rperm(i),:) will be the place where is the initial row i.
363 
364  // Algorithm avoiding the allocation of another matrix.
365  for (i = 0; i < m; i++)
366  {
367  // We get the index of row where the row initially placed on row i is.
368  i2 = rperm(i);
369  // We get the new index of this row.
370  i_ = row_perm(i);
371 
372  // We fill ind_tmp of the permuted indices of columns of row i.
373  n = A.GetRowSize(i2);
374  ind_tmp.Reallocate(n);
375  for (j = 0; j < n; j++)
376  ind_tmp(j) = col_perm(A.Index(i2,j));
377 
378  // We swap the two rows i and its destination row_perm(i).
379  A.SwapRow(i2, i_);
380  A.ReplaceIndexRow(i_, ind_tmp);
381 
382  // We update the indices iperm and rperm in order to keep in memory
383  // the place where the row row_perm(i) is.
384  int i_tmp = iperm(i_);
385  iperm(i_) = iperm(i2);
386  iperm(i2) = i_tmp;
387  rperm(iperm(i_)) = i_;
388  rperm(iperm(i2)) = i2;
389 
390  // We assemble the row i.
391  A.AssembleRow(i_);
392  }
393  }
394 
395 
397 
401  template<class T, class Prop, class Allocator>
402  void ApplyInversePermutation(Matrix<T, Prop, ArrayColSparse, Allocator>& A,
403  const IVect& row_perm, const IVect& col_perm)
404  {
405  int n = A.GetN();
406  IVect ind_tmp, iperm(n), rperm(n);
407  for (int i = 0; i < n; i++)
408  {
409  iperm(i) = i;
410  rperm(i) = i;
411  }
412  // A(:, rperm(i)) will be the place where is the initial column i.
413 
414  // Algorithm avoiding the allocation of another matrix.
415  for (int i = 0; i < n; i++)
416  {
417  // We get the index of column where the column initially placed on column i is.
418  int i2 = rperm(i);
419  // We get the new index of this column.
420  int i_ = col_perm(i);
421 
422  // We fill ind_tmp of the permuted indices of columns of row i.
423  int p = A.GetColumnSize(i2);
424  ind_tmp.Reallocate(p);
425  for (int j = 0; j < p; j++)
426  ind_tmp(j) = row_perm(A.Index(i2, j));
427 
428  // We swap the two rows i and its destination col_perm(i).
429  A.SwapColumn(i2, i_);
430  A.ReplaceIndexColumn(i_, ind_tmp);
431 
432  // We update the indices iperm and rperm in order to keep in memory
433  // the place where the column col_perm(i) is.
434  int i_tmp = iperm(i_);
435  iperm(i_) = iperm(i2);
436  iperm(i2) = i_tmp;
437  rperm(iperm(i_)) = i_;
438  rperm(iperm(i2)) = i2;
439 
440  // We assemble the column i (to sort row numbers)
441  A.AssembleColumn(i_);
442  }
443  }
444 
445 
447 
451  template<class T, class Prop, class Allocator>
452  void
454  const IVect& row_perm, const IVect& col_perm)
455  {
456  // It is assumed that the permuted matrix is still symmetric! For example,
457  // the user can provide row_perm = col_perm.
458  int m = A.GetM();
459  long nnz = A.GetDataSize();
460  Vector<int> IndRow(nnz), IndCol(nnz);
462 
463  // First we convert the matrix in coordinate format and we permute the
464  // indices.
465  // IndRow -> indices of the permuted rows
466  // IndCol -> indices of the permuted columns
467  long k = 0;
468  for (int i = 0; i < m; i++)
469  for (int j = 0; j < A.GetColumnSize(i); j++)
470  {
471  IndCol(k) = col_perm(i);
472  Val(k) = A.Value(i,j);
473  IndRow(k) = row_perm(A.Index(i, j));
474  if (IndCol(k) <= IndRow(k))
475  {
476  // We store only the superior part of the symmetric matrix.
477  int ind_tmp = IndRow(k);
478  IndRow(k) = IndCol(k);
479  IndCol(k) = ind_tmp;
480  }
481  k++;
482  }
483 
484  // We sort with respect to column numbers.
485  Sort(nnz, IndCol, IndRow, Val);
486 
487  // A is filled.
488  k = 0;
489  for (int i = 0; i < m; i++)
490  {
491  long first_index = k;
492  // We get the size of the column i.
493  while (k < nnz && IndCol(k) <= i)
494  k++;
495 
496  int size_column = k - first_index;
497  // If column not empty.
498  if (size_column > 0)
499  {
500  A.ReallocateColumn(i, size_column);
501  k = first_index;
502  Sort(k, k+size_column-1, IndRow, Val);
503  for (int j = 0; j < size_column; j++)
504  {
505  A.Index(i,j) = IndRow(k);
506  A.Value(i,j) = Val(k);
507  k++;
508  }
509  }
510  else
511  A.ClearColumn(i);
512  }
513  }
514 
515 
517 
521  template<class T, class Prop, class Allocator>
522  void
524  const IVect& row_perm, const IVect& col_perm)
525  {
526  // It is assumed that the permuted matrix is still symmetric! For example,
527  // the user can provide row_perm = col_perm.
528  int m = A.GetM();
529  long nnz = A.GetDataSize();
530  Vector<int> IndRow(nnz), IndCol(nnz);
532 
533  // First we convert the matrix in coordinate format and we permute the
534  // indices.
535  // IndRow -> indices of the permuted rows
536  // IndCol -> indices of the permuted columns
537  long k = 0;
538  for (int i = 0; i < m; i++)
539  {
540  for (int j = 0; j < A.GetRowSize(i); j++)
541  {
542  IndRow(k) = row_perm(i);
543  Val(k) = A.Value(i, j);
544  IndCol(k) = col_perm(A.Index(i, j));
545  if (IndCol(k) <= IndRow(k))
546  {
547  // We store only the superior part of the symmetric matrix.
548  int ind_tmp = IndRow(k);
549  IndRow(k) = IndCol(k);
550  IndCol(k) = ind_tmp;
551  }
552  k++;
553  }
554  }
555 
556  // We sort with respect to row numbers.
557  Sort(nnz, IndRow, IndCol, Val);
558 
559  // A is filled.
560  k = 0;
561  for (int i = 0; i < m; i++)
562  {
563  long first_index = k;
564  // We get the size of the row i.
565  while (k < nnz && IndRow(k) <= i)
566  k++;
567 
568  int size_row = k - first_index;
569  // If row not empty.
570  if (size_row > 0)
571  {
572  A.ReallocateRow(i, size_row);
573  k = first_index;
574  Sort(k, k+size_row-1, IndCol, Val);
575  for (int j = 0; j < size_row; j++)
576  {
577  A.Index(i,j) = IndCol(k);
578  A.Value(i,j) = Val(k);
579  k++;
580  }
581  }
582  else
583  A.ClearRow(i);
584  }
585  }
586 
587 
588  // ApplyInversePermutation //
590 
591 
593  // ApplyPermutation //
594 
595 
597 
601  template<class T, class Prop, class Allocator>
602  void ApplyPermutation(Matrix<T, Prop, RowSparse, Allocator>& A,
603  const Vector<int>& row_perm,
604  const Vector<int>& col_perm)
605  {
606  Vector<int> inv_row_perm(row_perm.GetM());
607  Vector<int> inv_col_perm(col_perm.GetM());
608  for (int i = 0; i < row_perm.GetM(); i++)
609  inv_row_perm(row_perm(i)) = i;
610 
611  for (int i = 0; i < col_perm.GetM(); i++)
612  inv_col_perm(col_perm(i)) = i;
613 
614  ApplyInversePermutation(A, inv_row_perm, inv_col_perm);
615  }
616 
617 
619 
623  template<class T, class Prop, class Allocator>
624  void ApplyPermutation(Matrix<T, Prop, ColSparse, Allocator>& A,
625  const Vector<int>& row_perm,
626  const Vector<int>& col_perm)
627  {
628  Vector<int> inv_row_perm(row_perm.GetM());
629  Vector<int> inv_col_perm(col_perm.GetM());
630  for (int i = 0; i < row_perm.GetM(); i++)
631  inv_row_perm(row_perm(i)) = i;
632 
633  for (int i = 0; i < col_perm.GetM(); i++)
634  inv_col_perm(col_perm(i)) = i;
635 
636  ApplyInversePermutation(A, inv_row_perm, inv_col_perm);
637  }
638 
639 
641 
645  template<class T, class Prop, class Allocator>
647  const Vector<int>& row_perm,
648  const Vector<int>& col_perm)
649  {
650  Vector<int> inv_row_perm(row_perm.GetM());
651  for (int i = 0; i < row_perm.GetM(); i++)
652  inv_row_perm(row_perm(i)) = i;
653 
654  ApplyInversePermutation(A, inv_row_perm, inv_row_perm);
655  }
656 
657 
659 
663  template<class T, class Prop, class Allocator>
665  const Vector<int>& row_perm,
666  const Vector<int>& col_perm)
667  {
668  Vector<int> inv_row_perm(row_perm.GetM());
669  for (int i = 0; i < row_perm.GetM(); i++)
670  inv_row_perm(row_perm(i)) = i;
671 
672  ApplyInversePermutation(A, inv_row_perm, inv_row_perm);
673  }
674 
675 
677 
681  template<class T, class Prop, class Allocator>
683  const Vector<int>& row_perm,
684  const Vector<int>& col_perm)
685  {
686  Vector<int> inv_row_perm(row_perm.GetM());
687  Vector<int> inv_col_perm(col_perm.GetM());
688  for (int i = 0; i < row_perm.GetM(); i++)
689  inv_row_perm(row_perm(i)) = i;
690 
691  for (int i = 0; i < col_perm.GetM(); i++)
692  inv_col_perm(col_perm(i)) = i;
693 
694  ApplyInversePermutation(A, inv_row_perm, inv_col_perm);
695  }
696 
697 
699 
703  template<class T, class Prop, class Allocator>
705  const Vector<int>& row_perm,
706  const Vector<int>& col_perm)
707  {
708  Vector<int> inv_row_perm(row_perm.GetM());
709  Vector<int> inv_col_perm(col_perm.GetM());
710  for (int i = 0; i < row_perm.GetM(); i++)
711  inv_row_perm(row_perm(i)) = i;
712 
713  for (int i = 0; i < col_perm.GetM(); i++)
714  inv_col_perm(col_perm(i)) = i;
715 
716  ApplyInversePermutation(A, inv_row_perm, inv_col_perm);
717  }
718 
719 
721 
725  template<class T, class Prop, class Allocator>
727  const Vector<int>& row_perm,
728  const Vector<int>& col_perm)
729  {
730  Vector<int> inv_row_perm(row_perm.GetM());
731  for (int i = 0; i < row_perm.GetM(); i++)
732  inv_row_perm(row_perm(i)) = i;
733 
734  ApplyInversePermutation(A, inv_row_perm, inv_row_perm);
735  }
736 
737 
739 
743  template<class T, class Prop, class Allocator>
745  const Vector<int>& row_perm,
746  const Vector<int>& col_perm)
747  {
748  Vector<int> inv_row_perm(row_perm.GetM());
749  for (int i = 0; i < row_perm.GetM(); i++)
750  inv_row_perm(row_perm(i)) = i;
751 
752  ApplyInversePermutation(A, inv_row_perm, inv_row_perm);
753  }
754 
755 
756  // ApplyPermutation //
758 
759 
761  // ScaleMatrix //
762 
763 
765 
768  template<class Prop, class T1, class Allocator1,
769  class T2, class Allocator2, class T3, class Allocator3>
771  const Vector<T2, VectFull, Allocator2>& scale_left,
772  const Vector<T3, VectFull, Allocator3>& scale_right)
773  {
774  T1* data = A.GetData();
775  long* ptr = A.GetPtr();
776  int* ind = A.GetInd();
777 
778  int m = A.GetM();
779  for (int i = 0; i < m; i++ )
780  for (long j = ptr[i]; j < ptr[i+1]; j++ )
781  data[j] *= scale_left(i) * scale_right(ind[j]);
782  }
783 
784 
786 
789  template<class Prop, class T1, class Allocator1,
790  class T2, class Allocator2, class T3, class Allocator3>
792  const Vector<T2, VectFull, Allocator2>& scale_left,
793  const Vector<T3, VectFull, Allocator3>& scale_right)
794  {
795  T1* data = A.GetData();
796  long* ptr = A.GetPtr();
797  int* ind = A.GetInd();
798 
799  for (int i = 0; i < A.GetN(); i++ )
800  for (long j = ptr[i]; j < ptr[i+1]; j++ )
801  data[j] *= scale_left(ind[j]) * scale_right(i);
802  }
803 
804 
806 
809  template<class Prop, class T1, class Allocator1,
810  class T2, class Allocator2, class T3, class Allocator3>
812  const Vector<T2, VectFull, Allocator2>& scale_left,
813  const Vector<T3, VectFull, Allocator3>& scale_right)
814  {
815  T1* data = A.GetData();
816  long* ptr = A.GetPtr();
817  int* ind = A.GetInd();
818 
819  int m = A.GetM();
820  for (int i = 0; i < m; i++ )
821  for (long j = ptr[i]; j < ptr[i+1]; j++ )
822  data[j] *= scale_left(i) * scale_left(ind[j]);
823  }
824 
825 
827 
830  template<class Prop, class T1, class Allocator1,
831  class T2, class Allocator2, class T3, class Allocator3>
833  const Vector<T2, VectFull, Allocator2>& scale_left,
834  const Vector<T3, VectFull, Allocator3>& scale_right)
835  {
836  T1* data = A.GetData();
837  long* ptr = A.GetPtr();
838  int* ind = A.GetInd();
839 
840  for (int i = 0; i < A.GetM(); i++ )
841  for (long j = ptr[i]; j < ptr[i+1]; j++ )
842  data[j] *= scale_left(i) * scale_left(ind[j]);
843  }
844 
845 
847 
850  template<class Prop, class T1, class Allocator1,
851  class T2, class Allocator2, class T3, class Allocator3>
853  const Vector<T2, VectFull, Allocator2>& scale_left,
854  const Vector<T3, VectFull, Allocator3>& scale_right)
855  {
856  int m = A.GetM();
857  for (int i = 0; i < m; i++ )
858  for (int j = 0; j < A.GetRowSize(i); j++ )
859  A.Value(i,j) *= scale_left(i) * scale_right(A.Index(i, j));
860 
861  }
862 
863 
865 
868  template<class Prop, class T1, class Allocator1,
869  class T2, class Allocator2, class T3, class Allocator3>
871  const Vector<T2, VectFull, Allocator2>& scale_left,
872  const Vector<T3, VectFull, Allocator3>& scale_right)
873  {
874  for (int i = 0; i < A.GetN(); i++ )
875  for (int j = 0; j < A.GetColumnSize(i); j++ )
876  A.Value(i, j) *= scale_right(i) * scale_left(A.Index(i, j));
877 
878  }
879 
880 
882 
885  template<class Prop, class T1, class Allocator1,
886  class T2, class Allocator2, class T3, class Allocator3>
888  const Vector<T2, VectFull, Allocator2>& scale_left,
889  const Vector<T3, VectFull, Allocator3>& scale_right)
890  {
891  int m = A.GetM();
892  for (int i = 0; i < m; i++ )
893  for (int j = 0; j < A.GetRowSize(i); j++ )
894  A.Value(i,j) *= scale_left(i) * scale_right(A.Index(i, j));
895 
896  }
897 
898 
900 
903  template<class Prop, class T1, class Allocator1,
904  class T2, class Allocator2, class T3, class Allocator3>
906  const Vector<T2, VectFull, Allocator2>& scale_left,
907  const Vector<T3, VectFull, Allocator3>& scale_right)
908  {
909  int m = A.GetM();
910  for (int i = 0; i < m; i++ )
911  for (int j = 0; j < A.GetColumnSize(i); j++ )
912  A.Value(i, j) *= scale_left(i) * scale_right(A.Index(i, j));
913 
914  }
915 
916 
917  // ScaleMatrix //
919 
920 
922  // ScaleLeftMatrix //
923 
924 
926 
929  template<class T1, class Allocator1,
930  class Prop, class T2, class Allocator2>
933  {
934  T1* data = A.GetData();
935  long* ptr = A.GetPtr();
936 
937  int m = A.GetM();
938  for (int i = 0; i < m; i++ )
939  for (long j = ptr[i]; j < ptr[i+1]; j++ )
940  data[j] *= scale(i);
941  }
942 
943 
945 
948  template<class T1, class Allocator1,
949  class Prop, class T2, class Allocator2>
952  {
953  T1* data = A.GetData();
954  long* ptr = A.GetPtr();
955  int* ind = A.GetInd();
956 
957  for (int i = 0; i < A.GetN(); i++ )
958  for (long j = ptr[i]; j < ptr[i+1]; j++ )
959  data[j] *= scale(ind[j]);
960  }
961 
962 
964 
967  template<class T1, class Allocator1,
968  class Prop, class T2, class Allocator2>
971  {
972  int m = A.GetM();
973  for (int i = 0; i < m; i++ )
974  for (int j = 0; j < A.GetRowSize(i); j++ )
975  A.Value(i,j) *= scale(i);
976  }
977 
978 
980 
983  template<class T1, class Allocator1,
984  class Prop, class T2, class Allocator2>
987  {
988  for (int i = 0; i < A.GetN(); i++ )
989  for (int j = 0; j < A.GetColumnSize(i); j++ )
990  A.Value(i, j) *= scale(A.Index(i, j));
991  }
992 
993 
994  // ScaleLeftMatrix //
996 
997 
999  // ScaleRightMatrix //
1000 
1001 
1003 
1006  template<class T1, class Allocator1,
1007  class Prop, class T2, class Allocator2>
1009  const Vector<T2, VectFull, Allocator2>& scale)
1010  {
1011  T1* data = A.GetData();
1012  long* ptr = A.GetPtr();
1013  int* ind = A.GetInd();
1014 
1015  int m = A.GetM();
1016  for (int i = 0; i < m; i++ )
1017  for (long j = ptr[i]; j < ptr[i+1]; j++ )
1018  data[j] *= scale(ind[j]);
1019  }
1020 
1021 
1023 
1026  template<class T1, class Allocator1,
1027  class Prop, class T2, class Allocator2>
1029  const Vector<T2, VectFull, Allocator2>& scale)
1030  {
1031  T1* data = A.GetData();
1032  long* ptr = A.GetPtr();
1033 
1034  for (int i = 0; i < A.GetN(); i++ )
1035  for (long j = ptr[i]; j < ptr[i+1]; j++ )
1036  data[j] *= scale(i);
1037  }
1038 
1039 
1041 
1044  template<class T1, class Allocator1,
1045  class Prop, class T2, class Allocator2>
1047  const Vector<T2, VectFull, Allocator2>& scale)
1048  {
1049  int m = A.GetM();
1050  for (int i = 0; i < m; i++ )
1051  for (int j = 0; j < A.GetRowSize(i); j++ )
1052  A.Value(i,j) *= scale(A.Index(i, j));
1053  }
1054 
1055 
1057 
1060  template<class T1, class Allocator1,
1061  class Prop, class T2, class Allocator2>
1063  const Vector<T2, VectFull, Allocator2>& scale)
1064  {
1065  for (int i = 0; i < A.GetN(); i++ )
1066  for (int j = 0; j < A.GetColumnSize(i); j++ )
1067  A.Value(i, j) *= scale(i);
1068  }
1069 
1070 
1071  // ScaleRightMatrix //
1073 
1074 
1075 } // end namespace
1076 
1077 #define SELDON_FILE_PERMUTATION_SCALING_MATRIX_CXX
1078 #endif
Seldon::Matrix_SymSparse::GetInd
int * GetInd() const
Returns (row or column) indices of non-zero entries.
Definition: Matrix_SymSparseInline.cxx:153
Seldon::Matrix< T, Prop, ArrayRowSymSparse, Allocator >::GetRowSize
int GetRowSize(int i) const
Returns the number of non-zero entries of a row.
Definition: Matrix_ArraySparseInline.cxx:1360
Seldon::Matrix< T, Prop, ArrayColSparse, Allocator >::SwapColumn
void SwapColumn(int i, int i_)
Swaps two columns.
Definition: Matrix_ArraySparseInline.cxx:587
Seldon::Vector< int >
Seldon::Matrix< T, Prop, ArrayRowSymSparse, Allocator >::ClearRow
void ClearRow(int i)
Clears a row.
Definition: Matrix_ArraySparseInline.cxx:1294
Seldon::Matrix
Definition: SeldonHeader.hxx:226
Seldon::Matrix< T, Prop, ArrayColSparse, Allocator >::GetColumnSize
int GetColumnSize(int i) const
Returns the number of non-zero entries of a column.
Definition: Matrix_ArraySparseInline.cxx:614
Seldon::Matrix< T, Prop, RowSparse, Allocator >
Row-major sparse-matrix class.
Definition: Matrix_Sparse.hxx:223
Seldon::Matrix_Sparse::SetData
void SetData(int i, int j, Vector< T, Storage0, Allocator0 > &values, Vector< long, Storage1, Allocator1 > &ptr, Vector< int, Storage2, Allocator2 > &ind)
Redefines the matrix.
Definition: Matrix_Sparse.cxx:241
Seldon::Vector< T, VectFull, Allocator >::Clear
void Clear()
Clears the vector.
Definition: VectorInline.cxx:355
Seldon::Matrix< T, Prop, ColSparse, Allocator >
Column-major sparse-matrix class.
Definition: Matrix_Sparse.hxx:197
Seldon::Matrix_SymSparse::GetPtr
long * GetPtr() const
Returns (row or column) start indices.
Definition: Matrix_SymSparseInline.cxx:138
Seldon::Matrix_SymSparse::GetDataSize
long GetDataSize() const
Returns the number of elements stored in memory.
Definition: Matrix_SymSparseInline.cxx:126
Seldon::Matrix_Base::GetData
pointer GetData() const
Returns a pointer to the data array.
Definition: Matrix_BaseInline.cxx:241
Seldon::Matrix< T, Prop, ArrayColSymSparse, Allocator >::ClearColumn
void ClearColumn(int i)
Clears a column.
Definition: Matrix_ArraySparseInline.cxx:987
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_Sparse::GetPtr
long * GetPtr() const
Returns (row or column) start indices.
Definition: Matrix_SparseInline.cxx:150
Seldon::Matrix< T, Prop, ArrayRowSparse, Allocator >::ReplaceIndexRow
void ReplaceIndexRow(int i, IVect &new_index)
Sets column numbers of non-zero entries of a row.
Definition: Matrix_ArraySparseInline.cxx:738
Seldon::Matrix< T, Prop, ArrayColSparse, Allocator >::AssembleColumn
void AssembleColumn(int i)
Assembles a column.
Definition: Matrix_ArraySparseInline.cxx:635
Seldon::Matrix_ArraySparse::Value
const T & Value(int num_row, int i) const
Returns j-th non-zero value of row/column i.
Definition: Matrix_ArraySparseInline.cxx:286
Seldon::Matrix< T, Prop, ArrayColSymSparse, Allocator >::ReallocateColumn
void ReallocateColumn(int i, int j)
Reallocates column i.
Definition: Matrix_ArraySparseInline.cxx:1001
Seldon::Matrix< T, Prop, ArrayColSparse, Allocator >::ReplaceIndexColumn
void ReplaceIndexColumn(int i, IVect &new_index)
Sets row numbers of non-zero entries of a column.
Definition: Matrix_ArraySparseInline.cxx:600
Seldon::Vector< T, VectFull, Allocator >
Full vector class.
Definition: Vector.hxx:88
Seldon::Matrix< T, Prop, ArrayRowSparse, Allocator >
Row-major sparse-matrix class.
Definition: Matrix_ArraySparse.hxx:214
Seldon::Matrix_ArraySparse::GetDataSize
long GetDataSize() const
Returns the number of elements stored in memory.
Definition: Matrix_ArraySparseInline.cxx:86
Seldon::Matrix< T, Prop, RowSymSparse, Allocator >
Row-major sparse-matrix class.
Definition: Matrix_SymSparse.hxx:218
Seldon::Matrix_SymSparse::SetData
void SetData(int i, int j, Vector< T, Storage0, Allocator0 > &values, Vector< long, Storage1, Allocator1 > &ptr, Vector< int, Storage2, Allocator2 > &ind)
Redefines the matrix.
Definition: Matrix_SymSparse.cxx:226
Seldon::Matrix_ArraySparse::Index
int Index(int num_row, int i) const
Returns column/row number of j-th non-zero value of row/column i.
Definition: Matrix_ArraySparseInline.cxx:325
Seldon::Matrix< T, Prop, ArrayColSymSparse, Allocator >
Column-major symmetric sparse-matrix class.
Definition: Matrix_ArraySparse.hxx:254
Seldon::Matrix< T, Prop, ColSymSparse, Allocator >
Column-major sparse-matrix class.
Definition: Matrix_SymSparse.hxx:192
Seldon::Matrix< T, Prop, ArrayRowSparse, Allocator >::AssembleRow
void AssembleRow(int i)
Assembles a row.
Definition: Matrix_ArraySparseInline.cxx:772
Seldon::Matrix_SymSparse::Clear
void Clear()
Clears the matrix.
Definition: Matrix_SymSparse.cxx:143
Seldon::Matrix< T, Prop, ArrayRowSymSparse, Allocator >::ReallocateRow
void ReallocateRow(int i, int j)
Reallocates row i.
Definition: Matrix_ArraySparseInline.cxx:1308
Seldon
Seldon namespace.
Definition: Array.cxx:24
Seldon::Matrix< T, Prop, ArrayRowSymSparse, Allocator >
Row-major symmetric sparse-matrix class.
Definition: Matrix_ArraySparse.hxx:311
Seldon::Matrix< T, Prop, ArrayRowSparse, Allocator >::GetRowSize
int GetRowSize(int i) const
Returns the number of non-zero entries of a row.
Definition: Matrix_ArraySparseInline.cxx:751
Seldon::Vector< T, VectFull, Allocator >::Reallocate
void Reallocate(size_t i)
Vector reallocation.
Definition: VectorInline.cxx:369
Seldon::Matrix_Sparse::GetInd
int * GetInd() const
Returns (row or column) indices of non-zero entries.
Definition: Matrix_SparseInline.cxx:165
Seldon::Matrix< T, Prop, ArrayColSparse, Allocator >
Column-major sparse-matrix class.
Definition: Matrix_ArraySparse.hxx:172
Seldon::Matrix< T, Prop, ArrayColSymSparse, Allocator >::GetColumnSize
int GetColumnSize(int i) const
Returns the number of non-zero entries of a column.
Definition: Matrix_ArraySparseInline.cxx:1054
Seldon::Matrix< T, Prop, ArrayRowSparse, Allocator >::SwapRow
void SwapRow(int i, int i_)
Swaps two rows.
Definition: Matrix_ArraySparseInline.cxx:725