3 * Implementation of GiNaC's index carrying objects. */
6 * GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
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.
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.
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
32 GINAC_IMPLEMENT_REGISTERED_CLASS(indexed, exprseq)
35 // default constructor, destructor, copy constructor assignment operator and helpers
42 debugmsg("indexed default constructor",LOGLEVEL_CONSTRUCT);
43 tinfo_key=TINFO_indexed;
48 void indexed::copy(const indexed & other)
50 inherited::copy(other);
53 void indexed::destroy(bool call_parent)
56 inherited::destroy(call_parent);
66 /** Construct indexed object with one index. The index must be of class idx
70 * @return newly constructed indexed object */
71 indexed::indexed(const ex & i1) : inherited(i1)
73 debugmsg("indexed constructor from ex",LOGLEVEL_CONSTRUCT);
74 tinfo_key=TINFO_indexed;
75 GINAC_ASSERT(all_of_type_idx());
78 /** Construct indexed object with two indices. The indices must be of class
81 * @param i1 First index
82 * @param i2 Second index
83 * @return newly constructed indexed object */
84 indexed::indexed(const ex & i1, const ex & i2) : inherited(i1,i2)
86 debugmsg("indexed constructor from ex,ex",LOGLEVEL_CONSTRUCT);
87 tinfo_key=TINFO_indexed;
88 GINAC_ASSERT(all_of_type_idx());
91 /** Construct indexed object with three indices. The indices must be of class
94 * @param i1 First index
95 * @param i2 Second index
96 * @param i3 Third index
97 * @return newly constructed indexed object */
98 indexed::indexed(const ex & i1, const ex & i2, const ex & i3)
101 debugmsg("indexed constructor from ex,ex,ex",LOGLEVEL_CONSTRUCT);
102 tinfo_key=TINFO_indexed;
103 GINAC_ASSERT(all_of_type_idx());
106 /** Construct indexed object with four indices. The indices must be of class
109 * @param i1 First index
110 * @param i2 Second index
111 * @param i3 Third index
112 * @param i4 Fourth index
113 * @return newly constructed indexed object */
114 indexed::indexed(const ex & i1, const ex & i2, const ex & i3, const ex & i4)
115 : inherited(i1,i2,i3,i4)
117 debugmsg("indexed constructor from ex,ex,ex,ex",LOGLEVEL_CONSTRUCT);
118 tinfo_key=TINFO_indexed;
119 GINAC_ASSERT(all_of_type_idx());
122 /** Construct indexed object with a specified vector of indices. The indices
123 * must be of class idx or a subclass.
125 * @param iv Vector of indices
126 * @return newly constructed indexed object */
127 indexed::indexed(const exvector & iv) : inherited(iv)
129 debugmsg("indexed constructor from exvector",LOGLEVEL_CONSTRUCT);
130 tinfo_key=TINFO_indexed;
131 GINAC_ASSERT(all_of_type_idx());
134 indexed::indexed(exvector * ivp) : inherited(ivp)
136 debugmsg("indexed constructor from exvector *",LOGLEVEL_CONSTRUCT);
137 tinfo_key=TINFO_indexed;
138 GINAC_ASSERT(all_of_type_idx());
145 /** Construct object from archive_node. */
146 indexed::indexed(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
148 debugmsg("indexed constructor from archive_node", LOGLEVEL_CONSTRUCT);
149 tinfo_key = TINFO_indexed;
152 /** Unarchive the object. */
153 ex indexed::unarchive(const archive_node &n, const lst &sym_lst)
155 return (new indexed(n, sym_lst))->setflag(status_flags::dynallocated);
158 /** Archive the object. */
159 void indexed::archive(archive_node &n) const
161 inherited::archive(n);
165 // functions overriding virtual functions from bases classes
170 void indexed::printraw(std::ostream & os) const
172 debugmsg("indexed printraw",LOGLEVEL_PRINT);
173 os << "indexed(indices=";
175 os << ",hash=" << hashvalue << ",flags=" << flags << ")";
178 void indexed::printtree(std::ostream & os, unsigned indent) const
180 debugmsg("indexed printtree",LOGLEVEL_PRINT);
181 os << std::string(indent,' ') << "indexed: " << seq.size() << " indices";
182 os << ",hash=" << hashvalue << ",flags=" << flags << std::endl;
183 printtreeindices(os,indent);
186 void indexed::print(std::ostream & os, unsigned upper_precedence) const
188 debugmsg("indexed print",LOGLEVEL_PRINT);
189 os << "UNNAMEDINDEX";
193 void indexed::printcsrc(std::ostream & os, unsigned type,
194 unsigned upper_precedence) const
196 debugmsg("indexed print csrc",LOGLEVEL_PRINT);
197 print(os,upper_precedence);
200 bool indexed::info(unsigned inf) const
202 if (inf==info_flags::indexed) return true;
203 if (inf==info_flags::has_indices) return seq.size()!=0;
204 return inherited::info(inf);
209 /** Implementation of ex::diff() for an indexed object. It always returns 0.
211 ex indexed::derivative(const symbol & s) const
216 int indexed::compare_same_type(const basic & other) const
218 GINAC_ASSERT(is_of_type(other,indexed));
219 return inherited::compare_same_type(other);
222 bool indexed::is_equal_same_type(const basic & other) const
224 GINAC_ASSERT(is_of_type(other,indexed));
225 return inherited::is_equal_same_type(other);
228 unsigned indexed::return_type(void) const
230 return return_types::noncommutative;
233 unsigned indexed::return_type_tinfo(void) const
238 ex indexed::thisexprseq(const exvector & v) const
243 ex indexed::thisexprseq(exvector * vp) const
249 // virtual functions which can be overridden by derived classes
255 // non-virtual functions in this class
260 void indexed::printrawindices(std::ostream & os) const
263 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
270 void indexed::printtreeindices(std::ostream & os, unsigned indent) const
273 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
274 os << std::string(indent+delta_indent,' ');
281 void indexed::printindices(std::ostream & os) const
287 exvector::const_iterator last=seq.end()-1;
288 exvector::const_iterator cit=seq.begin();
289 for (; cit!=last; ++cit) {
300 /** Check whether all indices are of class idx or a subclass. This function
301 * is used internally to make sure that all constructed indexed objects
302 * really carry indices and not some other classes. */
303 bool indexed::all_of_type_idx(void) const
305 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
306 if (!is_ex_of_type(*cit,idx)) return false;