Mercurial > octave-nkf
comparison libinterp/corefcn/data.cc @ 20594:a05a0432dff4
eliminate more simple uses of error_state
* data.cc: Eliminate simple uses of error_state.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 05 Oct 2015 23:42:49 -0400 |
parents | dd6345fd8a97 |
children | 32a0bf9906c1 |
comparison
equal
deleted
inserted
replaced
20593:c41595061186 | 20594:a05a0432dff4 |
---|---|
411 retval = do_hypot (args(0), args(1)); | 411 retval = do_hypot (args(0), args(1)); |
412 } | 412 } |
413 else if (nargin >= 3) | 413 else if (nargin >= 3) |
414 { | 414 { |
415 retval = args(0); | 415 retval = args(0); |
416 for (int i = 1; i < nargin && ! error_state; i++) | 416 for (int i = 1; i < nargin; i++) |
417 retval = do_hypot (retval, args(i)); | 417 retval = do_hypot (retval, args(i)); |
418 } | 418 } |
419 else | 419 else |
420 print_usage (); | 420 print_usage (); |
421 | 421 |
934 | 934 |
935 #define NATIVE_REDUCTION_1(FCN, TYPE, DIM) \ | 935 #define NATIVE_REDUCTION_1(FCN, TYPE, DIM) \ |
936 (arg.is_ ## TYPE ## _type ()) \ | 936 (arg.is_ ## TYPE ## _type ()) \ |
937 { \ | 937 { \ |
938 TYPE ## NDArray tmp = arg. TYPE ##_array_value (); \ | 938 TYPE ## NDArray tmp = arg. TYPE ##_array_value (); \ |
939 \ | 939 \ |
940 if (! error_state) \ | 940 retval = tmp.FCN (DIM); \ |
941 { \ | |
942 retval = tmp.FCN (DIM); \ | |
943 } \ | |
944 } | 941 } |
945 | 942 |
946 #define NATIVE_REDUCTION(FCN, BOOL_FCN) \ | 943 #define NATIVE_REDUCTION(FCN, BOOL_FCN) \ |
947 \ | 944 \ |
948 octave_value retval; \ | 945 octave_value retval; \ |
949 \ | 946 \ |
950 int nargin = args.length (); \ | 947 int nargin = args.length (); \ |
951 \ | 948 \ |
952 bool isnative = false; \ | 949 bool isnative = false; \ |
953 bool isdouble = false; \ | 950 bool isdouble = false; \ |
954 \ | 951 \ |
955 if (nargin > 1 && args(nargin - 1).is_string ()) \ | 952 if (nargin > 1 && args(nargin - 1).is_string ()) \ |
956 { \ | 953 { \ |
957 std::string str = args(nargin - 1).string_value (); \ | 954 std::string str = args(nargin - 1).string_value (); \ |
958 \ | 955 \ |
959 if (str == "native") \ | 956 if (str == "native") \ |
960 isnative = true; \ | 957 isnative = true; \ |
961 else if (str == "double") \ | 958 else if (str == "double") \ |
962 isdouble = true; \ | 959 isdouble = true; \ |
963 else \ | 960 else \ |
964 error ("sum: unrecognized string argument"); \ | 961 error ("sum: unrecognized string argument"); \ |
965 nargin --; \ | 962 nargin --; \ |
966 } \ | 963 } \ |
967 \ | 964 \ |
968 if (nargin == 1 || nargin == 2) \ | 965 if (nargin == 1 || nargin == 2) \ |
969 { \ | 966 { \ |
970 octave_value arg = args(0); \ | 967 octave_value arg = args(0); \ |
971 \ | 968 \ |
972 int dim = (nargin == 1 ? -1 : args(1).int_value (true) - 1); \ | 969 int dim = (nargin == 1 ? -1 : args(1).int_value (true) - 1); \ |
973 \ | 970 \ |
974 if (! error_state) \ | 971 if (dim >= -1) \ |
975 { \ | 972 { \ |
976 if (dim >= -1) \ | 973 if (arg.is_sparse_type ()) \ |
977 { \ | 974 { \ |
978 if (arg.is_sparse_type ()) \ | 975 if (arg.is_real_type ()) \ |
979 { \ | 976 { \ |
980 if (arg.is_real_type ()) \ | 977 SparseMatrix tmp = arg.sparse_matrix_value (); \ |
981 { \ | 978 \ |
982 SparseMatrix tmp = arg.sparse_matrix_value (); \ | 979 retval = tmp.FCN (dim); \ |
983 \ | |
984 if (! error_state) \ | |
985 retval = tmp.FCN (dim); \ | |
986 } \ | |
987 else \ | |
988 { \ | |
989 SparseComplexMatrix tmp \ | |
990 = arg.sparse_complex_matrix_value (); \ | |
991 \ | |
992 if (! error_state) \ | |
993 retval = tmp.FCN (dim); \ | |
994 } \ | |
995 } \ | 980 } \ |
996 else \ | 981 else \ |
997 { \ | 982 { \ |
998 if (isnative) \ | 983 SparseComplexMatrix tmp \ |
984 = arg.sparse_complex_matrix_value (); \ | |
985 \ | |
986 retval = tmp.FCN (dim); \ | |
987 } \ | |
988 } \ | |
989 else \ | |
990 { \ | |
991 if (isnative) \ | |
992 { \ | |
993 if NATIVE_REDUCTION_1 (FCN, uint8, dim) \ | |
994 else if NATIVE_REDUCTION_1 (FCN, uint16, dim) \ | |
995 else if NATIVE_REDUCTION_1 (FCN, uint32, dim) \ | |
996 else if NATIVE_REDUCTION_1 (FCN, uint64, dim) \ | |
997 else if NATIVE_REDUCTION_1 (FCN, int8, dim) \ | |
998 else if NATIVE_REDUCTION_1 (FCN, int16, dim) \ | |
999 else if NATIVE_REDUCTION_1 (FCN, int32, dim) \ | |
1000 else if NATIVE_REDUCTION_1 (FCN, int64, dim) \ | |
1001 else if (arg.is_bool_type ()) \ | |
999 { \ | 1002 { \ |
1000 if NATIVE_REDUCTION_1 (FCN, uint8, dim) \ | 1003 boolNDArray tmp = arg.bool_array_value (); \ |
1001 else if NATIVE_REDUCTION_1 (FCN, uint16, dim) \ | 1004 \ |
1002 else if NATIVE_REDUCTION_1 (FCN, uint32, dim) \ | 1005 retval = boolNDArray (tmp.BOOL_FCN (dim)); \ |
1003 else if NATIVE_REDUCTION_1 (FCN, uint64, dim) \ | 1006 } \ |
1004 else if NATIVE_REDUCTION_1 (FCN, int8, dim) \ | 1007 else if (arg.is_char_matrix ()) \ |
1005 else if NATIVE_REDUCTION_1 (FCN, int16, dim) \ | 1008 { \ |
1006 else if NATIVE_REDUCTION_1 (FCN, int32, dim) \ | 1009 error (#FCN, ": invalid char type"); \ |
1007 else if NATIVE_REDUCTION_1 (FCN, int64, dim) \ | 1010 } \ |
1008 else if (arg.is_bool_type ()) \ | 1011 else if (!isdouble && arg.is_single_type ()) \ |
1012 { \ | |
1013 if (arg.is_complex_type ()) \ | |
1009 { \ | 1014 { \ |
1010 boolNDArray tmp = arg.bool_array_value (); \ | 1015 FloatComplexNDArray tmp = \ |
1011 if (! error_state) \ | 1016 arg.float_complex_array_value (); \ |
1012 retval = boolNDArray (tmp.BOOL_FCN (dim)); \ | 1017 \ |
1013 } \ | 1018 retval = tmp.FCN (dim); \ |
1014 else if (arg.is_char_matrix ()) \ | |
1015 { \ | |
1016 error (#FCN, ": invalid char type"); \ | |
1017 } \ | |
1018 else if (!isdouble && arg.is_single_type ()) \ | |
1019 { \ | |
1020 if (arg.is_complex_type ()) \ | |
1021 { \ | |
1022 FloatComplexNDArray tmp = \ | |
1023 arg.float_complex_array_value (); \ | |
1024 \ | |
1025 if (! error_state) \ | |
1026 retval = tmp.FCN (dim); \ | |
1027 } \ | |
1028 else if (arg.is_real_type ()) \ | |
1029 { \ | |
1030 FloatNDArray tmp = arg.float_array_value (); \ | |
1031 \ | |
1032 if (! error_state) \ | |
1033 retval = tmp.FCN (dim); \ | |
1034 } \ | |
1035 } \ | |
1036 else if (arg.is_complex_type ()) \ | |
1037 { \ | |
1038 ComplexNDArray tmp = arg.complex_array_value (); \ | |
1039 \ | |
1040 if (! error_state) \ | |
1041 retval = tmp.FCN (dim); \ | |
1042 } \ | 1019 } \ |
1043 else if (arg.is_real_type ()) \ | 1020 else if (arg.is_real_type ()) \ |
1044 { \ | 1021 { \ |
1045 NDArray tmp = arg.array_value (); \ | 1022 FloatNDArray tmp = arg.float_array_value (); \ |
1046 \ | 1023 \ |
1047 if (! error_state) \ | 1024 retval = tmp.FCN (dim); \ |
1048 retval = tmp.FCN (dim); \ | |
1049 } \ | |
1050 else \ | |
1051 { \ | |
1052 gripe_wrong_type_arg (#FCN, arg); \ | |
1053 return retval; \ | |
1054 } \ | 1025 } \ |
1055 } \ | 1026 } \ |
1056 else if (arg.is_bool_type ()) \ | 1027 else if (arg.is_complex_type ()) \ |
1057 { \ | 1028 { \ |
1058 boolNDArray tmp = arg.bool_array_value (); \ | 1029 ComplexNDArray tmp = arg.complex_array_value (); \ |
1059 if (! error_state) \ | 1030 \ |
1060 retval = tmp.FCN (dim); \ | 1031 retval = tmp.FCN (dim); \ |
1061 } \ | |
1062 else if (!isdouble && arg.is_single_type ()) \ | |
1063 { \ | |
1064 if (arg.is_real_type ()) \ | |
1065 { \ | |
1066 FloatNDArray tmp = arg.float_array_value (); \ | |
1067 \ | |
1068 if (! error_state) \ | |
1069 retval = tmp.FCN (dim); \ | |
1070 } \ | |
1071 else if (arg.is_complex_type ()) \ | |
1072 { \ | |
1073 FloatComplexNDArray tmp = \ | |
1074 arg.float_complex_array_value (); \ | |
1075 \ | |
1076 if (! error_state) \ | |
1077 retval = tmp.FCN (dim); \ | |
1078 } \ | |
1079 } \ | 1032 } \ |
1080 else if (arg.is_real_type ()) \ | 1033 else if (arg.is_real_type ()) \ |
1081 { \ | 1034 { \ |
1082 NDArray tmp = arg.array_value (); \ | 1035 NDArray tmp = arg.array_value (); \ |
1083 \ | 1036 \ |
1084 if (! error_state) \ | 1037 retval = tmp.FCN (dim); \ |
1085 retval = tmp.FCN (dim); \ | |
1086 } \ | |
1087 else if (arg.is_complex_type ()) \ | |
1088 { \ | |
1089 ComplexNDArray tmp = arg.complex_array_value (); \ | |
1090 \ | |
1091 if (! error_state) \ | |
1092 retval = tmp.FCN (dim); \ | |
1093 } \ | 1038 } \ |
1094 else \ | 1039 else \ |
1095 { \ | 1040 { \ |
1096 gripe_wrong_type_arg (#FCN, arg); \ | 1041 gripe_wrong_type_arg (#FCN, arg); \ |
1097 return retval; \ | 1042 return retval; \ |
1098 } \ | 1043 } \ |
1099 } \ | 1044 } \ |
1045 else if (arg.is_bool_type ()) \ | |
1046 { \ | |
1047 boolNDArray tmp = arg.bool_array_value (); \ | |
1048 \ | |
1049 retval = tmp.FCN (dim); \ | |
1050 } \ | |
1051 else if (!isdouble && arg.is_single_type ()) \ | |
1052 { \ | |
1053 if (arg.is_real_type ()) \ | |
1054 { \ | |
1055 FloatNDArray tmp = arg.float_array_value (); \ | |
1056 \ | |
1057 retval = tmp.FCN (dim); \ | |
1058 } \ | |
1059 else if (arg.is_complex_type ()) \ | |
1060 { \ | |
1061 FloatComplexNDArray tmp = \ | |
1062 arg.float_complex_array_value (); \ | |
1063 \ | |
1064 retval = tmp.FCN (dim); \ | |
1065 } \ | |
1066 } \ | |
1067 else if (arg.is_real_type ()) \ | |
1068 { \ | |
1069 NDArray tmp = arg.array_value (); \ | |
1070 \ | |
1071 retval = tmp.FCN (dim); \ | |
1072 } \ | |
1073 else if (arg.is_complex_type ()) \ | |
1074 { \ | |
1075 ComplexNDArray tmp = arg.complex_array_value (); \ | |
1076 \ | |
1077 retval = tmp.FCN (dim); \ | |
1078 } \ | |
1079 else \ | |
1080 { \ | |
1081 gripe_wrong_type_arg (#FCN, arg); \ | |
1082 return retval; \ | |
1083 } \ | |
1100 } \ | 1084 } \ |
1101 else \ | 1085 else \ |
1102 error (#FCN ": invalid dimension argument = %d", dim + 1); \ | 1086 error (#FCN ": invalid dimension argument = %d", dim + 1); \ |
1103 } \ | 1087 } \ |
1104 \ | 1088 \ |
1105 } \ | 1089 } \ |
1106 else \ | 1090 else \ |
1107 print_usage (); \ | 1091 print_usage (); \ |
1108 \ | 1092 \ |
1109 return retval | 1093 return retval |
1118 { \ | 1102 { \ |
1119 octave_value arg = args(0); \ | 1103 octave_value arg = args(0); \ |
1120 \ | 1104 \ |
1121 int dim = (nargin == 1 ? -1 : args(1).int_value (true) - 1); \ | 1105 int dim = (nargin == 1 ? -1 : args(1).int_value (true) - 1); \ |
1122 \ | 1106 \ |
1123 if (! error_state) \ | 1107 if (dim >= -1) \ |
1124 { \ | 1108 { \ |
1125 if (dim >= -1) \ | 1109 if (arg.is_real_type ()) \ |
1126 { \ | 1110 { \ |
1127 if (arg.is_real_type ()) \ | 1111 if (arg.is_sparse_type ()) \ |
1128 { \ | 1112 { \ |
1129 if (arg.is_sparse_type ()) \ | 1113 SparseMatrix tmp = arg.sparse_matrix_value (); \ |
1130 { \ | 1114 \ |
1131 SparseMatrix tmp = arg.sparse_matrix_value (); \ | 1115 retval = tmp.FCN (dim); \ |
1132 \ | |
1133 if (! error_state) \ | |
1134 retval = tmp.FCN (dim); \ | |
1135 } \ | |
1136 else if (arg.is_single_type ()) \ | |
1137 { \ | |
1138 FloatNDArray tmp = arg.float_array_value (); \ | |
1139 \ | |
1140 if (! error_state) \ | |
1141 retval = tmp.FCN (dim); \ | |
1142 } \ | |
1143 else \ | |
1144 { \ | |
1145 NDArray tmp = arg.array_value (); \ | |
1146 \ | |
1147 if (! error_state) \ | |
1148 retval = tmp.FCN (dim); \ | |
1149 } \ | |
1150 } \ | 1116 } \ |
1151 else if (arg.is_complex_type ()) \ | 1117 else if (arg.is_single_type ()) \ |
1152 { \ | 1118 { \ |
1153 if (arg.is_sparse_type ()) \ | 1119 FloatNDArray tmp = arg.float_array_value (); \ |
1154 { \ | 1120 \ |
1155 SparseComplexMatrix tmp = arg.sparse_complex_matrix_value (); \ | 1121 retval = tmp.FCN (dim); \ |
1156 \ | |
1157 if (! error_state) \ | |
1158 retval = tmp.FCN (dim); \ | |
1159 } \ | |
1160 else if (arg.is_single_type ()) \ | |
1161 { \ | |
1162 FloatComplexNDArray tmp \ | |
1163 = arg.float_complex_array_value (); \ | |
1164 \ | |
1165 if (! error_state) \ | |
1166 retval = tmp.FCN (dim); \ | |
1167 } \ | |
1168 else \ | |
1169 { \ | |
1170 ComplexNDArray tmp = arg.complex_array_value (); \ | |
1171 \ | |
1172 if (! error_state) \ | |
1173 retval = tmp.FCN (dim); \ | |
1174 } \ | |
1175 } \ | 1122 } \ |
1176 else \ | 1123 else \ |
1177 { \ | 1124 { \ |
1178 gripe_wrong_type_arg (#FCN, arg); \ | 1125 NDArray tmp = arg.array_value (); \ |
1179 return retval; \ | 1126 \ |
1127 retval = tmp.FCN (dim); \ | |
1128 } \ | |
1129 } \ | |
1130 else if (arg.is_complex_type ()) \ | |
1131 { \ | |
1132 if (arg.is_sparse_type ()) \ | |
1133 { \ | |
1134 SparseComplexMatrix tmp = arg.sparse_complex_matrix_value (); \ | |
1135 \ | |
1136 retval = tmp.FCN (dim); \ | |
1137 } \ | |
1138 else if (arg.is_single_type ()) \ | |
1139 { \ | |
1140 FloatComplexNDArray tmp \ | |
1141 = arg.float_complex_array_value (); \ | |
1142 \ | |
1143 retval = tmp.FCN (dim); \ | |
1144 } \ | |
1145 else \ | |
1146 { \ | |
1147 ComplexNDArray tmp = arg.complex_array_value (); \ | |
1148 \ | |
1149 retval = tmp.FCN (dim); \ | |
1180 } \ | 1150 } \ |
1181 } \ | 1151 } \ |
1182 else \ | 1152 else \ |
1183 error (#FCN ": invalid dimension argument = %d", dim + 1); \ | 1153 { \ |
1154 gripe_wrong_type_arg (#FCN, arg); \ | |
1155 return retval; \ | |
1156 } \ | |
1184 } \ | 1157 } \ |
1158 else \ | |
1159 error (#FCN ": invalid dimension argument = %d", dim + 1); \ | |
1185 } \ | 1160 } \ |
1186 else \ | 1161 else \ |
1187 print_usage (); \ | 1162 print_usage (); \ |
1188 \ | 1163 \ |
1189 return retval | 1164 return retval |
1255 else | 1230 else |
1256 error ("cumsum: unrecognized string argument"); | 1231 error ("cumsum: unrecognized string argument"); |
1257 nargin --; | 1232 nargin --; |
1258 } | 1233 } |
1259 | 1234 |
1260 if (error_state) | |
1261 return retval; | |
1262 | |
1263 if (nargin == 1 || nargin == 2) | 1235 if (nargin == 1 || nargin == 2) |
1264 { | 1236 { |
1265 octave_value arg = args(0); | 1237 octave_value arg = args(0); |
1266 | 1238 |
1267 int dim = -1; | 1239 int dim = -1; |
1270 dim = args(1).int_value () - 1; | 1242 dim = args(1).int_value () - 1; |
1271 if (dim < 0) | 1243 if (dim < 0) |
1272 error ("cumsum: invalid dimension argument = %d", dim + 1); | 1244 error ("cumsum: invalid dimension argument = %d", dim + 1); |
1273 } | 1245 } |
1274 | 1246 |
1275 if (! error_state) | 1247 switch (arg.builtin_type ()) |
1276 { | 1248 { |
1277 switch (arg.builtin_type ()) | 1249 case btyp_double: |
1250 if (arg.is_sparse_type ()) | |
1251 retval = arg.sparse_matrix_value ().cumsum (dim); | |
1252 else | |
1253 retval = arg.array_value ().cumsum (dim); | |
1254 break; | |
1255 case btyp_complex: | |
1256 if (arg.is_sparse_type ()) | |
1257 retval = arg.sparse_complex_matrix_value ().cumsum (dim); | |
1258 else | |
1259 retval = arg.complex_array_value ().cumsum (dim); | |
1260 break; | |
1261 case btyp_float: | |
1262 if (isdouble) | |
1263 retval = arg.array_value ().cumsum (dim); | |
1264 else | |
1265 retval = arg.float_array_value ().cumsum (dim); | |
1266 break; | |
1267 case btyp_float_complex: | |
1268 if (isdouble) | |
1269 retval = arg.complex_array_value ().cumsum (dim); | |
1270 else | |
1271 retval = arg.float_complex_array_value ().cumsum (dim); | |
1272 break; | |
1273 | |
1274 #define MAKE_INT_BRANCH(X) \ | |
1275 case btyp_ ## X: \ | |
1276 if (isnative) \ | |
1277 retval = arg.X ## _array_value ().cumsum (dim); \ | |
1278 else \ | |
1279 retval = arg.array_value ().cumsum (dim); \ | |
1280 break; | |
1281 MAKE_INT_BRANCH (int8); | |
1282 MAKE_INT_BRANCH (int16); | |
1283 MAKE_INT_BRANCH (int32); | |
1284 MAKE_INT_BRANCH (int64); | |
1285 MAKE_INT_BRANCH (uint8); | |
1286 MAKE_INT_BRANCH (uint16); | |
1287 MAKE_INT_BRANCH (uint32); | |
1288 MAKE_INT_BRANCH (uint64); | |
1289 #undef MAKE_INT_BRANCH | |
1290 | |
1291 case btyp_bool: | |
1292 if (arg.is_sparse_type ()) | |
1278 { | 1293 { |
1279 case btyp_double: | 1294 SparseMatrix cs = arg.sparse_matrix_value ().cumsum (dim); |
1280 if (arg.is_sparse_type ()) | 1295 if (isnative) |
1281 retval = arg.sparse_matrix_value ().cumsum (dim); | 1296 retval = cs != 0.0; |
1282 else | 1297 else |
1283 retval = arg.array_value ().cumsum (dim); | 1298 retval = cs; |
1284 break; | 1299 } |
1285 case btyp_complex: | 1300 else |
1286 if (arg.is_sparse_type ()) | 1301 { |
1287 retval = arg.sparse_complex_matrix_value ().cumsum (dim); | 1302 NDArray cs = arg.bool_array_value ().cumsum (dim); |
1303 if (isnative) | |
1304 retval = cs != 0.0; | |
1288 else | 1305 else |
1289 retval = arg.complex_array_value ().cumsum (dim); | 1306 retval = cs; |
1290 break; | |
1291 case btyp_float: | |
1292 if (isdouble) | |
1293 retval = arg.array_value ().cumsum (dim); | |
1294 else | |
1295 retval = arg.float_array_value ().cumsum (dim); | |
1296 break; | |
1297 case btyp_float_complex: | |
1298 if (isdouble) | |
1299 retval = arg.complex_array_value ().cumsum (dim); | |
1300 else | |
1301 retval = arg.float_complex_array_value ().cumsum (dim); | |
1302 break; | |
1303 | |
1304 #define MAKE_INT_BRANCH(X) \ | |
1305 case btyp_ ## X: \ | |
1306 if (isnative) \ | |
1307 retval = arg.X ## _array_value ().cumsum (dim); \ | |
1308 else \ | |
1309 retval = arg.array_value ().cumsum (dim); \ | |
1310 break; | |
1311 MAKE_INT_BRANCH (int8); | |
1312 MAKE_INT_BRANCH (int16); | |
1313 MAKE_INT_BRANCH (int32); | |
1314 MAKE_INT_BRANCH (int64); | |
1315 MAKE_INT_BRANCH (uint8); | |
1316 MAKE_INT_BRANCH (uint16); | |
1317 MAKE_INT_BRANCH (uint32); | |
1318 MAKE_INT_BRANCH (uint64); | |
1319 #undef MAKE_INT_BRANCH | |
1320 | |
1321 case btyp_bool: | |
1322 if (arg.is_sparse_type ()) | |
1323 { | |
1324 SparseMatrix cs = arg.sparse_matrix_value ().cumsum (dim); | |
1325 if (isnative) | |
1326 retval = cs != 0.0; | |
1327 else | |
1328 retval = cs; | |
1329 } | |
1330 else | |
1331 { | |
1332 NDArray cs = arg.bool_array_value ().cumsum (dim); | |
1333 if (isnative) | |
1334 retval = cs != 0.0; | |
1335 else | |
1336 retval = cs; | |
1337 } | |
1338 break; | |
1339 | |
1340 default: | |
1341 gripe_wrong_type_arg ("cumsum", arg); | |
1342 } | 1307 } |
1308 break; | |
1309 | |
1310 default: | |
1311 gripe_wrong_type_arg ("cumsum", arg); | |
1343 } | 1312 } |
1344 } | 1313 } |
1345 else | 1314 else |
1346 print_usage (); | 1315 print_usage (); |
1347 | 1316 |
1554 else | 1523 else |
1555 error ("prod: unrecognized type argument '%s'", str.c_str ()); | 1524 error ("prod: unrecognized type argument '%s'", str.c_str ()); |
1556 nargin --; | 1525 nargin --; |
1557 } | 1526 } |
1558 | 1527 |
1559 if (error_state) | |
1560 return retval; | |
1561 | |
1562 if (nargin == 1 || nargin == 2) | 1528 if (nargin == 1 || nargin == 2) |
1563 { | 1529 { |
1564 octave_value arg = args(0); | 1530 octave_value arg = args(0); |
1565 | 1531 |
1566 int dim = -1; | 1532 int dim = -1; |
1569 dim = args(1).int_value () - 1; | 1535 dim = args(1).int_value () - 1; |
1570 if (dim < 0) | 1536 if (dim < 0) |
1571 error ("prod: invalid dimension DIM = %d", dim + 1); | 1537 error ("prod: invalid dimension DIM = %d", dim + 1); |
1572 } | 1538 } |
1573 | 1539 |
1574 if (! error_state) | 1540 switch (arg.builtin_type ()) |
1575 { | 1541 { |
1576 switch (arg.builtin_type ()) | 1542 case btyp_double: |
1543 if (arg.is_sparse_type ()) | |
1544 retval = arg.sparse_matrix_value ().prod (dim); | |
1545 else | |
1546 retval = arg.array_value ().prod (dim); | |
1547 break; | |
1548 case btyp_complex: | |
1549 if (arg.is_sparse_type ()) | |
1550 retval = arg.sparse_complex_matrix_value ().prod (dim); | |
1551 else | |
1552 retval = arg.complex_array_value ().prod (dim); | |
1553 break; | |
1554 case btyp_float: | |
1555 if (isdouble) | |
1556 retval = arg.float_array_value ().dprod (dim); | |
1557 else | |
1558 retval = arg.float_array_value ().prod (dim); | |
1559 break; | |
1560 case btyp_float_complex: | |
1561 if (isdouble) | |
1562 retval = arg.float_complex_array_value ().dprod (dim); | |
1563 else | |
1564 retval = arg.float_complex_array_value ().prod (dim); | |
1565 break; | |
1566 | |
1567 #define MAKE_INT_BRANCH(X) \ | |
1568 case btyp_ ## X: \ | |
1569 if (isnative) \ | |
1570 retval = arg.X ## _array_value ().prod (dim); \ | |
1571 else \ | |
1572 retval = arg.array_value ().prod (dim); \ | |
1573 break; | |
1574 MAKE_INT_BRANCH (int8); | |
1575 MAKE_INT_BRANCH (int16); | |
1576 MAKE_INT_BRANCH (int32); | |
1577 MAKE_INT_BRANCH (int64); | |
1578 MAKE_INT_BRANCH (uint8); | |
1579 MAKE_INT_BRANCH (uint16); | |
1580 MAKE_INT_BRANCH (uint32); | |
1581 MAKE_INT_BRANCH (uint64); | |
1582 #undef MAKE_INT_BRANCH | |
1583 | |
1584 // GAGME: Accursed Matlab compatibility... | |
1585 case btyp_char: | |
1586 retval = arg.array_value (true).prod (dim); | |
1587 break; | |
1588 case btyp_bool: | |
1589 if (arg.is_sparse_type ()) | |
1577 { | 1590 { |
1578 case btyp_double: | 1591 if (isnative) |
1579 if (arg.is_sparse_type ()) | 1592 retval = arg.sparse_bool_matrix_value ().all (dim); |
1593 else | |
1580 retval = arg.sparse_matrix_value ().prod (dim); | 1594 retval = arg.sparse_matrix_value ().prod (dim); |
1581 else | |
1582 retval = arg.array_value ().prod (dim); | |
1583 break; | |
1584 case btyp_complex: | |
1585 if (arg.is_sparse_type ()) | |
1586 retval = arg.sparse_complex_matrix_value ().prod (dim); | |
1587 else | |
1588 retval = arg.complex_array_value ().prod (dim); | |
1589 break; | |
1590 case btyp_float: | |
1591 if (isdouble) | |
1592 retval = arg.float_array_value ().dprod (dim); | |
1593 else | |
1594 retval = arg.float_array_value ().prod (dim); | |
1595 break; | |
1596 case btyp_float_complex: | |
1597 if (isdouble) | |
1598 retval = arg.float_complex_array_value ().dprod (dim); | |
1599 else | |
1600 retval = arg.float_complex_array_value ().prod (dim); | |
1601 break; | |
1602 | |
1603 #define MAKE_INT_BRANCH(X) \ | |
1604 case btyp_ ## X: \ | |
1605 if (isnative) \ | |
1606 retval = arg.X ## _array_value ().prod (dim); \ | |
1607 else \ | |
1608 retval = arg.array_value ().prod (dim); \ | |
1609 break; | |
1610 MAKE_INT_BRANCH (int8); | |
1611 MAKE_INT_BRANCH (int16); | |
1612 MAKE_INT_BRANCH (int32); | |
1613 MAKE_INT_BRANCH (int64); | |
1614 MAKE_INT_BRANCH (uint8); | |
1615 MAKE_INT_BRANCH (uint16); | |
1616 MAKE_INT_BRANCH (uint32); | |
1617 MAKE_INT_BRANCH (uint64); | |
1618 #undef MAKE_INT_BRANCH | |
1619 | |
1620 // GAGME: Accursed Matlab compatibility... | |
1621 case btyp_char: | |
1622 retval = arg.array_value (true).prod (dim); | |
1623 break; | |
1624 case btyp_bool: | |
1625 if (arg.is_sparse_type ()) | |
1626 { | |
1627 if (isnative) | |
1628 retval = arg.sparse_bool_matrix_value ().all (dim); | |
1629 else | |
1630 retval = arg.sparse_matrix_value ().prod (dim); | |
1631 } | |
1632 else if (isnative) | |
1633 retval = arg.bool_array_value ().all (dim); | |
1634 else | |
1635 retval = NDArray (arg.bool_array_value ().all (dim)); | |
1636 break; | |
1637 | |
1638 default: | |
1639 gripe_wrong_type_arg ("prod", arg); | |
1640 } | 1595 } |
1596 else if (isnative) | |
1597 retval = arg.bool_array_value ().all (dim); | |
1598 else | |
1599 retval = NDArray (arg.bool_array_value ().all (dim)); | |
1600 break; | |
1601 | |
1602 default: | |
1603 gripe_wrong_type_arg ("prod", arg); | |
1641 } | 1604 } |
1642 } | 1605 } |
1643 else | 1606 else |
1644 print_usage (); | 1607 print_usage (); |
1645 | 1608 |
1744 dv.resize (dim+1, 1); | 1707 dv.resize (dim+1, 1); |
1745 dv(dim) = n_args; | 1708 dv(dim) = n_args; |
1746 | 1709 |
1747 result.clear (dv); | 1710 result.clear (dv); |
1748 | 1711 |
1749 for (int j = 0; j < n_args && ! error_state; j++) | 1712 for (int j = 0; j < n_args; j++) |
1750 { | 1713 { |
1751 octave_quit (); | 1714 octave_quit (); |
1752 | 1715 |
1753 result(j) = octave_value_extract<T> (args(j)); | 1716 result(j) = octave_value_extract<T> (args(j)); |
1754 } | 1717 } |
1755 } | 1718 } |
1756 else | 1719 else |
1757 { | 1720 { |
1758 OCTAVE_LOCAL_BUFFER (Array<T>, array_list, n_args); | 1721 OCTAVE_LOCAL_BUFFER (Array<T>, array_list, n_args); |
1759 | 1722 |
1760 for (int j = 0; j < n_args && ! error_state; j++) | 1723 for (int j = 0; j < n_args; j++) |
1761 { | 1724 { |
1762 octave_quit (); | 1725 octave_quit (); |
1763 | 1726 |
1764 array_list[j] = octave_value_extract<TYPE> (args(j)); | 1727 array_list[j] = octave_value_extract<TYPE> (args(j)); |
1765 } | 1728 } |
1766 | 1729 |
1767 if (! error_state) | 1730 result = Array<T>::cat (dim, n_args, array_list); |
1768 result = Array<T>::cat (dim, n_args, array_list); | |
1769 } | 1731 } |
1770 } | 1732 } |
1771 | 1733 |
1772 template <class TYPE, class T> | 1734 template <class TYPE, class T> |
1773 static void | 1735 static void |
1776 int dim) | 1738 int dim) |
1777 { | 1739 { |
1778 int n_args = args.length (); | 1740 int n_args = args.length (); |
1779 OCTAVE_LOCAL_BUFFER (Sparse<T>, sparse_list, n_args); | 1741 OCTAVE_LOCAL_BUFFER (Sparse<T>, sparse_list, n_args); |
1780 | 1742 |
1781 for (int j = 0; j < n_args && ! error_state; j++) | 1743 for (int j = 0; j < n_args; j++) |
1782 { | 1744 { |
1783 octave_quit (); | 1745 octave_quit (); |
1784 | 1746 |
1785 sparse_list[j] = octave_value_extract<TYPE> (args(j)); | 1747 sparse_list[j] = octave_value_extract<TYPE> (args(j)); |
1786 } | 1748 } |
1787 | 1749 |
1788 if (! error_state) | 1750 result = Sparse<T>::cat (dim, n_args, sparse_list); |
1789 result = Sparse<T>::cat (dim, n_args, sparse_list); | |
1790 } | 1751 } |
1791 | 1752 |
1792 // Dispatcher. | 1753 // Dispatcher. |
1793 template<class TYPE> | 1754 template<class TYPE> |
1794 static TYPE | 1755 static TYPE |
1808 int dim) | 1769 int dim) |
1809 { | 1770 { |
1810 int n_args = args.length (); | 1771 int n_args = args.length (); |
1811 OCTAVE_LOCAL_BUFFER (MAP, map_list, n_args); | 1772 OCTAVE_LOCAL_BUFFER (MAP, map_list, n_args); |
1812 | 1773 |
1813 for (int j = 0; j < n_args && ! error_state; j++) | 1774 for (int j = 0; j < n_args; j++) |
1814 { | 1775 { |
1815 octave_quit (); | 1776 octave_quit (); |
1816 | 1777 |
1817 map_list[j] = octave_value_extract<MAP> (args(j)); | 1778 map_list[j] = octave_value_extract<MAP> (args(j)); |
1818 } | 1779 } |
1819 | 1780 |
1820 if (! error_state) | 1781 result = octave_map::cat (dim, n_args, map_list); |
1821 result = octave_map::cat (dim, n_args, map_list); | |
1822 } | 1782 } |
1823 | 1783 |
1824 static octave_map | 1784 static octave_map |
1825 do_single_type_concat_map (const octave_value_list& args, | 1785 do_single_type_concat_map (const octave_value_list& args, |
1826 int dim) | 1786 int dim) |
1899 // Have method for dominant type, so call it and let it handle | 1859 // Have method for dominant type, so call it and let it handle |
1900 // conversions. | 1860 // conversions. |
1901 | 1861 |
1902 octave_value_list tmp2 = fcn.do_multi_index_op (1, ovl); | 1862 octave_value_list tmp2 = fcn.do_multi_index_op (1, ovl); |
1903 | 1863 |
1904 if (! error_state) | 1864 if (tmp2.length () > 0) |
1905 { | 1865 retval = tmp2(0); |
1906 if (tmp2.length () > 0) | |
1907 retval = tmp2(0); | |
1908 else | |
1909 { | |
1910 error ("%s/%s method did not return a value", | |
1911 dtype.c_str (), cattype.c_str ()); | |
1912 goto done; | |
1913 } | |
1914 } | |
1915 else | 1866 else |
1916 goto done; | 1867 { |
1868 error ("%s/%s method did not return a value", | |
1869 dtype.c_str (), cattype.c_str ()); | |
1870 goto done; | |
1871 } | |
1917 } | 1872 } |
1918 else | 1873 else |
1919 { | 1874 { |
1920 // No method for dominant type, so attempt type conversions for | 1875 // No method for dominant type, so attempt type conversions for |
1921 // all elements that are not of the dominant type, then do the | 1876 // all elements that are not of the dominant type, then do the |
1931 std::string t1_type = elt.class_name (); | 1886 std::string t1_type = elt.class_name (); |
1932 | 1887 |
1933 if (t1_type == dtype) | 1888 if (t1_type == dtype) |
1934 tmp(j++) = elt; | 1889 tmp(j++) = elt; |
1935 else if (elt.is_object () || ! elt.is_empty ()) | 1890 else if (elt.is_object () || ! elt.is_empty ()) |
1936 { | 1891 tmp(j++) = attempt_type_conversion (elt, dtype); |
1937 tmp(j++) = attempt_type_conversion (elt, dtype); | |
1938 | |
1939 if (error_state) | |
1940 goto done; | |
1941 } | |
1942 } | 1892 } |
1943 | 1893 |
1944 tmp.resize (j); | 1894 tmp.resize (j); |
1945 | 1895 |
1946 octave_map m = do_single_type_concat_map (tmp, dim); | 1896 octave_map m = do_single_type_concat_map (tmp, dim); |
2133 // be some additional setup needed, and so this should be avoided. | 2083 // be some additional setup needed, and so this should be avoided. |
2134 | 2084 |
2135 octave_value tmp = args(0); | 2085 octave_value tmp = args(0); |
2136 tmp = tmp.resize (dim_vector (0,0)).resize (dv); | 2086 tmp = tmp.resize (dim_vector (0,0)).resize (dv); |
2137 | 2087 |
2138 if (error_state) | |
2139 return retval; | |
2140 | |
2141 int dv_len = dv.length (); | 2088 int dv_len = dv.length (); |
2142 Array<octave_idx_type> ra_idx (dim_vector (dv_len, 1), 0); | 2089 Array<octave_idx_type> ra_idx (dim_vector (dv_len, 1), 0); |
2143 | 2090 |
2144 for (int j = 0; j < n_args; j++) | 2091 for (int j = 0; j < n_args; j++) |
2145 { | 2092 { |
2146 // Can't fast return here to skip empty matrices as something | 2093 // Can't fast return here to skip empty matrices as something |
2147 // like cat (1,[],single ([])) must return an empty matrix of | 2094 // like cat (1,[],single ([])) must return an empty matrix of |
2148 // the right type. | 2095 // the right type. |
2149 tmp = do_cat_op (tmp, args(j), ra_idx); | 2096 tmp = do_cat_op (tmp, args(j), ra_idx); |
2150 | |
2151 if (error_state) | |
2152 return retval; | |
2153 | 2097 |
2154 dim_vector dv_tmp = args(j).dims (); | 2098 dim_vector dv_tmp = args(j).dims (); |
2155 | 2099 |
2156 if (dim >= dv_len) | 2100 if (dim >= dv_len) |
2157 { | 2101 { |
2633 for (int i = 0; i < n; i++) | 2577 for (int i = 0; i < n; i++) |
2634 vec(i)--; | 2578 vec(i)--; |
2635 | 2579 |
2636 octave_value ret = args(0).permute (vec, inv); | 2580 octave_value ret = args(0).permute (vec, inv); |
2637 | 2581 |
2638 if (! error_state) | 2582 retval = ret; |
2639 retval = ret; | |
2640 } | 2583 } |
2641 else | 2584 else |
2642 print_usage (); | 2585 print_usage (); |
2643 | 2586 |
2644 return retval; | 2587 return retval; |
3050 else | 2993 else |
3051 error ("sum: unrecognized type argument '%s'", str.c_str ()); | 2994 error ("sum: unrecognized type argument '%s'", str.c_str ()); |
3052 nargin --; | 2995 nargin --; |
3053 } | 2996 } |
3054 | 2997 |
3055 if (error_state) | |
3056 return retval; | |
3057 | |
3058 if (nargin == 1 || nargin == 2) | 2998 if (nargin == 1 || nargin == 2) |
3059 { | 2999 { |
3060 octave_value arg = args(0); | 3000 octave_value arg = args(0); |
3061 | 3001 |
3062 int dim = -1; | 3002 int dim = -1; |
3065 dim = args(1).int_value () - 1; | 3005 dim = args(1).int_value () - 1; |
3066 if (dim < 0) | 3006 if (dim < 0) |
3067 error ("sum: invalid dimension DIM = %d", dim + 1); | 3007 error ("sum: invalid dimension DIM = %d", dim + 1); |
3068 } | 3008 } |
3069 | 3009 |
3070 if (! error_state) | 3010 switch (arg.builtin_type ()) |
3071 { | 3011 { |
3072 switch (arg.builtin_type ()) | 3012 case btyp_double: |
3013 if (arg.is_sparse_type ()) | |
3073 { | 3014 { |
3074 case btyp_double: | 3015 if (isextra) |
3075 if (arg.is_sparse_type ()) | 3016 warning ("sum: 'extra' not yet implemented for sparse matrices"); |
3076 { | 3017 retval = arg.sparse_matrix_value ().sum (dim); |
3077 if (isextra) | 3018 } |
3078 warning ("sum: 'extra' not yet implemented for sparse matrices"); | 3019 else if (isextra) |
3079 retval = arg.sparse_matrix_value ().sum (dim); | 3020 retval = arg.array_value ().xsum (dim); |
3080 } | 3021 else |
3081 else if (isextra) | 3022 retval = arg.array_value ().sum (dim); |
3082 retval = arg.array_value ().xsum (dim); | 3023 break; |
3024 case btyp_complex: | |
3025 if (arg.is_sparse_type ()) | |
3026 { | |
3027 if (isextra) | |
3028 warning ("sum: 'extra' not yet implemented for sparse matrices"); | |
3029 retval = arg.sparse_complex_matrix_value ().sum (dim); | |
3030 } | |
3031 else if (isextra) | |
3032 retval = arg.complex_array_value ().xsum (dim); | |
3033 else | |
3034 retval = arg.complex_array_value ().sum (dim); | |
3035 break; | |
3036 case btyp_float: | |
3037 if (isdouble || isextra) | |
3038 retval = arg.float_array_value ().dsum (dim); | |
3039 else | |
3040 retval = arg.float_array_value ().sum (dim); | |
3041 break; | |
3042 case btyp_float_complex: | |
3043 if (isdouble || isextra) | |
3044 retval = arg.float_complex_array_value ().dsum (dim); | |
3045 else | |
3046 retval = arg.float_complex_array_value ().sum (dim); | |
3047 break; | |
3048 | |
3049 #define MAKE_INT_BRANCH(X) \ | |
3050 case btyp_ ## X: \ | |
3051 if (isnative) \ | |
3052 retval = arg.X ## _array_value ().sum (dim); \ | |
3053 else \ | |
3054 retval = arg.X ## _array_value ().dsum (dim); \ | |
3055 break; | |
3056 MAKE_INT_BRANCH (int8); | |
3057 MAKE_INT_BRANCH (int16); | |
3058 MAKE_INT_BRANCH (int32); | |
3059 MAKE_INT_BRANCH (int64); | |
3060 MAKE_INT_BRANCH (uint8); | |
3061 MAKE_INT_BRANCH (uint16); | |
3062 MAKE_INT_BRANCH (uint32); | |
3063 MAKE_INT_BRANCH (uint64); | |
3064 #undef MAKE_INT_BRANCH | |
3065 | |
3066 // GAGME: Accursed Matlab compatibility... | |
3067 case btyp_char: | |
3068 if (isextra) | |
3069 retval = arg.array_value (true).xsum (dim); | |
3070 else | |
3071 retval = arg.array_value (true).sum (dim); | |
3072 break; | |
3073 case btyp_bool: | |
3074 if (arg.is_sparse_type ()) | |
3075 { | |
3076 if (isnative) | |
3077 retval = arg.sparse_bool_matrix_value ().any (dim); | |
3083 else | 3078 else |
3084 retval = arg.array_value ().sum (dim); | 3079 retval = arg.sparse_bool_matrix_value ().sum (dim); |
3085 break; | |
3086 case btyp_complex: | |
3087 if (arg.is_sparse_type ()) | |
3088 { | |
3089 if (isextra) | |
3090 warning ("sum: 'extra' not yet implemented for sparse matrices"); | |
3091 retval = arg.sparse_complex_matrix_value ().sum (dim); | |
3092 } | |
3093 else if (isextra) | |
3094 retval = arg.complex_array_value ().xsum (dim); | |
3095 else | |
3096 retval = arg.complex_array_value ().sum (dim); | |
3097 break; | |
3098 case btyp_float: | |
3099 if (isdouble || isextra) | |
3100 retval = arg.float_array_value ().dsum (dim); | |
3101 else | |
3102 retval = arg.float_array_value ().sum (dim); | |
3103 break; | |
3104 case btyp_float_complex: | |
3105 if (isdouble || isextra) | |
3106 retval = arg.float_complex_array_value ().dsum (dim); | |
3107 else | |
3108 retval = arg.float_complex_array_value ().sum (dim); | |
3109 break; | |
3110 | |
3111 #define MAKE_INT_BRANCH(X) \ | |
3112 case btyp_ ## X: \ | |
3113 if (isnative) \ | |
3114 retval = arg.X ## _array_value ().sum (dim); \ | |
3115 else \ | |
3116 retval = arg.X ## _array_value ().dsum (dim); \ | |
3117 break; | |
3118 MAKE_INT_BRANCH (int8); | |
3119 MAKE_INT_BRANCH (int16); | |
3120 MAKE_INT_BRANCH (int32); | |
3121 MAKE_INT_BRANCH (int64); | |
3122 MAKE_INT_BRANCH (uint8); | |
3123 MAKE_INT_BRANCH (uint16); | |
3124 MAKE_INT_BRANCH (uint32); | |
3125 MAKE_INT_BRANCH (uint64); | |
3126 #undef MAKE_INT_BRANCH | |
3127 | |
3128 // GAGME: Accursed Matlab compatibility... | |
3129 case btyp_char: | |
3130 if (isextra) | |
3131 retval = arg.array_value (true).xsum (dim); | |
3132 else | |
3133 retval = arg.array_value (true).sum (dim); | |
3134 break; | |
3135 case btyp_bool: | |
3136 if (arg.is_sparse_type ()) | |
3137 { | |
3138 if (isnative) | |
3139 retval = arg.sparse_bool_matrix_value ().any (dim); | |
3140 else | |
3141 retval = arg.sparse_bool_matrix_value ().sum (dim); | |
3142 } | |
3143 else if (isnative) | |
3144 retval = arg.bool_array_value ().any (dim); | |
3145 else | |
3146 retval = arg.bool_array_value ().sum (dim); | |
3147 break; | |
3148 | |
3149 default: | |
3150 gripe_wrong_type_arg ("sum", arg); | |
3151 } | 3080 } |
3081 else if (isnative) | |
3082 retval = arg.bool_array_value ().any (dim); | |
3083 else | |
3084 retval = arg.bool_array_value ().sum (dim); | |
3085 break; | |
3086 | |
3087 default: | |
3088 gripe_wrong_type_arg ("sum", arg); | |
3152 } | 3089 } |
3153 } | 3090 } |
3154 else | 3091 else |
3155 print_usage (); | 3092 print_usage (); |
3156 | 3093 |
3391 { | 3328 { |
3392 if (arg.is_sparse_type ()) | 3329 if (arg.is_sparse_type ()) |
3393 { | 3330 { |
3394 SparseComplexMatrix val = arg.sparse_complex_matrix_value (); | 3331 SparseComplexMatrix val = arg.sparse_complex_matrix_value (); |
3395 | 3332 |
3396 if (! error_state) | 3333 retval = octave_value (new octave_sparse_complex_matrix (val)); |
3397 retval = octave_value (new octave_sparse_complex_matrix (val)); | |
3398 } | 3334 } |
3399 else if (arg.is_single_type ()) | 3335 else if (arg.is_single_type ()) |
3400 { | 3336 { |
3401 if (arg.numel () == 1) | 3337 if (arg.numel () == 1) |
3402 { | 3338 { |
3403 FloatComplex val = arg.float_complex_value (); | 3339 FloatComplex val = arg.float_complex_value (); |
3404 | 3340 |
3405 if (! error_state) | 3341 retval = octave_value (new octave_float_complex (val)); |
3406 retval = octave_value (new octave_float_complex (val)); | |
3407 } | 3342 } |
3408 else | 3343 else |
3409 { | 3344 { |
3410 FloatComplexNDArray val = arg.float_complex_array_value (); | 3345 FloatComplexNDArray val = arg.float_complex_array_value (); |
3411 | 3346 |
3412 if (! error_state) | 3347 retval = octave_value (new octave_float_complex_matrix (val)); |
3413 retval = octave_value (new octave_float_complex_matrix (val)); | |
3414 } | 3348 } |
3415 } | 3349 } |
3416 else | 3350 else |
3417 { | 3351 { |
3418 if (arg.numel () == 1) | 3352 if (arg.numel () == 1) |
3419 { | 3353 { |
3420 Complex val = arg.complex_value (); | 3354 Complex val = arg.complex_value (); |
3421 | 3355 |
3422 if (! error_state) | 3356 retval = octave_value (new octave_complex (val)); |
3423 retval = octave_value (new octave_complex (val)); | |
3424 } | 3357 } |
3425 else | 3358 else |
3426 { | 3359 { |
3427 ComplexNDArray val = arg.complex_array_value (); | 3360 ComplexNDArray val = arg.complex_array_value (); |
3428 | 3361 |
3429 if (! error_state) | 3362 retval = octave_value (new octave_complex_matrix (val)); |
3430 retval = octave_value (new octave_complex_matrix (val)); | |
3431 } | 3363 } |
3432 } | 3364 } |
3433 | 3365 |
3434 if (error_state) | 3366 if (error_state) |
3435 error ("complex: invalid conversion"); | 3367 error ("complex: invalid conversion"); |
3443 if (re.is_sparse_type () && im.is_sparse_type ()) | 3375 if (re.is_sparse_type () && im.is_sparse_type ()) |
3444 { | 3376 { |
3445 const SparseMatrix re_val = re.sparse_matrix_value (); | 3377 const SparseMatrix re_val = re.sparse_matrix_value (); |
3446 const SparseMatrix im_val = im.sparse_matrix_value (); | 3378 const SparseMatrix im_val = im.sparse_matrix_value (); |
3447 | 3379 |
3448 if (!error_state) | 3380 if (re.numel () == 1) |
3449 { | 3381 { |
3450 if (re.numel () == 1) | 3382 SparseComplexMatrix result; |
3383 if (re_val.nnz () == 0) | |
3384 result = Complex (0, 1) * SparseComplexMatrix (im_val); | |
3385 else | |
3386 { | |
3387 octave_idx_type nr = im_val.rows (); | |
3388 octave_idx_type nc = im_val.cols (); | |
3389 result = SparseComplexMatrix (nr, nc, re_val(0)); | |
3390 | |
3391 for (octave_idx_type j = 0; j < nc; j++) | |
3392 { | |
3393 octave_idx_type off = j * nr; | |
3394 for (octave_idx_type i = im_val.cidx (j); | |
3395 i < im_val.cidx (j + 1); i++) | |
3396 result.data (im_val.ridx (i) + off) += | |
3397 Complex (0, im_val.data (i)); | |
3398 } | |
3399 } | |
3400 retval = octave_value (new octave_sparse_complex_matrix (result)); | |
3401 } | |
3402 else if (im.numel () == 1) | |
3403 { | |
3404 SparseComplexMatrix result; | |
3405 if (im_val.nnz () == 0) | |
3406 result = SparseComplexMatrix (re_val); | |
3407 else | |
3408 { | |
3409 octave_idx_type nr = re_val.rows (); | |
3410 octave_idx_type nc = re_val.cols (); | |
3411 result = SparseComplexMatrix (nr, nc, | |
3412 Complex (0, im_val(0))); | |
3413 | |
3414 for (octave_idx_type j = 0; j < nc; j++) | |
3415 { | |
3416 octave_idx_type off = j * nr; | |
3417 for (octave_idx_type i = re_val.cidx (j); | |
3418 i < re_val.cidx (j + 1); i++) | |
3419 result.data (re_val.ridx (i) + off) += | |
3420 re_val.data (i); | |
3421 } | |
3422 } | |
3423 retval = octave_value (new octave_sparse_complex_matrix (result)); | |
3424 } | |
3425 else | |
3426 { | |
3427 if (re_val.dims () == im_val.dims ()) | |
3451 { | 3428 { |
3452 SparseComplexMatrix result; | 3429 SparseComplexMatrix result; |
3453 if (re_val.nnz () == 0) | 3430 result = SparseComplexMatrix (re_val) |
3454 result = Complex (0, 1) * SparseComplexMatrix (im_val); | 3431 + Complex (0, 1) * SparseComplexMatrix (im_val); |
3455 else | 3432 retval = octave_value ( |
3456 { | 3433 new octave_sparse_complex_matrix (result)); |
3457 octave_idx_type nr = im_val.rows (); | |
3458 octave_idx_type nc = im_val.cols (); | |
3459 result = SparseComplexMatrix (nr, nc, re_val(0)); | |
3460 | |
3461 for (octave_idx_type j = 0; j < nc; j++) | |
3462 { | |
3463 octave_idx_type off = j * nr; | |
3464 for (octave_idx_type i = im_val.cidx (j); | |
3465 i < im_val.cidx (j + 1); i++) | |
3466 result.data (im_val.ridx (i) + off) += | |
3467 Complex (0, im_val.data (i)); | |
3468 } | |
3469 } | |
3470 retval = octave_value (new octave_sparse_complex_matrix (result)); | |
3471 } | 3434 } |
3472 else if (im.numel () == 1) | 3435 else |
3436 error ("complex: dimension mismatch"); | |
3437 } | |
3438 } | |
3439 else if (re.is_single_type () || im.is_single_type ()) | |
3440 { | |
3441 if (re.numel () == 1) | |
3442 { | |
3443 float re_val = re.float_value (); | |
3444 | |
3445 if (im.numel () == 1) | |
3473 { | 3446 { |
3474 SparseComplexMatrix result; | 3447 float im_val = im.double_value (); |
3475 if (im_val.nnz () == 0) | 3448 |
3476 result = SparseComplexMatrix (re_val); | 3449 retval = octave_value (new octave_float_complex |
3477 else | 3450 (FloatComplex (re_val, im_val))); |
3478 { | |
3479 octave_idx_type nr = re_val.rows (); | |
3480 octave_idx_type nc = re_val.cols (); | |
3481 result = SparseComplexMatrix (nr, nc, | |
3482 Complex (0, im_val(0))); | |
3483 | |
3484 for (octave_idx_type j = 0; j < nc; j++) | |
3485 { | |
3486 octave_idx_type off = j * nr; | |
3487 for (octave_idx_type i = re_val.cidx (j); | |
3488 i < re_val.cidx (j + 1); i++) | |
3489 result.data (re_val.ridx (i) + off) += | |
3490 re_val.data (i); | |
3491 } | |
3492 } | |
3493 retval = octave_value (new octave_sparse_complex_matrix (result)); | |
3494 } | 3451 } |
3495 else | 3452 else |
3496 { | 3453 { |
3454 const FloatNDArray im_val = im.float_array_value (); | |
3455 | |
3456 FloatComplexNDArray result (im_val.dims (), | |
3457 FloatComplex ()); | |
3458 | |
3459 for (octave_idx_type i = 0; i < im_val.numel (); i++) | |
3460 result.xelem (i) = FloatComplex (re_val, im_val(i)); | |
3461 | |
3462 retval = octave_value (new octave_float_complex_matrix | |
3463 (result)); | |
3464 } | |
3465 } | |
3466 else | |
3467 { | |
3468 const FloatNDArray re_val = re.float_array_value (); | |
3469 | |
3470 if (im.numel () == 1) | |
3471 { | |
3472 float im_val = im.float_value (); | |
3473 | |
3474 FloatComplexNDArray result (re_val.dims (), | |
3475 FloatComplex ()); | |
3476 | |
3477 for (octave_idx_type i = 0; i < re_val.numel (); i++) | |
3478 result.xelem (i) = FloatComplex (re_val(i), im_val); | |
3479 | |
3480 retval = octave_value (new octave_float_complex_matrix | |
3481 (result)); | |
3482 } | |
3483 else | |
3484 { | |
3485 const FloatNDArray im_val = im.float_array_value (); | |
3486 | |
3497 if (re_val.dims () == im_val.dims ()) | 3487 if (re_val.dims () == im_val.dims ()) |
3498 { | 3488 { |
3499 SparseComplexMatrix result; | 3489 FloatComplexNDArray result (re_val.dims (), |
3500 result = SparseComplexMatrix (re_val) | 3490 FloatComplex ()); |
3501 + Complex (0, 1) * SparseComplexMatrix (im_val); | 3491 |
3502 retval = octave_value ( | 3492 for (octave_idx_type i = 0; i < re_val.numel (); i++) |
3503 new octave_sparse_complex_matrix (result)); | 3493 result.xelem (i) = FloatComplex (re_val(i), |
3494 im_val(i)); | |
3495 | |
3496 retval = octave_value (new octave_float_complex_matrix | |
3497 (result)); | |
3504 } | 3498 } |
3505 else | 3499 else |
3506 error ("complex: dimension mismatch"); | 3500 error ("complex: dimension mismatch"); |
3507 } | 3501 } |
3508 } | 3502 } |
3509 } | 3503 } |
3510 else if (re.is_single_type () || im.is_single_type ()) | |
3511 { | |
3512 if (re.numel () == 1) | |
3513 { | |
3514 float re_val = re.float_value (); | |
3515 | |
3516 if (im.numel () == 1) | |
3517 { | |
3518 float im_val = im.double_value (); | |
3519 | |
3520 if (! error_state) | |
3521 retval = octave_value ( | |
3522 new octave_float_complex (FloatComplex (re_val, | |
3523 im_val))); | |
3524 } | |
3525 else | |
3526 { | |
3527 const FloatNDArray im_val = im.float_array_value (); | |
3528 | |
3529 if (! error_state) | |
3530 { | |
3531 FloatComplexNDArray result (im_val.dims (), | |
3532 FloatComplex ()); | |
3533 | |
3534 for (octave_idx_type i = 0; i < im_val.numel (); i++) | |
3535 result.xelem (i) = FloatComplex (re_val, im_val(i)); | |
3536 | |
3537 retval = octave_value ( | |
3538 new octave_float_complex_matrix (result)); | |
3539 } | |
3540 } | |
3541 } | |
3542 else | |
3543 { | |
3544 const FloatNDArray re_val = re.float_array_value (); | |
3545 | |
3546 if (im.numel () == 1) | |
3547 { | |
3548 float im_val = im.float_value (); | |
3549 | |
3550 if (! error_state) | |
3551 { | |
3552 FloatComplexNDArray result (re_val.dims (), | |
3553 FloatComplex ()); | |
3554 | |
3555 for (octave_idx_type i = 0; i < re_val.numel (); i++) | |
3556 result.xelem (i) = FloatComplex (re_val(i), im_val); | |
3557 | |
3558 retval = octave_value ( | |
3559 new octave_float_complex_matrix (result)); | |
3560 } | |
3561 } | |
3562 else | |
3563 { | |
3564 const FloatNDArray im_val = im.float_array_value (); | |
3565 | |
3566 if (! error_state) | |
3567 { | |
3568 if (re_val.dims () == im_val.dims ()) | |
3569 { | |
3570 FloatComplexNDArray result (re_val.dims (), | |
3571 FloatComplex ()); | |
3572 | |
3573 for (octave_idx_type i = 0; i < re_val.numel (); i++) | |
3574 result.xelem (i) = FloatComplex (re_val(i), | |
3575 im_val(i)); | |
3576 | |
3577 retval = octave_value ( | |
3578 new octave_float_complex_matrix (result)); | |
3579 } | |
3580 else | |
3581 error ("complex: dimension mismatch"); | |
3582 } | |
3583 } | |
3584 } | |
3585 } | |
3586 else if (re.numel () == 1) | 3504 else if (re.numel () == 1) |
3587 { | 3505 { |
3588 double re_val = re.double_value (); | 3506 double re_val = re.double_value (); |
3589 | 3507 |
3590 if (im.numel () == 1) | 3508 if (im.numel () == 1) |
3591 { | 3509 { |
3592 double im_val = im.double_value (); | 3510 double im_val = im.double_value (); |
3593 | 3511 |
3594 if (! error_state) | 3512 retval = octave_value (new octave_complex |
3595 retval = octave_value (new octave_complex (Complex (re_val, | 3513 (Complex (re_val, im_val))); |
3596 im_val))); | |
3597 } | 3514 } |
3598 else | 3515 else |
3599 { | 3516 { |
3600 const NDArray im_val = im.array_value (); | 3517 const NDArray im_val = im.array_value (); |
3601 | 3518 |
3602 if (! error_state) | 3519 ComplexNDArray result (im_val.dims (), Complex ()); |
3603 { | 3520 |
3604 ComplexNDArray result (im_val.dims (), Complex ()); | 3521 for (octave_idx_type i = 0; i < im_val.numel (); i++) |
3605 | 3522 result.xelem (i) = Complex (re_val, im_val(i)); |
3606 for (octave_idx_type i = 0; i < im_val.numel (); i++) | 3523 |
3607 result.xelem (i) = Complex (re_val, im_val(i)); | 3524 retval = octave_value (new octave_complex_matrix (result)); |
3608 | |
3609 retval = octave_value (new octave_complex_matrix (result)); | |
3610 } | |
3611 } | 3525 } |
3612 } | 3526 } |
3613 else | 3527 else |
3614 { | 3528 { |
3615 const NDArray re_val = re.array_value (); | 3529 const NDArray re_val = re.array_value (); |
3616 | 3530 |
3617 if (im.numel () == 1) | 3531 if (im.numel () == 1) |
3618 { | 3532 { |
3619 double im_val = im.double_value (); | 3533 double im_val = im.double_value (); |
3620 | 3534 |
3621 if (! error_state) | 3535 ComplexNDArray result (re_val.dims (), Complex ()); |
3622 { | 3536 |
3623 ComplexNDArray result (re_val.dims (), Complex ()); | 3537 for (octave_idx_type i = 0; i < re_val.numel (); i++) |
3624 | 3538 result.xelem (i) = Complex (re_val(i), im_val); |
3625 for (octave_idx_type i = 0; i < re_val.numel (); i++) | 3539 |
3626 result.xelem (i) = Complex (re_val(i), im_val); | 3540 retval = octave_value (new octave_complex_matrix (result)); |
3627 | |
3628 retval = octave_value (new octave_complex_matrix (result)); | |
3629 } | |
3630 } | 3541 } |
3631 else | 3542 else |
3632 { | 3543 { |
3633 const NDArray im_val = im.array_value (); | 3544 const NDArray im_val = im.array_value (); |
3634 | 3545 |
3635 if (! error_state) | 3546 if (re_val.dims () == im_val.dims ()) |
3636 { | 3547 { |
3637 if (re_val.dims () == im_val.dims ()) | 3548 ComplexNDArray result (re_val.dims (), Complex ()); |
3638 { | 3549 |
3639 ComplexNDArray result (re_val.dims (), Complex ()); | 3550 for (octave_idx_type i = 0; i < re_val.numel (); i++) |
3640 | 3551 result.xelem (i) = Complex (re_val(i), im_val(i)); |
3641 for (octave_idx_type i = 0; i < re_val.numel (); i++) | 3552 |
3642 result.xelem (i) = Complex (re_val(i), im_val(i)); | 3553 retval = octave_value ( |
3643 | 3554 new octave_complex_matrix (result)); |
3644 retval = octave_value ( | |
3645 new octave_complex_matrix (result)); | |
3646 } | |
3647 else | |
3648 error ("complex: dimension mismatch"); | |
3649 } | 3555 } |
3556 else | |
3557 error ("complex: dimension mismatch"); | |
3650 } | 3558 } |
3651 } | 3559 } |
3652 | 3560 |
3653 if (error_state) | 3561 if (error_state) |
3654 error ("complex: invalid conversion"); | 3562 error ("complex: invalid conversion"); |
4020 { | 3928 { |
4021 std::string nm = args(nargin-1).string_value (); | 3929 std::string nm = args(nargin-1).string_value (); |
4022 nargin--; | 3930 nargin--; |
4023 | 3931 |
4024 dt = oct_data_conv::string_to_data_type (nm); | 3932 dt = oct_data_conv::string_to_data_type (nm); |
4025 | |
4026 if (error_state) | |
4027 return retval; | |
4028 } | 3933 } |
4029 | 3934 |
4030 switch (nargin) | 3935 switch (nargin) |
4031 { | 3936 { |
4032 case 0: | 3937 case 0: |
4052 } | 3957 } |
4053 } | 3958 } |
4054 break; | 3959 break; |
4055 } | 3960 } |
4056 | 3961 |
4057 if (! error_state) | 3962 dims.chop_trailing_singletons (); |
4058 { | 3963 |
4059 dims.chop_trailing_singletons (); | 3964 check_dimensions (dims, fcn); |
4060 | 3965 |
4061 check_dimensions (dims, fcn); | 3966 // FIXME: perhaps this should be made extensible by |
4062 | 3967 // using the class name to lookup a function to call to create |
4063 // FIXME: perhaps this should be made extensible by | 3968 // the new value. |
4064 // using the class name to lookup a function to call to create | 3969 |
4065 // the new value. | 3970 // Note that automatic narrowing will handle conversion from |
4066 | 3971 // NDArray to scalar. |
4067 // Note that automatic narrowing will handle conversion from | 3972 |
4068 // NDArray to scalar. | 3973 switch (dt) |
4069 | 3974 { |
4070 if (! error_state) | 3975 case oct_data_conv::dt_int8: |
4071 { | 3976 retval = int8NDArray (dims, val); |
4072 switch (dt) | 3977 break; |
4073 { | 3978 |
4074 case oct_data_conv::dt_int8: | 3979 case oct_data_conv::dt_uint8: |
4075 retval = int8NDArray (dims, val); | 3980 retval = uint8NDArray (dims, val); |
4076 break; | 3981 break; |
4077 | 3982 |
4078 case oct_data_conv::dt_uint8: | 3983 case oct_data_conv::dt_int16: |
4079 retval = uint8NDArray (dims, val); | 3984 retval = int16NDArray (dims, val); |
4080 break; | 3985 break; |
4081 | 3986 |
4082 case oct_data_conv::dt_int16: | 3987 case oct_data_conv::dt_uint16: |
4083 retval = int16NDArray (dims, val); | 3988 retval = uint16NDArray (dims, val); |
4084 break; | 3989 break; |
4085 | 3990 |
4086 case oct_data_conv::dt_uint16: | 3991 case oct_data_conv::dt_int32: |
4087 retval = uint16NDArray (dims, val); | 3992 retval = int32NDArray (dims, val); |
4088 break; | 3993 break; |
4089 | 3994 |
4090 case oct_data_conv::dt_int32: | 3995 case oct_data_conv::dt_uint32: |
4091 retval = int32NDArray (dims, val); | 3996 retval = uint32NDArray (dims, val); |
4092 break; | 3997 break; |
4093 | 3998 |
4094 case oct_data_conv::dt_uint32: | 3999 case oct_data_conv::dt_int64: |
4095 retval = uint32NDArray (dims, val); | 4000 retval = int64NDArray (dims, val); |
4096 break; | 4001 break; |
4097 | 4002 |
4098 case oct_data_conv::dt_int64: | 4003 case oct_data_conv::dt_uint64: |
4099 retval = int64NDArray (dims, val); | 4004 retval = uint64NDArray (dims, val); |
4100 break; | 4005 break; |
4101 | 4006 |
4102 case oct_data_conv::dt_uint64: | 4007 case oct_data_conv::dt_single: |
4103 retval = uint64NDArray (dims, val); | 4008 retval = FloatNDArray (dims, val); |
4104 break; | 4009 break; |
4105 | 4010 |
4106 case oct_data_conv::dt_single: | 4011 case oct_data_conv::dt_double: |
4107 retval = FloatNDArray (dims, val); | 4012 { |
4108 break; | 4013 if (val == 1 && dims.length () == 2 && dims(0) == 1) |
4109 | 4014 retval = Range (1.0, 0.0, dims(1)); // packed form |
4110 case oct_data_conv::dt_double: | 4015 else |
4111 { | 4016 retval = NDArray (dims, val); |
4112 if (val == 1 && dims.length () == 2 && dims(0) == 1) | 4017 } |
4113 retval = Range (1.0, 0.0, dims(1)); // packed form | 4018 break; |
4114 else | 4019 |
4115 retval = NDArray (dims, val); | 4020 case oct_data_conv::dt_logical: |
4116 } | 4021 retval = boolNDArray (dims, val); |
4117 break; | 4022 break; |
4118 | 4023 |
4119 case oct_data_conv::dt_logical: | 4024 default: |
4120 retval = boolNDArray (dims, val); | 4025 error ("%s: invalid class name", fcn); |
4121 break; | 4026 break; |
4122 | |
4123 default: | |
4124 error ("%s: invalid class name", fcn); | |
4125 break; | |
4126 } | |
4127 } | |
4128 } | 4027 } |
4129 | 4028 |
4130 return retval; | 4029 return retval; |
4131 } | 4030 } |
4132 | 4031 |
4146 { | 4045 { |
4147 std::string nm = args(nargin-1).string_value (); | 4046 std::string nm = args(nargin-1).string_value (); |
4148 nargin--; | 4047 nargin--; |
4149 | 4048 |
4150 dt = oct_data_conv::string_to_data_type (nm); | 4049 dt = oct_data_conv::string_to_data_type (nm); |
4151 | |
4152 if (error_state) | |
4153 return retval; | |
4154 } | 4050 } |
4155 | 4051 |
4156 switch (nargin) | 4052 switch (nargin) |
4157 { | 4053 { |
4158 case 0: | 4054 case 0: |
4178 } | 4074 } |
4179 } | 4075 } |
4180 break; | 4076 break; |
4181 } | 4077 } |
4182 | 4078 |
4183 if (! error_state) | 4079 dims.chop_trailing_singletons (); |
4184 { | 4080 |
4185 dims.chop_trailing_singletons (); | 4081 check_dimensions (dims, fcn); |
4186 | 4082 |
4187 check_dimensions (dims, fcn); | 4083 // Note that automatic narrowing will handle conversion from |
4188 | 4084 // NDArray to scalar. |
4189 // Note that automatic narrowing will handle conversion from | 4085 |
4190 // NDArray to scalar. | 4086 switch (dt) |
4191 | 4087 { |
4192 if (! error_state) | 4088 case oct_data_conv::dt_single: |
4193 { | 4089 retval = FloatNDArray (dims, fval); |
4194 switch (dt) | 4090 break; |
4195 { | 4091 |
4196 case oct_data_conv::dt_single: | 4092 case oct_data_conv::dt_double: |
4197 retval = FloatNDArray (dims, fval); | 4093 retval = NDArray (dims, val); |
4198 break; | 4094 break; |
4199 | 4095 |
4200 case oct_data_conv::dt_double: | 4096 default: |
4201 retval = NDArray (dims, val); | 4097 error ("%s: invalid class name", fcn); |
4202 break; | 4098 break; |
4203 | |
4204 default: | |
4205 error ("%s: invalid class name", fcn); | |
4206 break; | |
4207 } | |
4208 } | |
4209 } | 4099 } |
4210 | 4100 |
4211 return retval; | 4101 return retval; |
4212 } | 4102 } |
4213 | 4103 |
4226 { | 4116 { |
4227 std::string nm = args(nargin-1).string_value (); | 4117 std::string nm = args(nargin-1).string_value (); |
4228 nargin--; | 4118 nargin--; |
4229 | 4119 |
4230 dt = oct_data_conv::string_to_data_type (nm); | 4120 dt = oct_data_conv::string_to_data_type (nm); |
4231 | |
4232 if (error_state) | |
4233 return retval; | |
4234 } | 4121 } |
4235 | 4122 |
4236 switch (nargin) | 4123 switch (nargin) |
4237 { | 4124 { |
4238 case 0: | 4125 case 0: |
4258 } | 4145 } |
4259 } | 4146 } |
4260 break; | 4147 break; |
4261 } | 4148 } |
4262 | 4149 |
4263 if (! error_state) | 4150 dims.chop_trailing_singletons (); |
4264 { | 4151 |
4265 dims.chop_trailing_singletons (); | 4152 check_dimensions (dims, fcn); |
4266 | 4153 |
4267 check_dimensions (dims, fcn); | 4154 // Note that automatic narrowing will handle conversion from |
4268 | 4155 // NDArray to scalar. |
4269 // Note that automatic narrowing will handle conversion from | 4156 |
4270 // NDArray to scalar. | 4157 switch (dt) |
4271 | 4158 { |
4272 if (! error_state) | 4159 case oct_data_conv::dt_single: |
4273 { | 4160 retval = FloatNDArray (dims, static_cast<float> (val)); |
4274 switch (dt) | 4161 break; |
4275 { | 4162 |
4276 case oct_data_conv::dt_single: | 4163 case oct_data_conv::dt_double: |
4277 retval = FloatNDArray (dims, static_cast<float> (val)); | 4164 retval = NDArray (dims, val); |
4278 break; | 4165 break; |
4279 | 4166 |
4280 case oct_data_conv::dt_double: | 4167 default: |
4281 retval = NDArray (dims, val); | 4168 error ("%s: invalid class name", fcn); |
4282 break; | 4169 break; |
4283 | |
4284 default: | |
4285 error ("%s: invalid class name", fcn); | |
4286 break; | |
4287 } | |
4288 } | |
4289 } | 4170 } |
4290 | 4171 |
4291 return retval; | 4172 return retval; |
4292 } | 4173 } |
4293 | 4174 |
4307 { | 4188 { |
4308 std::string nm = args(nargin-1).string_value (); | 4189 std::string nm = args(nargin-1).string_value (); |
4309 nargin--; | 4190 nargin--; |
4310 | 4191 |
4311 dt = oct_data_conv::string_to_data_type (nm); | 4192 dt = oct_data_conv::string_to_data_type (nm); |
4312 | |
4313 if (error_state) | |
4314 return retval; | |
4315 } | 4193 } |
4316 | 4194 |
4317 switch (nargin) | 4195 switch (nargin) |
4318 { | 4196 { |
4319 case 0: | 4197 case 0: |
4339 } | 4217 } |
4340 } | 4218 } |
4341 break; | 4219 break; |
4342 } | 4220 } |
4343 | 4221 |
4344 if (! error_state) | 4222 dims.chop_trailing_singletons (); |
4345 { | 4223 |
4346 dims.chop_trailing_singletons (); | 4224 check_dimensions (dims, fcn); |
4347 | 4225 |
4348 check_dimensions (dims, fcn); | 4226 // Note that automatic narrowing will handle conversion from |
4349 | 4227 // NDArray to scalar. |
4350 // Note that automatic narrowing will handle conversion from | 4228 |
4351 // NDArray to scalar. | 4229 switch (dt) |
4352 | 4230 { |
4353 if (! error_state) | 4231 case oct_data_conv::dt_single: |
4354 { | 4232 retval = FloatComplexNDArray (dims, |
4355 switch (dt) | 4233 static_cast<FloatComplex> (val)); |
4356 { | 4234 break; |
4357 case oct_data_conv::dt_single: | 4235 |
4358 retval = FloatComplexNDArray (dims, | 4236 case oct_data_conv::dt_double: |
4359 static_cast<FloatComplex> (val)); | 4237 retval = ComplexNDArray (dims, val); |
4360 break; | 4238 break; |
4361 | 4239 |
4362 case oct_data_conv::dt_double: | 4240 default: |
4363 retval = ComplexNDArray (dims, val); | 4241 error ("%s: invalid class name", fcn); |
4364 break; | 4242 break; |
4365 | |
4366 default: | |
4367 error ("%s: invalid class name", fcn); | |
4368 break; | |
4369 } | |
4370 } | |
4371 } | 4243 } |
4372 | 4244 |
4373 return retval; | 4245 return retval; |
4374 } | 4246 } |
4375 | 4247 |
4407 } | 4279 } |
4408 } | 4280 } |
4409 break; | 4281 break; |
4410 } | 4282 } |
4411 | 4283 |
4412 if (! error_state) | 4284 dims.chop_trailing_singletons (); |
4413 { | 4285 |
4414 dims.chop_trailing_singletons (); | 4286 check_dimensions (dims, fcn); |
4415 | 4287 |
4416 check_dimensions (dims, fcn); | 4288 // Note that automatic narrowing will handle conversion from |
4417 | 4289 // NDArray to scalar. |
4418 // Note that automatic narrowing will handle conversion from | 4290 |
4419 // NDArray to scalar. | 4291 retval = boolNDArray (dims, val); |
4420 | |
4421 if (! error_state) | |
4422 retval = boolNDArray (dims, val); | |
4423 } | |
4424 | 4292 |
4425 return retval; | 4293 return retval; |
4426 } | 4294 } |
4427 | 4295 |
4428 DEFUN (ones, args, , | 4296 DEFUN (ones, args, , |
4729 { | 4597 { |
4730 if (args(0).is_single_type ()) | 4598 if (args(0).is_single_type ()) |
4731 { | 4599 { |
4732 Array<float> x = args(0).float_array_value (); | 4600 Array<float> x = args(0).float_array_value (); |
4733 | 4601 |
4734 if (! error_state) | 4602 Array<float> epsval (x.dims ()); |
4603 | |
4604 for (octave_idx_type i = 0; i < x.numel (); i++) | |
4735 { | 4605 { |
4736 Array<float> epsval (x.dims ()); | 4606 float val = ::fabsf (x(i)); |
4737 | 4607 if (xisnan (val) || xisinf (val)) |
4738 for (octave_idx_type i = 0; i < x.numel (); i++) | 4608 epsval(i) = lo_ieee_nan_value (); |
4609 else if (val < std::numeric_limits<float>::min ()) | |
4610 epsval(i) = powf (2.0, -149e0); | |
4611 else | |
4739 { | 4612 { |
4740 float val = ::fabsf (x(i)); | 4613 int expon; |
4741 if (xisnan (val) || xisinf (val)) | 4614 gnulib::frexpf (val, &expon); |
4742 epsval(i) = lo_ieee_nan_value (); | 4615 epsval(i) = std::pow (2.0f, |
4743 else if (val < std::numeric_limits<float>::min ()) | 4616 static_cast<float> (expon - 24)); |
4744 epsval(i) = powf (2.0, -149e0); | |
4745 else | |
4746 { | |
4747 int expon; | |
4748 gnulib::frexpf (val, &expon); | |
4749 epsval(i) = std::pow (2.0f, | |
4750 static_cast<float> (expon - 24)); | |
4751 } | |
4752 } | 4617 } |
4618 } | |
4619 | |
4620 retval = epsval; | |
4621 } | |
4622 else | |
4623 { | |
4624 Array<double> x = args(0).array_value (); | |
4625 | |
4626 Array<double> epsval (x.dims ()); | |
4627 | |
4628 for (octave_idx_type i = 0; i < x.numel (); i++) | |
4629 { | |
4630 double val = ::fabs (x(i)); | |
4631 if (xisnan (val) || xisinf (val)) | |
4632 epsval(i) = lo_ieee_nan_value (); | |
4633 else if (val < std::numeric_limits<double>::min ()) | |
4634 epsval(i) = pow (2.0, -1074e0); | |
4635 else | |
4636 { | |
4637 int expon; | |
4638 gnulib::frexp (val, &expon); | |
4639 epsval(i) = std::pow (2.0, | |
4640 static_cast<double> (expon - 53)); | |
4641 } | |
4642 | |
4753 retval = epsval; | 4643 retval = epsval; |
4754 } | |
4755 } | |
4756 else | |
4757 { | |
4758 Array<double> x = args(0).array_value (); | |
4759 | |
4760 if (! error_state) | |
4761 { | |
4762 Array<double> epsval (x.dims ()); | |
4763 | |
4764 for (octave_idx_type i = 0; i < x.numel (); i++) | |
4765 { | |
4766 double val = ::fabs (x(i)); | |
4767 if (xisnan (val) || xisinf (val)) | |
4768 epsval(i) = lo_ieee_nan_value (); | |
4769 else if (val < std::numeric_limits<double>::min ()) | |
4770 epsval(i) = pow (2.0, -1074e0); | |
4771 else | |
4772 { | |
4773 int expon; | |
4774 gnulib::frexp (val, &expon); | |
4775 epsval(i) = std::pow (2.0, | |
4776 static_cast<double> (expon - 53)); | |
4777 } | |
4778 retval = epsval; | |
4779 } | |
4780 } | 4644 } |
4781 } | 4645 } |
4782 } | 4646 } |
4783 else | 4647 else |
4784 retval = fill_matrix (args, std::numeric_limits<double>::epsilon (), | 4648 retval = fill_matrix (args, std::numeric_limits<double>::epsilon (), |
5109 | 4973 |
5110 // FIXME: perhaps this should be made extensible by using | 4974 // FIXME: perhaps this should be made extensible by using |
5111 // the class name to lookup a function to call to create the new | 4975 // the class name to lookup a function to call to create the new |
5112 // value. | 4976 // value. |
5113 | 4977 |
5114 if (! error_state) | 4978 switch (dt) |
5115 { | 4979 { |
5116 switch (dt) | 4980 case oct_data_conv::dt_int8: |
5117 { | 4981 retval = identity_matrix<int8NDArray> (nr, nc); |
5118 case oct_data_conv::dt_int8: | 4982 break; |
5119 retval = identity_matrix<int8NDArray> (nr, nc); | 4983 |
5120 break; | 4984 case oct_data_conv::dt_uint8: |
5121 | 4985 retval = identity_matrix<uint8NDArray> (nr, nc); |
5122 case oct_data_conv::dt_uint8: | 4986 break; |
5123 retval = identity_matrix<uint8NDArray> (nr, nc); | 4987 |
5124 break; | 4988 case oct_data_conv::dt_int16: |
5125 | 4989 retval = identity_matrix<int16NDArray> (nr, nc); |
5126 case oct_data_conv::dt_int16: | 4990 break; |
5127 retval = identity_matrix<int16NDArray> (nr, nc); | 4991 |
5128 break; | 4992 case oct_data_conv::dt_uint16: |
5129 | 4993 retval = identity_matrix<uint16NDArray> (nr, nc); |
5130 case oct_data_conv::dt_uint16: | 4994 break; |
5131 retval = identity_matrix<uint16NDArray> (nr, nc); | 4995 |
5132 break; | 4996 case oct_data_conv::dt_int32: |
5133 | 4997 retval = identity_matrix<int32NDArray> (nr, nc); |
5134 case oct_data_conv::dt_int32: | 4998 break; |
5135 retval = identity_matrix<int32NDArray> (nr, nc); | 4999 |
5136 break; | 5000 case oct_data_conv::dt_uint32: |
5137 | 5001 retval = identity_matrix<uint32NDArray> (nr, nc); |
5138 case oct_data_conv::dt_uint32: | 5002 break; |
5139 retval = identity_matrix<uint32NDArray> (nr, nc); | 5003 |
5140 break; | 5004 case oct_data_conv::dt_int64: |
5141 | 5005 retval = identity_matrix<int64NDArray> (nr, nc); |
5142 case oct_data_conv::dt_int64: | 5006 break; |
5143 retval = identity_matrix<int64NDArray> (nr, nc); | 5007 |
5144 break; | 5008 case oct_data_conv::dt_uint64: |
5145 | 5009 retval = identity_matrix<uint64NDArray> (nr, nc); |
5146 case oct_data_conv::dt_uint64: | 5010 break; |
5147 retval = identity_matrix<uint64NDArray> (nr, nc); | 5011 |
5148 break; | 5012 case oct_data_conv::dt_single: |
5149 | 5013 retval = FloatDiagMatrix (nr, nc, 1.0f); |
5150 case oct_data_conv::dt_single: | 5014 break; |
5151 retval = FloatDiagMatrix (nr, nc, 1.0f); | 5015 |
5152 break; | 5016 case oct_data_conv::dt_double: |
5153 | 5017 retval = DiagMatrix (nr, nc, 1.0); |
5154 case oct_data_conv::dt_double: | 5018 break; |
5155 retval = DiagMatrix (nr, nc, 1.0); | 5019 |
5156 break; | 5020 case oct_data_conv::dt_logical: |
5157 | 5021 retval = identity_matrix<boolNDArray> (nr, nc); |
5158 case oct_data_conv::dt_logical: | 5022 break; |
5159 retval = identity_matrix<boolNDArray> (nr, nc); | 5023 |
5160 break; | 5024 default: |
5161 | 5025 error ("eye: invalid class name"); |
5162 default: | 5026 break; |
5163 error ("eye: invalid class name"); | |
5164 break; | |
5165 } | |
5166 } | 5027 } |
5167 | 5028 |
5168 return retval; | 5029 return retval; |
5169 } | 5030 } |
5170 | 5031 |
5232 { | 5093 { |
5233 std::string nm = args(nargin-1).string_value (); | 5094 std::string nm = args(nargin-1).string_value (); |
5234 nargin--; | 5095 nargin--; |
5235 | 5096 |
5236 dt = oct_data_conv::string_to_data_type (nm); | 5097 dt = oct_data_conv::string_to_data_type (nm); |
5237 | |
5238 if (error_state) | |
5239 return retval; | |
5240 } | 5098 } |
5241 | 5099 |
5242 switch (nargin) | 5100 switch (nargin) |
5243 { | 5101 { |
5244 case 0: | 5102 case 0: |
5248 case 1: | 5106 case 1: |
5249 { | 5107 { |
5250 octave_idx_type nr, nc; | 5108 octave_idx_type nr, nc; |
5251 get_dimensions (args(0), "eye", nr, nc); | 5109 get_dimensions (args(0), "eye", nr, nc); |
5252 | 5110 |
5253 if (! error_state) | 5111 retval = identity_matrix (nr, nc, dt); |
5254 retval = identity_matrix (nr, nc, dt); | |
5255 } | 5112 } |
5256 break; | 5113 break; |
5257 | 5114 |
5258 case 2: | 5115 case 2: |
5259 { | 5116 { |
5260 octave_idx_type nr, nc; | 5117 octave_idx_type nr, nc; |
5261 get_dimensions (args(0), args(1), "eye", nr, nc); | 5118 get_dimensions (args(0), args(1), "eye", nr, nc); |
5262 | 5119 |
5263 if (! error_state) | 5120 retval = identity_matrix (nr, nc, dt); |
5264 retval = identity_matrix (nr, nc, dt); | |
5265 } | 5121 } |
5266 break; | 5122 break; |
5267 | 5123 |
5268 default: | 5124 default: |
5269 print_usage (); | 5125 print_usage (); |
5529 { | 5385 { |
5530 dim_vector dv; | 5386 dim_vector dv; |
5531 dv.resize (nargin - 1); | 5387 dv.resize (nargin - 1); |
5532 for (octave_idx_type i = 1; i < nargin; i++) | 5388 for (octave_idx_type i = 1; i < nargin; i++) |
5533 dv(i-1) = static_cast<octave_idx_type> (args(i).scalar_value ()); | 5389 dv(i-1) = static_cast<octave_idx_type> (args(i).scalar_value ()); |
5534 if (!error_state) | 5390 |
5535 { | 5391 retval = args(0); |
5536 retval = args(0); | 5392 retval = retval.resize (dv, true); |
5537 retval = retval.resize (dv, true); | |
5538 } | |
5539 | |
5540 } | 5393 } |
5541 else | 5394 else |
5542 print_usage (); | 5395 print_usage (); |
5396 | |
5543 return retval; | 5397 return retval; |
5544 } | 5398 } |
5545 | 5399 |
5546 // FIXME: should use octave_idx_type for dimensions. | 5400 // FIXME: should use octave_idx_type for dimensions. |
5547 | 5401 |
5630 } | 5484 } |
5631 else | 5485 else |
5632 { | 5486 { |
5633 new_dims(i-1) = args(i).idx_type_value (); | 5487 new_dims(i-1) = args(i).idx_type_value (); |
5634 | 5488 |
5635 if (error_state) | 5489 if (new_dims(i-1) < 0) |
5636 break; | |
5637 else if (new_dims(i-1) < 0) | |
5638 { | 5490 { |
5639 error ("reshape: SIZE must be non-negative"); | 5491 error ("reshape: SIZE must be non-negative"); |
5640 break; | 5492 break; |
5641 } | 5493 } |
5642 } | 5494 } |
5643 } | 5495 } |
5644 | 5496 |
5645 if (! error_state && (empty_dim > 0)) | 5497 if (empty_dim > 0) |
5646 { | 5498 { |
5647 octave_idx_type nel = new_dims.numel (); | 5499 octave_idx_type nel = new_dims.numel (); |
5648 | 5500 |
5649 if (nel == 0) | 5501 if (nel == 0) |
5650 new_dims(empty_dim-1) = 0; | 5502 new_dims(empty_dim-1) = 0; |
5665 { | 5517 { |
5666 print_usage (); | 5518 print_usage (); |
5667 return retval; | 5519 return retval; |
5668 } | 5520 } |
5669 | 5521 |
5670 if (! error_state) | 5522 retval = args(0).reshape (new_dims); |
5671 retval = args(0).reshape (new_dims); | |
5672 | 5523 |
5673 return retval; | 5524 return retval; |
5674 } | 5525 } |
5675 | 5526 |
5676 /* | 5527 /* |
5718 int nargin = args.length (); | 5569 int nargin = args.length (); |
5719 | 5570 |
5720 if (nargin < 1 || nargin > 2) | 5571 if (nargin < 1 || nargin > 2) |
5721 print_usage () ; | 5572 print_usage () ; |
5722 | 5573 |
5723 if (! error_state && nargin == 2) | 5574 if (nargin == 2) |
5724 { | 5575 { |
5725 dim = args(1).idx_type_value (); | 5576 dim = args(1).idx_type_value (); |
5726 | 5577 |
5727 if (dim < 1) | 5578 if (dim < 1) |
5728 error ("vec: DIM must be greater than zero"); | 5579 error ("vec: DIM must be greater than zero"); |
5729 } | 5580 } |
5730 | 5581 |
5731 if (! error_state) | 5582 octave_value colon (octave_value::magic_colon_t); |
5732 { | 5583 octave_value arg = args(0); |
5733 octave_value colon (octave_value::magic_colon_t); | 5584 retval = arg.single_subsref ("(", colon); |
5734 octave_value arg = args(0); | 5585 |
5735 retval = arg.single_subsref ("(", colon); | 5586 |
5736 | 5587 if (dim > 1) |
5737 | 5588 { |
5738 if (! error_state && dim > 1) | 5589 dim_vector new_dims = dim_vector::alloc (dim); |
5739 { | 5590 |
5740 dim_vector new_dims = dim_vector::alloc (dim); | 5591 for (int i = 0; i < dim-1; i++) |
5741 | 5592 new_dims(i) = 1; |
5742 for (int i = 0; i < dim-1; i++) | 5593 |
5743 new_dims(i) = 1; | 5594 new_dims(dim-1) = retval.numel (); |
5744 | 5595 |
5745 new_dims(dim-1) = retval.numel (); | 5596 retval = retval.reshape (new_dims); |
5746 | |
5747 retval = retval.reshape (new_dims); | |
5748 } | |
5749 } | 5597 } |
5750 | 5598 |
5751 return retval; | 5599 return retval; |
5752 } | 5600 } |
5753 | 5601 |
5892 error ("norm: unrecognized option: %s", str.c_str ()); | 5740 error ("norm: unrecognized option: %s", str.c_str ()); |
5893 // we've handled the last parameter, so act as if it was removed | 5741 // we've handled the last parameter, so act as if it was removed |
5894 nargin --; | 5742 nargin --; |
5895 } | 5743 } |
5896 | 5744 |
5897 if (! error_state) | 5745 octave_value p_arg = (nargin > 1) ? args(1) : octave_value (2); |
5746 | |
5747 if (p_arg.is_empty ()) | |
5748 p_arg = octave_value (2); | |
5749 else if (p_arg.is_string ()) | |
5898 { | 5750 { |
5899 octave_value p_arg = (nargin > 1) ? args(1) : octave_value (2); | 5751 std::string str = p_arg.string_value (); |
5900 | 5752 if ((strflag == sfcols || strflag == sfrows)) |
5901 if (p_arg.is_empty ()) | |
5902 p_arg = octave_value (2); | |
5903 else if (p_arg.is_string ()) | |
5904 { | 5753 { |
5905 std::string str = p_arg.string_value (); | 5754 if (str == "cols" || str == "columns" || str == "rows") |
5906 if ((strflag == sfcols || strflag == sfrows)) | 5755 error ("norm: invalid combination of options"); |
5907 { | 5756 else if (str == "fro") |
5908 if (str == "cols" || str == "columns" || str == "rows") | 5757 p_arg = octave_value (2); |
5909 error ("norm: invalid combination of options"); | 5758 else if (str == "inf") |
5910 else if (str == "fro") | 5759 p_arg = octave_Inf; |
5911 p_arg = octave_value (2); | |
5912 else if (str == "inf") | |
5913 p_arg = octave_Inf; | |
5914 else | |
5915 error ("norm: unrecognized option: %s", str.c_str ()); | |
5916 } | |
5917 else | 5760 else |
5918 error ("norm: invalid combination of options"); | 5761 error ("norm: unrecognized option: %s", str.c_str ()); |
5919 } | 5762 } |
5920 else if (! p_arg.is_scalar_type ()) | 5763 else |
5921 gripe_wrong_type_arg ("norm", p_arg, true); | 5764 error ("norm: invalid combination of options"); |
5922 | 5765 } |
5923 if (! error_state) | 5766 else if (! p_arg.is_scalar_type ()) |
5924 { | 5767 gripe_wrong_type_arg ("norm", p_arg, true); |
5925 switch (strflag) | 5768 |
5926 { | 5769 switch (strflag) |
5927 case sfmatrix: | 5770 { |
5928 retval(0) = xnorm (x_arg, p_arg); | 5771 case sfmatrix: |
5929 break; | 5772 retval(0) = xnorm (x_arg, p_arg); |
5930 case sfcols: | 5773 break; |
5931 retval(0) = xcolnorms (x_arg, p_arg); | 5774 case sfcols: |
5932 break; | 5775 retval(0) = xcolnorms (x_arg, p_arg); |
5933 case sfrows: | 5776 break; |
5934 retval(0) = xrownorms (x_arg, p_arg); | 5777 case sfrows: |
5935 break; | 5778 retval(0) = xrownorms (x_arg, p_arg); |
5936 case sffrob: | 5779 break; |
5937 retval(0) = xfrobnorm (x_arg); | 5780 case sffrob: |
5938 break; | 5781 retval(0) = xfrobnorm (x_arg); |
5939 case sfinf: | 5782 break; |
5940 retval(0) = xnorm (x_arg, octave_Inf); | 5783 case sfinf: |
5941 break; | 5784 retval(0) = xnorm (x_arg, octave_Inf); |
5942 } | 5785 break; |
5943 } | |
5944 } | 5786 } |
5945 } | 5787 } |
5946 else | 5788 else |
5947 error ("norm: only valid for 2-D objects"); | 5789 error ("norm: only valid for 2-D objects"); |
5948 } | 5790 } |
6576 } | 6418 } |
6577 else | 6419 else |
6578 error ("toc: invalid ID"); | 6420 error ("toc: invalid ID"); |
6579 } | 6421 } |
6580 | 6422 |
6581 if (! error_state) | 6423 if (start_time < 0) |
6582 { | 6424 error ("toc called before timer set"); |
6583 if (start_time < 0) | 6425 else |
6584 error ("toc called before timer set"); | 6426 { |
6427 octave_time now; | |
6428 | |
6429 double tmp = now.double_value () - start_time; | |
6430 | |
6431 if (nargout > 0) | |
6432 retval = tmp; | |
6585 else | 6433 else |
6586 { | 6434 octave_stdout << "Elapsed time is " << tmp << " seconds.\n"; |
6587 octave_time now; | |
6588 | |
6589 double tmp = now.double_value () - start_time; | |
6590 | |
6591 if (nargout > 0) | |
6592 retval = tmp; | |
6593 else | |
6594 octave_stdout << "Elapsed time is " << tmp << " seconds.\n"; | |
6595 } | |
6596 } | 6435 } |
6597 } | 6436 } |
6598 | 6437 |
6599 return retval; | 6438 return retval; |
6600 } | 6439 } |
7140 else | 6979 else |
7141 smode = get_sort_mode_option (args(1), "second"); | 6980 smode = get_sort_mode_option (args(1), "second"); |
7142 } | 6981 } |
7143 else | 6982 else |
7144 error ("issorted: second argument must be a string"); | 6983 error ("issorted: second argument must be a string"); |
7145 | |
7146 if (error_state) | |
7147 return retval; | |
7148 } | 6984 } |
7149 | 6985 |
7150 octave_value arg = args(0); | 6986 octave_value arg = args(0); |
7151 | 6987 |
7152 if (by_rows) | 6988 if (by_rows) |
7246 | 7082 |
7247 try | 7083 try |
7248 { | 7084 { |
7249 idx_vector n = args(1).index_vector (); | 7085 idx_vector n = args(1).index_vector (); |
7250 | 7086 |
7251 if (error_state) | |
7252 return retval; | |
7253 | |
7254 switch (argx.builtin_type ()) | 7087 switch (argx.builtin_type ()) |
7255 { | 7088 { |
7256 case btyp_double: | 7089 case btyp_double: |
7257 retval = argx.array_value ().nth_element (n, dim); | 7090 retval = argx.array_value ().nth_element (n, dim); |
7258 break; | 7091 break; |
7335 idx_vector idx = args(0).index_vector (); | 7168 idx_vector idx = args(0).index_vector (); |
7336 octave_idx_type n = -1; | 7169 octave_idx_type n = -1; |
7337 if (nargin == 3) | 7170 if (nargin == 3) |
7338 n = args(2).idx_type_value (true); | 7171 n = args(2).idx_type_value (true); |
7339 | 7172 |
7340 if (! error_state) | 7173 octave_value vals = args(1); |
7174 | |
7175 if (vals.is_range ()) | |
7341 { | 7176 { |
7342 octave_value vals = args(1); | 7177 Range r = vals.range_value (); |
7343 if (vals.is_range ()) | 7178 if (r.inc () == 0) |
7344 { | 7179 vals = r.base (); |
7345 Range r = vals.range_value (); | 7180 } |
7346 if (r.inc () == 0) | 7181 |
7347 vals = r.base (); | 7182 if (vals.is_single_type ()) |
7348 } | 7183 { |
7349 | 7184 if (vals.is_complex_type ()) |
7350 if (vals.is_single_type ()) | 7185 retval = do_accumarray_sum (idx, |
7351 { | 7186 vals.float_complex_array_value (), |
7352 if (vals.is_complex_type ()) | 7187 n); |
7353 retval = do_accumarray_sum (idx, | |
7354 vals.float_complex_array_value (), | |
7355 n); | |
7356 else | |
7357 retval = do_accumarray_sum (idx, vals.float_array_value (), n); | |
7358 } | |
7359 else if (vals.is_numeric_type () || vals.is_bool_type ()) | |
7360 { | |
7361 if (vals.is_complex_type ()) | |
7362 retval = do_accumarray_sum (idx, | |
7363 vals.complex_array_value (), | |
7364 n); | |
7365 else | |
7366 retval = do_accumarray_sum (idx, vals.array_value (), n); | |
7367 } | |
7368 else | 7188 else |
7369 gripe_wrong_type_arg ("accumarray", vals); | 7189 retval = do_accumarray_sum (idx, vals.float_array_value (), n); |
7370 } | 7190 } |
7371 } | 7191 else if (vals.is_numeric_type () || vals.is_bool_type ()) |
7372 catch (index_exception& e) | 7192 { |
7373 { | 7193 if (vals.is_complex_type ()) |
7374 error ("__accumarray_sum__: invalid IDX %s. %s", | 7194 retval = do_accumarray_sum (idx, |
7375 e.idx(), e.explain ()); | 7195 vals.complex_array_value (), |
7376 } | 7196 n); |
7197 else | |
7198 retval = do_accumarray_sum (idx, vals.array_value (), n); | |
7199 } | |
7200 else | |
7201 gripe_wrong_type_arg ("accumarray", vals); | |
7202 } | |
7203 catch (index_exception& e) | |
7204 { | |
7205 error ("__accumarray_sum__: invalid IDX %s. %s", | |
7206 e.idx(), e.explain ()); | |
7207 } | |
7377 } | 7208 } |
7378 else | 7209 else |
7379 print_usage (); | 7210 print_usage (); |
7380 | 7211 |
7381 return retval; | 7212 return retval; |
7423 idx_vector idx = args(0).index_vector (); | 7254 idx_vector idx = args(0).index_vector (); |
7424 octave_idx_type n = -1; | 7255 octave_idx_type n = -1; |
7425 if (nargin == 4) | 7256 if (nargin == 4) |
7426 n = args(3).idx_type_value (true); | 7257 n = args(3).idx_type_value (true); |
7427 | 7258 |
7428 if (! error_state) | 7259 octave_value vals = args(1); |
7260 octave_value zero = args(2); | |
7261 | |
7262 switch (vals.builtin_type ()) | |
7429 { | 7263 { |
7430 octave_value vals = args(1); | 7264 case btyp_double: |
7431 octave_value zero = args(2); | 7265 retval = do_accumarray_minmax (idx, vals.array_value (), n, ismin, |
7432 | 7266 zero.double_value ()); |
7433 switch (vals.builtin_type ()) | 7267 break; |
7434 { | 7268 case btyp_float: |
7435 case btyp_double: | 7269 retval = do_accumarray_minmax (idx, vals.float_array_value (), n, |
7436 retval = do_accumarray_minmax (idx, vals.array_value (), n, ismin, | 7270 ismin, zero.float_value ()); |
7437 zero.double_value ()); | 7271 break; |
7438 break; | 7272 case btyp_complex: |
7439 case btyp_float: | 7273 retval = do_accumarray_minmax (idx, vals.complex_array_value (), |
7440 retval = do_accumarray_minmax (idx, vals.float_array_value (), n, | 7274 n, ismin, zero.complex_value ()); |
7441 ismin, zero.float_value ()); | 7275 break; |
7442 break; | 7276 case btyp_float_complex: |
7443 case btyp_complex: | 7277 retval = do_accumarray_minmax (idx, |
7444 retval = do_accumarray_minmax (idx, vals.complex_array_value (), | 7278 vals.float_complex_array_value (), |
7445 n, ismin, zero.complex_value ()); | 7279 n, ismin, |
7446 break; | 7280 zero.float_complex_value ()); |
7447 case btyp_float_complex: | 7281 break; |
7448 retval = do_accumarray_minmax (idx, | |
7449 vals.float_complex_array_value (), | |
7450 n, ismin, | |
7451 zero.float_complex_value ()); | |
7452 break; | |
7453 #define MAKE_INT_BRANCH(X) \ | 7282 #define MAKE_INT_BRANCH(X) \ |
7454 case btyp_ ## X: \ | 7283 case btyp_ ## X: \ |
7455 retval = do_accumarray_minmax (idx, vals.X ## _array_value (), \ | 7284 retval = do_accumarray_minmax (idx, vals.X ## _array_value (), \ |
7456 n, ismin, \ | 7285 n, ismin, \ |
7457 zero.X ## _scalar_value ()); \ | 7286 zero.X ## _scalar_value ()); \ |
7458 break; | 7287 break; |
7459 | 7288 |
7460 MAKE_INT_BRANCH (int8); | 7289 MAKE_INT_BRANCH (int8); |
7461 MAKE_INT_BRANCH (int16); | 7290 MAKE_INT_BRANCH (int16); |
7462 MAKE_INT_BRANCH (int32); | 7291 MAKE_INT_BRANCH (int32); |
7463 MAKE_INT_BRANCH (int64); | 7292 MAKE_INT_BRANCH (int64); |
7464 MAKE_INT_BRANCH (uint8); | 7293 MAKE_INT_BRANCH (uint8); |
7465 MAKE_INT_BRANCH (uint16); | 7294 MAKE_INT_BRANCH (uint16); |
7466 MAKE_INT_BRANCH (uint32); | 7295 MAKE_INT_BRANCH (uint32); |
7467 MAKE_INT_BRANCH (uint64); | 7296 MAKE_INT_BRANCH (uint64); |
7468 #undef MAKE_INT_BRANCH | 7297 #undef MAKE_INT_BRANCH |
7469 case btyp_bool: | 7298 case btyp_bool: |
7470 retval = do_accumarray_minmax (idx, vals.array_value (), n, ismin, | 7299 retval = do_accumarray_minmax (idx, vals.array_value (), n, ismin, |
7471 zero.bool_value ()); | 7300 zero.bool_value ()); |
7472 break; | 7301 break; |
7473 default: | 7302 default: |
7474 gripe_wrong_type_arg ("accumarray", vals); | 7303 gripe_wrong_type_arg ("accumarray", vals); |
7475 } | |
7476 } | 7304 } |
7477 } | 7305 } |
7478 catch (index_exception& e) | 7306 catch (index_exception& e) |
7479 { | 7307 { |
7480 error ("do_accumarray_minmax_fun: invalid index %s. %s", | 7308 error ("do_accumarray_minmax_fun: invalid index %s. %s", |
7556 | 7384 |
7557 octave_idx_type n = -1; | 7385 octave_idx_type n = -1; |
7558 if (nargin == 4) | 7386 if (nargin == 4) |
7559 n = args(3).idx_type_value (true); | 7387 n = args(3).idx_type_value (true); |
7560 | 7388 |
7561 if (! error_state) | 7389 octave_value vals = args(1); |
7390 | |
7391 if (vals.is_single_type ()) | |
7562 { | 7392 { |
7563 octave_value vals = args(1); | 7393 if (vals.is_complex_type ()) |
7564 | 7394 retval = do_accumdim_sum (idx, |
7565 if (vals.is_single_type ()) | 7395 vals.float_complex_array_value (), |
7566 { | 7396 dim, n); |
7567 if (vals.is_complex_type ()) | |
7568 retval = do_accumdim_sum (idx, | |
7569 vals.float_complex_array_value (), | |
7570 dim, n); | |
7571 else | |
7572 retval = do_accumdim_sum (idx, vals.float_array_value (), | |
7573 dim, n); | |
7574 } | |
7575 else if (vals.is_numeric_type () || vals.is_bool_type ()) | |
7576 { | |
7577 if (vals.is_complex_type ()) | |
7578 retval = do_accumdim_sum (idx, vals.complex_array_value (), | |
7579 dim, n); | |
7580 else | |
7581 retval = do_accumdim_sum (idx, vals.array_value (), dim, n); | |
7582 } | |
7583 else | 7397 else |
7584 gripe_wrong_type_arg ("accumdim", vals); | 7398 retval = do_accumdim_sum (idx, vals.float_array_value (), |
7399 dim, n); | |
7585 } | 7400 } |
7401 else if (vals.is_numeric_type () || vals.is_bool_type ()) | |
7402 { | |
7403 if (vals.is_complex_type ()) | |
7404 retval = do_accumdim_sum (idx, vals.complex_array_value (), | |
7405 dim, n); | |
7406 else | |
7407 retval = do_accumdim_sum (idx, vals.array_value (), dim, n); | |
7408 } | |
7409 else | |
7410 gripe_wrong_type_arg ("accumdim", vals); | |
7586 } | 7411 } |
7587 catch (index_exception& e) | 7412 catch (index_exception& e) |
7588 { | 7413 { |
7589 error ("__accumdim_sum__: invalid IDX %s. %s", e.idx(), e.explain ()); | 7414 error ("__accumdim_sum__: invalid IDX %s. %s", e.idx(), e.explain ()); |
7590 } | 7415 } |
7929 if (nargin < 1 || nargin > 3) | 7754 if (nargin < 1 || nargin > 3) |
7930 print_usage (); | 7755 print_usage (); |
7931 else if (! (args(0).is_numeric_type () || args(0).is_bool_type ())) | 7756 else if (! (args(0).is_numeric_type () || args(0).is_bool_type ())) |
7932 error ("diff: X must be numeric or logical"); | 7757 error ("diff: X must be numeric or logical"); |
7933 | 7758 |
7934 if (! error_state) | 7759 int dim = -1; |
7935 { | 7760 octave_idx_type order = 1; |
7936 int dim = -1; | 7761 if (nargin > 1) |
7937 octave_idx_type order = 1; | 7762 { |
7938 if (nargin > 1) | 7763 if (args(1).is_scalar_type ()) |
7939 { | 7764 order = args(1).idx_type_value (true, false); |
7940 if (args(1).is_scalar_type ()) | 7765 else if (! args(1).is_zero_by_zero ()) |
7941 order = args(1).idx_type_value (true, false); | 7766 error ("order K must be a scalar or []"); |
7942 else if (! args(1).is_zero_by_zero ()) | 7767 if (order < 0) |
7943 error ("order K must be a scalar or []"); | 7768 error ("order K must be non-negative"); |
7944 if (! error_state && order < 0) | 7769 } |
7945 error ("order K must be non-negative"); | 7770 |
7946 } | 7771 if (nargin > 2) |
7947 | 7772 { |
7948 if (nargin > 2) | 7773 dim = args(2).int_value (true, false); |
7949 { | 7774 if (dim < 1 || dim > args(0).ndims ()) |
7950 dim = args(2).int_value (true, false); | 7775 error ("DIM must be a valid dimension"); |
7951 if (! error_state && (dim < 1 || dim > args(0).ndims ())) | 7776 else |
7952 error ("DIM must be a valid dimension"); | 7777 dim -= 1; |
7953 else | 7778 } |
7954 dim -= 1; | 7779 |
7955 } | 7780 retval = do_diff (args(0), order, dim); |
7956 | |
7957 if (! error_state) | |
7958 retval = do_diff (args(0), order, dim); | |
7959 } | |
7960 | 7781 |
7961 return retval; | 7782 return retval; |
7962 } | 7783 } |
7963 | 7784 |
7964 /* | 7785 /* |
8039 if (args.length () == 2) | 7860 if (args.length () == 2) |
8040 { | 7861 { |
8041 octave_value x = args(0); | 7862 octave_value x = args(0); |
8042 | 7863 |
8043 const Matrix rm = args(1).matrix_value (); | 7864 const Matrix rm = args(1).matrix_value (); |
8044 if (error_state) | 7865 |
8045 return retval; | 7866 if (rm.rows () != 2 || rm.ndims () != 2) |
8046 else if (rm.rows () != 2 || rm.ndims () != 2) | |
8047 { | 7867 { |
8048 error ("repelems: R must be a matrix with two rows"); | 7868 error ("repelems: R must be a matrix with two rows"); |
8049 return retval; | 7869 return retval; |
8050 } | 7870 } |
8051 else | 7871 else |
8123 else if (args(0).is_complex_type () | 7943 else if (args(0).is_complex_type () |
8124 || args(0).is_sparse_type ()) | 7944 || args(0).is_sparse_type ()) |
8125 error ("base64_encode: encoding complex or sparse data is not supported"); | 7945 error ("base64_encode: encoding complex or sparse data is not supported"); |
8126 else if (args(0).is_integer_type ()) | 7946 else if (args(0).is_integer_type ()) |
8127 { | 7947 { |
8128 #define MAKE_INT_BRANCH(X) \ | 7948 #define MAKE_INT_BRANCH(X) \ |
8129 if (args(0).is_ ## X ## _type ()) \ | 7949 if (args(0).is_ ## X ## _type ()) \ |
8130 { \ | 7950 { \ |
8131 const X##NDArray in = args(0). X## _array_value (); \ | 7951 const X##NDArray in = args(0). X## _array_value (); \ |
8132 size_t inlen = \ | 7952 size_t inlen = \ |
8133 in.numel () * sizeof (X## _t) / sizeof (char); \ | 7953 in.numel () * sizeof (X## _t) / sizeof (char); \ |
8134 const char* inc = \ | 7954 const char* inc = \ |
8135 reinterpret_cast<const char*> (in.data ()); \ | 7955 reinterpret_cast<const char*> (in.data ()); \ |
8136 char* out; \ | 7956 char* out; \ |
8137 if (! error_state \ | 7957 if (octave_base64_encode (inc, inlen, &out)) \ |
8138 && octave_base64_encode (inc, inlen, &out)) \ | 7958 { \ |
8139 { \ | 7959 retval(0) = octave_value (out); \ |
8140 retval(0) = octave_value (out); \ | 7960 ::free (out); \ |
8141 ::free (out); \ | 7961 } \ |
8142 } \ | |
8143 } | 7962 } |
8144 | 7963 |
8145 MAKE_INT_BRANCH(int8) | 7964 MAKE_INT_BRANCH(int8) |
8146 else MAKE_INT_BRANCH(int16) | 7965 else MAKE_INT_BRANCH(int16) |
8147 else MAKE_INT_BRANCH(int32) | 7966 else MAKE_INT_BRANCH(int32) |
8161 size_t inlen; | 7980 size_t inlen; |
8162 inlen = in.numel () * sizeof (float) / sizeof (char); | 7981 inlen = in.numel () * sizeof (float) / sizeof (char); |
8163 const char* inc; | 7982 const char* inc; |
8164 inc = reinterpret_cast<const char*> (in.data ()); | 7983 inc = reinterpret_cast<const char*> (in.data ()); |
8165 char* out; | 7984 char* out; |
8166 if (! error_state | 7985 if (octave_base64_encode (inc, inlen, &out)) |
8167 && octave_base64_encode (inc, inlen, &out)) | |
8168 { | 7986 { |
8169 retval(0) = octave_value (out); | 7987 retval(0) = octave_value (out); |
8170 ::free (out); | 7988 ::free (out); |
8171 } | 7989 } |
8172 } | 7990 } |
8176 size_t inlen; | 7994 size_t inlen; |
8177 inlen = in.numel () * sizeof (double) / sizeof (char); | 7995 inlen = in.numel () * sizeof (double) / sizeof (char); |
8178 const char* inc; | 7996 const char* inc; |
8179 inc = reinterpret_cast<const char*> (in.data ()); | 7997 inc = reinterpret_cast<const char*> (in.data ()); |
8180 char* out; | 7998 char* out; |
8181 if (! error_state | 7999 if (octave_base64_encode (inc, inlen, &out)) |
8182 && octave_base64_encode (inc, inlen, &out)) | |
8183 { | 8000 { |
8184 retval(0) = octave_value (out); | 8001 retval(0) = octave_value (out); |
8185 ::free (out); | 8002 ::free (out); |
8186 } | 8003 } |
8187 } | 8004 } |
8229 if (nargin > 1) | 8046 if (nargin > 1) |
8230 { | 8047 { |
8231 const Array<octave_idx_type> size = | 8048 const Array<octave_idx_type> size = |
8232 args(1).octave_idx_type_vector_value (); | 8049 args(1).octave_idx_type_vector_value (); |
8233 | 8050 |
8234 if (! error_state) | 8051 dims = dim_vector::alloc (size.numel ()); |
8235 { | 8052 for (octave_idx_type i = 0; i < size.numel (); i++) |
8236 dims = dim_vector::alloc (size.numel ()); | 8053 dims(i) = size(i); |
8237 for (octave_idx_type i = 0; i < size.numel (); i++) | |
8238 dims(i) = size(i); | |
8239 } | |
8240 } | 8054 } |
8241 | 8055 |
8242 const std::string str = args(0).string_value (); | 8056 const std::string str = args(0).string_value (); |
8243 | 8057 |
8244 if (! error_state) | 8058 Array<double> res = octave_base64_decode (str); |
8245 { | 8059 |
8246 Array<double> res = octave_base64_decode (str); | 8060 if (nargin > 1) |
8247 | 8061 res = res.reshape (dims); |
8248 if (nargin > 1) | 8062 |
8249 res = res.reshape (dims); | 8063 retval = res; |
8250 | |
8251 retval = res; | |
8252 } | |
8253 } | 8064 } |
8254 | 8065 |
8255 return retval; | 8066 return retval; |
8256 } | 8067 } |
8257 | 8068 |