Matrix_PointersInline.cxx
1 // Copyright (C) 2001-2011 Vivien Mallet
2 // Copyright (C) 2003-2011 Marc DuruflĂ©
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_MATRIX_POINTERS_INLINE_CXX
22 
23 #include "Matrix_Pointers.hxx"
24 
25 namespace Seldon
26 {
27 
28 
29  /****************
30  * CONSTRUCTORS *
31  ****************/
32 
33 
35 
38  template <class T, class Prop, class Storage, class Allocator>
40  Matrix_Base<T, Allocator>()
41  {
42  me_ = NULL;
43  }
44 
45 
46  /**************
47  * DESTRUCTOR *
48  **************/
49 
50 
52  template <class T, class Prop, class Storage, class Allocator>
54  {
55  this->Clear();
56  }
57 
58 
59  /*******************
60  * BASIC FUNCTIONS *
61  *******************/
62 
63 
65 
71  template <class T, class Prop, class Storage, class Allocator>
73  {
74  return long(this->m_) * long(this->n_);
75  }
76 
77 
79  template<class T, class Prop, class Storage, class Allocator>
81  {
82  size_t taille = sizeof(*this) + size_t(this->GetDataSize())*sizeof(T);
83  taille += size_t(Storage::GetFirst(this->m_, this->n_))*sizeof(pointer);
84  return taille;
85  }
86 
87 
89 
94  template <class T, class Prop, class Storage, class Allocator>
95  inline typename
96  Matrix_Pointers<T, Prop, Storage, Allocator>::pointer*
98  {
99  return me_;
100  }
101 
102 
104 
107  template <class T, class Prop, class Storage, class Allocator>
109  {
110  return Storage::GetSecond(this->m_, this->n_);
111  }
112 
113 
114  /**********************************
115  * ELEMENT ACCESS AND AFFECTATION *
116  **********************************/
117 
118 
120 
125  template <class T, class Prop, class Storage, class Allocator>
126  inline typename Matrix_Pointers<T, Prop, Storage, Allocator>::pointer
128  const
129  {
130  long lgth = Storage::GetSecond(this->m_, this->n_);
131  return this->data_ + long(Storage::GetFirst(i, j)) * lgth
132  + long(Storage::GetSecond(i, j));
133  }
134 
135 
137 
143  template <class T, class Prop, class Storage, class Allocator>
144  inline typename Matrix_Pointers<T, Prop, Storage, Allocator>::reference
146  {
147 
148 #ifdef SELDON_CHECK_BOUNDS
149  CheckBounds(i, j, this->m_, this->n_, "Matrix_Pointers");
150 #endif
151 
152  return me_[Storage::GetFirst(i, j)][Storage::GetSecond(i, j)];
153  }
154 
155 
157 
163  template <class T, class Prop, class Storage, class Allocator>
166  ::operator() (int i, int j) const
167  {
168 
169 #ifdef SELDON_CHECK_BOUNDS
170  CheckBounds(i, j, this->m_, this->n_, "Matrix_Pointers");
171 #endif
172 
173  return me_[Storage::GetFirst(i, j)][Storage::GetSecond(i, j)];
174  }
175 
176 
178 
184  template <class T, class Prop, class Storage, class Allocator>
185  inline typename Matrix_Pointers<T, Prop, Storage, Allocator>::reference
187  {
188 
189 #ifdef SELDON_CHECK_BOUNDS
190  CheckBounds(i, j, this->m_, this->n_, "Matrix_Pointers");
191 #endif
192 
193  return me_[Storage::GetFirst(i, j)][Storage::GetSecond(i, j)];
194  }
195 
196 
198 
204  template <class T, class Prop, class Storage, class Allocator>
205  inline typename Matrix_Pointers<T, Prop, Storage, Allocator>::reference
207  {
208  return Val(i, j);
209  }
210 
211 
213 
219  template <class T, class Prop, class Storage, class Allocator>
221  ::const_reference
223  {
224 
225 #ifdef SELDON_CHECK_BOUNDS
226  CheckBounds(i, j, this->m_, this->n_, "Matrix_Pointers");
227 #endif
228 
229  return me_[Storage::GetFirst(i, j)][Storage::GetSecond(i, j)];
230  }
231 
232 
234 
240  template <class T, class Prop, class Storage, class Allocator>
242  ::const_reference
244  {
245  return Val(i, j);
246  }
247 
248 
250 
255  template <class T, class Prop, class Storage, class Allocator>
256  inline typename Matrix_Pointers<T, Prop, Storage, Allocator>::reference
258  {
259 
260 #ifdef SELDON_CHECK_BOUNDS
261  CheckBounds(i, this->GetDataSize(), "Matrix_Pointers");
262 #endif
263 
264  return this->data_[i];
265  }
266 
267 
269 
274  template <class T, class Prop, class Storage, class Allocator>
276  ::const_reference
278  {
279 
280 #ifdef SELDON_CHECK_BOUNDS
281  CheckBounds(i, this->GetDataSize(), "Matrix_Pointers");
282 #endif
283 
284  return this->data_[i];
285  }
286 
287 
289 
294  template <class T, class Prop, class Storage, class Allocator>
296  ::Set(int i, int j, const T& val)
297  {
298  this->Val(i, j) = val;
299  }
300 
301 
303 
308  template <class T, class Prop, class Storage, class Allocator>
312  {
313  this->Copy(A);
314 
315  return *this;
316  }
317 
318 
320 
325  template <class T, class Prop, class Storage, class Allocator>
328  {
329  this->Reallocate(A.GetM(), A.GetN());
330 
331  Allocator::memorycpy(this->data_, A.GetData(), this->GetDataSize());
332  }
333 
334 
335 #ifdef SELDON_WITH_VIRTUAL
336  template <class T, class Prop, class Storage, class Allocator>
339  ::IsSymmetric() const
340  {
341  return false;
342  }
343 
344 
346  template <class T, class Prop, class Storage, class Allocator>
347  inline void Matrix_Pointers<T, Prop, Storage, Allocator>
348  ::AddInteraction(int i, int j, const T& val)
349  {
350  // not implemented since T can be a complex structure
351  abort();
352  }
353 
354 
356  template <class T, class Prop, class Storage, class Allocator>
357  inline void Matrix_Pointers<T, Prop, Storage, Allocator>
358  ::AddInteractionRow(int i, int n, const Vector<int>& col,
359  const Vector<T>& val, bool sorted)
360  {
361  // not implemented since T can be a complex structure
362  abort();
363  }
364 
365 
367  template <class T, class Prop, class Storage, class Allocator>
368  inline void Matrix_Pointers<T, Prop, Storage, Allocator>
369  ::AddInteractionColumn(int i, int n, const Vector<int>& row,
370  const Vector<T>& val, bool sorted)
371  {
372  // not implemented since T can be a complex structure
373  abort();
374  }
375 
376 
378  template <class T, class Prop, class Storage, class Allocator>
379  inline void Matrix_Pointers<T, Prop, Storage, Allocator>
380  ::ClearRow(int i)
381  {
382  // not implemented since T can be a complex structure
383  abort();
384  }
385 
386 
388  template <class T, class Prop, class Storage, class Allocator>
389  inline void Matrix_Pointers<T, Prop, Storage, Allocator>
390  ::MltAddVector(const Treal& alpha, const Vector<Treal>& x,
391  const Treal& beta, Vector<Treal>& y) const
392  {
393  // not implemented since T can be a complex structure
394  throw Undefined("MltAddVector", "Not implemented");
395  }
396 
397 
399  template <class T, class Prop, class Storage, class Allocator>
400  inline void Matrix_Pointers<T, Prop, Storage, Allocator>
401  ::MltAddVector(const Tcplx& alpha, const Vector<Tcplx>& x,
402  const Tcplx& beta, Vector<Tcplx>& y) const
403  {
404  // not implemented since T can be a complex structure
405  throw Undefined("MltAddVector", "Not implemented");
406  }
407 
408 
410  template <class T, class Prop, class Storage, class Allocator>
411  inline void Matrix_Pointers<T, Prop, Storage, Allocator>
412  ::MltAddVector(const Treal& alpha, const SeldonTranspose&,
413  const Vector<Treal>& x,
414  const Treal& beta, Vector<Treal>& y) const
415  {
416  // not implemented since T can be a complex structure
417  throw Undefined("MltAddVector", "Not implemented");
418  }
419 
420 
422  template <class T, class Prop, class Storage, class Allocator>
423  inline void Matrix_Pointers<T, Prop, Storage, Allocator>
424  ::MltAddVector(const Tcplx& alpha, const SeldonTranspose&,
425  const Vector<Tcplx>& x,
426  const Tcplx& beta, Vector<Tcplx>& y) const
427  {
428  // not implemented since T can be a complex structure
429  throw Undefined("MltAddVector", "Not implemented");
430  }
431 
432 
434  template <class T, class Prop, class Storage, class Allocator>
435  inline void Matrix_Pointers<T, Prop, Storage, Allocator>
436  ::MltVector(const Vector<Treal>& x, Vector<Treal>& y) const
437  {
438  // not implemented since T can be a complex structure
439  throw Undefined("MltVector", "Not implemented");
440  }
441 
442 
444  template <class T, class Prop, class Storage, class Allocator>
445  inline void Matrix_Pointers<T, Prop, Storage, Allocator>
446  ::MltVector(const Vector<Tcplx>& x, Vector<Tcplx>& y) const
447  {
448  // not implemented since T can be a complex structure
449  throw Undefined("MltVector", "Not implemented");
450  }
451 
452 
454  template <class T, class Prop, class Storage, class Allocator>
455  inline void Matrix_Pointers<T, Prop, Storage, Allocator>
456  ::MltVector(const SeldonTranspose&,
457  const Vector<Treal>& x, Vector<Treal>& y) const
458  {
459  // not implemented since T can be a complex structure
460  throw Undefined("MltVector with transpose", "Not implemented");
461  }
462 
463 
465  template <class T, class Prop, class Storage, class Allocator>
466  inline void Matrix_Pointers<T, Prop, Storage, Allocator>
467  ::MltVector(const SeldonTranspose&,
468  const Vector<Tcplx>& x, Vector<Tcplx>& y) const
469  {
470  // not implemented since T can be a complex structure
471  throw Undefined("MltVector with transpose", "Not implemented");
472  }
473 
474 #endif
475 
476 
478  // MATRIX<COLMAJOR> //
480 
481 
482  /****************
483  * CONSTRUCTORS *
484  ****************/
485 
486 
488 
491  template <class T, class Prop, class Allocator>
493  Matrix_Pointers<T, Prop, ColMajor, Allocator>()
494  {
495  }
496 
497 
499 
503  template <class T, class Prop, class Allocator>
505  Matrix_Pointers<T, Prop, ColMajor, Allocator>(i, j)
506  {
507  }
508 
509 
511  template <class T, class Prop, class Allocator>
514  Matrix_Pointers<T, Prop, ColMajor, Allocator>(A)
515  {
516  }
517 
518 
520 
523  template <class T, class Prop, class Allocator>
524  template <class T0>
527  {
528  this->Fill(x);
529 
530  return *this;
531  }
532 
533 
535 
540  template <class T, class Prop, class Allocator>
544  {
545  this->Copy(A);
546 
547  return *this;
548  }
549 
550 
552 
555  template <class T, class Prop, class Allocator> template<class T0>
558  {
559  for (long i = 0; i < long(this->m_) * long(this->n_); i++)
560  this->data_[i] *= alpha;
561 
562  return *this;
563  }
564 
565 
567  // MATRIX<ROWMAJOR> //
569 
570 
571  /****************
572  * CONSTRUCTORS *
573  ****************/
574 
575 
577 
580  template <class T, class Prop, class Allocator>
582  Matrix_Pointers<T, Prop, RowMajor, Allocator>()
583  {
584  }
585 
586 
588 
592  template <class T, class Prop, class Allocator>
594  Matrix_Pointers<T, Prop, RowMajor, Allocator>(i, j)
595  {
596  }
597 
598 
600  template <class T, class Prop, class Allocator>
603  Matrix_Pointers<T, Prop, RowMajor, Allocator>(A)
604  {
605  }
606 
607 
608  /*****************
609  * OTHER METHODS *
610  *****************/
611 
612 
614 
617  template <class T, class Prop, class Allocator>
618  template <class T0>
621  {
622  this->Fill(x);
623 
624  return *this;
625  }
626 
627 
629 
634  template <class T, class Prop, class Allocator>
638  {
639  this->Copy(A);
640 
641  return *this;
642  }
643 
644 
646 
649  template <class T, class Prop, class Allocator> template<class T0>
652  {
653  for (long i = 0; i < long(this->m_)*long(this->n_); i++)
654  this->data_[i] *= alpha;
655 
656  return *this;
657  }
658 
659 
660 } // namespace Seldon.
661 
662 #define SELDON_FILE_MATRIX_POINTERS_INLINE_CXX
663 #endif
Seldon::RowMajor
Definition: Storage.hxx:45
Seldon::Matrix_Base
Base class for all matrices.
Definition: Matrix_Base.hxx:143
Seldon::Matrix_Pointers
Full matrix class.
Definition: Matrix_Pointers.hxx:37
Seldon::Matrix
Definition: SeldonHeader.hxx:226
Seldon::Matrix_Base< T, typename SeldonDefaultAllocator< Storage, T >::allocator >::GetData
pointer GetData() const
Returns a pointer to the data array.
Definition: Matrix_BaseInline.cxx:241
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< T, Prop, RowMajor, Allocator >
Row-major full-matrix class.
Definition: Matrix_Pointers.hxx:207
Seldon
Seldon namespace.
Definition: Array.cxx:24
Seldon::Matrix_Pointers::Matrix_Pointers
Matrix_Pointers()
Default constructor.
Definition: Matrix_PointersInline.cxx:39
Seldon::Matrix< T, Prop, ColMajor, Allocator >
Column-major full-matrix class.
Definition: Matrix_Pointers.hxx:176
Seldon::ColMajor
Definition: Storage.hxx:33