@@ -210,7 +210,10 @@ def __init__(
210
210
self .num_cores_row = self .Ncores // ((ncol - 1 ) // NrowsMax + 1 )
211
211
self .num_cores_col = self .Ncores // self .num_cores_row
212
212
self .cores = [
213
- [self ._make_core (params_ [r * c ]) for c in range (self .num_cores_col )]
213
+ [
214
+ self ._make_core (params_ [r * self .num_cores_col + c ])
215
+ for c in range (self .num_cores_col )
216
+ ]
214
217
for r in range (self .num_cores_row )
215
218
]
216
219
@@ -224,9 +227,9 @@ def __init__(
224
227
# 3) Otherwise partition based on row/col_partition_strategy (max or even)
225
228
226
229
if nrow % self .num_cores_row == 0 :
227
- self .NrowsVec = (nrow // self .num_cores_row ) * np .ones (
230
+ self .NrowsVec = (nrow // self .num_cores_row ) * xp .ones (
228
231
self .num_cores_row ,
229
- dtype = np .int32 ,
232
+ dtype = xp .int32 ,
230
233
)
231
234
else :
232
235
# prio_partition = True in (
@@ -321,15 +324,15 @@ def set_matrix(
321
324
Raises:
322
325
ValueError: Matrix is not valid for the input parameters.
323
326
"""
324
- matrix = np .asarray (matrix )
327
+ matrix = xp .asarray (matrix )
325
328
if self .shape != matrix .shape :
326
329
raise ValueError ("Matrix shape must match AnalogCore shape" )
327
330
328
331
if verbose :
329
- print ("Min/Max matrix values" , np .min (matrix ), np .max (matrix ))
332
+ print ("Min/Max matrix values" , xp .min (matrix ), xp .max (matrix ))
330
333
331
334
if (
332
- matrix .dtype == np .complex64 or matrix .dtype == np .complex128
335
+ matrix .dtype == xp .complex64 or matrix .dtype == xp .complex128
333
336
) and not self .complex_valued :
334
337
raise ValueError (
335
338
(
@@ -343,9 +346,9 @@ def set_matrix(
343
346
# Break up complex matrix into real and imaginary quadrants
344
347
if self .complex_valued :
345
348
Nx , Ny = matrix .shape
346
- matrix_real = np .real (matrix )
347
- matrix_imag = np .imag (matrix )
348
- mcopy = np .zeros ((2 * Nx , 2 * Ny ), dtype = matrix_real .dtype )
349
+ matrix_real = xp .real (matrix )
350
+ matrix_imag = xp .imag (matrix )
351
+ mcopy = xp .zeros ((2 * Nx , 2 * Ny ), dtype = matrix_real .dtype )
349
352
mcopy [0 :Nx , 0 :Ny ] = matrix_real
350
353
mcopy [Nx :, 0 :Ny ] = matrix_imag
351
354
mcopy [0 :Nx , Ny :] = - matrix_imag
@@ -357,8 +360,8 @@ def set_matrix(
357
360
# If the values would exceed this range then you would have to reprogram all
358
361
# matrix values based on the new range, so instead we will clip and warn
359
362
if error_mask :
360
- mat_max = np .max (matrix )
361
- mat_min = np .min (matrix )
363
+ mat_max = xp .max (matrix )
364
+ mat_min = xp .min (matrix )
362
365
363
366
# Adding an epsilon here to avoid erroreous errors
364
367
if mat_max > (self .max + self ._eps ) or mat_min < (self .min - self ._eps ):
@@ -468,7 +471,7 @@ def get_matrix(self) -> npt.NDArray:
468
471
if self .Ncores == 1 :
469
472
matrix = self .cores [0 ][0 ]._read_matrix ()
470
473
else :
471
- matrix = np .zeros ((self .nrow , self .ncol ))
474
+ matrix = xp .zeros ((self .nrow , self .ncol ))
472
475
for row , row_start , row_end in self .row_partition_bounds :
473
476
for col , col_start , col_end in self .col_partition_bounds :
474
477
matrix [row_start :row_end , col_start :col_end ] = self .cores [row ][
@@ -866,7 +869,7 @@ def __getitem__(self, item):
866
869
def __setitem__ (self , key , value ):
867
870
rslice , cslice , full_mask , _ = self ._create_mask (key )
868
871
expanded_mat = self .get_matrix ()
869
- expanded_mat [rslice , cslice ] = np .asarray (value )
872
+ expanded_mat [rslice , cslice ] = xp .asarray (value )
870
873
error_mask = None if full_mask else (rslice , cslice )
871
874
self .set_matrix (expanded_mat , error_mask = error_mask )
872
875
@@ -950,21 +953,21 @@ def _set_limits_percentile(constraints, input_, reset=False):
950
953
"""
951
954
if (constraints .min is None or constraints .max is None ) or reset :
952
955
if constraints .percentile >= 1.0 :
953
- X_max = np .max (np .abs (input_ ))
956
+ X_max = xp .max (xp .abs (input_ ))
954
957
X_max *= constraints .percentile
955
958
min_ = - X_max
956
959
max_ = X_max
957
960
958
961
elif constraints .percentile < 1.0 :
959
- X_posmax = np .percentile (input_ , 100 * constraints .percentile )
960
- X_negmax = np .percentile (input_ , 100 - 100 * constraints .percentile )
961
- X_max = np .max (np .abs (np .array ([X_posmax , X_negmax ])))
962
+ X_posmax = xp .percentile (input_ , 100 * constraints .percentile )
963
+ X_negmax = xp .percentile (input_ , 100 - 100 * constraints .percentile )
964
+ X_max = xp .max (xp .abs (xp .array ([X_posmax , X_negmax ])))
962
965
min_ = - X_max
963
966
max_ = X_max
964
967
965
968
# Ensure min_ and max_ aren't the same for uniform inputs
966
969
if min_ == max_ :
967
- eps = np .finfo (float ).eps
970
+ eps = xp .finfo (float ).eps
968
971
min_ -= eps
969
972
max_ += eps
970
973
return (min_ , max_ )
@@ -1054,7 +1057,7 @@ def set_matrix(
1054
1057
verbose : bool = False ,
1055
1058
error_mask : tuple [slice , slice ] | None = None ,
1056
1059
) -> None :
1057
- matrix = np .asarray (matrix )
1060
+ matrix = xp .asarray (matrix )
1058
1061
self .parent .set_matrix (matrix .T , verbose = verbose , error_mask = error_mask )
1059
1062
1060
1063
def matvec (self , vec : npt .ArrayLike ) -> npt .NDArray :
@@ -1157,7 +1160,7 @@ def get_matrix(self) -> npt.NDArray:
1157
1160
def set_matrix (self , matrix : npt .ArrayLike , verbose : bool = False , error_mask = None ):
1158
1161
# TODO: Do we need to do anything with error_mask here?
1159
1162
expanded_mat = self .parent .get_matrix ()
1160
- expanded_mat [self .rslice , self .cslice ] = np .asarray (matrix )
1163
+ expanded_mat [self .rslice , self .cslice ] = xp .asarray (matrix )
1161
1164
self .parent .set_matrix (
1162
1165
expanded_mat ,
1163
1166
verbose = verbose ,
@@ -1172,7 +1175,7 @@ def matvec(self, other: npt.ArrayLike) -> npt.NDArray:
1172
1175
f"Dimension mismatch: { self .shape } , { other .shape } " ,
1173
1176
)
1174
1177
1175
- vec_in = np .zeros (self .parent .shape [1 ], dtype = other .dtype )
1178
+ vec_in = xp .zeros (self .parent .shape [1 ], dtype = other .dtype )
1176
1179
vec_in [self .cslice ] = other .flatten ()
1177
1180
1178
1181
vec_out = self .parent .matvec (vec_in )
@@ -1195,7 +1198,7 @@ def matmat(self, other: npt.ArrayLike) -> npt.NDArray:
1195
1198
# out-of-slice rows
1196
1199
# For col slices we're just leaving empty entires in the input matrix
1197
1200
# corrosponding to missing rows
1198
- mat_in = np .zeros ((self .parent .shape [1 ], other .shape [1 ]), dtype = other .dtype )
1201
+ mat_in = xp .zeros ((self .parent .shape [1 ], other .shape [1 ]), dtype = other .dtype )
1199
1202
for i in range (self .parent .shape [1 ])[self .cslice ]:
1200
1203
mat_in [i ] = other [(i - self .cslice .start ) // self .cslice .step ]
1201
1204
mat_out = self .parent .matmat (mat_in )
@@ -1209,7 +1212,7 @@ def vecmat(self, other: npt.ArrayLike) -> npt.NDArray:
1209
1212
f"Dimension mismatch: { other .shape } , { self .shape } " ,
1210
1213
)
1211
1214
1212
- vec_in = np .zeros (self .parent .shape [0 ], dtype = other .dtype )
1215
+ vec_in = xp .zeros (self .parent .shape [0 ], dtype = other .dtype )
1213
1216
vec_in [self .rslice ] = other .flatten ()
1214
1217
1215
1218
vec_out = self .parent .vecmat (vec_in )
@@ -1228,7 +1231,7 @@ def rmatmat(self, other: npt.ArrayLike) -> npt.NDArray:
1228
1231
f"Dimension mismatch: { other .shape } , { self .shape } " ,
1229
1232
)
1230
1233
1231
- mat_in = np .zeros ((other .shape [0 ], self .parent .shape [0 ]), dtype = other .dtype )
1234
+ mat_in = xp .zeros ((other .shape [0 ], self .parent .shape [0 ]), dtype = other .dtype )
1232
1235
for i in range (self .parent .shape [0 ])[self .rslice ]:
1233
1236
mat_in .T [i ] = other .T [(i - self .rslice .start ) // self .rslice .step ]
1234
1237
0 commit comments