[14981e]: / src / ov-mp.cc  Maximize  Restore  History

Download this file

245 lines (213 with data), 7.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
Copyright (C) 2014 Pascal Dupuis <cdemills@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; If not, see <http://www.gnu.org/licenses/>.
*/
#include <octave/config.h>
#include <octave/oct.h>
#include <octave/ops.h>
#include "ov-mp.h"
octave_mp::octave_mp(void)
:octave_base_value(), prec(mpfr_get_default_prec ()), rnd(mpfr_get_default_rounding_mode ())
{
mpfr_init_set_ui (scalar, 0, rnd);
}
octave_mp::octave_mp (signed long si, mpfr_prec_t sprec, mp_rnd_t srnd)
:octave_base_value(), prec(sprec), rnd(srnd)
{
mpfr_init_set_si (scalar, si, rnd);
}
octave_mp::octave_mp (unsigned long ui, mpfr_prec_t sprec, mp_rnd_t srnd)
:octave_base_value(), prec(sprec), rnd(srnd)
{
mpfr_init_set_ui (scalar, ui, rnd);
}
octave_mp::octave_mp (const octave_mp& s)
:octave_base_value(), prec(s.prec), rnd(s.rnd)
{
mpfr_init_set(scalar, s.scalar, rnd);
}
octave_mp::octave_mp (const double& s, mpfr_prec_t sprec, mp_rnd_t srnd)
:octave_base_value(), prec(sprec), rnd(srnd)
{
mpfr_init_set_d(scalar, s, rnd);
}
octave_mp::octave_mp (const std::string& s, mpfr_prec_t sprec, mp_rnd_t srnd)
:octave_base_value(), prec(sprec), rnd(srnd)
{
mpfr_init_set_ui (scalar, 0, rnd);
if (0 == s.compare("pi"))
{
(void) mpfr_const_pi (scalar, rnd);
}
else
{
if (0 == s.compare("log2"))
(void) mpfr_const_log2 (scalar, rnd);
else
(void) mpfr_set_str (scalar, s.c_str(), 0, rnd);
}
}
void
octave_mp::print (std::ostream& os, bool pr_as_read_syntax) const
{
/* This code is extracted from mpfrc++ */
char format[128];
int digits;
char *s = NULL;
digits = prec /* static_cast<int>(os.precision ()) */ ;
sprintf(format, "%%.%dRNg", digits);
if (!(mpfr_asprintf (&s,format, scalar) < 0))
{
os << std::string(s) << " with prec " << prec;
mpfr_free_str(s);
}
}
octave_value
octave_mp::oct_binop_proxy (const octave_base_value& a1, const octave_base_value& a2,
int (*func) (mpfr_ptr rop, mpfr_srcptr op1, mpfr_srcptr op2, mpfr_rnd_t rnd))
{
const octave_mp *arg1, *arg2;
octave_mp *retval;
octave_stdout << "first arg of type " << a1.static_type_id () << "\n";
octave_stdout << "second arg of type " << a2.static_type_id () << "\n";
if (a1.static_type_id() == octave_mp::static_type_id ())
{ /* a1 has the right type */
arg1 = reinterpret_cast<const octave_mp*>(&a1);
retval = new octave_mp(static_cast<long unsigned int>(0),
arg1->prec, arg1->rnd);
if (a2.static_type_id() == retval->static_type_id())
{
arg2 = reinterpret_cast<const octave_mp*>(&a2);
} else {
arg2 = new octave_mp(a2.double_value (), arg1->prec,
arg1->rnd);
}
}
else
{
arg2 = reinterpret_cast<const octave_mp*>(&a2);
arg1 = new octave_mp(a1.double_value (), arg2->prec, arg2->rnd);
}
(void) func(retval->scalar, arg1->scalar, arg2->scalar, arg1->rnd);
return octave_value (retval);
}
octave_value
octave_mp::map (unary_mapper_t umap) const
{
switch (umap)
{
case umap_imag:
return 0.0;
case umap_real:
case umap_conj:
return this->clone ();
/*
This is as mapping func must be developped
case umap_abs:
{
octave_mp* retval = new octave_mp(static_cast<long unsigned int>(0), prec, rnd);
(void) mpfr_abs(retval->scalar, scalar, rnd);
return octave_value (retval);
}
*/
#define SCALAR_MAPPER(UMAP,FCN) \
case (umap_ ## UMAP): { \
octave_mp* retval = new octave_mp(static_cast<long unsigned int>(0), prec, rnd); \
(void) mpfr_ ## FCN (retval->scalar, scalar, rnd); \
return octave_value (retval); \
}
SCALAR_MAPPER (log, log);
SCALAR_MAPPER (log2, log2);
SCALAR_MAPPER (log10, log10);
SCALAR_MAPPER (exp, exp);
SCALAR_MAPPER (cos, cos);
SCALAR_MAPPER (sin, sin);
SCALAR_MAPPER (tan, tan);
SCALAR_MAPPER (acos, acos);
SCALAR_MAPPER (asin, asin);
SCALAR_MAPPER (atan, atan);
SCALAR_MAPPER (cosh, cosh);
SCALAR_MAPPER (sinh, sinh);
SCALAR_MAPPER (tanh, tanh);
SCALAR_MAPPER (acosh, acosh);
SCALAR_MAPPER (asinh, asinh);
SCALAR_MAPPER (atanh, atanh);
SCALAR_MAPPER (abs, abs);
SCALAR_MAPPER (sqrt, sqrt);
/*
SCALAR_MAPPER (acos, rc_acos);
SCALAR_MAPPER (acosh, rc_acosh);
SCALAR_MAPPER (angle, ::arg);
SCALAR_MAPPER (arg, ::arg);
SCALAR_MAPPER (asin, rc_asin);
SCALAR_MAPPER (asinh, ::asinh);
SCALAR_MAPPER (atan, ::atan);
SCALAR_MAPPER (atanh, rc_atanh);
SCALAR_MAPPER (erf, ::erf);
SCALAR_MAPPER (erfinv, ::erfinv);
SCALAR_MAPPER (erfcinv, ::erfcinv);
SCALAR_MAPPER (erfc, ::erfc);
SCALAR_MAPPER (erfcx, ::erfcx);
SCALAR_MAPPER (erfi, ::erfi);
SCALAR_MAPPER (dawson, ::dawson);
SCALAR_MAPPER (gamma, xgamma);
SCALAR_MAPPER (lgamma, rc_lgamma);
SCALAR_MAPPER (cbrt, ::cbrt);
SCALAR_MAPPER (ceil, ::ceil);
SCALAR_MAPPER (cos, ::cos);
SCALAR_MAPPER (cosh, ::cosh);
SCALAR_MAPPER (exp, ::exp);
SCALAR_MAPPER (expm1, ::expm1);
SCALAR_MAPPER (fix, ::fix);
SCALAR_MAPPER (floor, gnulib::floor);
SCALAR_MAPPER (log, rc_log);
SCALAR_MAPPER (log2, rc_log2);
SCALAR_MAPPER (log10, rc_log10);
SCALAR_MAPPER (log1p, rc_log1p);
SCALAR_MAPPER (round, xround);
SCALAR_MAPPER (roundb, xroundb);
SCALAR_MAPPER (signum, ::signum);
SCALAR_MAPPER (sin, ::sin);
SCALAR_MAPPER (sinh, ::sinh);
SCALAR_MAPPER (sqrt, rc_sqrt);
SCALAR_MAPPER (tan, ::tan);
SCALAR_MAPPER (tanh, ::tanh);
SCALAR_MAPPER (finite, xfinite);
SCALAR_MAPPER (isinf, xisinf);
SCALAR_MAPPER (isna, octave_is_NA);
SCALAR_MAPPER (isnan, xisnan);
SCALAR_MAPPER (xsignbit, xsignbit);
*/
default:
if (umap >= umap_xisalnum && umap <= umap_xtoupper)
{
octave_value str_conv = convert_to_str (true, true);
return error_state ? octave_value () : str_conv.map (umap);
}
else
return octave_base_value::map (umap);
}
}
DEFINE_OCTAVE_ALLOCATOR (octave_mp);
DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_mp, "mp", "multi-precision");
static octave_base_value *
default_numeric_conversion_function (const octave_base_value& a)
{
CAST_CONV_ARG (const octave_mp&);
return new octave_mp (v.double_value ());
}
octave_base_value::type_conv_info
octave_mp::numeric_conversion_function (void) const
{
return octave_base_value::type_conv_info (default_numeric_conversion_function,
octave_mp::static_type_id ());
}