NEST  2.6.0,not_revisioned_source_dir@0
position.h
Go to the documentation of this file.
1 /*
2  * position.h
3  *
4  * This file is part of NEST.
5  *
6  * Copyright (C) 2004 The NEST Initiative
7  *
8  * NEST is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * NEST is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with NEST. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #ifndef POSITION_H
24 #define POSITION_H
25 
26 #include <vector>
27 #include <string>
28 #include <iostream>
29 #include <sstream>
30 #include <cassert>
31 #include <cmath>
32 #include "nest.h"
33 #include "token.h"
34 #include "compose.hpp"
35 #include "exceptions.h"
36 
37 namespace nest
38 {
39 
40  // It is necessary to declare the template for operator<< first in order
41  // to get the friend declaration to work
42  template <int D, class T>
43  class Position;
44 
45  template <int D, class T>
46  std::ostream & operator<<(std::ostream & os, const Position<D,T> & pos);
47 
48  template <int D, class T=double_t>
49  class Position
50  {
51  public:
52  template<int OD, class OT>
53  friend class Position;
54 
58  Position();
59 
63  Position(const T&, const T&);
64 
68  Position(const T&, const T&, const T&);
69 
73  Position(const T * const y);
74 
78  Position(const std::vector<T> &y);
79 
83  Position(const Position & other);
84 
85  template<class U>
87 
91  operator std::vector<T>() const;
92 
96  T &operator[](int i);
97 
101  const T &operator[](int i) const;
102 
107  Token getToken() const;
108 
113  template <class OT>
114  Position operator+(const Position<D,OT> &other) const;
115 
120  template <class OT>
121  Position operator-(const Position<D,OT> &other) const;
122 
127  Position operator-() const;
128 
133  template <class OT>
134  Position operator*(const Position<D,OT> &other) const;
135 
140  template <class OT>
141  Position operator/(const Position<D,OT> &other) const;
142 
147  Position operator+(const T &) const;
148 
153  Position operator-(const T &) const;
154 
159  Position operator*(const T &) const;
160 
165  Position operator/(const T &) const;
166 
172  template <class OT>
174 
180  template <class OT>
182 
188  template <class OT>
190 
196  template <class OT>
198 
203  Position &operator+=(const T &);
204 
209  Position &operator-=(const T &);
210 
215  Position &operator*=(const T &);
216 
221  Position &operator/=(const T &);
222 
226  bool operator==(const Position &y) const;
227 
231  bool operator!=(const Position &y) const;
232 
236  bool operator<(const Position &y) const;
237 
241  bool operator>(const Position &y) const;
242 
246  bool operator<=(const Position &y) const;
247 
251  bool operator>=(const Position &y) const;
252 
257  T length() const;
258 
262  operator std::string() const;
263 
273  void print(std::ostream& out, char sep = ' ') const;
274 
278  friend std::ostream & operator<< <>(std::ostream & os, const Position<D,T> & pos);
279 
280  protected:
281  T x_[D];
282  };
283 
288  template<int D>
289  struct Box
290  {
291  Box() {}
292  Box(const Position<D> &ll, const Position<D> &ur) :
293  lower_left(ll), upper_right(ur)
294  {}
295 
298  };
299 
303  template<int D>
304  class MultiIndex: public Position<D,int> {
305  public:
307  Position<D,int>(), lower_left_(), upper_right_()
308  {}
309 
311  Position<D,int>(), lower_left_(), upper_right_(ur)
312  {}
313 
315  Position<D,int>(ll), lower_left_(ll), upper_right_(ur)
316  {}
317 
319  {
320  // Try increasing the first coordinate first, resetting it and
321  // continuing with the next if the first one overflows, and so on
322  for(int i=0;i<D;++i) {
323  this->x_[i]++;
324  if (this->x_[i]<upper_right_[i])
325  return *this;
326  this->x_[i] = lower_left_[i];
327  }
328  // If we reach this point, we are outside of bounds. The upper
329  // right point is used as a marker to show that we have reached the
330  // end.
331  for(int i=0;i<D;++i)
332  this->x_[i] = upper_right_[i];
333  return *this;
334  }
335 
337  {
338  MultiIndex tmp = *this;
339  ++*this;
340  return tmp;
341  }
342 
344  { return lower_left_; }
346  { return upper_right_; }
347 
348  private:
351  };
352 
353 
354  template <int D, class T>
355  inline
357  {
358  for(int i=0;i<D;++i)
359  x_[i] = 0;
360  }
361 
362  template <int D, class T>
363  inline
364  Position<D,T>::Position(const T& x, const T& y)
365  {
366  assert(D==2);
367  x_[0] = x;
368  x_[1] = y;
369  }
370 
371  template <int D, class T>
372  inline
373  Position<D,T>::Position(const T& x, const T& y, const T& z)
374  {
375  assert(D==3);
376  x_[0] = x;
377  x_[1] = y;
378  x_[2] = z;
379  }
380 
381  template <int D, class T>
382  inline
383  Position<D,T>::Position(const T * const y)
384  {
385  for(int i=0;i<D;++i)
386  x_[i] = y[i];
387  }
388 
389  template <int D, class T>
390  inline
391  Position<D,T>::Position(const std::vector<T> &y)
392  {
393  if(y.size() != D) {
394  throw BadProperty(String::compose("Expected a %1-dimensional position.", D));
395  }
396  std::copy(y.begin(), y.end(), x_);
397  }
398 
399  template <int D, class T>
400  inline
402  {
403  for(int i=0;i<D;++i)
404  x_[i] = other.x_[i];
405  }
406 
407  template <int D, class T>
408  template <class U>
409  inline
411  {
412  for(int i=0;i<D;++i)
413  x_[i] = other.x_[i];
414  }
415 
416  template <int D, class T>
417  Position<D,T>::operator std::vector<T>() const
418  {
419  std::vector<double_t> result;
420 
421  for(int i=0;i<D;++i)
422  result.push_back(x_[i]);
423 
424  return result;
425  }
426 
427  template <int D, class T>
428  inline
430  {
431  return x_[i];
432  }
433 
434  template <int D, class T>
435  inline
436  const T & Position<D,T>::operator[](int i) const
437  {
438  return x_[i];
439  }
440 
441  template <int D, class T>
443  {
444  std::vector<T> result = std::vector<T>(*this);
445 
446  return Token(result);
447  }
448 
449  template <int D, class T>
450  template <class OT>
451  inline
453  {
454  Position p = *this;
455  p += other;
456  return p;
457  }
458 
459  template <int D, class T>
460  template <class OT>
461  inline
463  {
464  Position p = *this;
465  p -= other;
466  return p;
467  }
468 
469  template <int D, class T>
470  inline
472  {
473  Position p;
474  p -= *this;
475  return p;
476  }
477 
478  template <int D, class T>
479  template <class OT>
480  inline
482  {
483  Position p = *this;
484  p *= other;
485  return p;
486  }
487 
488  template <int D, class T>
489  template <class OT>
490  inline
492  {
493  Position p = *this;
494  p /= other;
495  return p;
496  }
497 
498  template <int D, class T>
499  inline
501  {
502  Position p = *this;
503  p += a;
504  return p;
505  }
506 
507  template <int D, class T>
508  inline
510  {
511  Position p = *this;
512  p -= a;
513  return p;
514  }
515 
516  template <int D, class T>
517  inline
519  {
520  Position p = *this;
521  p *= a;
522  return p;
523  }
524 
525  template <int D, class T>
526  inline
528  {
529  Position p = *this;
530  p /= a;
531  return p;
532  }
533 
534  template <int D, class T>
535  template <class OT>
536  inline
538  {
539  for(int i=0;i<D;++i)
540  x_[i] += other.x_[i];
541  return *this;
542  }
543 
544  template <int D, class T>
545  template <class OT>
546  inline
548  {
549  for(int i=0;i<D;++i)
550  x_[i] -= other.x_[i];
551  return *this;
552  }
553 
554  template <int D, class T>
555  template <class OT>
556  inline
558  {
559  for(int i=0;i<D;++i)
560  x_[i] *= other.x_[i];
561  return *this;
562  }
563 
564  template <int D, class T>
565  template <class OT>
566  inline
568  {
569  for(int i=0;i<D;++i)
570  x_[i] /= other.x_[i];
571  return *this;
572  }
573 
574  template <int D, class T>
575  inline
577  {
578  for(int i=0;i<D;++i)
579  x_[i] += a;
580  return *this;
581  }
582 
583  template <int D, class T>
584  inline
586  {
587  for(int i=0;i<D;++i)
588  x_[i] -= a;
589  return *this;
590  }
591 
592  template <int D, class T>
593  inline
595  {
596  for(int i=0;i<D;++i)
597  x_[i] *= a;
598  return *this;
599  }
600 
601  template <int D, class T>
602  inline
604  {
605  for(int i=0;i<D;++i)
606  x_[i] /= a;
607  return *this;
608  }
609 
610  template <int D, class T>
611  inline
613  {
614  for(int i=0;i<D;++i) {
615  if (x_[i] != y.x_[i]) return false;
616  }
617  return true;
618  }
619 
620  template <int D, class T>
621  inline
623  {
624  for(int i=0;i<D;++i) {
625  if (x_[i] != y.x_[i]) return true;
626  }
627  return false;
628  }
629 
630  template <int D, class T>
631  inline
633  {
634  for(int i=0;i<D;++i) {
635  if (x_[i] >= y.x_[i]) return false;
636  }
637  return true;
638  }
639 
640  template <int D, class T>
641  inline
643  {
644  for(int i=0;i<D;++i) {
645  if (x_[i] <= y.x_[i]) return false;
646  }
647  return true;
648  }
649 
650  template <int D, class T>
651  inline
653  {
654  for(int i=0;i<D;++i) {
655  if (x_[i] > y.x_[i]) return false;
656  }
657  return true;
658  }
659 
660  template <int D, class T>
661  inline
663  {
664  for(int i=0;i<D;++i) {
665  if (x_[i] < y.x_[i]) return false;
666  }
667  return true;
668  }
669 
670  template <int D, class T>
672  {
673  T lensq = 0;
674  for(int i=0;i<D;++i)
675  lensq += x_[i]*x_[i];
676  return std::sqrt(lensq);
677  }
678 
679  template <int D, class T>
680  Position<D,T>::operator std::string() const
681  {
682  std::stringstream ss;
683  ss << *this;
684  return ss.str();
685  }
686 
687  template <int D, class T>
688  void Position<D,T>::print(std::ostream& out, char sep) const
689  {
690  out << x_[0];
691  for(int i=1;i<D;++i)
692  out << sep << x_[i];
693  }
694 
695  template <int D, class T>
696  std::ostream & operator<<(std::ostream & os, const Position<D,T> & pos)
697  {
698  os << "(";
699  if (D>0)
700  os << pos.x_[0];
701  for(int i=1;i<D;++i)
702  os << ", " << pos.x_[i];
703  os << ")";
704  return os;
705  }
706 
707 } // namespace nest
708 
709 #endif
Definition: position.h:43
Box(const Position< D > &ll, const Position< D > &ur)
Definition: position.h:292
An index into a multidimensional array.
Definition: position.h:304
Position()
Default constructor, initializing all coordinates to zero.
Definition: position.h:356
MultiIndex(const Position< D, int > &ll, const Position< D, int > &ur)
Definition: position.h:314
MultiIndex()
Definition: position.h:306
void print(std::ostream &out, char sep= ' ') const
Print position to output stream.
Definition: position.h:688
Position operator*(const Position< D, OT > &other) const
Elementwise multiplication.
MultiIndex(const Position< D, int > &ur)
Definition: position.h:310
Position< D, int > get_lower_left() const
Definition: position.h:343
Position operator/(const Position< D, OT > &other) const
Elementwise division.
const Name a("a")
Specific to Brette & Gerstner 2005 (aeif_cond-*)
Definition: nest_names.h:41
assert(pNet!=0)
bool operator>(const Position &y) const
Definition: position.h:642
Position< D, int > lower_left_
Definition: position.h:349
Position< D, int > upper_right_
Definition: position.h:350
T x_[D]
Definition: position.h:281
const Name y("y")
Definition: topology_names.h:52
const Name other("other")
Node type.
Definition: nest_names.h:216
MultiIndex & operator++()
Definition: position.h:318
A box is defined by the lower left corner (minimum coordinates) and the upper right corner (maximum c...
Definition: position.h:289
bool operator==(const Position &y) const
Definition: position.h:612
bool operator<=(const Position &y) const
Definition: position.h:652
Position & operator*=(const Position< D, OT > &)
In-place elementwise multiplication.
const Name x("x")
current scaling factor of the synaptic weight [0...1] (Tsodyks2_connection)
Definition: nest_names.h:356
Exception to be thrown if a status parameter is incomplete or inconsistent.
Definition: exceptions.h:420
Position< D > upper_right
Definition: position.h:297
T & operator[](int i)
Definition: position.h:429
Position & operator+=(const Position< D, OT > &)
In-place elementwise addition.
bool operator!=(const Position &y) const
Definition: position.h:622
Position & operator/=(const Position< D, OT > &)
In-place elementwise division.
MultiIndex operator++(int)
Definition: position.h:336
Position operator+(const Position< D, OT > &other) const
Elementwise addition.
Position< D > lower_left
Definition: position.h:296
bool operator<(const Position &y) const
Definition: position.h:632
Default types used by the NEST kernel.
Position operator-() const
Unary minus.
Definition: position.h:471
A type-independent container for C++-types.
Definition: token.h:68
T length() const
Length of Position vector.
Definition: position.h:671
const Name p("p")
current release probability (Tsodyks2_connection)
Definition: nest_names.h:218
Position< D, int > get_upper_right() const
Definition: position.h:345
Position & operator-=(const Position< D, OT > &)
In-place elementwise subtraction.
bool operator>=(const Position &y) const
Definition: position.h:662
Box()
Definition: position.h:291
Token getToken() const
Moves Position variables into an array.
Definition: position.h:442