[go: up one dir, main page]

Menu

[r41]: / src / ncsymbol.cpp  Maximize  Restore  History

Download this file

355 lines (307 with data), 10.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/** @file ncsymbol.cpp
*
* Implementation of GiNaC's symbolic objects - non-commutative. */
/*
* GiNaC Copyright (C) 2011 Jan Rheinländer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
// Copied from symbol.cpp and modified a bit
#include "ncsymbol.h"
#include "ginac/lst.h"
#include "ginac/archive.h"
//#include "ginac/tostring.h"
//#include "ginac/utils.h"
//#include "ginac/hash_seed.h"
#include "ginac/inifcns.h"
#include "ginac/expairseq.h"
#include "ginac/relational.h"
#include "ginac/pseries.h"
#include <map>
#include <stdexcept>
#include <string>
namespace GiNaC {
GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(ncsymbol, basic,
print_func<print_context>(&ncsymbol::do_print).
print_func<print_latex>(&ncsymbol::do_print_latex).
print_func<print_tree>(&ncsymbol::do_print_tree).
print_func<print_python_repr>(&ncsymbol::do_print_python_repr))
//////////
// default constructor
//////////
ncsymbol::ncsymbol() : serial(next_serial++), name(""), TeX_name("")
{
setflag(status_flags::evaluated | status_flags::expanded);
}
//////////
// other constructors
//////////
// public
ncsymbol::ncsymbol(const std::string & initname) : serial(next_serial++),
name(initname), TeX_name("")
{
setflag(status_flags::evaluated | status_flags::expanded);
}
ncsymbol::ncsymbol(const std::string & initname, const std::string & texname) :
serial(next_serial++), name(initname), TeX_name(texname)
{
setflag(status_flags::evaluated | status_flags::expanded);
}
//////////
// archiving
//////////
/** Read object from archive_node. */
void ncsymbol::read_archive(const archive_node &n, lst &sym_lst)
{
inherited::read_archive(n, sym_lst);
serial = next_serial++;
std::string tmp_name;
n.find_string("name", tmp_name);
// If ncsymbol is in sym_lst, return the existing ncsymbol
for (lst::const_iterator it = sym_lst.begin(); it != sym_lst.end(); ++it) {
if (is_a<ncsymbol>(*it) && (ex_to<ncsymbol>(*it).name == tmp_name)) {
*this = ex_to<ncsymbol>(*it);
return;
}
}
name = tmp_name;
if (!n.find_string("TeXname", TeX_name))
TeX_name = std::string("");
setflag(status_flags::evaluated | status_flags::expanded);
setflag(status_flags::dynallocated);
sym_lst.append(*this);
}
/** Archive the object. */
void ncsymbol::archive(archive_node &n) const
{
inherited::archive(n);
// XXX: we should not archive anonymous ncsymbols.
if (!name.empty())
n.add_string("name", name);
if (!TeX_name.empty())
n.add_string("TeX_name", TeX_name);
}
//////////
// functions overriding virtual functions from base classes
//////////
/** Return default TeX name for ncsymbol. This recognizes some greek letters. */
static const std::string& get_default_TeX_name(const std::string& name);
// public
void ncsymbol::do_print(const print_context & c, unsigned level) const
{
if (!name.empty())
c.s << name;
else
c.s << "ncsymbol" << serial;
}
void ncsymbol::do_print_latex(const print_latex & c, unsigned level) const
{
if (!TeX_name.empty())
c.s << TeX_name;
else if (!name.empty())
c.s << get_default_TeX_name(name);
else
c.s << "ncsymbol" << serial;
}
void ncsymbol::do_print_tree(const print_tree & c, unsigned level) const
{
c.s << std::string(level, ' ') << name << " (" << class_name() << ")" << " @" << this
<< ", serial=" << serial
<< std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
<< ", domain=" << get_domain() << ", non-commutative composite"
<< std::endl;
}
void ncsymbol::do_print_python_repr(const print_python_repr & c, unsigned level) const
{
c.s << class_name() << "('";
if (!name.empty())
c.s << name;
else
c.s << "ncsymbol" << serial;
if (!TeX_name.empty())
c.s << "','" << TeX_name;
c.s << "')";
}
bool ncsymbol::info(unsigned inf) const
{
switch (inf) {
//case info_flags::symbol:
case info_flags::polynomial:
case info_flags::integer_polynomial:
case info_flags::cinteger_polynomial:
case info_flags::rational_polynomial:
case info_flags::crational_polynomial:
case info_flags::rational_function:
case info_flags::expanded:
return true;
case info_flags::real:
return get_domain() == domain::real || get_domain() == domain::positive;
case info_flags::positive:
case info_flags::nonnegative:
return get_domain() == domain::positive;
case info_flags::has_indices:
return false;
}
return inherited::info(inf);
}
// Copied from pseries.cpp
ex ncsymbol::series(const relational & r, int order, unsigned options) const
{
epvector seq;
const ex point = r.rhs();
GINAC_ASSERT(is_a<ncsymbol>(r.lhs()));
if (this->is_equal_same_type(ex_to<ncsymbol>(r.lhs()))) {
if (order > 0 && !point.is_zero())
seq.push_back(expair(point, ex(numeric(0))));
if (order > 1)
seq.push_back(expair(ex(numeric(1)), ex(numeric(1))));
else
seq.push_back(expair(Order(ex(numeric(1))), numeric(order)));
} else
seq.push_back(expair(*this, ex(numeric(0))));
return pseries(r, seq);
}
// Copied from normal.cpp
ex ncsymbol::normal(exmap & repl, exmap & rev_lookup, int level) const
{
return (new lst(*this, ex(numeric(1))))->setflag(status_flags::dynallocated);
}
// Copied from normal.cpp
ex ncsymbol::to_polynomial(exmap & repl) const
{
return *this;
}
// Copied from normal.cpp
ex ncsymbol::to_rational(exmap & repl) const
{
return *this;
}
ex ncsymbol::conjugate() const
{
return conjugate_function(*this).hold();
}
ex ncsymbol::real_part() const
{
return real_part_function(*this).hold();
}
ex ncsymbol::imag_part() const
{
return imag_part_function(*this).hold();
}
bool ncsymbol::is_polynomial(const ex & var) const
{
return true;
}
// protected
/** Implementation of ex::diff() for single differentiation of a ncsymbol.
* It returns 1 or 0.
*
* @see ex::diff */
/*ex ncsymbol::derivative(const symbol & s) const
{
if (compare_same_type(s))
return _ex0;
else
return _ex1;
}*/
int ncsymbol::compare_same_type(const basic & other) const
{
GINAC_ASSERT(is_a<ncsymbol>(other));
const ncsymbol *o = static_cast<const ncsymbol *>(&other);
if (serial==o->serial) return 0;
return serial < o->serial ? -1 : 1;
}
bool ncsymbol::is_equal_same_type(const basic & other) const
{
GINAC_ASSERT(is_a<ncsymbol>(other));
const ncsymbol *o = static_cast<const ncsymbol *>(&other);
return serial==o->serial;
}
/*unsigned ncsymbol::calchash() const
{
unsigned seed = make_hash_seed(typeid(*this));
hashvalue = golden_ratio_hash(seed ^ serial);
setflag(status_flags::hash_calculated);
return hashvalue;
}*/
//////////
// virtual functions which can be overridden by derived classes
//////////
// none
//////////
// non-virtual functions in this class
//////////
/** Return default TeX name for ncsymbol. This recognizes some greek letters. */
static const std::string& get_default_TeX_name(const std::string& name)
{
static std::map<std::string, std::string> standard_names;
static bool names_initialized = false;
if (!names_initialized) {
standard_names["alpha"] = std::string("\\alpha");
standard_names["beta"] = std::string("\\beta");;
standard_names["gamma"] = std::string("\\gamma");;
standard_names["delta"] = std::string("\\delta");;
standard_names["epsilon"] = std::string("\\epsilon");
standard_names["varepsilon"] = std::string("\\varepsilon");
standard_names["zeta"] = std::string("\\zeta");
standard_names["eta" ] = std::string("\\eta" );
standard_names["theta"] = std::string("\\theta");
standard_names["vartheta"] = std::string("\\vartheta");
standard_names["iota"] = std::string("\\iota");
standard_names["kappa"] = std::string("\\kappa");
standard_names["lambda"] = std::string("\\lambda");
standard_names["mu"] = std::string("\\mu");
standard_names["nu"] = std::string("\\nu");
standard_names["xi"] = std::string("\\xi");
standard_names["omicron"] = std::string("\\omicron");
standard_names["pi"] = std::string("\\pi");
standard_names["varpi"] = std::string("\\varpi");
standard_names["rho"] = std::string("\\rho");
standard_names["varrho"] = std::string("\\varrho");
standard_names["sigma"] = std::string("\\sigma");
standard_names["varsigma"] = std::string("\\varsigma");
standard_names["tau"] = std::string("\\tau");
standard_names["upsilon"] = std::string("\\upsilon");
standard_names["phi"] = std::string("\\phi");
standard_names["varphi"] = std::string("\\varphi");
standard_names["chi"] = std::string("\\chi");
standard_names["psi"] = std::string("\\psi");
standard_names["omega"] = std::string("\\omega");
standard_names["Gamma"] = std::string("\\Gamma");
standard_names["Delta"] = std::string("\\Delta");
standard_names["Theta"] = std::string("\\Theta");
standard_names["Lambda"] = std::string("\\Lambda");
standard_names["Xi"] = std::string("\\Xi");
standard_names["Pi"] = std::string("\\Pi");
standard_names["Sigma"] = std::string("\\Sigma");
standard_names["Upsilon"] = std::string("\\Upsilon");
standard_names["Phi"] = std::string("\\Phi");
standard_names["Psi"] = std::string("\\Psi");
standard_names["Omega"] = std::string("\\Omega");
names_initialized = true;
}
std::map<std::string, std::string>::const_iterator it = standard_names.find(name);
if (it != standard_names.end())
return it->second;
else
return name;
}
GINAC_BIND_UNARCHIVER(ncsymbol);
//////////
// static member variables
//////////
// private
unsigned ncsymbol::next_serial = 0;
} // namespace GiNaC