CXXR (C++ R) API
ComplexVector.h
Go to the documentation of this file.
1 /*CXXR $Id: ComplexVector.h 1378 2013-04-26 13:39:20Z arr $
2  *CXXR
3  *CXXR This file is part of CXXR, a project to refactor the R interpreter
4  *CXXR into C++. It may consist in whole or in part of program code and
5  *CXXR documentation taken from the R project itself, incorporated into
6  *CXXR CXXR (and possibly MODIFIED) under the terms of the GNU General Public
7  *CXXR Licence.
8  *CXXR
9  *CXXR CXXR is Copyright (C) 2008-13 Andrew R. Runnalls, subject to such other
10  *CXXR copyrights and copyright restrictions as may be stated below.
11  *CXXR
12  *CXXR CXXR is not part of the R project, and bugs and other issues should
13  *CXXR not be reported via r-bugs or other R project channels; instead refer
14  *CXXR to the CXXR website.
15  *CXXR */
16 
17 /*
18  * R : A Computer Language for Statistical Data Analysis
19  * Copyright (C) 1995, 1996 Robert Gentleman and Ross Ihaka
20  * Copyright (C) 1999-2006 The R Development Core Team.
21  * Andrew Runnalls (C) 2008
22  *
23  * This program is free software; you can redistribute it and/or modify
24  * it under the terms of the GNU General Public License as published by
25  * the Free Software Foundation; either version 2.1 of the License, or
26  * (at your option) any later version.
27  *
28  * This program is distributed in the hope that it will be useful,
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31  * GNU Lesser General Public License for more details.
32  *
33  * You should have received a copy of the GNU General Public License
34  * along with this program; if not, a copy is available at
35  * http://www.r-project.org/Licenses/
36  */
37 
42 #ifndef COMPLEXVECTOR_H
43 #define COMPLEXVECTOR_H
44 
45 #include "CXXR/VectorBase.h"
46 #include "R_ext/Complex.h"
47 
48 #ifdef __cplusplus
49 
50 #include "R_ext/Arith.h"
51 #include "CXXR/FixedVector.hpp"
52 #include "CXXR/SEXP_downcast.hpp"
53 
54 namespace CXXR {
63  struct Complex : public Rcomplex {
69  {}
70 
77  Complex(double rl, double im = 0.0)
78  {
79  r = rl;
80  i = im;
81  }
82 
89  Complex& operator=(double rhs)
90  {
91  r = rhs;
92  i = 0;
93  return *this;
94  }
95 
96  template <class Archive>
97  void serialize(Archive& ar, const unsigned int version)
98  {
99  ar & BOOST_SERIALIZATION_NVP(r);
100  ar & BOOST_SERIALIZATION_NVP(i);
101  }
102  };
103 
104  // Template specializations:
105  namespace ElementTraits {
106  template <>
107  struct NAFunc<Complex> {
108  const Complex& operator()() const
109  {
110  static Complex na(NA_REAL, NA_REAL);
111  return na;
112  }
113  };
114  }
115 
116  template <>
118  {
119  return "complex";
120  }
121 
125 } // namespace CXXR
126 
127 BOOST_CLASS_EXPORT_KEY(CXXR::ComplexVector)
128 
129 extern "C" {
130 #endif /* __cplusplus */
131 
136 #ifndef __cplusplus
137  Rboolean Rf_isComplex(SEXP s);
138 #else
139  inline Rboolean Rf_isComplex(SEXP s)
140  {
141  return Rboolean(s && TYPEOF(s) == CPLXSXP);
142  }
143 #endif
144 
152 #ifndef __cplusplus
153 Rcomplex *COMPLEX(SEXP x);
154 #else
155 inline Rcomplex *COMPLEX(SEXP x)
156 {
157  using namespace CXXR;
158  return &(*SEXP_downcast<ComplexVector*>(x, false))[0];
159 }
160 #endif
161 
162 #ifdef __cplusplus
163 }
164 #endif
165 
166 #endif /* COMPLEXVECTOR_H */