@@ -1039,7 +1039,7 @@ protected
10391039 Function fn;
10401040 list< Dimension > dims;
10411041 Dimension dim1, dim2;
1042- Integer i;
1042+ Integer i, ndims ;
10431043 algorithm
10441044 Call . UNTYPED_CALL (ref = fn_ref, arguments = args, named_args = named_args) := call;
10451045 assertNoNamedParams("matrix" , named_args, info);
@@ -1051,32 +1051,32 @@ protected
10511051
10521052 (arg, ty, variability) := Typing . typeExp(listHead(args), origin, info);
10531053 dims := Type . arrayDims(ty);
1054+ ndims := listLength(dims);
1055+
1056+ if ndims < 2 then
1057+ // matrix(A) where A is a scalar or vector returns promote(A, 2).
1058+ (callExp, ty) := Expression . promote(arg, ty, 2 );
1059+ elseif ndims == 2 then
1060+ // matrix(A) where A is a matrix just returns A.
1061+ callExp := arg;
1062+ else
1063+ // matrix requires all but the first two dimensions to have size 1.
1064+ dim1 :: dim2 :: dims := dims;
1065+ i := 3 ;
1066+
1067+ for dim in dims loop
1068+ if Dimension . isKnown(dim) and Dimension . size(dim) > 1 then
1069+ Error . addSourceMessageAndFail(Error . INVALID_ARRAY_DIM_IN_CONVERSION_OP ,
1070+ {String (i), "matrix" , "1" , Dimension . toString(dim)}, info);
1071+ end if ;
10541072
1055- dims := match listLength(dims)
1056- case 0 then {Dimension . fromInteger(1 ), Dimension . fromInteger(1 )};
1057- case 1 then {listHead(dims), Dimension . fromInteger(1 )};
1058- case 2 then dims;
1059- else
1060- algorithm
1061- // matrix requires all but the first two dimensions to have size 1.
1062- dim1 :: dim2 :: dims := dims;
1063- i := 3 ;
1064-
1065- for dim in dims loop
1066- if Dimension . isKnown(dim) and Dimension . size(dim) > 1 then
1067- Error . addSourceMessageAndFail(Error . INVALID_ARRAY_DIM_IN_CONVERSION_OP ,
1068- {String (i), "matrix" , "1" , Dimension . toString(dim)}, info);
1069- end if ;
1070-
1071- i := i + 1 ;
1072- end for ;
1073- then
1074- {dim1, dim2};
1075- end match;
1073+ i := i + 1 ;
1074+ end for ;
10761075
1077- ty := Type . ARRAY (Type . arrayElementType(ty), dims);
1078- {fn} := Function . typeRefCache(fn_ref);
1079- callExp := Expression . CALL (Call . makeTypedCall(fn, {arg}, variability, ty));
1076+ ty := Type . ARRAY (Type . arrayElementType(ty), {dim1, dim2});
1077+ {fn} := Function . typeRefCache(fn_ref);
1078+ callExp := Expression . CALL (Call . makeTypedCall(fn, {arg}, variability, ty));
1079+ end if ;
10801080 end typeMatrixCall;
10811081
10821082 function typeCatCall
0 commit comments