Cadabra
Computer algebra system for field theory problems
py_algorithms.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <pybind11/pybind11.h>
4 #include "py_ex.hh"
5 #include "py_helpers.hh"
6 #include "py_kernel.hh"
7 #include "py_progress.hh"
8 
9 namespace cadabra {
10 
17  template <class Algo>
18  Ex_ptr apply_algo_base(Algo& algo, Ex_ptr ex, bool deep, bool repeat, unsigned int depth, bool pre_order=false)
19  {
20  Ex::iterator it = ex->begin();
21  if (ex->is_valid(it)) {
23  algo.set_progress_monitor(pm);
24  if(pre_order)
25  ex->update_state(algo.apply_pre_order(repeat));
26  else
27  ex->update_state(algo.apply_generic(it, deep, repeat, depth));
29  }
30 
31  return ex;
32  }
33 
34  template <class Algo>
35  Ex_ptr apply_algo(Ex_ptr ex, bool deep, bool repeat, unsigned int depth)
36  {
37  Algo algo(*get_kernel_from_scope(), *ex);
38  return apply_algo_base(algo, ex, deep, repeat, depth, false);
39  }
40 
41  template <class Algo, typename Arg1>
42  Ex_ptr apply_algo(Ex_ptr ex, Arg1 arg1, bool deep, bool repeat, unsigned int depth)
43  {
44  Algo algo(*get_kernel_from_scope(), *ex, arg1);
45  return apply_algo_base(algo, ex, deep, repeat, depth, false);
46  }
47 
48  template <class Algo, typename Arg1, typename Arg2>
49  Ex_ptr apply_algo(Ex_ptr ex, Arg1 arg1, Arg2 arg2, bool deep, bool repeat, unsigned int depth)
50  {
51  Algo algo(*get_kernel_from_scope(), *ex, arg1, arg2);
52  return apply_algo_base(algo, ex, deep, repeat, depth, false);
53  }
54 
55  template <class Algo, typename Arg1, typename Arg2, typename Arg3>
56  Ex_ptr apply_algo(Ex_ptr ex, Arg1 arg1, Arg2 arg2, Arg3 arg3, bool deep, bool repeat, unsigned int depth)
57  {
58  Algo algo(*get_kernel_from_scope(), *ex, arg1, arg2, arg3);
59  return apply_algo_base(algo, ex, deep, repeat, depth, false);
60  }
61 
62 
71  template<class Algo, typename... Args, typename... PyArgs>
72  void def_algo(pybind11::module& m, const char* name, bool deep, bool repeat, unsigned int depth, PyArgs... pyargs)
73  {
74  m.def(name,
75  &apply_algo<Algo, Args...>,
76  pybind11::arg("ex"),
77  std::forward<PyArgs>(pyargs)...,
78  pybind11::arg("deep") = deep,
79  pybind11::arg("repeat") = repeat,
80  pybind11::arg("depth") = depth,
81  pybind11::doc(read_manual("algorithms", name).c_str()),
82  pybind11::return_value_policy::reference_internal);
83  }
84 
85  template <class Algo>
86  Ex_ptr apply_algo_preorder(Ex_ptr ex, bool deep, bool repeat, unsigned int depth)
87  {
88  Algo algo(*get_kernel_from_scope(), *ex);
89  return apply_algo_base(algo, ex, deep, repeat, depth, true);
90  }
91 
92  template <class Algo, typename Arg1>
93  Ex_ptr apply_algo_preorder(Ex_ptr ex, Arg1 arg1, bool deep, bool repeat, unsigned int depth)
94  {
95  Algo algo(*get_kernel_from_scope(), *ex, arg1);
96  return apply_algo_base(algo, ex, deep, repeat, depth, true);
97  }
98 
99  template <class Algo, typename Arg1, typename Arg2>
100  Ex_ptr apply_algo_preorder(Ex_ptr ex, Arg1 arg1, Arg2 arg2, bool deep, bool repeat, unsigned int depth)
101  {
102  Algo algo(*get_kernel_from_scope(), *ex, arg1, arg2);
103  return apply_algo_base(algo, ex, deep, repeat, depth, true);
104  }
105 
113  template<class Algo, typename... Args, typename... PyArgs>
114  void def_algo_preorder(pybind11::module& m, const char* name, bool deep, bool repeat, unsigned int depth, PyArgs... pyargs)
115  {
116  m.def(name,
117  &apply_algo_preorder<Algo, Args...>,
118  pybind11::arg("ex"),
119  std::forward<PyArgs>(pyargs)...,
120  pybind11::arg("deep") = deep,
121  pybind11::arg("repeat") = repeat,
122  pybind11::arg("depth") = depth,
123  pybind11::doc(read_manual("algorithms", name).c_str()),
124  pybind11::return_value_policy::reference_internal);
125  }
126 
127 
128  void init_algorithms(pybind11::module& m);
129  }
Object keeping track of time spent in nested execution blocks, and keeping track of out-of-band messa...
Definition: ProgressMonitor.hh:17
void def_algo(pybind11::module &m, const char *name, bool deep, bool repeat, unsigned int depth, PyArgs... pyargs)
Method to declare a Python function with variable number of arguments, and make that call a C++ algor...
Definition: py_algorithms.hh:72
Kernel * get_kernel_from_scope()
Get a pointer to the currently visible kernel.
Definition: py_kernel.cc:41
void def_algo_preorder(pybind11::module &m, const char *name, bool deep, bool repeat, unsigned int depth, PyArgs... pyargs)
Method to declare a Python function with variable number of arguments, and make that call a C++ algor...
Definition: py_algorithms.hh:114
Ex_ptr apply_algo_base(Algo &algo, Ex_ptr ex, bool deep, bool repeat, unsigned int depth, bool pre_order=false)
Generic internal entry point for the Python side to execute a C++ algorithm.
Definition: py_algorithms.hh:18
Functions to handle the exchange properties of two or more symbols in a product.
Definition: Adjform.cc:83
Ex_ptr apply_algo(Ex_ptr ex, bool deep, bool repeat, unsigned int depth)
Definition: py_algorithms.hh:35
ProgressMonitor * get_progress_monitor()
Definition: py_progress.cc:18
void call_post_process(Kernel &kernel, Ex_ptr ex)
Definition: py_ex.cc:567
void init_algorithms(py::module &m)
Definition: py_algorithms.cc:74
std::string read_manual(const char *category, const char *name)
Definition: py_helpers.cc:30
std::shared_ptr< Ex > Ex_ptr
Definition: py_ex.hh:9
Ex_ptr apply_algo_preorder(Ex_ptr ex, bool deep, bool repeat, unsigned int depth)
Definition: py_algorithms.hh:86