14
14
XQUANTITIES = ANGLEQUANTITIES + DQUANTITIES + QQUANTITIES
15
15
XUNITS = ["degrees" , "radians" , "rad" , "deg" , "inv_angs" , "inv_nm" , "nm-1" , "A-1" ]
16
16
17
- x_grid_length_mismatch_emsg = (
18
- "The two objects have different x -array lengths. "
19
- "Please ensure the length of the x -value during initialization is identical."
17
+ y_grid_length_mismatch_emsg = (
18
+ "The two objects have different y -array lengths. "
19
+ "Please ensure the length of the y -value during initialization is identical."
20
20
)
21
21
22
22
invalid_add_type_emsg = (
23
23
"You may only add a DiffractionObject with another DiffractionObject or a scalar value. "
24
24
"Please rerun by adding another DiffractionObject instance or a scalar value. "
25
- "e.g., my_do_1 + my_do_2 or my_do + 10"
25
+ "e.g., my_do_1 + my_do_2 or my_do + 10 or 10 + my_do "
26
26
)
27
27
28
28
@@ -175,56 +175,57 @@ def __eq__(self, other):
175
175
return True
176
176
177
177
def __add__ (self , other ):
178
- """Add a scalar value or another DiffractionObject to the xarrays of
179
- the DiffractionObject.
178
+ """Add a scalar value or another DiffractionObject to the yarray of the
179
+ DiffractionObject.
180
180
181
181
Parameters
182
182
----------
183
183
other : DiffractionObject or int or float
184
184
The object to add to the current DiffractionObject. If `other` is a scalar value,
185
- it will be added to all xarrays . The length of the xarrays must match if `other` is
185
+ it will be added to all yarray . The length of the yarray must match if `other` is
186
186
an instance of DiffractionObject.
187
187
188
188
Returns
189
189
-------
190
190
DiffractionObject
191
- The new and deep-copied DiffractionObject instance after adding values to the xarrays .
191
+ The new and deep-copied DiffractionObject instance after adding values to the yarray .
192
192
193
193
Raises
194
194
------
195
195
ValueError
196
- Raised when the length of the xarrays of the two DiffractionObject instances do not match.
196
+ Raised when the length of the yarray of the two DiffractionObject instances do not match.
197
197
TypeError
198
198
Raised when the type of `other` is not an instance of DiffractionObject, int, or float.
199
199
200
200
Examples
201
201
--------
202
- Add a scalar value to the xarrays of the DiffractionObject instance:
202
+ Add a scalar value to the yarray of the DiffractionObject instance:
203
203
>>> new_do = my_do + 10.1
204
+ >>> new_do = 10.1 + my_do
204
205
205
- Add the xarrays of two DiffractionObject instances:
206
+ Add the yarray of two DiffractionObject instances:
206
207
>>> new_do = my_do_1 + my_do_2
207
208
"""
208
209
210
+ self ._check_operation_compatibility (other )
209
211
summed_do = deepcopy (self )
210
- # Add scalar value to all xarrays by broadcasting
211
212
if isinstance (other , (int , float )):
212
- summed_do ._all_arrays [:, 1 ] += other
213
- summed_do ._all_arrays [:, 2 ] += other
214
- summed_do ._all_arrays [:, 3 ] += other
215
- # Add xarrays of two DiffractionObject instances
216
- elif isinstance (other , DiffractionObject ):
217
- if len (self .on_tth ()[0 ]) != len (other .on_tth ()[0 ]):
218
- raise ValueError (x_grid_length_mismatch_emsg )
219
- summed_do ._all_arrays [:, 1 ] += other .on_q ()[0 ]
220
- summed_do ._all_arrays [:, 2 ] += other .on_tth ()[0 ]
221
- summed_do ._all_arrays [:, 3 ] += other .on_d ()[0 ]
222
- else :
223
- raise TypeError (invalid_add_type_emsg )
213
+ summed_do ._all_arrays [:, 0 ] += other
214
+ if isinstance (other , DiffractionObject ):
215
+ summed_do ._all_arrays [:, 0 ] += other .all_arrays [:, 0 ]
224
216
return summed_do
225
217
226
218
__radd__ = __add__
227
219
220
+ def _check_operation_compatibility (self , other ):
221
+ if not isinstance (other , (DiffractionObject , int , float )):
222
+ raise TypeError (invalid_add_type_emsg )
223
+ if isinstance (other , DiffractionObject ):
224
+ self_yarray = self .all_arrays [:, 0 ]
225
+ other_yarray = other .all_arrays [:, 0 ]
226
+ if len (self_yarray ) != len (other_yarray ):
227
+ raise ValueError (y_grid_length_mismatch_emsg )
228
+
228
229
def __sub__ (self , other ):
229
230
subtracted = deepcopy (self )
230
231
if isinstance (other , int ) or isinstance (other , float ) or isinstance (other , np .ndarray ):
@@ -233,7 +234,7 @@ def __sub__(self, other):
233
234
elif not isinstance (other , DiffractionObject ):
234
235
raise TypeError ("I only know how to subtract two Scattering_object objects" )
235
236
elif self .on_tth [0 ].all () != other .on_tth [0 ].all ():
236
- raise RuntimeError (x_grid_length_mismatch_emsg )
237
+ raise RuntimeError (y_grid_length_mismatch_emsg )
237
238
else :
238
239
subtracted .on_tth [1 ] = self .on_tth [1 ] - other .on_tth [1 ]
239
240
subtracted .on_q [1 ] = self .on_q [1 ] - other .on_q [1 ]
@@ -247,7 +248,7 @@ def __rsub__(self, other):
247
248
elif not isinstance (other , DiffractionObject ):
248
249
raise TypeError ("I only know how to subtract two Scattering_object objects" )
249
250
elif self .on_tth [0 ].all () != other .on_tth [0 ].all ():
250
- raise RuntimeError (x_grid_length_mismatch_emsg )
251
+ raise RuntimeError (y_grid_length_mismatch_emsg )
251
252
else :
252
253
subtracted .on_tth [1 ] = other .on_tth [1 ] - self .on_tth [1 ]
253
254
subtracted .on_q [1 ] = other .on_q [1 ] - self .on_q [1 ]
@@ -261,7 +262,7 @@ def __mul__(self, other):
261
262
elif not isinstance (other , DiffractionObject ):
262
263
raise TypeError ("I only know how to multiply two Scattering_object objects" )
263
264
elif self .on_tth [0 ].all () != other .on_tth [0 ].all ():
264
- raise RuntimeError (x_grid_length_mismatch_emsg )
265
+ raise RuntimeError (y_grid_length_mismatch_emsg )
265
266
else :
266
267
multiplied .on_tth [1 ] = self .on_tth [1 ] * other .on_tth [1 ]
267
268
multiplied .on_q [1 ] = self .on_q [1 ] * other .on_q [1 ]
@@ -273,7 +274,7 @@ def __rmul__(self, other):
273
274
multiplied .on_tth [1 ] = other * self .on_tth [1 ]
274
275
multiplied .on_q [1 ] = other * self .on_q [1 ]
275
276
elif self .on_tth [0 ].all () != other .on_tth [0 ].all ():
276
- raise RuntimeError (x_grid_length_mismatch_emsg )
277
+ raise RuntimeError (y_grid_length_mismatch_emsg )
277
278
else :
278
279
multiplied .on_tth [1 ] = self .on_tth [1 ] * other .on_tth [1 ]
279
280
multiplied .on_q [1 ] = self .on_q [1 ] * other .on_q [1 ]
@@ -287,7 +288,7 @@ def __truediv__(self, other):
287
288
elif not isinstance (other , DiffractionObject ):
288
289
raise TypeError ("I only know how to multiply two Scattering_object objects" )
289
290
elif self .on_tth [0 ].all () != other .on_tth [0 ].all ():
290
- raise RuntimeError (x_grid_length_mismatch_emsg )
291
+ raise RuntimeError (y_grid_length_mismatch_emsg )
291
292
else :
292
293
divided .on_tth [1 ] = self .on_tth [1 ] / other .on_tth [1 ]
293
294
divided .on_q [1 ] = self .on_q [1 ] / other .on_q [1 ]
@@ -299,7 +300,7 @@ def __rtruediv__(self, other):
299
300
divided .on_tth [1 ] = other / self .on_tth [1 ]
300
301
divided .on_q [1 ] = other / self .on_q [1 ]
301
302
elif self .on_tth [0 ].all () != other .on_tth [0 ].all ():
302
- raise RuntimeError (x_grid_length_mismatch_emsg )
303
+ raise RuntimeError (y_grid_length_mismatch_emsg )
303
304
else :
304
305
divided .on_tth [1 ] = other .on_tth [1 ] / self .on_tth [1 ]
305
306
divided .on_q [1 ] = other .on_q [1 ] / self .on_q [1 ]
0 commit comments