REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
TRestComplex.cxx
1 /******************** REST disclaimer ***********************************
2  * This file is part of the REST software framework. *
3  * *
4  * Copyright (C) 2016 GIFNA/TREX (University of Zaragoza) *
5  * For more information see http://gifna.unizar.es/trex *
6  * *
7  * REST is free software: you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation, either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * REST is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have a copy of the GNU General Public License along with *
18  * REST in $REST_PATH/LICENSE. *
19  * If not, see http://www.gnu.org/licenses/. *
20  * For the list of contributors see $REST_PATH/CREDITS. *
21  *************************************************************************/
22 
66 
67 #include "TRestComplex.h"
68 
69 #include <iostream>
70 #include <sstream>
71 
72 ClassImp(TRestComplex);
73 
76 
77 TRestComplex::TRestComplex(mpfr::mpreal re, mpfr::mpreal im, Bool_t polar) : fRe(re), fIm(im) {
78  if (polar) {
79  if (re < 0) {
80  re = -re;
81  }
82  fRe = re * mpfr::cos(im);
83  fIm = re * mpfr::sin(im);
84  }
85 }
86 
88 
89 std::ostream& operator<<(std::ostream& out, const TRestComplex& c) {
90  int pr = out.precision();
91 
92  int realExponent = (int)mpfr::abs(mpfr::log(mpfr::abs(c.fRe)) / mpfr::log(10));
93  int complexExponent = (int)mpfr::abs(mpfr::log(mpfr::abs(c.fIm)) / mpfr::log(10));
94 
95  out.precision(mpfr::bits2digits(mpfr::mpreal::get_default_prec()) - realExponent);
96  out << "(" << c.fRe;
97  out.precision(mpfr::bits2digits(mpfr::mpreal::get_default_prec()) - complexExponent);
98  out << "," << c.fIm << "i)";
99  out.precision(pr);
100  return out;
101 }
102 
104 
105 std::istream& operator>>(std::istream& in, TRestComplex& c) {
106  in >> c.fRe >> c.fIm;
107  return in;
108 }
109 
110 namespace cling {
111 std::string printValue(TRestComplex* c) {
112  std::stringstream s;
113  s << *c;
114  return s.str();
115 }
116 } // namespace cling
A generic class to handle complex numbers with real precision.
Definition: TRestComplex.h:33
mpfr::mpreal fIm
The imaginary part of the complex number using MPFR precision.
Definition: TRestComplex.h:38
mpfr::mpreal fRe
The real part of the complex number using MPFR precision.
Definition: TRestComplex.h:36