|
GiNaC
1.6.2
|
00001 00005 /* 00006 * GiNaC Copyright (C) 1999-2011 Johannes Gutenberg University Mainz, Germany 00007 * 00008 * This program is free software; you can redistribute it and/or modify 00009 * it under the terms of the GNU General Public License as published by 00010 * the Free Software Foundation; either version 2 of the License, or 00011 * (at your option) any later version. 00012 * 00013 * This program is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU General Public License 00019 * along with this program; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 */ 00022 00023 #ifndef GINAC_STRUCTURE_H 00024 #define GINAC_STRUCTURE_H 00025 00026 #include "ex.h" 00027 #include "ncmul.h" 00028 #include "numeric.h" 00029 #include "operators.h" 00030 #include "print.h" 00031 00032 #include <functional> 00033 00034 namespace GiNaC { 00035 00037 template <class T> 00038 class compare_all_equal { 00039 protected: 00040 static bool struct_is_equal(const T * t1, const T * t2) { return true; } 00041 static int struct_compare(const T * t1, const T * t2) { return 0; } 00042 00043 // disallow destruction of structure through a compare_all_equal* 00044 protected: 00045 ~compare_all_equal() {} 00046 }; 00047 00048 00051 template <class T> 00052 class compare_std_less { 00053 protected: 00054 static bool struct_is_equal(const T * t1, const T * t2) 00055 { 00056 return std::equal_to<T>()(*t1, *t2); 00057 } 00058 00059 static int struct_compare(const T * t1, const T * t2) 00060 { 00061 if (std::less<T>()(*t1, *t2)) 00062 return -1; 00063 else if (std::less<T>()(*t2, *t1)) 00064 return 1; 00065 else 00066 return 0; 00067 } 00068 00069 // disallow destruction of structure through a compare_std_less* 00070 protected: 00071 ~compare_std_less() {} 00072 }; 00073 00074 00076 template <class T> 00077 class compare_bitwise { 00078 protected: 00079 static bool struct_is_equal(const T * t1, const T * t2) 00080 { 00081 const char * cp1 = reinterpret_cast<const char *>(t1); 00082 const char * cp2 = reinterpret_cast<const char *>(t2); 00083 00084 return std::equal(cp1, cp1 + sizeof(T), cp2); 00085 } 00086 00087 static int struct_compare(const T * t1, const T * t2) 00088 { 00089 const unsigned char * cp1 = reinterpret_cast<const unsigned char *>(t1); 00090 const unsigned char * cp2 = reinterpret_cast<const unsigned char *>(t2); 00091 typedef std::pair<const unsigned char *, const unsigned char *> cppair; 00092 00093 cppair res = std::mismatch(cp1, cp1 + sizeof(T), cp2); 00094 00095 if (res.first == cp1 + sizeof(T)) 00096 return 0; 00097 else if (*res.first < *res.second) 00098 return -1; 00099 else 00100 return 1; 00101 } 00102 00103 // disallow destruction of structure through a compare_bitwise* 00104 protected: 00105 ~compare_bitwise() {} 00106 }; 00107 00108 00109 // Select default comparison policy 00110 template <class T, template <class> class ComparisonPolicy = compare_all_equal> class structure; 00111 00112 00114 template <class T, template <class> class ComparisonPolicy> 00115 class structure : public basic, public ComparisonPolicy<T> { 00116 GINAC_DECLARE_REGISTERED_CLASS(structure, basic) 00117 00118 // helpers 00119 static const char *get_class_name() { return "structure"; } 00120 // constructors 00121 public: 00123 structure(const T & t) : obj(t) { } 00124 00125 // functions overriding virtual functions from base classes 00126 // All these are just defaults that can be specialized by the user 00127 public: 00128 // evaluation 00129 ex eval(int level = 0) const { return hold(); } 00130 ex evalf(int level = 0) const { return inherited::evalf(level); } 00131 ex evalm() const { return inherited::evalm(); } 00132 protected: 00133 ex eval_ncmul(const exvector & v) const { return hold_ncmul(v); } 00134 public: 00135 ex eval_indexed(const basic & i) const { return i.hold(); } 00136 00137 // printing 00138 void print(const print_context & c, unsigned level = 0) const { inherited::print(c, level); } 00139 unsigned precedence() const { return 70; } 00140 00141 // info 00142 bool info(unsigned inf) const { return false; } 00143 00144 // operand access 00145 size_t nops() const { return 0; } 00146 ex op(size_t i) const { return inherited::op(i); } 00147 ex operator[](const ex & index) const { return inherited::operator[](index); } 00148 ex operator[](size_t i) const { return inherited::operator[](i); } 00149 ex & let_op(size_t i) { return inherited::let_op(i); } 00150 ex & operator[](const ex & index) { return inherited::operator[](index); } 00151 ex & operator[](size_t i) { return inherited::operator[](i); } 00152 00153 // pattern matching 00154 bool has(const ex & other, unsigned options = 0) const { return inherited::has(other, options); } 00155 bool match(const ex & pattern, exmap& repl_lst) const { return inherited::match(pattern, repl_lst); } 00156 protected: 00157 bool match_same_type(const basic & other) const { return true; } 00158 public: 00159 00160 // substitutions 00161 ex subs(const exmap & m, unsigned options = 0) const { return inherited::subs(m, options); } 00162 00163 // function mapping 00164 ex map(map_function & f) const { return inherited::map(f); } 00165 00166 // degree/coeff 00167 int degree(const ex & s) const { return inherited::degree(s); } 00168 int ldegree(const ex & s) const { return inherited::ldegree(s); } 00169 ex coeff(const ex & s, int n = 1) const { return inherited::coeff(s, n); } 00170 00171 // expand/collect 00172 ex expand(unsigned options = 0) const { return inherited::expand(options); } 00173 ex collect(const ex & s, bool distributed = false) const { return inherited::collect(s, distributed); } 00174 00175 // differentiation and series expansion 00176 protected: 00177 ex derivative(const symbol & s) const { return inherited::derivative(s); } 00178 public: 00179 ex series(const relational & r, int order, unsigned options = 0) const { return inherited::series(r, order, options); } 00180 00181 // rational functions 00182 ex normal(exmap & repl, exmap & rev_lookup, int level = 0) const { return inherited::normal(repl, rev_lookup, level); } 00183 ex to_rational(exmap & repl) const { return inherited::to_rational(repl); } 00184 ex to_polynomial(exmap & repl) const { return inherited::to_polynomial(repl); } 00185 00186 // polynomial algorithms 00187 numeric integer_content() const { return 1; } 00188 ex smod(const numeric & xi) const { return *this; } 00189 numeric max_coefficient() const { return 1; } 00190 00191 // indexed objects 00192 exvector get_free_indices() const { return exvector(); } 00193 ex add_indexed(const ex & self, const ex & other) const { return self + other; } 00194 ex scalar_mul_indexed(const ex & self, const numeric & other) const { return self * ex(other); } 00195 bool contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const { return false; } 00196 00197 // noncommutativity 00198 unsigned return_type() const { return return_types::commutative; } 00199 return_type_t return_type_tinfo() const 00200 { 00201 return_type_t r; 00202 r.rl = 0; 00203 r.tinfo = &typeid(*this); 00204 return r; 00205 } 00206 00207 protected: 00208 bool is_equal_same_type(const basic & other) const 00209 { 00210 GINAC_ASSERT(is_a<structure>(other)); 00211 const structure & o = static_cast<const structure &>(other); 00212 00213 return this->struct_is_equal(&obj, &o.obj); 00214 } 00215 00216 unsigned calchash() const { return inherited::calchash(); } 00217 00218 // non-virtual functions in this class 00219 public: 00220 // access to embedded structure 00221 const T *operator->() const { return &obj; } 00222 T &get_struct() { return obj; } 00223 const T &get_struct() const { return obj; } 00224 private: 00225 T obj; 00226 }; 00227 00228 00230 template <class T, template <class> class CP> 00231 structure<T, CP>::structure() { } 00232 00234 template <class T, template <class> class CP> 00235 int structure<T, CP>::compare_same_type(const basic & other) const 00236 { 00237 GINAC_ASSERT(is_a<structure>(other)); 00238 const structure & o = static_cast<const structure &>(other); 00239 00240 return this->struct_compare(&obj, &o.obj); 00241 } 00242 00243 template <class T, template <class> class CP> 00244 registered_class_info structure<T, CP>::reg_info = registered_class_info(registered_class_options(structure::get_class_name(), "basic", typeid(structure<T, CP>))); 00245 00246 } // namespace GiNaC 00247 00248 #endif // ndef GINAC_STRUCTURE_H