| 1 |
#ifndef RBIMPL_INTERN_COMPLEX_H /*-*-C++-*-vi:se ft=cpp:*/ |
| 2 |
#define RBIMPL_INTERN_COMPLEX_H |
| 3 |
/** |
| 4 |
* @file |
| 5 |
* @author Ruby developers <[email protected]> |
| 6 |
* @copyright This file is a part of the programming language Ruby. |
| 7 |
* Permission is hereby granted, to either redistribute and/or |
| 8 |
* modify this file, provided that the conditions mentioned in the |
| 9 |
* file COPYING are met. Consult the file for details. |
| 10 |
* @warning Symbols prefixed with either `RBIMPL` or `rbimpl` are |
| 11 |
* implementation details. Don't take them as canon. They could |
| 12 |
* rapidly appear then vanish. The name (path) of this header file |
| 13 |
* is also an implementation detail. Do not expect it to persist |
| 14 |
* at the place it is now. Developers are free to move it anywhere |
| 15 |
* anytime at will. |
| 16 |
* @note To ruby-core: remember that this header can be possibly |
| 17 |
* recursively included from extension libraries written in C++. |
| 18 |
* Do not expect for instance `__VA_ARGS__` is always available. |
| 19 |
* We assume C99 for ruby itself but we don't assume languages of |
| 20 |
* extension libraries. They could be written in C++98. |
| 21 |
* @brief Public APIs related to ::rb_cComplex. |
| 22 |
*/ |
| 23 |
#include "ruby/internal/attr/deprecated.h" |
| 24 |
#include "ruby/internal/attr/pure.h" |
| 25 |
#include "ruby/internal/dllexport.h" |
| 26 |
#include "ruby/internal/value.h" |
| 27 |
#include "ruby/internal/arithmetic/long.h" /* INT2FIX is here. */ |
| 28 |
|
| 29 |
RBIMPL_SYMBOL_EXPORT_BEGIN() |
| 30 |
|
| 31 |
/* complex.c */ |
| 32 |
|
| 33 |
/** |
| 34 |
* Identical to rb_complex_new(), except it assumes both arguments are not |
| 35 |
* instances of ::rb_cComplex. It is thus dangerous for extension libraries. |
| 36 |
* |
| 37 |
* @param[in] real Real part, in any numeric except Complex. |
| 38 |
* @param[in] imag Imaginary part, in any numeric except Complex. |
| 39 |
* @return An instance of ::rb_cComplex whose value is `real + (imag)i`. |
| 40 |
*/ |
| 41 |
VALUE rb_complex_raw(VALUE real, VALUE imag); |
| 42 |
|
| 43 |
/** |
| 44 |
* Shorthand of `x+0i`. It practically converts `x` into a Complex of the |
| 45 |
* identical value. |
| 46 |
* |
| 47 |
* @param[in] x Any numeric except Complex. |
| 48 |
* @return An instance of ::rb_cComplex, whose value is `x + 0i`. |
| 49 |
*/ |
| 50 |
#define rb_complex_raw1(x) rb_complex_raw((x), INT2FIX(0)) |
| 51 |
|
| 52 |
/** @alias{rb_complex_raw} */ |
| 53 |
#define rb_complex_raw2(x,y) rb_complex_raw((x), (y)) |
| 54 |
|
| 55 |
/** |
| 56 |
* Constructs a Complex, by first multiplying the imaginary part with `1i` then |
| 57 |
* adds it to the real part. This definition doesn't need both arguments be |
| 58 |
* real numbers. It can happily combine two instances of ::rb_cComplex (with |
| 59 |
* rotating the latter one). |
| 60 |
* |
| 61 |
* @param[in] real An instance of ::rb_cNumeric. |
| 62 |
* @param[in] imag Another instance of ::rb_cNumeric. |
| 63 |
* @return An instance of ::rb_cComplex whose value is `imag * 1i + real`. |
| 64 |
*/ |
| 65 |
VALUE rb_complex_new(VALUE real, VALUE imag); |
| 66 |
|
| 67 |
/** |
| 68 |
* Shorthand of `x+0i`. It practically converts `x` into a Complex of the |
| 69 |
* identical value. |
| 70 |
* |
| 71 |
* @param[in] x Any numeric value. |
| 72 |
* @return An instance of ::rb_cComplex, whose value is `x + 0i`. |
| 73 |
*/ |
| 74 |
#define rb_complex_new1(x) rb_complex_new((x), INT2FIX(0)) |
| 75 |
|
| 76 |
/** @alias{rb_complex_new} */ |
| 77 |
#define rb_complex_new2(x,y) rb_complex_new((x), (y)) |
| 78 |
|
| 79 |
/** |
| 80 |
* Constructs a Complex using polar representations. Unlike rb_complex_new() |
| 81 |
* it makes no sense to pass non-real instances to this function. |
| 82 |
* |
| 83 |
* @param[in] abs Magnitude, in any numeric except Complex. |
| 84 |
* @param[in] arg Angle, in radians, in any numeric except Complex. |
| 85 |
* @return An instance of ::rb_cComplex which denotes the given polar |
| 86 |
* coordinates. |
| 87 |
*/ |
| 88 |
VALUE rb_complex_new_polar(VALUE abs, VALUE arg); |
| 89 |
|
| 90 |
RBIMPL_ATTR_DEPRECATED(("by: rb_complex_new_polar")) |
| 91 |
/** @old{rb_complex_new_polar} */ |
| 92 |
VALUE rb_complex_polar(VALUE abs, VALUE arg); |
| 93 |
|
| 94 |
RBIMPL_ATTR_PURE() |
| 95 |
/** |
| 96 |
* Queries the real part of the passed Complex. |
| 97 |
* |
| 98 |
* @param[in] z An instance of ::rb_cComplex. |
| 99 |
* @return Its real part, which is an instance of ::rb_cNumeric. |
| 100 |
*/ |
| 101 |
VALUE rb_complex_real(VALUE z); |
| 102 |
|
| 103 |
RBIMPL_ATTR_PURE() |
| 104 |
/** |
| 105 |
* Queries the imaginary part of the passed Complex. |
| 106 |
* |
| 107 |
* @param[in] z An instance of ::rb_cComplex. |
| 108 |
* @return Its imaginary part, which is an instance of ::rb_cNumeric. |
| 109 |
*/ |
| 110 |
VALUE rb_complex_imag(VALUE z); |
| 111 |
|
| 112 |
/** |
| 113 |
* Performs addition of the passed two objects. |
| 114 |
* |
| 115 |
* @param[in] x An instance of ::rb_cComplex. |
| 116 |
* @param[in] y Arbitrary ruby object. |
| 117 |
* @return What `x + y` evaluates to. |
| 118 |
* @see rb_num_coerce_bin() |
| 119 |
*/ |
| 120 |
VALUE rb_complex_plus(VALUE x, VALUE y); |
| 121 |
|
| 122 |
/** |
| 123 |
* Performs subtraction of the passed two objects. |
| 124 |
* |
| 125 |
* @param[in] x An instance of ::rb_cComplex. |
| 126 |
* @param[in] y Arbitrary ruby object. |
| 127 |
* @return What `x - y` evaluates to. |
| 128 |
* @see rb_num_coerce_bin() |
| 129 |
*/ |
| 130 |
VALUE rb_complex_minus(VALUE x, VALUE y); |
| 131 |
|
| 132 |
/** |
| 133 |
* Performs multiplication of the passed two objects. |
| 134 |
* |
| 135 |
* @param[in] x An instance of ::rb_cComplex. |
| 136 |
* @param[in] y Arbitrary ruby object. |
| 137 |
* @return What `x * y` evaluates to. |
| 138 |
* @see rb_num_coerce_bin() |
| 139 |
*/ |
| 140 |
VALUE rb_complex_mul(VALUE x, VALUE y); |
| 141 |
|
| 142 |
/** |
| 143 |
* Performs division of the passed two objects. |
| 144 |
* |
| 145 |
* @param[in] x An instance of ::rb_cComplex. |
| 146 |
* @param[in] y Arbitrary ruby object. |
| 147 |
* @return What `x / y` evaluates to. |
| 148 |
* @see rb_num_coerce_bin() |
| 149 |
*/ |
| 150 |
VALUE rb_complex_div(VALUE x, VALUE y); |
| 151 |
|
| 152 |
/** |
| 153 |
* Performs negation of the passed object. |
| 154 |
* |
| 155 |
* @param[in] z An instance of ::rb_cComplex. |
| 156 |
* @return What `-z` evaluates to. |
| 157 |
*/ |
| 158 |
VALUE rb_complex_uminus(VALUE z); |
| 159 |
|
| 160 |
/** |
| 161 |
* Performs complex conjugation of the passed object. |
| 162 |
* |
| 163 |
* @param[in] z An instance of ::rb_cComplex. |
| 164 |
* @return Its complex conjugate, in ::rb_cComplex. |
| 165 |
*/ |
| 166 |
VALUE rb_complex_conjugate(VALUE z); |
| 167 |
|
| 168 |
/** |
| 169 |
* Queries the absolute (or the magnitude) of the passed object. |
| 170 |
* |
| 171 |
* @param[in] z An instance of ::rb_cComplex. |
| 172 |
* @return Its magnitude, in ::rb_cFloat. |
| 173 |
*/ |
| 174 |
VALUE rb_complex_abs(VALUE z); |
| 175 |
|
| 176 |
/** |
| 177 |
* Queries the argument (or the angle) of the passed object. |
| 178 |
* |
| 179 |
* @param[in] z An instance of ::rb_cComplex. |
| 180 |
* @return Its magnitude, in ::rb_cFloat. |
| 181 |
*/ |
| 182 |
VALUE rb_complex_arg(VALUE z); |
| 183 |
|
| 184 |
/** |
| 185 |
* Performs exponentiation of the passed two objects. |
| 186 |
* |
| 187 |
* @param[in] base An instance of ::rb_cComplex. |
| 188 |
* @param[in] exp Arbitrary ruby object. |
| 189 |
* @return What `base ** exp` evaluates to. |
| 190 |
* @see rb_num_coerce_bin() |
| 191 |
*/ |
| 192 |
VALUE rb_complex_pow(VALUE base, VALUE exp); |
| 193 |
|
| 194 |
/** |
| 195 |
* Identical to rb_complex_new(), except it takes the arguments as C's double |
| 196 |
* instead of Ruby's object. |
| 197 |
* |
| 198 |
* @param[in] real Real part. |
| 199 |
* @param[in] imag Imaginary part. |
| 200 |
* @return An instance of ::rb_cComplex whose value is `real + (imag)i`. |
| 201 |
*/ |
| 202 |
VALUE rb_dbl_complex_new(double real, double imag); |
| 203 |
|
| 204 |
/** @alias{rb_complex_plus} */ |
| 205 |
#define rb_complex_add rb_complex_plus |
| 206 |
|
| 207 |
/** @alias{rb_complex_minus} */ |
| 208 |
#define rb_complex_sub rb_complex_minus |
| 209 |
|
| 210 |
/** @alias{rb_complex_uminus} */ |
| 211 |
#define rb_complex_nagate rb_complex_uminus |
| 212 |
|
| 213 |
/** |
| 214 |
* Converts various values into a Complex. This function accepts: |
| 215 |
* |
| 216 |
* - Instances of ::rb_cComplex (taken as-is), |
| 217 |
* - Instances of ::rb_cNumeric (adds `0i`), |
| 218 |
* - Instances of ::rb_cString (parses), |
| 219 |
* - Other objects that respond to `#to_c`. |
| 220 |
* |
| 221 |
* It (possibly recursively) applies `#to_c` until both sides become a Complex |
| 222 |
* value, then computes `imag * 1i + real`. |
| 223 |
* |
| 224 |
* As a special case, passing ::RUBY_Qundef to `imag` is the same as passing |
| 225 |
* `RB_INT2NUM(0)`. |
| 226 |
* |
| 227 |
* @param[in] real Real part (see above). |
| 228 |
* @param[in] imag Imaginary part (see above). |
| 229 |
* @exception rb_eTypeError Passed something not described above. |
| 230 |
* @return An instance of ::rb_cComplex whose value is `1i * imag + real`. |
| 231 |
* |
| 232 |
* @internal |
| 233 |
* |
| 234 |
* This was the implementation of `Kernel#Complex` before, but they diverged. |
| 235 |
*/ |
| 236 |
VALUE rb_Complex(VALUE real, VALUE imag); |
| 237 |
|
| 238 |
/** |
| 239 |
* Shorthand of `x+0i`. It practically converts `x` into a Complex of the |
| 240 |
* identical value. |
| 241 |
* |
| 242 |
* @param[in] x ::rb_cNumeric, ::rb_cString, or something that responds to |
| 243 |
* `#to_c`. |
| 244 |
* @return An instance of ::rb_cComplex, whose value is `x + 0i`. |
| 245 |
*/ |
| 246 |
#define rb_Complex1(x) rb_Complex((x), INT2FIX(0)) |
| 247 |
|
| 248 |
/** @alias{rb_Complex} */ |
| 249 |
#define rb_Complex2(x,y) rb_Complex((x), (y)) |
| 250 |
|
| 251 |
RBIMPL_SYMBOL_EXPORT_END() |
| 252 |
|
| 253 |
#endif /* RBIMPL_INTERN_COMPLEX_H */ |