[go: up one dir, main page]

Menu

[r7]: / src / utils.h  Maximize  Restore  History

Download this file

221 lines (180 with data), 7.7 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
/***************************************************************************
utils.h - Utility functions for EQC - header file
-------------------
begin : Mon Oct 22 2001
copyright : (C) 2001 by Jan Rheinlaender
email : jrheinlaender@users.sourceforge.net
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
#ifndef UTILS_H
#define UTILS_H
/**
* @author Jan Rheinlaender
* @short Some utitlity functions for eqc
*/
#include <set>
#include <string>
#include <stdexcept>
#include <cln/cln.h> // *** added in 0.7
#include <ginac/ginac.h>
//#ifdef __MINGW32__ // *** added in 1.0, changed in 1.2, changed in 1.4.0
// #include <ext/hash_fun.h>
#ifndef _MSC_VER
#include <backward/hash_fun.h>
#endif
using namespace GiNaC;
#include "expression.h"
//#include "msgdriver.h" // *** changed in 1.3.1
//--------------- Helper functions for dealing with sets ----------------
/// This structure is necessary to use sets of const char*. It defines the string comparison
struct ltstr {
/// Compare the strings
bool operator()(const char* s1, const char* s2) const {
return strcmp(s1, s2) < 0;
}
};
/**
* Print a set on a stream
* @param s The set to be printed
* @param os The stream to print on
*/
void set_print(std::set<const char*, ltstr> &s, std::ostream &os);
// *** changed in 1.3.1
/// Print a set on a message stream
//message &operator<<(message &ms, std::set<const char*, ltstr> &s); *** moved to msgdriver.h in 1.3.1
/**
* Check whether the set contains a string
* @param s The set
* @param element The string to look up in the set
* @returns A boolean indication whether the element belongs to the set
*/
bool set_has(std::set<const char*, ltstr> &s, const std::string &element);
/**
* Add an element to a set
* @param s The set to add an element to
* @param element The element to add
* @returns The modified set
*/
std::set<const char *, ltstr> set_add(const std::set<const char*, ltstr> &s, const std::string &element);
//--------------------- Miscellaneous functions -----------------------------------------
/// Return the basis of the power
// !!!This code relies on the canonical ordering not being changed!!!
inline ex get_basis(const power &p) { return (p.op(0)); }
/// Return the exponent of the power
// !!!This code relies on the canonical ordering not being changed!!!
inline ex get_exp(const power &p) { return (p.op(1)); }
/// Checks whether the argument is equal to one within the limits of GiNaC::Digits
bool is_equal_one(const numeric &n); // *** added in 1.0
/// Checks whether the argument is zero within the limits of GiNaC::Digits
bool is_equal_zero(const numeric &n); // *** added in 1.0
/**
* Checks whether a GiNaC expression is a negative power. This is the case if the exponent
* is a negative expression (@see is_negex())
* @param p The power to be inspected
* @returns True if this is a negative power
*/
bool is_negpower(const ex &p);
/**
* Checks whether a GiNaC expression is negative. This is not as easy as it appears because a simple
* comparison e < 0 does not work for expressions like -x.
* @param e The expression to be inspected
* @returns True if this is a negative expression
*/
bool is_negex(const ex &e);
/**
* Checks whether an expression is a quantity.
* @param quantity The expression to inspect
* @returns True if the expression is a quantity
*/
const bool is_quantity (const expression &quantity); // *** changed ex to expression in 0.4
/// Return the expression as a GiNaC lst object
inline lst make_lst(const expression &e) { // *** added in 0.5
return (is_a<lst>(e) ? ex_to<lst>(e) : lst(e)); // *** changed in 0.9
} // make_lst()
/// Create a list from the matrix
lst make_lst_from_matrix(const expression &e); // *** added in 1.3.0
/// Return the expression as a GiNaC matrix object
matrix make_matrix(const expression &e); // *** added in 1.3.0
/// Return the expression as an exvector object
// *** added in 0.6
exvector make_exvector(const expression &e);
/// Make a latex label printable within a \\tag{} command
const std::string ltx_label(const std::string &l); // *** added in 0.8
/** *** moved here from func.h in 0.7
* Print an exvector an a stream
* @param os The stream to print on
* @param v The exvector to print
* @returns The stream (for concatenation of << operators)
**/
std::ostream &operator<<(std::ostream &os, const exvector &v);
/// Convert a numeric to an unsigned integer (without plausibility testing!)
const unsigned numeric_to_uint(const numeric &n); // *** added in 1.0
/// Convert a numeric to a signed integer (without plausibility testing!)
const int numeric_to_int(const numeric &n); // *** added in 1.0
const std::string extostr(const expression& e); // *** added in 1.3.1
//----------------------Printing of GiNaC types on message streams---------------
// *** added in 0.7
/// Print a GiNaC object on a message stream
//message &operator<<(message &ms, const GiNaC::basic &o);
/// Print a GiNaC lst on a message stream
//message &operator<<(message &ms, const GiNaC::lst &l);
/// Print a GiNaC ex on a message stream
//message &operator<<(message &ms, const GiNaC::ex &e);
/// Print a GiNaC exvector on a message stream
//message &operator<<(message &ms, const GiNaC::exvector &v);
/// Print a GiNaC exmap on a message stream
// *** added in 0.8
//message &operator<<(message &ms, const GiNaC::exmap &m);
/// Print a vector of expressions on a message stream
// *** added in 1.0
//message &operator<<(message &ms, const std::vector<expression> &v);
/// Print a cln object on a message stream
//message &operator<<(message &ms, const cln::cl_R &x);
//--------------------- Helper functions for dealing with hash_map ----------
// *** added in 1.0
/// Hash function for a std::string key
#ifndef _MSC_VER
namespace __gnu_cxx {
template<> struct hash<const std::string>{
std::size_t operator()(const std::string& x) const {
return __stl_hash_string(x.c_str());
}
};
}
#endif
/// Equality function for std::string
struct eqstr {
/// compare two strings
bool operator()(const std::string s1, const std::string s2) const {
return s1==s2;
}
};
//--------------------- map functions for simplifications -------------------
// *** added in 1.0
/// Expand powers with non-integer exponents and reduce double powers of units
struct expand_real_powers : public map_function {
/// Map the expression
ex operator()(const ex &e);
};
/// Reduce double powers (this is unsafe, since sqrt(x^2) = +/- x
struct reduce_double_powers : public map_function {
/// Map the expression
ex operator()(const ex &e);
};
/// Gather single square roots in a mul into one root
struct gather_sqrt : public map_function {
/// Map the expression
ex operator()(const ex &e);
};
//------------------- arithmetic with lists -----------------------------------------------
// *** added in 1.3.0
//matrix operator*(const lst &l, const ex &e);
//ex operator*(const ex &e, const lst &l);
#endif