comparison src/data.cc @ 9813:8fa32b527d9a

improve & partially revert previous change
author Jaroslav Hajek <highegg@gmail.com>
date Fri, 13 Nov 2009 11:45:39 +0100
parents f80c566bc751
children 6867676107f3
comparison
equal deleted inserted replaced
9812:f80c566bc751 9813:8fa32b527d9a
974 %!assert (size (hypot (rand (2, 3, 4), 1)), [2, 3, 4]) 974 %!assert (size (hypot (rand (2, 3, 4), 1)), [2, 3, 4])
975 %!assert (size (hypot (1, rand (2, 3, 4))), [2, 3, 4]) 975 %!assert (size (hypot (1, rand (2, 3, 4))), [2, 3, 4])
976 %!assert (size (hypot (1, 2)), [1, 1]) 976 %!assert (size (hypot (1, 2)), [1, 1])
977 %!assert (hypot (1:10, 1:10), sqrt(2) * [1:10], 16*eps) 977 %!assert (hypot (1:10, 1:10), sqrt(2) * [1:10], 16*eps)
978 %!assert (hypot (single(1:10), single(1:10)), single(sqrt(2) * [1:10])); 978 %!assert (hypot (single(1:10), single(1:10)), single(sqrt(2) * [1:10]));
979 */
980
981 template<typename T, typename ET>
982 void
983 map_2_xlog2 (const Array<T>& x, Array<T>& f, Array<ET>& e)
984 {
985 f = Array<T>(x.dims ());
986 e = Array<ET>(x.dims ());
987 for (octave_idx_type i = 0; i < x.numel (); i++)
988 {
989 int exp;
990 f.xelem (i) = xlog2 (x(i), exp);
991 e.xelem (i) = exp;
992 }
993 }
994
995 DEFUN (log2, args, nargout,
996 "-*- texinfo -*-\n\
997 @deftypefn {Mapping Function} {} log2 (@var{x})\n\
998 @deftypefnx {Mapping Function} {[@var{f}, @var{e}] =} log2 (@var{x})\n\
999 Compute the base-2 logarithm of each element of @var{x}.\n\
1000 \n\
1001 If called with two output arguments, split @var{x} into\n\
1002 binary mantissa and exponent so that\n\
1003 @tex\n\
1004 ${1 \\over 2} \\le \\left| f \\right| < 1$\n\
1005 @end tex\n\
1006 @ifnottex\n\
1007 @code{1/2 <= abs(f) < 1}\n\
1008 @end ifnottex\n\
1009 and @var{e} is an integer. If\n\
1010 @tex\n\
1011 $x = 0$, $f = e = 0$.\n\
1012 @end tex\n\
1013 @ifnottex\n\
1014 @code{x = 0}, @code{f = e = 0}.\n\
1015 @end ifnottex\n\
1016 @seealso{pow2, log, log10, exp}\n\
1017 @end deftypefn")
1018 {
1019 octave_value_list retval;
1020
1021 if (args.length () == 1)
1022 {
1023 if (nargout < 2)
1024 retval(0) = args(0).log2 ();
1025 else if (args(0).is_single_type ())
1026 {
1027 if (args(0).is_real_type ())
1028 {
1029 FloatNDArray f;
1030 FloatNDArray x = args(0).float_array_value ();
1031 // FIXME -- should E be an int value?
1032 FloatMatrix e;
1033 map_2_xlog2 (x, f, e);
1034 retval (1) = e;
1035 retval (0) = f;
1036 }
1037 else if (args(0).is_complex_type ())
1038 {
1039 FloatComplexNDArray f;
1040 FloatComplexNDArray x = args(0).float_complex_array_value ();
1041 // FIXME -- should E be an int value?
1042 FloatNDArray e;
1043 map_2_xlog2 (x, f, e);
1044 retval (1) = e;
1045 retval (0) = f;
1046 }
1047 }
1048 else if (args(0).is_real_type ())
1049 {
1050 NDArray f;
1051 NDArray x = args(0).array_value ();
1052 // FIXME -- should E be an int value?
1053 Matrix e;
1054 map_2_xlog2 (x, f, e);
1055 retval (1) = e;
1056 retval (0) = f;
1057 }
1058 else if (args(0).is_complex_type ())
1059 {
1060 ComplexNDArray f;
1061 ComplexNDArray x = args(0).complex_array_value ();
1062 // FIXME -- should E be an int value?
1063 NDArray e;
1064 map_2_xlog2 (x, f, e);
1065 retval (1) = e;
1066 retval (0) = f;
1067 }
1068 else
1069 gripe_wrong_type_arg ("log2", args(0));
1070 }
1071 else
1072 print_usage ();
1073
1074 return retval;
1075 }
1076
1077 /*
1078 %!assert(log2 ([1/4, 1/2, 1, 2, 4]), [-2, -1, 0, 1, 2]);
1079 %!assert(log2(Inf), Inf);
1080 %!assert(isnan(log2(NaN)));
1081 %!assert(log2(4*i), 2 + log2(1*i));
1082 %!assert(log2(complex(0,Inf)), Inf + log2(i));
1083
1084 %!test
1085 %! [f, e] = log2 ([0,-1; 2,-4; Inf,-Inf]);
1086 %! assert (f, [0,-0.5; 0.5,-0.5; Inf,-Inf]);
1087 %! assert (e(1:2,:), [0,1;2,3])
1088
1089 %!test
1090 %! [f, e] = log2 (complex (zeros (3, 2), [0,-1; 2,-4; Inf,-Inf]));
1091 %! assert (f, complex (zeros (3, 2), [0,-0.5; 0.5,-0.5; Inf,-Inf]));
1092 %! assert (e(1:2,:), [0,1; 2,3]);
979 */ 1093 */
980 1094
981 DEFUN (fmod, args, , 1095 DEFUN (fmod, args, ,
982 "-*- texinfo -*-\n\ 1096 "-*- texinfo -*-\n\
983 @deftypefn {Mapping Function} {} fmod (@var{x}, @var{y})\n\ 1097 @deftypefn {Mapping Function} {} fmod (@var{x}, @var{y})\n\