TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
real_or_complex.hpp
1// Copyright (c) 2017-2018 Commissariat à l'énergie atomique et aux énergies alternatives (CEA)
2// Copyright (c) 2017-2018 Centre national de la recherche scientifique (CNRS)
3// Copyright (c) 2018 Simons Foundation
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You may obtain a copy of the License at
16// https://www.gnu.org/licenses/gpl-3.0.txt
17//
18// Authors: Olivier Parcollet, Nils Wentzell
19
20#pragma once
21
22#include <cpp2py/cpp2py.hpp>
23
24namespace cpp2py {
25
26 template <> struct py_converter<triqs::utility::real_or_complex> {
27 using c_t = triqs::utility::real_or_complex;
28 using conv_d_t = py_converter<double>;
29 using conv_c_t = py_converter<std::complex<double>>;
30
31#ifdef C2PY_INCLUDED
32 static std::string tp_name() {
33 std::ostringstream out;
34 out << ::c2py::python_typename<double>() << " | " << ::c2py::python_typename<std::complex<double>>();
35 return out.str();
36 }
37#endif
38
39 static PyObject *c2py(c_t const &x) {
40 if (x.is_real()) return conv_d_t::c2py(double(x));
41 return conv_c_t::c2py(std::complex<double>(x));
42 }
43
44 static bool is_convertible(PyObject *ob, bool raise_exception) {
45 bool ok = conv_d_t::is_convertible(ob, false) || conv_c_t::is_convertible(ob, false);
46 if (ok) return true;
47 if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert to real_or_complex"); }
48 return false;
49 }
50
51 static c_t py2c(PyObject *ob) {
52 if (conv_d_t::is_convertible(ob, false)) return conv_d_t::py2c(ob);
53 return conv_c_t::py2c(ob);
54 }
55 };
56} // namespace cpp2py
Additional converters for gf.