00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <cstdlib>
00024
00025 #include <iosfwd>
00026 #include <string>
00027
00028 #include "mx-base.h"
00029 #include "oct-alloc.h"
00030 #include "str-vec.h"
00031
00032 #include "error.h"
00033 #include "oct-stream.h"
00034 #include "ov-base.h"
00035 #include "ov-base-int.h"
00036 #include "ov-typeinfo.h"
00037 #include "gripes.h"
00038
00039 #include "ov-re-mat.h"
00040 #include "ov-scalar.h"
00041
00042 class
00043 OCTINTERP_API
00044 OCTAVE_VALUE_INT_MATRIX_T
00045 : public octave_base_int_matrix<intNDArray<OCTAVE_INT_T> >
00046 {
00047 public:
00048
00049 OCTAVE_VALUE_INT_MATRIX_T (void)
00050 : octave_base_int_matrix<intNDArray<OCTAVE_INT_T> > () { }
00051
00052 OCTAVE_VALUE_INT_MATRIX_T (const intNDArray<OCTAVE_INT_T>& nda)
00053 : octave_base_int_matrix<intNDArray<OCTAVE_INT_T> > (nda) { }
00054
00055 OCTAVE_VALUE_INT_MATRIX_T (const Array<OCTAVE_INT_T>& nda)
00056 : octave_base_int_matrix<intNDArray<OCTAVE_INT_T> >
00057 (intNDArray<OCTAVE_INT_T> (nda)) { }
00058
00059 ~OCTAVE_VALUE_INT_MATRIX_T (void) { }
00060
00061 octave_base_value *clone (void) const
00062 { return new OCTAVE_VALUE_INT_MATRIX_T (*this); }
00063
00064 octave_base_value *empty_clone (void) const
00065 { return new OCTAVE_VALUE_INT_MATRIX_T (); }
00066
00067 bool OCTAVE_TYPE_PREDICATE_FUNCTION (void) const { return true; }
00068
00069 bool is_integer_type (void) const { return true; }
00070
00071 builtin_type_t builtin_type (void) const { return OCTAVE_INT_BTYP; }
00072
00073 private:
00074
00075 template <class IM>
00076 IM convert_gripe () const
00077 {
00078 typedef typename IM::element_type dest_el_type;
00079 typedef intNDArray<OCTAVE_INT_T>::element_type src_el_type;
00080 dest_el_type::clear_conv_flag ();
00081 IM retval (matrix);
00082 if (dest_el_type::get_trunc_flag ())
00083 gripe_truncated_conversion (src_el_type::type_name (),
00084 dest_el_type::type_name ());
00085 dest_el_type::clear_conv_flag ();
00086 return retval;
00087 }
00088
00089 public:
00090
00091 int8NDArray
00092 int8_array_value (void) const { return convert_gripe<int8NDArray> (); }
00093
00094 int16NDArray
00095 int16_array_value (void) const { return convert_gripe<int16NDArray> (); }
00096
00097 int32NDArray
00098 int32_array_value (void) const { return convert_gripe<int32NDArray> (); }
00099
00100 int64NDArray
00101 int64_array_value (void) const { return convert_gripe<int64NDArray> (); }
00102
00103 uint8NDArray
00104 uint8_array_value (void) const { return convert_gripe<uint8NDArray> (); }
00105
00106 uint16NDArray
00107 uint16_array_value (void) const { return convert_gripe<uint16NDArray> (); }
00108
00109 uint32NDArray
00110 uint32_array_value (void) const { return convert_gripe<uint32NDArray> (); }
00111
00112 uint64NDArray
00113 uint64_array_value (void) const { return convert_gripe<uint64NDArray> (); }
00114
00115 double
00116 double_value (bool = false) const
00117 {
00118 double retval = lo_ieee_nan_value ();
00119
00120 if (numel () > 0)
00121 {
00122 gripe_implicit_conversion ("Octave:array-as-scalar",
00123 type_name (), "real scalar");
00124
00125 retval = matrix(0).double_value ();
00126 }
00127 else
00128 gripe_invalid_conversion (type_name (), "real scalar");
00129
00130 return retval;
00131
00132 }
00133
00134 float
00135 float_value (bool = false) const
00136 {
00137 float retval = lo_ieee_float_nan_value ();
00138
00139 if (numel () > 0)
00140 {
00141 gripe_implicit_conversion ("Octave:array-as-scalar",
00142 type_name (), "real scalar");
00143
00144 retval = matrix(0).float_value ();
00145 }
00146 else
00147 gripe_invalid_conversion (type_name (), "real scalar");
00148
00149 return retval;
00150
00151 }
00152
00153 double scalar_value (bool = false) const { return double_value (); }
00154
00155 float float_scalar_value (bool = false) const { return float_value (); }
00156
00157 Matrix
00158 matrix_value (bool = false) const
00159 {
00160 Matrix retval;
00161 dim_vector dv = dims ();
00162 if (dv.length () > 2)
00163 error ("invalid conversion of %s to Matrix", type_name().c_str ());
00164 else
00165 {
00166 retval = Matrix (dv(0), dv(1));
00167 double *vec = retval.fortran_vec ();
00168 octave_idx_type nel = matrix.numel ();
00169 for (octave_idx_type i = 0; i < nel; i++)
00170 vec[i] = matrix(i).double_value ();
00171 }
00172 return retval;
00173 }
00174
00175 FloatMatrix
00176 float_matrix_value (bool = false) const
00177 {
00178 FloatMatrix retval;
00179 dim_vector dv = dims ();
00180 if (dv.length () > 2)
00181 error ("invalid conversion of %s to FloatMatrix", type_name().c_str ());
00182 else
00183 {
00184 retval = FloatMatrix (dv(0), dv(1));
00185 float *vec = retval.fortran_vec ();
00186 octave_idx_type nel = matrix.numel ();
00187 for (octave_idx_type i = 0; i < nel; i++)
00188 vec[i] = matrix(i).float_value ();
00189 }
00190 return retval;
00191 }
00192
00193 ComplexMatrix
00194 complex_matrix_value (bool = false) const
00195 {
00196 ComplexMatrix retval;
00197 dim_vector dv = dims();
00198 if (dv.length () > 2)
00199 error ("invalid conversion of %s to Matrix", type_name().c_str ());
00200 else
00201 {
00202 retval = ComplexMatrix (dv(0), dv(1));
00203 Complex *vec = retval.fortran_vec ();
00204 octave_idx_type nel = matrix.numel ();
00205 for (octave_idx_type i = 0; i < nel; i++)
00206 vec[i] = Complex (matrix(i).double_value ());
00207 }
00208 return retval;
00209 }
00210
00211 FloatComplexMatrix
00212 float_complex_matrix_value (bool = false) const
00213 {
00214 FloatComplexMatrix retval;
00215 dim_vector dv = dims();
00216 if (dv.length () > 2)
00217 error ("invalid conversion of %s to FloatMatrix", type_name().c_str ());
00218 else
00219 {
00220 retval = FloatComplexMatrix (dv(0), dv(1));
00221 FloatComplex *vec = retval.fortran_vec ();
00222 octave_idx_type nel = matrix.numel ();
00223 for (octave_idx_type i = 0; i < nel; i++)
00224 vec[i] = FloatComplex (matrix(i).float_value ());
00225 }
00226 return retval;
00227 }
00228
00229 NDArray
00230 array_value (bool = false) const
00231 {
00232 NDArray retval (matrix.dims ());
00233 double *vec = retval.fortran_vec ();
00234 octave_idx_type nel = matrix.numel ();
00235 for (octave_idx_type i = 0; i < nel; i++)
00236 vec[i] = matrix(i).double_value ();
00237 return retval;
00238 }
00239
00240 FloatNDArray
00241 float_array_value (bool = false) const
00242 {
00243 FloatNDArray retval (matrix.dims ());
00244 float *vec = retval.fortran_vec ();
00245 octave_idx_type nel = matrix.numel ();
00246 for (octave_idx_type i = 0; i < nel; i++)
00247 vec[i] = matrix(i).float_value ();
00248 return retval;
00249 }
00250
00251 ComplexNDArray
00252 complex_array_value (bool = false) const
00253 {
00254 ComplexNDArray retval (matrix.dims ());
00255 Complex *vec = retval.fortran_vec ();
00256 octave_idx_type nel = matrix.numel ();
00257 for (octave_idx_type i = 0; i < nel; i++)
00258 vec[i] = Complex (matrix(i).double_value ());
00259 return retval;
00260 }
00261
00262 FloatComplexNDArray
00263 float_complex_array_value (bool = false) const
00264 {
00265 FloatComplexNDArray retval (matrix.dims ());
00266 FloatComplex *vec = retval.fortran_vec ();
00267 octave_idx_type nel = matrix.numel ();
00268 for (octave_idx_type i = 0; i < nel; i++)
00269 vec[i] = FloatComplex (matrix(i).float_value ());
00270 return retval;
00271 }
00272
00273 boolNDArray
00274 bool_array_value (bool warn = false) const
00275 {
00276 boolNDArray retval (dims ());
00277
00278 octave_idx_type nel = numel ();
00279
00280 if (warn && matrix.any_element_not_one_or_zero ())
00281 gripe_logical_conversion ();
00282
00283 bool *vec = retval.fortran_vec ();
00284 for (octave_idx_type i = 0; i < nel; i++)
00285 vec[i] = matrix(i).bool_value ();
00286
00287 return retval;
00288 }
00289
00290 charNDArray
00291 char_array_value (bool = false) const
00292 {
00293 charNDArray retval (dims ());
00294
00295 octave_idx_type nel = numel ();
00296
00297 char *vec = retval.fortran_vec ();
00298 for (octave_idx_type i = 0; i < nel; i++)
00299 vec[i] = matrix(i).char_value ();
00300
00301 return retval;
00302 }
00303
00304
00305 void increment (void)
00306 {
00307 matrix_ref() += OCTAVE_INT_T (1);
00308 if (OCTAVE_INT_T::get_math_trunc_flag ())
00309 gripe_unop_integer_math_truncated ("++", type_name (). c_str ());
00310
00311 OCTAVE_INT_T::clear_conv_flag ();
00312 }
00313
00314 void decrement (void)
00315 {
00316 matrix_ref() -= OCTAVE_INT_T (1);
00317 if (OCTAVE_INT_T::get_math_trunc_flag ())
00318 gripe_unop_integer_math_truncated ("--", type_name (). c_str ());
00319 OCTAVE_INT_T::clear_conv_flag ();
00320 }
00321
00322 void changesign (void)
00323 {
00324 matrix_ref ().changesign ();
00325 if (OCTAVE_INT_T::get_math_trunc_flag ())
00326 gripe_unop_integer_math_truncated ("-", type_name (). c_str ());
00327 OCTAVE_INT_T::clear_conv_flag ();
00328 }
00329
00330 idx_vector index_vector (void) const
00331 { return idx_cache ? *idx_cache : set_idx_cache (idx_vector (matrix)); }
00332
00333 int write (octave_stream& os, int block_size,
00334 oct_data_conv::data_type output_type, int skip,
00335 oct_mach_info::float_format flt_fmt) const
00336 { return os.write (matrix, block_size, output_type, skip, flt_fmt); }
00337
00338
00339
00340 void *mex_get_data (void) const { return matrix.mex_get_data (); }
00341
00342 mxArray *as_mxArray (void) const
00343 {
00344 mxArray *retval = new mxArray (OCTAVE_INT_MX_CLASS, dims (), mxREAL);
00345
00346 OCTAVE_INT_T::val_type *pr = static_cast<OCTAVE_INT_T::val_type *> (retval->get_data ());
00347
00348 mwSize nel = numel ();
00349
00350 const OCTAVE_INT_T *p = matrix.data ();
00351
00352 for (mwIndex i = 0; i < nel; i++)
00353 pr[i] = p[i].value ();
00354
00355 return retval;
00356 }
00357
00358 #define MAT_MAPPER(FCN) \
00359 octave_value FCN (void) const { return matrix.FCN (); }
00360
00361 MAT_MAPPER (abs)
00362 MAT_MAPPER (signum)
00363
00364 #undef MAT_MAPPER
00365
00366 octave_value imag (void) const
00367 {
00368 return intNDArray<OCTAVE_INT_T> (matrix.dims (),
00369 static_cast<OCTAVE_INT_T>(0));
00370 }
00371
00372 #define NO_OP_MAPPER(FCN) \
00373 octave_value FCN (void) const { return octave_value (matrix); }
00374
00375 NO_OP_MAPPER (ceil)
00376 NO_OP_MAPPER (conj)
00377 NO_OP_MAPPER (fix)
00378 NO_OP_MAPPER (floor)
00379 NO_OP_MAPPER (real)
00380 NO_OP_MAPPER (round)
00381 NO_OP_MAPPER (roundb)
00382
00383 #undef NO_OP_MAPPER
00384
00385 #define BOOL_MAPPER(FCN, VAL) \
00386 octave_value FCN (void) const { return boolNDArray (matrix.dims (), VAL); }
00387
00388 BOOL_MAPPER (finite, true)
00389 BOOL_MAPPER (isinf, false)
00390 BOOL_MAPPER (isna, false)
00391 BOOL_MAPPER (isnan, false)
00392
00393 #undef BOOL_MAPPER
00394
00395 private:
00396
00397 DECLARE_OCTAVE_ALLOCATOR
00398
00399 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
00400 };
00401
00402 class
00403 OCTINTERP_API
00404 OCTAVE_VALUE_INT_SCALAR_T
00405 : public octave_base_int_scalar<OCTAVE_INT_T>
00406 {
00407 public:
00408
00409 OCTAVE_VALUE_INT_SCALAR_T (void)
00410 : octave_base_int_scalar<OCTAVE_INT_T> () { }
00411
00412 OCTAVE_VALUE_INT_SCALAR_T (const OCTAVE_INT_T& nda)
00413 : octave_base_int_scalar<OCTAVE_INT_T> (nda) { }
00414
00415 ~OCTAVE_VALUE_INT_SCALAR_T (void) { }
00416
00417 octave_base_value *clone (void) const
00418 { return new OCTAVE_VALUE_INT_SCALAR_T (*this); }
00419
00420 octave_base_value *empty_clone (void) const
00421 { return new OCTAVE_VALUE_INT_MATRIX_T (); }
00422
00423 octave_value do_index_op (const octave_value_list& idx,
00424 bool resize_ok = false)
00425 {
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436 octave_value tmp
00437 (new OCTAVE_VALUE_INT_MATRIX_T
00438 (OCTAVE_VALUE_INT_NDARRAY_EXTRACTOR_FUNCTION ()));
00439
00440 return tmp.do_index_op (idx, resize_ok);
00441 }
00442
00443 bool OCTAVE_TYPE_PREDICATE_FUNCTION (void) const { return true; }
00444
00445 bool is_integer_type (void) const { return true; }
00446
00447 builtin_type_t builtin_type (void) const { return OCTAVE_INT_BTYP; }
00448
00449 private:
00450
00451 template <class IS>
00452 IS convert_gripe () const
00453 {
00454 typedef IS dest_el_type;
00455 typedef OCTAVE_INT_T src_el_type;
00456 dest_el_type::clear_conv_flag ();
00457 IS retval (scalar);
00458
00459 if (dest_el_type::get_trunc_flag ())
00460 gripe_truncated_conversion (src_el_type::type_name (),
00461 dest_el_type::type_name ());
00462 dest_el_type::clear_conv_flag ();
00463 return retval;
00464 }
00465
00466 public:
00467
00468 octave_int8
00469 int8_scalar_value (void) const { return convert_gripe<octave_int8> (); }
00470
00471 octave_int16
00472 int16_scalar_value (void) const { return convert_gripe<octave_int16> (); }
00473
00474 octave_int32
00475 int32_scalar_value (void) const { return convert_gripe<octave_int32> (); }
00476
00477 octave_int64
00478 int64_scalar_value (void) const { return convert_gripe<octave_int64> (); }
00479
00480 octave_uint8
00481 uint8_scalar_value (void) const { return convert_gripe<octave_uint8> (); }
00482
00483 octave_uint16
00484 uint16_scalar_value (void) const { return convert_gripe<octave_uint16> (); }
00485
00486 octave_uint32
00487 uint32_scalar_value (void) const { return convert_gripe<octave_uint32> (); }
00488
00489 octave_uint64
00490 uint64_scalar_value (void) const { return convert_gripe<octave_uint64> (); }
00491
00492 int8NDArray
00493 int8_array_value (void) const
00494 { return int8NDArray (dim_vector (1, 1), int8_scalar_value ()); }
00495
00496 int16NDArray
00497 int16_array_value (void) const
00498 { return int16NDArray (dim_vector (1, 1), int16_scalar_value ()); }
00499
00500 int32NDArray
00501 int32_array_value (void) const
00502 { return int32NDArray (dim_vector (1, 1), int32_scalar_value ()); }
00503
00504 int64NDArray
00505 int64_array_value (void) const
00506 { return int64NDArray (dim_vector (1, 1), int64_scalar_value ()); }
00507
00508 uint8NDArray
00509 uint8_array_value (void) const
00510 { return uint8NDArray (dim_vector (1, 1), uint8_scalar_value ()); }
00511
00512 uint16NDArray
00513 uint16_array_value (void) const
00514 { return uint16NDArray (dim_vector (1, 1), uint16_scalar_value ()); }
00515
00516 uint32NDArray
00517 uint32_array_value (void) const
00518 { return uint32NDArray (dim_vector (1, 1), uint32_scalar_value ()); }
00519
00520 uint64NDArray
00521 uint64_array_value (void) const
00522 { return uint64NDArray (dim_vector (1, 1), uint64_scalar_value ()); }
00523
00524 octave_value resize (const dim_vector& dv, bool fill = false) const
00525 {
00526 if (fill)
00527 {
00528 intNDArray<OCTAVE_INT_T> retval (dv, 0);
00529 if (dv.numel())
00530 retval(0) = scalar;
00531 return retval;
00532 }
00533 else
00534 {
00535 intNDArray<OCTAVE_INT_T> retval (dv);
00536 if (dv.numel())
00537 retval(0) = scalar;
00538 return retval;
00539 }
00540 }
00541
00542 double double_value (bool = false) const { return scalar.double_value (); }
00543
00544 float float_value (bool = false) const { return scalar.float_value (); }
00545
00546 double scalar_value (bool = false) const { return scalar.double_value (); }
00547
00548 float float_scalar_value (bool = false) const { return scalar.float_value (); }
00549
00550 Matrix
00551 matrix_value (bool = false) const
00552 {
00553 Matrix retval (1, 1);
00554 retval(0,0) = scalar.double_value ();
00555 return retval;
00556 }
00557
00558 FloatMatrix
00559 float_matrix_value (bool = false) const
00560 {
00561 FloatMatrix retval (1, 1);
00562 retval(0,0) = scalar.float_value ();
00563 return retval;
00564 }
00565
00566 ComplexMatrix
00567 complex_matrix_value (bool = false) const
00568 {
00569 ComplexMatrix retval (1, 1);
00570 retval(0,0) = Complex (scalar.double_value ());
00571 return retval;
00572 }
00573
00574 FloatComplexMatrix
00575 float_complex_matrix_value (bool = false) const
00576 {
00577 FloatComplexMatrix retval (1, 1);
00578 retval(0,0) = FloatComplex (scalar.float_value ());
00579 return retval;
00580 }
00581
00582 NDArray
00583 array_value (bool = false) const
00584 {
00585 NDArray retval (dim_vector (1, 1));
00586 retval(0) = scalar.double_value ();
00587 return retval;
00588 }
00589
00590 FloatNDArray
00591 float_array_value (bool = false) const
00592 {
00593 FloatNDArray retval (dim_vector (1, 1));
00594 retval(0) = scalar.float_value ();
00595 return retval;
00596 }
00597
00598 ComplexNDArray
00599 complex_array_value (bool = false) const
00600 {
00601 ComplexNDArray retval (dim_vector (1, 1));
00602 retval(0) = FloatComplex (scalar.double_value ());
00603 return retval;
00604 }
00605
00606 FloatComplexNDArray
00607 float_complex_array_value (bool = false) const
00608 {
00609 FloatComplexNDArray retval (dim_vector (1, 1));
00610 retval(0) = FloatComplex (scalar.float_value ());
00611 return retval;
00612 }
00613
00614 boolNDArray
00615 bool_array_value (bool warn = false) const
00616 {
00617 boolNDArray retval (dim_vector (1, 1));
00618
00619 if (warn && scalar != 0.0 && scalar != 1.0)
00620 gripe_logical_conversion ();
00621
00622 retval(0) = scalar.bool_value ();
00623
00624 return retval;
00625 }
00626
00627 charNDArray
00628 char_array_value (bool = false) const
00629 {
00630 charNDArray retval (dim_vector (1, 1));
00631 retval(0) = scalar.char_value ();
00632 return retval;
00633 }
00634
00635 void increment (void)
00636 {
00637 scalar += OCTAVE_INT_T (1);
00638 if (OCTAVE_INT_T::get_math_trunc_flag ())
00639 gripe_unop_integer_math_truncated ("++", type_name (). c_str ());
00640 OCTAVE_INT_T::clear_conv_flag ();
00641 }
00642
00643 void decrement (void)
00644 {
00645 scalar -= OCTAVE_INT_T (1);
00646 if (OCTAVE_INT_T::get_math_trunc_flag ())
00647 gripe_unop_integer_math_truncated ("--", type_name (). c_str ());
00648 OCTAVE_INT_T::clear_conv_flag ();
00649 }
00650
00651 idx_vector index_vector (void) const { return idx_vector (scalar); }
00652
00653 int write (octave_stream& os, int block_size,
00654 oct_data_conv::data_type output_type, octave_idx_type skip,
00655 oct_mach_info::float_format flt_fmt) const
00656 {
00657 return os.write (OCTAVE_VALUE_INT_NDARRAY_EXTRACTOR_FUNCTION (),
00658 block_size, output_type, skip, flt_fmt);
00659 }
00660
00661
00662
00663 void *mex_get_data (void) const { return scalar.mex_get_data (); }
00664
00665 mxArray *as_mxArray (void) const
00666 {
00667 mxArray *retval = new mxArray (OCTAVE_INT_MX_CLASS, 1, 1, mxREAL);
00668
00669 OCTAVE_INT_T::val_type *pr = static_cast<OCTAVE_INT_T::val_type *> (retval->get_data ());
00670
00671 pr[0] = scalar.value ();
00672
00673 return retval;
00674 }
00675
00676 #define SCALAR_MAPPER(FCN) \
00677 octave_value FCN (void) const { return scalar.FCN (); }
00678
00679 SCALAR_MAPPER (abs)
00680 SCALAR_MAPPER (signum)
00681
00682 #undef SCALAR_MAPPER
00683
00684 octave_value imag (void) const { return static_cast<OCTAVE_INT_T>(0); }
00685
00686 #define NO_OP_MAPPER(FCN) \
00687 octave_value FCN (void) const { return octave_value (scalar); }
00688
00689 NO_OP_MAPPER (ceil)
00690 NO_OP_MAPPER (conj)
00691 NO_OP_MAPPER (fix)
00692 NO_OP_MAPPER (floor)
00693 NO_OP_MAPPER (real)
00694 NO_OP_MAPPER (round)
00695 NO_OP_MAPPER (roundb)
00696
00697 #undef NO_OP_MAPPER
00698
00699 #define BOOL_MAPPER(FCN, VAL) octave_value FCN (void) const { return VAL; }
00700
00701 BOOL_MAPPER (finite, true)
00702 BOOL_MAPPER (isinf, false)
00703 BOOL_MAPPER (isna, false)
00704 BOOL_MAPPER (isnan, false)
00705
00706 #undef BOOL_MAPPER
00707
00708 private:
00709
00710 DECLARE_OCTAVE_ALLOCATOR
00711
00712 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
00713 };
00714
00715
00716
00717
00718
00719