GiNaC 1.8.10
Gt_helpers.h
Go to the documentation of this file.
1
5/*
6 * GiNaC Copyright (C) 1999-2026 Johannes Gutenberg University Mainz, Germany
7 *
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.
12 *
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.
17 *
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#ifndef GINAC_EMPL_HELPERS_H
24#define GINAC_EMPL_HELPERS_H
25
26#include <set>
27#include "ex.h"
28#include "hash_map.h"
29
30namespace GiNaC {
31namespace Gt_detail {
32
33// Recurse through an expression and apply transformation to each contained object of a given type
34// Cache function calls and reuse them if possible
35// This is more flexible and efficient than using collect() to avoid repeated function calls
36template<typename Type>
38{
39public:
41 const std::function<ex(const ex&)>& func_obj,
42 const std::function<ex(const ex&)>& func_default = [](const ex& obj) -> ex { return obj; })
44
45 // Apply to an expression
46 ex operator()(const ex& input) { return impl(input); }
47
48 // For debugging
49 void print_cache() const;
50 const exhashmap<ex>& get_cache() const { return cache; }
51 void clear_cache() { cache.clear();}
52
53private:
54 // Check whether a given ex is of Type
55 template<typename T = Type>
56 static typename std::enable_if<std::is_base_of<basic, T>::value, bool>::type check_type(const ex& expr);
57 // Overload for functions instead of expression types
58 template<typename T = Type>
59 static typename std::enable_if<!std::is_base_of<basic, T>::value, bool>::type check_type(const ex& expr);
60
61 // Recurse through expression, find objects of Type, check or update cache, return transformed expression
62 ex impl(const ex& expr);
63
64private:
65 const std::function<ex(const ex&)> func_obj; // function to transform the desired object
66 const std::function<ex(const ex&)> func_default; // function to transform any other object
67 exhashmap<ex> cache; // previously encountered objects and function call results
68};
69
70
71
72// Used to deconcatenate integration paths in (elliptic) multiple polylogs
73// Find all ways to distribute `n' objects into `m' buckets
74// Result e.g. n=2, m=3 => [(0,1,1),(1,0,1),(1,1,0),(0,0,2),(0,2,0),(2,0,0)]
75// n: number of kernels, m: number of paths
76// Since the order of kernels and paths is fixed (kernels ascending, paths descending)
77// this is enough information to construct all required permutations
78// e.g. n=2 kernels (a1,a2) and m=3 paths (p1,p2,p3):
79// (0,1,1) => 0 kernels to last path
80// 1 kernel to middle path -> G(a1;p2)
81// 1 kernel to first path -> G(a2;p1) => G(a1;p2)*G(a2;p1)
82// (0,0,2) => 0 kernels to last and middle path
83// 2 kernel to first path -> G(a1,a2;p1)
84std::vector<std::vector<int>> integer_partition(const int n, const int m);
85
86// Write an iterated integral that is integrated along a given path as a combination of iterated integrals, integrated along a straight line.
87// The input is an iterated integral with kernels `args' that is integrated along the piecewise straight path given by `endpoints'.
88// This function deconcatenates the path and constructs new iterated integrals containing subsets of the kernels of the original integral.
89// The function `construct' should construct a new iterated integral object with the given kernels and straight path. If the transformation
90// requires shifts of the kernel arguments, this function has to apply them too.
91template<typename Kernel>
93 const std::vector<Kernel>& args,
94 const std::vector<ex>& endpoints,
95 const std::function<ex(std::vector<Kernel>& new_args, const ex& start, const ex& end)>& construct
96);
97
98
99
101
102// Class that represents a single term of the integrand of an integral along a piecewise straight path
103// Used during iterative integration of elliptic multiple polylogs
105{
106public:
107 pathintegral_term(ex prefactor, int power, const std::vector<ex>& polylog = {}, ex denom=0)
109
110 pathintegral_term(const pathintegral_term& a, const pathintegral_term& b, int power_offset=0); // construct as product
111
112 // Integrate along a path and add the results to the given vector, add terms from partial integration/fractioning back to integrand
113 void integrate(const ex& start, pathintegral_term_list& integrand, pathintegral_term_list& result) const;
114
115 // Evaluate for a given upper bound
116 ex evaluate(const ex& upper_bound, const std::vector<ex>& path) const;
117
118private:
119 // Evaluate the regular MPL along the given path
120 // Return result in terms of held G functions. They are held, such that their numerical evaluation can be cached
121 static ex G_path(const std::vector<ex>& args, const std::vector<ex>& path);
122 static ex G_path(const std::vector<ex>& args, const ex& start, const ex& end);
123
125 friend std::ostream & operator<<(std::ostream & os, const pathintegral_term & term);
126 friend bool operator<(const pathintegral_term & lh, const pathintegral_term & rh);
127
128 mutable ex prefactor; // independent of integration variable
129 int power; // power of integration variable
130 std::vector<ex> polylog; // optional: arguments of a regular MPL
131 ex denom; // optional, only in integrand: a term 1/(integration variable-denom) (or 0 if not present)
132};
133std::ostream & operator<<(std::ostream & os, const pathintegral_term & term);
134bool operator<(const pathintegral_term & lh, const pathintegral_term & rh);
135
136// A sum of terms in the integrand, automatically merge/cancel terms that differ only in their prefactor
138{
139 std::set<pathintegral_term> terms;
140
141public:
142 std::set<pathintegral_term>::const_iterator begin() const { return terms.begin(); }
143 std::set<pathintegral_term>::const_iterator end() const { return terms.end(); }
144
145 void clear() { terms.clear(); }
146 bool empty() const { return terms.empty(); }
147 size_t size() const { return terms.size(); }
148
149 // Add a single term, taking care of possible combination/cancellation of terms
150 void add(pathintegral_term&& term);
151 // Forward arguments to constructor of pathintegral_term
152 template<typename... Args, typename = typename std::enable_if<(sizeof...(Args)>1)>::type>
153 void add(Args&& ...args)
154 {
155 add(pathintegral_term(std::forward<Args>(args)...));
156 }
157
158 // Add multiple terms
159 void add(const pathintegral_term_list& other);
160
161 // Remove the first element from the container and return it
163};
164
165} // namespace Gt_detail
166} // namespace GiNaC
167
168#endif // ndef GINAC_EMPL_HELPERS_H
const std::function< ex(const ex &)> func_default
Definition Gt_helpers.h:66
TransformExpressionWithCache(const std::function< ex(const ex &)> &func_obj, const std::function< ex(const ex &)> &func_default=[](const ex &obj) -> ex { return obj;})
Definition Gt_helpers.h:40
static std::enable_if<!std::is_base_of< basic, T >::value, bool >::type check_type(const ex &expr)
const exhashmap< ex > & get_cache() const
Definition Gt_helpers.h:50
const std::function< ex(const ex &)> func_obj
Definition Gt_helpers.h:65
static std::enable_if< std::is_base_of< basic, T >::value, bool >::type check_type(const ex &expr)
std::set< pathintegral_term >::const_iterator begin() const
Definition Gt_helpers.h:142
std::set< pathintegral_term > terms
Definition Gt_helpers.h:139
std::set< pathintegral_term >::const_iterator end() const
Definition Gt_helpers.h:143
pathintegral_term(ex prefactor, int power, const std::vector< ex > &polylog={}, ex denom=0)
Definition Gt_helpers.h:107
friend bool operator<(const pathintegral_term &lh, const pathintegral_term &rh)
void integrate(const ex &start, pathintegral_term_list &integrand, pathintegral_term_list &result) const
static ex G_path(const std::vector< ex > &args, const std::vector< ex > &path)
ex evaluate(const ex &upper_bound, const std::vector< ex > &path) const
friend std::ostream & operator<<(std::ostream &os, const pathintegral_term &term)
Sum of expressions.
Definition add.h:31
Lightweight wrapper for GiNaC's symbolic objects.
Definition ex.h:72
This class holds a two-component object, a basis and and exponent representing exponentiation.
Definition power.h:38
Interface to GiNaC's light-weight expression handles.
size_t n
Definition factor.cpp:1431
mvec m
Definition factor.cpp:757
Replacement for map<> using hash tables.
ex deconcatenate_path(const std::vector< Kernel > &args, const std::vector< ex > &endpoints, const std::function< ex(std::vector< Kernel > &new_args, const ex &start, const ex &end)> &construct)
bool operator<(const pathintegral_term &a, const pathintegral_term &b)
std::ostream & operator<<(std::ostream &os, const pathintegral_term &term)
std::vector< std::vector< int > > integer_partition(const int n, const int m)
Definition add.cpp:35
std::unordered_map< ex, T, Hash, KeyEqual, Allocator > exhashmap
Definition hash_map.h:33
ex evalf(const ex &thisex)
Definition ex.h:784

This page is part of the GiNaC developer's reference. It was generated automatically by doxygen. For an introduction, see the tutorial.