/***************************************************************************
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