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
23
24#include <c2py/c2py.hpp>
25#include <Python.h>
26
27#include <complex>
28#include <sstream>
29#include <string>
30
31namespace c2py {
32
33 template <> struct py_converter<triqs::utility::real_or_complex> {
34 using c_t = triqs::utility::real_or_complex;
35 using conv_d_t = py_converter<double>;
36 using conv_c_t = py_converter<std::complex<double>>;
37
38 static std::string tp_name() {
39 std::ostringstream out;
40 out << ::c2py::python_typename<double>() << " | " << ::c2py::python_typename<std::complex<double>>();
41 return out.str();
42 }
43
44 static PyObject *c2py(c_t const &x) {
45 if (x.is_real()) return conv_d_t::c2py(double(x));
46 return conv_c_t::c2py(std::complex<double>(x));
47 }
48
49 static bool is_convertible(PyObject *ob, bool raise_exception) {
50 bool ok = conv_d_t::is_convertible(ob, false) || conv_c_t::is_convertible(ob, false);
51 if (ok) return true;
52 if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert to real_or_complex"); }
53 return false;
54 }
55
56 static c_t py2c(PyObject *ob) {
57 if (conv_d_t::is_convertible(ob, false)) return conv_d_t::py2c(ob);
58 return conv_c_t::py2c(ob);
59 }
60 };
61
62} // namespace c2py
Additional converters for gf.
Provides a type that decides at runtime whether it is real or complex.