Cadabra
Computer algebra system for field theory problems
PythonCdb.hh
Go to the documentation of this file.
1 /*
2 
3  Cadabra: a field-theory motivated computer algebra system.
4  Copyright (C) 2001-2014 Kasper Peeters <kasper.peeters@phi-sci.com>
5 
6  This program is free software: you can redistribute it and/or
7  modify it under the terms of the GNU General Public License as
8  published by the Free Software Foundation, either version 3 of the
9  License, or (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 
19 */
20 
21 #pragma once
22 
23 #include <cmath>
24 #include <pybind11/pybind11.h>
25 #include <stdexcept>
26 #include "Storage.hh"
27 #include "Kernel.hh"
28 #include "Algorithm.hh"
29 
30 
31 
37 
38 bool __eq__Ex_Ex(std::shared_ptr<cadabra::Ex>, std::shared_ptr<cadabra::Ex>);
39 
43 
44 bool __eq__Ex_int(std::shared_ptr<cadabra::Ex>, int);
45 
47 
48 std::shared_ptr<cadabra::Ex> fetch_from_python(const std::string& nm);
49 
53 
54 std::string Ex_str_(std::shared_ptr<cadabra::Ex>);
55 std::string Ex_repr_(std::shared_ptr<cadabra::Ex>);
56 
68 
69 std::string Ex_latex_(std::shared_ptr<cadabra::Ex>);
70 
80 
81 pybind11::object Ex_to_Sympy(std::shared_ptr<cadabra::Ex>);
82 
85 
86 std::string Ex_to_Sympy_string(std::shared_ptr<cadabra::Ex>);
87 
88 
92 
93 cadabra::Ex operator+(std::shared_ptr<cadabra::Ex> ex1, std::shared_ptr<cadabra::Ex> ex2);
94 
98 
99 cadabra::Ex operator-(std::shared_ptr<cadabra::Ex> ex1, std::shared_ptr<cadabra::Ex> ex2);
100 
105 
106 class BaseProperty {
107 };
108 
134 
135 template<class T>
136 class Property : public std::enable_shared_from_this<Property<T>>, public BaseProperty {
137  public:
138  Property(std::shared_ptr<cadabra::Ex> obj, std::shared_ptr<cadabra::Ex> params=0);
139 
141  std::string str_() const;
142 
144  std::string latex_() const;
145 
147  std::string repr_() const;
148 
149 
150  private:
151  // We keep a pointer to the C++ property, so it is possible to
152  // query properties using the Python interface. However, this C++
153  // object is owned by the C++ kernel and does not get destroyed
154  // when the Python object goes out of scope.
155 
156  // When the Python object survives the local scope, results are
157  // undefined.
158  T *prop;
159 
160  // We also keep a shared pointer to the expression for which we
161  // have defined this property, so that we can print sensible
162  // information.
163  std::shared_ptr<cadabra::Ex> for_obj;
164 };
165 
166 
228 
232 
240 
242 
247 
251 
252 void call_post_process(cadabra::Kernel&, std::shared_ptr<cadabra::Ex> ex);
253 
254 std::string replace_all(std::string, const std::string& old, const std::string& nw);
cadabra::Kernel * create_scope_from_global()
Definition: PythonCdb.cc:917
void call_post_process(cadabra::Kernel &, std::shared_ptr< cadabra::Ex > ex)
Run the post-process Python function (if defined) on the given expression.
Definition: PythonCdb.cc:1105
cadabra::Ex operator-(std::shared_ptr< cadabra::Ex > ex1, std::shared_ptr< cadabra::Ex > ex2)
Subtract two expressions, adding a top-level node if required.
Basic storage class for symbolic mathemematical expressions.
Definition: Storage.hh:139
std::string Ex_repr_(std::shared_ptr< cadabra::Ex >)
Definition: PythonCdb.cc:542
T * prop
Definition: PythonCdb.hh:158
cadabra::Kernel * create_empty_scope()
Definition: PythonCdb.cc:924
cadabra::Kernel * create_scope()
Setup of kernels in current scope, callable from Python.
Definition: PythonCdb.cc:910
std::string Ex_str_(std::shared_ptr< cadabra::Ex >)
Generate the Python str() and repr() representation of the Ex object.
Definition: PythonCdb.cc:505
cadabra::Kernel * get_kernel_from_scope()
Get a pointer to the currently visible kernel.
Definition: PythonCdb.cc:866
std::shared_ptr< cadabra::Ex > fetch_from_python(const std::string &nm)
Fetch an Ex object from the Python side using its Python identifier.
Definition: PythonCdb.cc:613
bool __eq__Ex_int(std::shared_ptr< cadabra::Ex >, int)
Comparison operator for Ex objects in Python.
Definition: PythonCdb.cc:651
Helper class to ensure that all Python property objects derive from the same base class...
Definition: PythonCdb.hh:106
std::string str_() const
Human-readable form in text, i.e. no special formatting.
Definition: PythonCdb.cc:1010
std::string repr_() const
Python-parseable form. FIXME: not correct right now.
Definition: PythonCdb.cc:1047
Property(std::shared_ptr< cadabra::Ex > obj, std::shared_ptr< cadabra::Ex > params=0)
Definition: PythonCdb.cc:1000
cadabra::Ex operator+(std::shared_ptr< cadabra::Ex > ex1, std::shared_ptr< cadabra::Ex > ex2)
Add two expressions, adding a top-level node if required.
std::string latex_() const
Human-readable form using LaTeX markup.
Definition: PythonCdb.cc:1020
Property is a templated wrapper around a C++ property object.
Definition: PythonCdb.hh:136
std::shared_ptr< cadabra::Ex > for_obj
Definition: PythonCdb.hh:163
bool __eq__Ex_Ex(std::shared_ptr< cadabra::Ex >, std::shared_ptr< cadabra::Ex >)
Comparison operator for Ex objects in Python.
Definition: PythonCdb.cc:646
std::string replace_all(std::string, const std::string &old, const std::string &nw)
Replace all occurrences of a substring in the original string.
Definition: PythonCdb.cc:1687
std::string Ex_latex_(std::shared_ptr< cadabra::Ex >)
The Python &#39;print&#39; function always calls the &#39;str&#39; member on objects to be printed.
Definition: PythonCdb.cc:533
Definition: Kernel.hh:14
void inject_defaults(cadabra::Kernel *)
Inject properties directly into the Kernel, even if the kernel is not yet on the Python stack (needed...
Definition: PythonCdb.cc:930
std::string Ex_to_Sympy_string(std::shared_ptr< cadabra::Ex >)
Similar to Ex_to_Sympy, but only producing a string which can be parsed by Sympy, instead of a full-f...
Definition: PythonCdb.cc:553
pybind11::object Ex_to_Sympy(std::shared_ptr< cadabra::Ex >)
Outputs a Cadabra &#39;Ex&#39; as a Sympy expression.
Definition: PythonCdb.cc:570