Skip to content

Commit d301be9

Browse files
committed
fix: ensure support for real-to-complex casting in boolean and mask array indexing
1 parent ba4ce18 commit d301be9

File tree

5 files changed

+211
-9
lines changed

5 files changed

+211
-9
lines changed

lib/node_modules/@stdlib/array/to-fancy/lib/set_elements.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@
2121
// MODULES //
2222

2323
var isMostlySafeCast = require( '@stdlib/array/base/assert/is-mostly-safe-data-type-cast' );
24+
var isRealDataType = require( '@stdlib/array/base/assert/is-real-data-type' );
25+
var isComplexDataType = require( '@stdlib/array/base/assert/is-complex-floating-point-data-type' );
2426
var isCollection = require( '@stdlib/assert/is-collection' );
2527
var scalar2array = require( '@stdlib/array/from-scalar' );
2628
var dtype = require( '@stdlib/array/dtype' );
2729
var put = require( '@stdlib/array/put' );
30+
var place = require( '@stdlib/array/place' );
31+
var convert = require( '@stdlib/array/convert' );
2832
var where = require( '@stdlib/array/base/where' ).assign;
29-
var place = require( '@stdlib/array/base/place' );
3033
var format = require( '@stdlib/string/format' );
3134
var prop2array = require( './prop2array.js' );
3235
var errMessage = require( './error_message.js' );
@@ -88,24 +91,31 @@ function setElements( target, property, value, ctx ) {
8891
}
8992
return true;
9093
}
94+
if ( idx.type === 'bool' ) {
95+
try {
96+
place( target, idx.data, v, {
97+
'mode': 'strict_broadcast'
98+
});
99+
} catch ( err ) {
100+
throw new err.constructor( errMessage( err.message ) );
101+
}
102+
return true;
103+
}
91104
if ( vdt === void 0 ) {
92105
vdt = dtype( value ) || 'generic';
93106
}
94107
// Safe casts are always allowed and allow same kind casts (i.e., downcasts) only when the target array data type is floating-point...
95108
if ( !isMostlySafeCast( vdt, tdt ) ) {
96109
throw new TypeError( format( 'invalid operation. Assigned value cannot be safely cast to the target array data type. Data types: [%s, %s].', vdt, tdt ) );
97110
}
98-
if ( idx.type === 'bool' ) {
99-
try {
100-
place( target, idx.data, v, 'strict_broadcast' );
101-
} catch ( err ) {
102-
throw new err.constructor( errMessage( err.message ) );
103-
}
104-
return true;
111+
// When performing a real-to-complex assignment, interpret the real-valued array as containing real components with implied imaginary components equal to zero and explicitly convert to a complex-valued array...
112+
if ( isComplexDataType( tdt ) && isRealDataType( vdt ) ) {
113+
v = convert( v, tdt );
105114
}
106115
if ( idx.type === 'mask' ) {
116+
// NOTE: we intentionally deviate from boolean array indexing here and interpret the mask as applying to both the target and values array, thus requiring that the assigned value array be broadcast compatible with the target array and NOT just the selected elements as in boolean array indexing
107117
try {
108-
where( idx.data, target, v, target, 1, 0 ); // note: intentionally deviate from boolean array indexing here and interpret the mask as applying to both the target and values array, thus requiring that the assigned value array be broadcast compatible with the target array and NOT just the selected elements as in boolean array indexing
118+
where( idx.data, target, v, target, 1, 0 );
109119
} catch ( err ) {
110120
throw new err.constructor( errMessage( err.message ) );
111121
}

lib/node_modules/@stdlib/array/to-fancy/test/test.set.boolean_array.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,51 @@ tape( 'the function returns an array-like object supporting casting (complex128,
10521052
t.end();
10531053
});
10541054

1055+
tape( 'the function returns an array-like object supporting casting (complex128, float64)', opts, function test( t ) {
1056+
var expected;
1057+
var idx;
1058+
var x;
1059+
var y;
1060+
1061+
x = new Complex128Array( [ 1, 2, 3, 4, 5, 6, 7, 8 ] );
1062+
y = array2fancy( x );
1063+
1064+
idx = array2fancy.idx( [ true, true, true, true ] );
1065+
expected = new Complex128Array( [ 9, 0, 10, 0, 11, 0, 12, 0 ] );
1066+
y[ idx ] = new Float64Array( [ 9, 10, 11, 12 ] );
1067+
1068+
t.strictEqual( isSameComplex128Array( y, expected ), true, 'returns expected value' );
1069+
1070+
x = new Complex128Array( [ 1, 2, 3, 4, 5, 6, 7, 8 ] );
1071+
y = array2fancy( x );
1072+
1073+
idx = array2fancy.idx( new BooleanArray( [ true, true, false, false ] ) );
1074+
expected = new Complex128Array( [ 17, 0, 18, 0, 5, 6, 7, 8 ] );
1075+
y[ idx ] = new Float64Array( [ 17, 18 ] );
1076+
1077+
t.strictEqual( isSameComplex128Array( y, expected ), true, 'returns expected value' );
1078+
1079+
x = new Complex128Array( [ 1, 2, 3, 4, 5, 6, 7, 8 ] );
1080+
y = array2fancy( x );
1081+
1082+
idx = array2fancy.idx( [ false, true, true, false ] );
1083+
expected = new Complex128Array( [ 1, 2, 17, 0, 18, 0, 7, 8 ] );
1084+
y[ idx ] = new Float64Array( [ 17, 18 ] );
1085+
1086+
t.strictEqual( isSameComplex128Array( y, expected ), true, 'returns expected value' );
1087+
1088+
x = new Complex128Array( [ 1, 2, 3, 4, 5, 6, 7, 8 ] );
1089+
y = array2fancy( x );
1090+
1091+
idx = array2fancy.idx( new BooleanArray( [ true, false, true, false ] ) );
1092+
expected = new Complex128Array( [ 17, 0, 3, 4, 18, 0, 7, 8 ] );
1093+
y[ idx ] = new Float64Array( [ 17, 18 ] );
1094+
1095+
t.strictEqual( isSameComplex128Array( y, expected ), true, 'returns expected value' );
1096+
1097+
t.end();
1098+
});
1099+
10551100
tape( 'the function returns an array-like object supporting downcasting of floating-point arrays (float32, float64)', opts, function test( t ) {
10561101
var expected;
10571102
var idx;

lib/node_modules/@stdlib/array/to-fancy/test/test.set.integer_array.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,6 +1240,60 @@ tape( 'the function returns an array-like object supporting casting (complex128,
12401240
t.end();
12411241
});
12421242

1243+
tape( 'the function returns an array-like object supporting casting (complex128, float64)', opts, function test( t ) {
1244+
var expected;
1245+
var idx;
1246+
var x;
1247+
var y;
1248+
1249+
x = new Complex128Array( [ 1, 2, 3, 4, 5, 6, 7, 8 ] );
1250+
y = array2fancy( x );
1251+
1252+
idx = array2fancy.idx( [ 0, 1, 2, 3 ] );
1253+
expected = new Complex128Array( [ 9, 0, 10, 0, 11, 0, 12, 0 ] );
1254+
y[ idx ] = new Float64Array( [ 9, 10, 11, 12 ] );
1255+
1256+
t.strictEqual( isSameComplex128Array( y, expected ), true, 'returns expected value' );
1257+
1258+
x = new Complex128Array( [ 1, 2, 3, 4, 5, 6, 7, 8 ] );
1259+
y = array2fancy( x );
1260+
1261+
idx = array2fancy.idx( [ 0, 1 ] );
1262+
expected = new Complex128Array( [ 17, 0, 18, 0, 5, 6, 7, 8 ] );
1263+
y[ idx ] = new Float64Array( [ 17, 18 ] );
1264+
1265+
t.strictEqual( isSameComplex128Array( y, expected ), true, 'returns expected value' );
1266+
1267+
x = new Complex128Array( [ 1, 2, 3, 4, 5, 6, 7, 8 ] );
1268+
y = array2fancy( x );
1269+
1270+
idx = array2fancy.idx( [ 1, 2 ] );
1271+
expected = new Complex128Array( [ 1, 2, 17, 0, 18, 0, 7, 8 ] );
1272+
y[ idx ] = new Float64Array( [ 17, 18 ] );
1273+
1274+
t.strictEqual( isSameComplex128Array( y, expected ), true, 'returns expected value' );
1275+
1276+
x = new Complex128Array( [ 1, 2, 3, 4, 5, 6, 7, 8 ] );
1277+
y = array2fancy( x );
1278+
1279+
idx = array2fancy.idx( [ 0, 2 ] );
1280+
expected = new Complex128Array( [ 17, 0, 3, 4, 18, 0, 7, 8 ] );
1281+
y[ idx ] = new Float64Array( [ 17, 18 ] );
1282+
1283+
t.strictEqual( isSameComplex128Array( y, expected ), true, 'returns expected value' );
1284+
1285+
x = new Complex128Array( [ 1, 2, 3, 4, 5, 6, 7, 8 ] );
1286+
y = array2fancy( x );
1287+
1288+
idx = array2fancy.idx( [ -1, -3 ] );
1289+
expected = new Complex128Array( [ 1, 2, 18, 0, 5, 6, 17, 0 ] );
1290+
y[ idx ] = new Float64Array( [ 17, 18 ] );
1291+
1292+
t.strictEqual( isSameComplex128Array( y, expected ), true, 'returns expected value' );
1293+
1294+
t.end();
1295+
});
1296+
12431297
tape( 'the function returns an array-like object supporting downcasting of floating-point arrays (float32, float64)', opts, function test( t ) {
12441298
var expected;
12451299
var idx;

lib/node_modules/@stdlib/array/to-fancy/test/test.set.mask_array.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,51 @@ tape( 'the function returns an array-like object supporting casting (complex128,
10521052
t.end();
10531053
});
10541054

1055+
tape( 'the function returns an array-like object supporting casting (complex128, float64)', opts, function test( t ) {
1056+
var expected;
1057+
var idx;
1058+
var x;
1059+
var y;
1060+
1061+
x = new Complex128Array( [ 1, 2, 3, 4, 5, 6, 7, 8 ] );
1062+
y = array2fancy( x );
1063+
1064+
idx = array2fancy.idx( new Uint8Array( [ 0, 0, 0, 0 ] ) );
1065+
expected = new Complex128Array( [ 9, 0, 10, 0, 11, 0, 12, 0 ] );
1066+
y[ idx ] = new Float64Array( [ 9, 10, 11, 12 ] );
1067+
1068+
t.strictEqual( isSameComplex128Array( y, expected ), true, 'returns expected value' );
1069+
1070+
x = new Complex128Array( [ 1, 2, 3, 4, 5, 6, 7, 8 ] );
1071+
y = array2fancy( x );
1072+
1073+
idx = array2fancy.idx( new Uint8Array( [ 0, 0, 1, 1 ] ) );
1074+
expected = new Complex128Array( [ 17, 0, 18, 0, 5, 6, 7, 8 ] );
1075+
y[ idx ] = new Float64Array( [ 17, 18, 0, 0 ] );
1076+
1077+
t.strictEqual( isSameComplex128Array( y, expected ), true, 'returns expected value' );
1078+
1079+
x = new Complex128Array( [ 1, 2, 3, 4, 5, 6, 7, 8 ] );
1080+
y = array2fancy( x );
1081+
1082+
idx = array2fancy.idx( new Uint8Array( [ 1, 0, 0, 1 ] ) );
1083+
expected = new Complex128Array( [ 1, 2, 17, 0, 18, 0, 7, 8 ] );
1084+
y[ idx ] = new Float64Array( [ 0, 17, 18, 0 ] );
1085+
1086+
t.strictEqual( isSameComplex128Array( y, expected ), true, 'returns expected value' );
1087+
1088+
x = new Complex128Array( [ 1, 2, 3, 4, 5, 6, 7, 8 ] );
1089+
y = array2fancy( x );
1090+
1091+
idx = array2fancy.idx( new Uint8Array( [ 0, 1, 0, 1 ] ) );
1092+
expected = new Complex128Array( [ 17, 0, 3, 4, 18, 0, 7, 8 ] );
1093+
y[ idx ] = new Float64Array( [ 17, 0, 18, 0 ] );
1094+
1095+
t.strictEqual( isSameComplex128Array( y, expected ), true, 'returns expected value' );
1096+
1097+
t.end();
1098+
});
1099+
10551100
tape( 'the function returns an array-like object supporting downcasting of floating-point arrays (float32, float64)', opts, function test( t ) {
10561101
var expected;
10571102
var idx;

lib/node_modules/@stdlib/array/to-fancy/test/test.set.subsequence.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,54 @@ tape( 'the function returns an array-like object supporting casting (complex128,
11081108
t.end();
11091109
});
11101110

1111+
tape( 'the function returns an array-like object supporting casting (complex128, float64)', opts, function test( t ) {
1112+
var expected;
1113+
var x;
1114+
var y;
1115+
1116+
x = new Complex128Array( [ 1, 2, 3, 4, 5, 6, 7, 8 ] );
1117+
y = array2fancy( x );
1118+
1119+
expected = new Complex128Array( [ 9, 0, 10, 0, 11, 0, 12, 0 ] );
1120+
y[ ':' ] = new Float64Array( [ 9, 10, 11, 12 ] );
1121+
1122+
t.strictEqual( isSameComplex128Array( y, expected ), true, 'returns expected value' );
1123+
1124+
x = new Complex128Array( [ 1, 2, 3, 4, 5, 6, 7, 8 ] );
1125+
y = array2fancy( x );
1126+
1127+
expected = new Complex128Array( [ 17, 0, 18, 0, 5, 6, 7, 8 ] );
1128+
y[ ':2' ] = new Float64Array( [ 17, 18 ] );
1129+
1130+
t.strictEqual( isSameComplex128Array( y, expected ), true, 'returns expected value' );
1131+
1132+
x = new Complex128Array( [ 1, 2, 3, 4, 5, 6, 7, 8 ] );
1133+
y = array2fancy( x );
1134+
1135+
expected = new Complex128Array( [ 1, 2, 17, 0, 18, 0, 7, 8 ] );
1136+
y[ '1:3' ] = new Float64Array( [ 17, 18 ] );
1137+
1138+
t.strictEqual( isSameComplex128Array( y, expected ), true, 'returns expected value' );
1139+
1140+
x = new Complex128Array( [ 1, 2, 3, 4, 5, 6, 7, 8 ] );
1141+
y = array2fancy( x );
1142+
1143+
expected = new Complex128Array( [ 17, 0, 3, 4, 18, 0, 7, 8 ] );
1144+
y[ '::2' ] = new Float64Array( [ 17, 18 ] );
1145+
1146+
t.strictEqual( isSameComplex128Array( y, expected ), true, 'returns expected value' );
1147+
1148+
x = new Complex128Array( [ 1, 2, 3, 4, 5, 6, 7, 8 ] );
1149+
y = array2fancy( x );
1150+
1151+
expected = new Complex128Array( [ 1, 2, 18, 0, 5, 6, 17, 0 ] );
1152+
y[ '-1::-2' ] = new Float64Array( [ 17, 18 ] );
1153+
1154+
t.strictEqual( isSameComplex128Array( y, expected ), true, 'returns expected value' );
1155+
1156+
t.end();
1157+
});
1158+
11111159
tape( 'the function returns an array-like object supporting downcasting of floating-point arrays (float32, float64)', opts, function test( t ) {
11121160
var expected;
11131161
var x;

0 commit comments

Comments
 (0)