atlas  0.6
ratvec.h
Go to the documentation of this file.
1 
5 /*
6  Copyright (C) 2004,2005 Fokko du Cloux
7  part of the Atlas of Lie Groups and Representations
8 
9  For license information see the LICENSE file
10 */
11 
12 #ifndef RATVEC_H /* guard against multiple inclusions */
13 #define RATVEC_H
14 
15 #include <vector>
16 #include "matrix.h"
17 #include "arithmetic.h"
18 
19 // extra defs for windows compilation -spc
20 #ifdef WIN32
21 #include "constants.h"
22 #endif
23 
24 namespace atlas {
25 
26 namespace ratvec {
27 
28 // type definitions
29 
35 template <typename C> // a signed integral type
36 class RationalVector
37 {
38  typedef matrix::Vector<C> V; // local abbreviation
39 
40  V d_num; // vector of integers, representing the numerators
41  arithmetic::Denom_t d_denom; // a positive common denominator
42 
43  public:
44 
45 // constructors and destructors
46  explicit RationalVector(size_t r): d_num(r,0), d_denom(1){} // zero vector
47 
48  // Build the RationalVector with numerator v and denominator d.
49  template <typename C1>
50  RationalVector(const matrix::Vector<C1>& v, C d);
51 
52 // accessors
53  // unsigned denominator requires care: plain % or / taboo; so export signed
54  C denominator() const { return (C)d_denom; }
55  const V& numerator() const { return d_num; }
56  size_t size() const { return d_num.size(); }
57 
58  bool operator== (const RationalVector& a) const;
59  bool operator!= (const RationalVector& a) const
60  { return not operator==(a); }
61  bool operator< (const RationalVector& a) const; // comparison, for STL use
62 
63  RationalVector operator+(const RationalVector& v) const;
65  { return *this=*this+v; }
67  { return RationalVector(-d_num,d_denom); }
69  { return *this+-v; }
71  { return *this=*this-v; }
75 
76  template <typename C1>
78  { d_num.add(v.begin(),denominator()); return *this; }
79  template <typename C1>
81  { d_num.subtract(v.begin(),denominator()); return *this; }
82 
83  template <typename C1>
85  { return RationalVector(*this)+=v; }
86  template <typename C1>
88  { return RationalVector(*this)-= v; }
89 
94 
95 /*
96  Returns the scalar product of |*this| and |w|, which are assumed to be of
97  same size and such that the scalar product is integral.
98 
99  A very long standing bug was to forget to cast |d_denom| to integer before
100  the division. With that omission the scalar product is \emph{implicitly}
101  cast to |unsigned int| instead, with desastrous consequences for the result.
102 
103  NOTE : this implementation does not worry about overflow. It is appropriate
104  only for small denominators.
105 */
106  template <typename C1>
107  C dot(const matrix::Vector<C1>& w) const
108  {
109  return w.dot(d_num)/(C)d_denom; // order is imposed here by return type |C|
110  }
111 
112 //manipulators
114  V& numerator() { return d_num; } // allow direct manipulation
115 
116 }; // |template <typename C> class RationalVector|
117 
118 
119 
120 
121 // Functions
122 
123 // left-multiply by a matrix
124 template<typename C1, typename C2>
125  RationalVector<C2> operator*
126  (const matrix::Matrix<C1>& M, const RationalVector<C2>& v);
127 
128 // right-multiply by a matrix
129 template<typename C1, typename C2>
130  RationalVector<C2> operator*
131  ( const RationalVector<C2>& v,const matrix::Matrix<C1>& M);
132 
133 // project to fixed points of involution
134 template<typename C1, typename C2>
137 {
138  v.numerator() += M*v.numerator();
139  return (v/=2).normalize();
140 }
141 
142 // project to dual fixed points of involution
143 template<typename C1, typename C2>
146 {
147  v.numerator() += M.right_prod(v.numerator());
148  return (v/=2).normalize();
149 }
150 
151 
152 
153 
154 } // |namespace ratvec|
155 } // |namespace atlas|
156 #endif
bool operator==(const RationalVector &a) const
Definition: ratvec.cpp:35
C1 dot(const Vector< C1 > &v) const
Definition: matrix.cpp:147
RationalVector & operator+=(const RationalVector &v)
Definition: ratvec.h:64
Vector< C1 > right_prod(const Vector< C1 > &) const
Definition: matrix.cpp:359
Definition: arithmetic.h:54
RationalVector< C2 > & symmetrise(const matrix::Matrix< C1 > &M, RationalVector< C2 > &v)
Definition: ratvec.h:136
V d_num
Definition: ratvec.h:40
RationalVector operator-(const RationalVector &v) const
Definition: ratvec.h:68
V & numerator()
Definition: ratvec.h:114
RationalVector & operator/=(C n)
Definition: ratvec.cpp:88
RationalVector operator+(const RationalVector &v) const
Definition: ratvec.cpp:55
bool operator<(const RationalVector &a) const
Definition: ratvec.cpp:42
Vector & subtract(I b, C c)
Definition: matrix.h:84
RationalVector & operator-=(const RationalVector &v)
Definition: ratvec.h:70
const V & numerator() const
Definition: ratvec.h:55
RationalVector & operator%=(C n)
Definition: ratvec.cpp:102
bool operator!=(const RationalVector &a) const
Definition: ratvec.h:59
arithmetic::Denom_t d_denom
Definition: ratvec.h:41
C dot(const matrix::Vector< C1 > &w) const
Definition: ratvec.h:107
C denominator() const
Definition: ratvec.h:54
RationalVector & operator+=(const matrix::Vector< C1 > &v)
Definition: ratvec.h:77
RationalVector & normalize()
Definition: ratvec.cpp:135
unsigned long long int Denom_t
Definition: Atlas.h:69
size_t size() const
Definition: ratvec.h:56
Definition: Atlas.h:78
RationalVector(size_t r)
Definition: ratvec.h:46
RationalVector operator-(const matrix::Vector< C1 > &v) const
Definition: ratvec.h:87
RationalVector & operator-=(const matrix::Vector< C1 > &v)
Definition: ratvec.h:80
Vector & add(I b, C c)
RationalVector & operator*=(C n)
Definition: ratvec.cpp:69
RationalVector operator-() const
Definition: ratvec.h:66
matrix::Vector< C > V
Definition: ratvec.h:38
unsigned long n
Definition: axis.cpp:77
Definition: Atlas.h:38
Element of lattice tensored with rational numbers.
Definition: Atlas.h:83
RationalVector operator*(const arithmetic::Rational &r) const
Definition: ratvec.cpp:115
RationalVector operator+(const matrix::Vector< C1 > &v) const
Definition: ratvec.h:84
RationalVector operator/(const arithmetic::Rational &r) const
Definition: ratvec.cpp:128
Vertex v
Definition: graph.cpp:116
Definition: Atlas.h:80