[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: problem with sum (...)
From: |
David Bateman |
Subject: |
Re: problem with sum (...) |
Date: |
Mon, 05 Nov 2007 16:16:25 +0100 |
User-agent: |
Thunderbird 1.5.0.7 (X11/20060921) |
Ben Abbott wrote:
> I've encountered a problem with the internal function sum(), using 2.9.16
>
> The test below produces the correct result
>
> sum ([true, false])
> ans = 1
>
> The result is *not* logical (which is consistent with Matlab).
>
> islogical (sum ([true, false]))
> ans = 0
>
> However, when asked to return the result in the native format of the
> inputs (as Matlab does) ...
>
> islogical (sum ([true, false], 'native'))
> error: invalid conversion from string to real scalar
> error: octave_base_value::int_value (): wrong type argument
> `sq_string'
> error: evaluating argument list element number 1
The "native" argument is not implemented and so what is happening above
is that the argument 'native' is taken as defining the dimension along
which to add the values..
>
> While Matlab produces this result ans=1, which is consistent with
> Octave when the test is modified as ...
>
> islogical (logical (sum ([true, false])))
> ans = 1
>
> When the arguments to sum are characters and accompanied by 'native',
> Matlab produces an error.
>
> I'm not up to c++ programing. So I will not be any help in modifying
> the internal function, sum ().
I don't see why matlab should produce an error here.. Adding character
values should be legal, though perhaps a dumb thing to do.. Equally I
don't see why matlab chose to limit this argument only to the "sum"
function and not also include the "cumsum", "prod", etc functions as
well. In any case consider the attached patch that adds this
functionality to "sum".. Perhaps the other data reduction operators
should also include this functionality?
An example of the behavior when this patch is installed in then..
octave:1> a = sum([true,true],'native')
a = 1
octave:2> whos a
*** local user variables:
Prot Name Size Bytes Class
==== ==== ==== ===== =====
rwd a 1x1 1 logical
Total is 1 element using 1 byte
octave:3> a = sum([1:4],'native')
a = 10
octave:4> whos a
*** local user variables:
Prot Name Size Bytes Class
==== ==== ==== ===== =====
rwd a 1x1 8 double
Total is 1 element using 8 bytes
octave:5> a = sum(uint8([1:4]),'native')
a = 10
octave:6> whos a
*** local user variables:
Prot Name Size Bytes Class
==== ==== ==== ===== =====
rwd a 1x1 1 uint8
Total is 1 element using 1 byte
D.
--
David Bateman address@hidden
Motorola Labs - Paris +33 1 69 35 48 04 (Ph)
Parc Les Algorithmes, Commune de St Aubin +33 6 72 01 06 33 (Mob)
91193 Gif-Sur-Yvette FRANCE +33 1 69 35 77 01 (Fax)
The information contained in this communication has been classified as:
[x] General Business Information
[ ] Motorola Internal Use Only
[ ] Motorola Confidential Proprietary
2007-11-05 David Bateman <address@hidden>
* data.cc (DATA_REDUCTION): Handle the 'native' and 'double'
arguments of the Fsum function.
* OPERATORS/op-bm-bm.cc (matrix_to_bool_matrix,
scalar_to_bool_matrix): New type conversion functions.
(install_bm_bm_ops): Install new type conversions functions.
*** ./src/data.cc.orig1 2007-11-05 14:51:04.238672315 +0100
--- ./src/data.cc 2007-11-05 16:11:37.258846671 +0100
***************
*** 370,381 ****
return retval;
}
! #define DATA_REDUCTION(FCN) \
\
octave_value retval; \
\
int nargin = args.length (); \
\
if (nargin == 1 || nargin == 2) \
{ \
octave_value arg = args(0); \
--- 370,397 ----
return retval;
}
! #define DATA_REDUCTION(FCN, STRARGS) \
\
octave_value retval; \
\
int nargin = args.length (); \
\
+ bool isnative = false; \
+ \
+ if (STRARGS && args(nargin - 1).is_string ()) \
+ { \
+ std::string str = args(nargin - 1).string_value (); \
+ \
+ if (! error_state) \
+ { \
+ if (str == "native") \
+ isnative = true; \
+ else if (str != "double") /* Ignore double as no single type */ \
+ error ("sum: unrecognized string argument"); \
+ nargin --; \
+ } \
+ } \
+ \
if (nargin == 1 || nargin == 2) \
{ \
octave_value arg = args(0); \
***************
*** 405,414 ****
--- 421,441 ----
gripe_wrong_type_arg (#FCN, arg); \
return retval; \
} \
+ if (isnative) \
+ { \
+ octave_base_value::type_conv_fcn cf \
+ = octave_value_typeinfo::lookup_type_conv_op \
+ (retval.type_id (), arg.type_id ()); \
+ if (cf) \
+ retval = octave_value (cf (retval.get_rep ())); \
+ else \
+ error (#FCN ": type conversion falied"); \
+ } \
} \
else \
error (#FCN ": invalid dimension argument = %d", dim + 1); \
} \
+ \
} \
else \
print_usage (); \
***************
*** 427,433 ****
same orientation as @var{x}.\n\
@end deftypefn")
{
! DATA_REDUCTION (cumprod);
}
DEFUN (cumsum, args, ,
--- 454,460 ----
same orientation as @var{x}.\n\
@end deftypefn")
{
! DATA_REDUCTION (cumprod, false);
}
DEFUN (cumsum, args, ,
***************
*** 441,447 ****
same orientation as @var{x}.\n\
@end deftypefn")
{
! DATA_REDUCTION (cumsum);
}
template <class T>
--- 468,474 ----
same orientation as @var{x}.\n\
@end deftypefn")
{
! DATA_REDUCTION (cumsum, false);
}
template <class T>
***************
*** 671,677 ****
return the product of the elements.\n\
@end deftypefn")
{
! DATA_REDUCTION (prod);
}
static octave_value
--- 698,704 ----
return the product of the elements.\n\
@end deftypefn")
{
! DATA_REDUCTION (prod, false);
}
static octave_value
***************
*** 1213,1226 ****
DEFUN (sum, args, ,
"-*- texinfo -*-\n\
@deftypefn {Built-in Function} {} sum (@var{x}, @var{dim})\n\
Sum of elements along dimension @var{dim}. If @var{dim} is\n\
omitted, it defaults to 1 (column-wise sum).\n\
\n\
As a special case, if @var{x} is a vector and @var{dim} is omitted,\n\
return the sum of the elements.\n\
@end deftypefn")
{
! DATA_REDUCTION (sum);
}
DEFUN (sumsq, args, ,
--- 1240,1265 ----
DEFUN (sum, args, ,
"-*- texinfo -*-\n\
@deftypefn {Built-in Function} {} sum (@var{x}, @var{dim})\n\
+ @deftypefnx {Built-in Function} {} sum (@dots{}, 'native')\n\
Sum of elements along dimension @var{dim}. If @var{dim} is\n\
omitted, it defaults to 1 (column-wise sum).\n\
\n\
As a special case, if @var{x} is a vector and @var{dim} is omitted,\n\
return the sum of the elements.\n\
+ \n\
+ If the optional argument 'native' is given, then the sum is performed\n\
+ in the same type as the original argument, rather than in the default\n\
+ double type. For example\n\
+ \n\
+ @example\n\
+ sum ([true, true])\n\
+ @result{} 2\n\
+ sum ([true, true], 'native')\n\
+ @result{} true\n\
+ @end example\n\
@end deftypefn")
{
! DATA_REDUCTION (sum, true);
}
DEFUN (sumsq, args, ,
***************
*** 1239,1245 ****
but it uses less memory and avoids calling conj if @var{x} is real.\n\
@end deftypefn")
{
! DATA_REDUCTION (sumsq);
}
DEFUN (islogical, args, ,
--- 1278,1284 ----
but it uses less memory and avoids calling conj if @var{x} is real.\n\
@end deftypefn")
{
! DATA_REDUCTION (sumsq, false);
}
DEFUN (islogical, args, ,
*** ./src/OPERATORS/op-bm-bm.cc.orig1 2007-11-05 14:51:24.328655870 +0100
--- ./src/OPERATORS/op-bm-bm.cc 2007-11-05 15:48:38.641239298 +0100
***************
*** 29,34 ****
--- 29,35 ----
#include "oct-obj.h"
#include "ov.h"
#include "ov-bool-mat.h"
+ #include "ov-scalar.h"
#include "ov-range.h"
#include "ov-re-mat.h"
#include "ov-re-sparse.h"
***************
*** 100,105 ****
--- 101,109 ----
return octave_value ();
}
+ DEFCONVFN (matrix_to_bool_matrix, matrix, bool)
+ DEFCONVFN (scalar_to_bool_matrix, scalar, bool)
+
void
install_bm_bm_ops (void)
{
***************
*** 119,124 ****
--- 123,131 ----
INSTALL_CATOP (octave_bool_matrix, octave_matrix, bm_m);
INSTALL_CATOP (octave_matrix, octave_bool_matrix, m_bm);
+ INSTALL_CONVOP (octave_matrix, octave_bool_matrix, matrix_to_bool_matrix);
+ INSTALL_CONVOP (octave_scalar, octave_bool_matrix, scalar_to_bool_matrix);
+
INSTALL_ASSIGNOP (op_asn_eq, octave_bool_matrix, octave_bool_matrix,
assign);
INSTALL_ASSIGNOP (op_asn_eq, octave_bool_matrix, octave_matrix,
conv_and_assign);
- Re: problem with sum (...), (continued)
- proposed patch for ismember.m, Ben Abbott, 2007/11/06
- proposed patch for ismember.m, John W. Eaton, 2007/11/07
- Re: proposed patch for ismember.m, Ben Abbott, 2007/11/07
- Re: proposed patch for ismember.m, John W. Eaton, 2007/11/07
- Re: proposed patch for ismember.m, Ben Abbott, 2007/11/07
- Re: proposed patch for ismember.m, John W. Eaton, 2007/11/07
- Re: proposed patch for ismember.m, Ben Abbott, 2007/11/08
Re: problem with sum (...),
David Bateman <=
Re: problem with sum (...), David Bateman, 2007/11/06
Re: problem with sum (...), Ben Abbott, 2007/11/05
Re: problem with sum (...), John W. Eaton, 2007/11/06