*** src/data.cc.orig5 2004-07-30 10:02:31.000000000 +0200 --- src/data.cc 2004-08-30 15:37:03.000000000 +0200 *************** *** 1208,1215 **** int nargin = args.length (); int ndim = 0; - int type = 0; dim_vector dims; --- 1208,1216 ---- int nargin = args.length (); + std::string nm = "double"; + int ndim = 0; dim_vector dims; *************** *** 1217,1231 **** if (nargin > 0 && args(nargin-1).is_string ()) { nargin--; ! // XXX FIXME XXX -- allow type of the resulting matrix to be ! // specified, e.g. ! // ! // zeros(n1, n2, ..., 'real') ! // zeros(n1, n2, ..., 'complex') ! // ! // type = get_type (args(nargin).string_value ()); } // determine matrix dimension --- 1218,1230 ---- if (nargin > 0 && args(nargin-1).is_string ()) { + nm = args(nargin-1).string_value(); nargin--; ! if (nm != "int8" && nm != "int16" && nm != "int32" && nm != "int64" && ! nm != "uint8" && nm != "uint16" && nm != "uint32" && nm != "uint64" ! && nm != "double") ! error ("%s: Unrecognized or illegal classname", fcn); } // determine matrix dimension *************** *** 1234,1240 **** { case 0: ndim = 0; - type = 0; break; case 1: --- 1233,1238 ---- *************** *** 1277,1302 **** if (! error_state) { - // Construct either scalar, matrix or N-d array. ! switch (ndim) { ! case 0: ! retval = val; ! break; ! ! case 1: ! retval = Matrix (dims(0), dims(0), val); ! break; ! ! case 2: ! retval = Matrix (dims(0), dims(1), val); ! break; ! ! default: ! retval = NDArray (dims, val); ! break; } } } --- 1275,1336 ---- if (! error_state) { ! #define INT_FILL_MATRIX(TYPE) \ ! { \ ! switch (ndim) \ ! { \ ! case 0: \ ! retval = octave_ ## TYPE (val); \ ! break; \ ! \ ! default: \ ! retval = TYPE ## NDArray (dims, val); \ ! break; \ ! } \ ! } ! ! if (nm == "int8") ! INT_FILL_MATRIX (int8) ! else if (nm == "int16") ! INT_FILL_MATRIX (int16) ! else if (nm == "int32") ! INT_FILL_MATRIX (int32) ! else if (nm == "int64") ! INT_FILL_MATRIX (int64) ! else if (nm == "uint8") ! INT_FILL_MATRIX (uint8) ! else if (nm == "uint16") ! INT_FILL_MATRIX (uint16) ! else if (nm == "uint32") ! INT_FILL_MATRIX (uint32) ! else if (nm == "uint64") ! INT_FILL_MATRIX (uint64) ! else { ! // Construct either scalar, matrix or N-d array. ! switch (ndim) ! { ! case 0: ! retval = val; ! break; ! ! case 1: ! retval = Matrix (dims(0), dims(0), val); ! break; ! ! case 2: ! retval = Matrix (dims(0), dims(1), val); ! break; ! ! default: ! retval = NDArray (dims, val); ! break; ! } } + + #undef INT_FILL_MATRIX + } } *************** *** 1308,1313 **** --- 1342,1348 ---- @deftypefn {Built-in Function} {} ones (@var{x})\n\ @deftypefnx {Built-in Function} {} ones (@var{n}, @var{m})\n\ @deftypefnx {Built-in Function} {} ones (@var{n}, @var{m}, @var{k},...)\n\ + @deftypefnx {Built-in Function} {} ones (..., @var{class})\n\ Return a matrix or N-dimensional array whose elements are all 1.\n\ The arguments are handled the same as the arguments for @code{eye}.\n\ \n\ *************** *** 1317,1322 **** --- 1352,1364 ---- @example\n\ val_matrix = val * ones (n, m)\n\ @end example\n\ + \n\ + The optional argument @var{class}, allows @code{ones} to return an array of\n\ + the specified type, like\n\ + \n\ + @example\n\ + val = ones (n,m, \"uint8\")\n\ + @end example\n\ @end deftypefn") { return fill_matrix (args, 1.0, "ones"); *************** *** 1327,1343 **** --- 1369,1460 ---- @deftypefn {Built-in Function} {} zeros (@var{x})\n\ @deftypefnx {Built-in Function} {} zeros (@var{n}, @var{m})\n\ @deftypefnx {Built-in Function} {} zeros (@var{n}, @var{m}, @var{k},...)\n\ + @deftypefnx {Built-in Function} {} zeros (..., @var{class})\n\ Return a matrix or N-dimensional array whose elements are all 0.\n\ The arguments are handled the same as the arguments for @code{eye}.\n\ + \n\ + The optional argument @var{class}, allows @code{zeros} to return an array of\n\ + the specified type, like\n\ + \n\ + @example\n\ + val = zeros (n,m, \"uint8\")\n\ + @end example\n\ @end deftypefn") { return fill_matrix (args, 0.0, "zeros"); } + static octave_value + identity_matrix (int nr, int nc, const std::string& nm) + { + octave_value retval; + + #define INT_EYE_MATRIX(TYPE) \ + { \ + if (nr == 1 && nc == 1) \ + retval = octave_ ## TYPE (1); \ + else \ + { \ + dim_vector dims (nr, nc); \ + TYPE ## NDArray m (dims, octave_ ## TYPE (0));\ + if (nr > 0 && nc > 0) \ + { \ + int n = std::min (nr, nc); \ + \ + for (int i = 0; i < n; i++) \ + m (i, i) = octave_ ## TYPE (1); \ + } \ + retval = m; \ + } \ + } + + if (nm == "int8") + INT_EYE_MATRIX (int8) + else if (nm == "int16") + INT_EYE_MATRIX (int16) + else if (nm == "int32") + INT_EYE_MATRIX (int32) + else if (nm == "int64") + INT_EYE_MATRIX (int64) + else if (nm == "uint8") + INT_EYE_MATRIX (uint8) + else if (nm == "uint16") + INT_EYE_MATRIX (uint16) + else if (nm == "uint32") + INT_EYE_MATRIX (uint32) + else if (nm == "uint64") + INT_EYE_MATRIX (uint64) + else + { + if (nr == 1 && nc == 1) + retval = 1.0; + else + { + + Matrix m (nr, nc, 0.0); + + if (nr > 0 && nc > 0) + { + int n = std::min (nr, nc); + + for (int i = 0; i < n; i++) + m (i, i) = 1.0; + } + + retval = m; + } + } + + #undef INT_EYE_MATRIX + + return retval; + } + DEFUN (eye, args, , "-*- texinfo -*-\n\ @deftypefn {Built-in Function} {} eye (@var{x})\n\ @deftypefnx {Built-in Function} {} eye (@var{n}, @var{m})\n\ + @deftypefnx {Built-in Function} {} eye (..., @var{class})\n\ Return an identity matrix. If invoked with a single scalar argument,\n\ @code{eye} returns a square matrix with the dimension specified. If you\n\ supply two scalar arguments, @code{eye} takes them to be the number of\n\ *************** *** 1366,1383 **** @end group\n\ @end example\n\ \n\ For compatibility with @sc{Matlab}, calling @code{eye} with no arguments\n\ is equivalent to calling it with an argument of 1.\n\ @end deftypefn") { octave_value retval; int nargin = args.length (); switch (nargin) { case 0: ! retval = 1.0; break; case 1: --- 1483,1522 ---- @end group\n\ @end example\n\ \n\ + The optional argument @var{class}, allows @code{eye} to return an array of\n\ + the specified type, like\n\ + \n\ + @example\n\ + val = zeros (n,m, \"uint8\")\n\ + @end example\n\ + \n\ For compatibility with @sc{Matlab}, calling @code{eye} with no arguments\n\ is equivalent to calling it with an argument of 1.\n\ @end deftypefn") { octave_value retval; + std::string nm = "double"; + int nargin = args.length (); + // Check for type information. + + if (nargin > 0 && args(nargin-1).is_string ()) + { + nm = args(nargin-1).string_value(); + nargin--; + + if (nm != "int8" && nm != "int16" && nm != "int32" && nm != "int64" && + nm != "uint8" && nm != "uint16" && nm != "uint32" && nm != "uint64" + && nm != "double") + error ("eye: Unrecognized or illegal classname"); + } + switch (nargin) { case 0: ! retval = identity_matrix (1, 1, nm); break; case 1: *************** *** 1386,1392 **** get_dimensions (args(0), "eye", nr, nc); if (! error_state) ! retval = identity_matrix (nr, nc); } break; --- 1525,1531 ---- get_dimensions (args(0), "eye", nr, nc); if (! error_state) ! retval = identity_matrix (nr, nc, nm); } break; *************** *** 1396,1402 **** get_dimensions (args(0), args(1), "eye", nr, nc); if (! error_state) ! retval = identity_matrix (nr, nc); } break; --- 1535,1541 ---- get_dimensions (args(0), args(1), "eye", nr, nc); if (! error_state) ! retval = identity_matrix (nr, nc, nm); } break; *** scripts/general/repmat.m.orig5 2004-08-30 15:58:04.000000000 +0200 --- scripts/general/repmat.m 2004-08-30 15:59:02.000000000 +0200 *************** *** 55,67 **** if (isstr (a)) x = setstr (toascii (a) * ones (idx)); else ! x = a * ones(idx); endif elseif (ndims (a) == 2 && length (idx) < 3) if (isstr (a)) x = setstr (kron (ones (idx), toascii (a))); ! else x = kron (ones (idx), a); endif else aidx = size(a); --- 55,71 ---- if (isstr (a)) x = setstr (toascii (a) * ones (idx)); else ! x = a * ones(idx, class(a)); endif elseif (ndims (a) == 2 && length (idx) < 3) if (isstr (a)) x = setstr (kron (ones (idx), toascii (a))); ! elseif (strcmp (class(a), "double")) x = kron (ones (idx), a); + else + aidx = size(a); + x = a (kron (ones (1, idx(1)), 1:aidx(1)), + kron (ones (1, idx(2)), 1:aidx(2))); endif else aidx = size(a); *************** *** 70,76 **** elseif (length(aidx) < length(idx)) aidx = [aidx, ones(1,length(idx)-length(aidx))]; endif ! cidx = cell (); for i=1:length(aidx) cidx{i} = kron (ones (1, idx(i)), 1:aidx(i)); endfor --- 74,80 ---- elseif (length(aidx) < length(idx)) aidx = [aidx, ones(1,length(idx)-length(aidx))]; endif ! cidx = cell (1, length (aidx)); for i=1:length(aidx) cidx{i} = kron (ones (1, idx(i)), 1:aidx(i)); endfor