]> www.ginac.de Git - ginac.git/blob - ginac/idx.h
ea885feef1d56c86f043b328b5b4f279c010fbe7
[ginac.git] / ginac / idx.h
1 /** @file idx.h
2  *
3  *  Interface to GiNaC's indices. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
7  *
8  *  This program 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  *  This program 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 this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #ifndef __GINAC_IDX_H__
24 #define __GINAC_IDX_H__
25
26 #include "ex.h"
27
28 namespace GiNaC {
29
30
31 /** This class holds one index of an indexed object. Indices can
32  *  theoretically consist of any symbolic expression but they are usually
33  *  only just a symbol (e.g. "mu", "i") or numeric (integer). Indices belong
34  *  to a space with a certain numeric or symbolic dimension. */
35 class idx : public basic
36 {
37         GINAC_DECLARE_REGISTERED_CLASS(idx, basic)
38
39         // other constructors
40 public:
41         /** Construct index with given value and dimension.
42          *
43          *  @param v Value of index (numeric or symbolic)
44          *  @param dim Dimension of index space (numeric or symbolic)
45          *  @return newly constructed index */
46         explicit idx(const ex & v, const ex & dim);
47
48         // functions overriding virtual functions from bases classes
49 public:
50         void printraw(std::ostream & os) const;
51         void printtree(std::ostream & os, unsigned indent) const;
52         void print(std::ostream & os, unsigned upper_precedence=0) const;
53         bool info(unsigned inf) const;
54 protected:
55         ex subs(const lst & ls, const lst & lr) const;
56
57         // new virtual functions in this class
58 public:
59         /** Check whether the index forms a dummy index pair with another index
60          *  of the same type. */
61         virtual bool is_dummy_pair_same_type(const basic & other) const;
62
63         // non-virtual functions in this class
64 public:
65         /** Get value of index. */
66         ex get_value(void) const {return value;}
67
68         /** Check whether the index is numeric. */
69         bool is_numeric(void) const {return is_ex_exactly_of_type(value, numeric);}
70
71         /** Check whether the index is symbolic. */
72         bool is_symbolic(void) const {return !is_ex_exactly_of_type(value, numeric);}
73
74         /** Get dimension of index space. */
75         ex get_dim(void) const {return dim;}
76
77         /** Check whether the dimension is numeric. */
78         bool is_dim_numeric(void) const {return is_ex_exactly_of_type(dim, numeric);}
79
80         /** Check whether the dimension is symbolic. */
81         bool is_dim_symbolic(void) const {return !is_ex_exactly_of_type(dim, numeric);}
82
83 protected:
84         ex value; /**< Expression that constitutes the index (numeric or symbolic name) */
85         ex dim;   /**< Dimension of space (can be symbolic or numeric) */
86 };
87
88
89 /** This class holds an index with a variance (co- or contravariant). There
90  *  is an associated metric tensor that can be used to raise/lower indices. */
91 class varidx : public idx
92 {
93         GINAC_DECLARE_REGISTERED_CLASS(varidx, idx)
94
95         // other constructors
96 public:
97         /** Construct index with given value, dimension and variance.
98          *
99          *  @param v Value of index (numeric or symbolic)
100          *  @param dim Dimension of index space (numeric or symbolic)
101          *  @param covariant Make covariant index (default is contravariant)
102          *  @return newly constructed index */
103         varidx(const ex & v, const ex & dim, bool covariant = false);
104
105         // functions overriding virtual functions from bases classes
106 public:
107         void print(std::ostream & os, unsigned upper_precedence=0) const;
108         bool is_dummy_pair_same_type(const basic & other) const;
109
110         // non-virtual functions in this class
111 public:
112         /** Check whether the index is covariant. */
113         bool is_covariant(void) const {return covariant;}
114
115         /** Check whether the index is contravariant (not covariant). */
116         bool is_contravariant(void) const {return !covariant;}
117
118         /** Make a new index with the same value but the opposite variance. */
119         ex toggle_variance(void) const;
120
121         // member variables
122 protected:
123         bool covariant; /**< x.mu, default is contravariant: x~mu */
124 };
125
126
127 // utility functions
128 inline const idx &ex_to_idx(const ex & e)
129 {
130         return static_cast<const idx &>(*e.bp);
131 }
132
133 inline const varidx &ex_to_varidx(const ex & e)
134 {
135         return static_cast<const varidx &>(*e.bp);
136 }
137
138 /** Check whether two indices form a dummy pair. */
139 bool is_dummy_pair(const idx & i1, const idx & i2);
140
141 /** Check whether two expressions form a dummy index pair. */
142 bool is_dummy_pair(const ex & e1, const ex & e2);
143
144
145 } // namespace GiNaC
146
147 #endif // ndef __GINAC_IDX_H__