comparison src/data.cc @ 4481:cfbaee1f562f

[project @ 2003-08-06 13:56:07 by jwe]
author jwe
date Wed, 06 Aug 2003 13:56:07 +0000
parents d4d1a0be55f7
children 65f47f8a92a2
comparison
equal deleted inserted replaced
4480:b96f2c55d5a3 4481:cfbaee1f562f
926 { 926 {
927 octave_value retval; 927 octave_value retval;
928 928
929 int nargin = args.length (); 929 int nargin = args.length ();
930 930
931 int ndim = 0;
932 int type = 0;
933
934 Array<int> dims;
935
936 // Check for type information.
937
938 if (nargin > 0 && args(nargin-1).is_string ())
939 {
940 nargin--;
941
942 // XXX FIXME XXX -- allow type of the resulting matrix to be
943 // specified, e.g.
944 //
945 // zeros(n1, n2, ..., 'real')
946 // zeros(n1, n2, ..., 'complex')
947 //
948 // type = get_type (args(nargin).string_value ());
949 }
950
951 // determine matrix dimension
952
931 switch (nargin) 953 switch (nargin)
932 { 954 {
933 case 0: 955 case 0:
934 retval = val; 956 ndim = 0;
957 type = 0;
935 break; 958 break;
936 959
937 case 1: 960 case 1:
938 { 961 get_dimensions (args(0), fcn, dims);
939 int nr, nc;
940 get_dimensions (args(0), fcn, nr, nc);
941
942 if (! error_state)
943 retval = Matrix (nr, nc, val);
944 }
945 break; 962 break;
946 963
947 case 2: 964 default:
948 { 965 {
949 int nr, nc; 966 dims.resize (nargin);
950 get_dimensions (args(0), args(1), fcn, nr, nc); 967
951 968 for (int i = 0; i < nargin; i++)
952 if (! error_state) 969 {
953 retval = Matrix (nr, nc, val); 970 dims(i) = args(i).is_empty () ? 0 : args(i).nint_value ();
954 } 971
955 break; 972 if (error_state)
956 973 {
957 default: 974 error ("%s: expecting scalar arguments", fcn);
958 print_usage (fcn); 975 break;
959 break; 976 }
977 }
978 }
979 break;
980 }
981
982 if (! error_state)
983 {
984 ndim = dims.length ();
985
986 check_dimensions (dims, fcn);
987
988 if (! error_state)
989 {
990 // Construct either scalar, matrix or N-d array.
991
992 switch (ndim)
993 {
994 case 0:
995 retval = val;
996 break;
997
998 case 1:
999 retval = Matrix (dims(0), dims(0), val);
1000 break;
1001
1002 case 2:
1003 retval = Matrix (dims(0), dims(1), val);
1004 break;
1005
1006 default:
1007 retval = ArrayN<double> (dims, val);
1008 break;
1009 }
1010 }
960 } 1011 }
961 1012
962 return retval; 1013 return retval;
963 } 1014 }
964 1015
965 DEFUN (ones, args, , 1016 DEFUN (ones, args, ,
966 "-*- texinfo -*-\n\ 1017 "-*- texinfo -*-\n\
967 @deftypefn {Built-in Function} {} ones (@var{x})\n\ 1018 @deftypefn {Built-in Function} {} ones (@var{x})\n\
968 @deftypefnx {Built-in Function} {} ones (@var{n}, @var{m})\n\ 1019 @deftypefnx {Built-in Function} {} ones (@var{n}, @var{m})\n\
969 Return a matrix whose elements are all 1. The arguments are handled\n\ 1020 @deftypefnx {Built-in Function} {} ones (@var{n}, @var{m}, @var{k},...)\n\
970 the same as the arguments for @code{eye}.\n\ 1021 Return a matrix or N-dimensional array whose elements are all 1.\n\
1022 The arguments are handled the same as the arguments for @code{eye}.\n\
971 \n\ 1023 \n\
972 If you need to create a matrix whose values are all the same, you should\n\ 1024 If you need to create a matrix whose values are all the same, you should\n\
973 use an expression like\n\ 1025 use an expression like\n\
974 \n\ 1026 \n\
975 @example\n\ 1027 @example\n\
982 1034
983 DEFUN (zeros, args, , 1035 DEFUN (zeros, args, ,
984 "-*- texinfo -*-\n\ 1036 "-*- texinfo -*-\n\
985 @deftypefn {Built-in Function} {} zeros (@var{x})\n\ 1037 @deftypefn {Built-in Function} {} zeros (@var{x})\n\
986 @deftypefnx {Built-in Function} {} zeros (@var{n}, @var{m})\n\ 1038 @deftypefnx {Built-in Function} {} zeros (@var{n}, @var{m})\n\
987 Return a matrix whose elements are all 0. The arguments are handled\n\ 1039 @deftypefnx {Built-in Function} {} zeros (@var{n}, @var{m}, @var{k},...)\n\
988 the same as the arguments for @code{eye}.\n\ 1040 Return a matrix or N-dimensional array whose elements are all 0.\n\
1041 The arguments are handled the same as the arguments for @code{eye}.\n\
989 @end deftypefn") 1042 @end deftypefn")
990 { 1043 {
991 return fill_matrix (args, 0.0, "zeros"); 1044 return fill_matrix (args, 0.0, "zeros");
992 } 1045 }
993 1046