1414XQUANTITIES = ANGLEQUANTITIES + DQUANTITIES + QQUANTITIES
1515XUNITS = ["degrees" , "radians" , "rad" , "deg" , "inv_angs" , "inv_nm" , "nm-1" , "A-1" ]
1616
17- x_grid_emsg = (
18- "objects are not on the same x-grid. You may add them using the self.add method "
19- "and specifying how to handle the mismatch."
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+ )
21+
22+ invalid_add_type_emsg = (
23+ "You may only add a DiffractionObject with another DiffractionObject or a scalar value. "
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 or 10 + my_do"
2026)
2127
2228
@@ -169,32 +175,56 @@ def __eq__(self, other):
169175 return True
170176
171177 def __add__ (self , other ):
172- summed = deepcopy (self )
173- if isinstance (other , int ) or isinstance (other , float ) or isinstance (other , np .ndarray ):
174- summed .on_tth [1 ] = self .on_tth [1 ] + other
175- summed .on_q [1 ] = self .on_q [1 ] + other
176- elif not isinstance (other , DiffractionObject ):
177- raise TypeError ("I only know how to sum two DiffractionObject objects" )
178- elif self .on_tth [0 ].all () != other .on_tth [0 ].all ():
179- raise RuntimeError (x_grid_emsg )
180- else :
181- summed .on_tth [1 ] = self .on_tth [1 ] + other .on_tth [1 ]
182- summed .on_q [1 ] = self .on_q [1 ] + other .on_q [1 ]
183- return summed
178+ """Add a scalar value or another DiffractionObject to the yarray of the
179+ DiffractionObject.
184180
185- def __radd__ (self , other ):
186- summed = deepcopy (self )
187- if isinstance (other , int ) or isinstance (other , float ) or isinstance (other , np .ndarray ):
188- summed .on_tth [1 ] = self .on_tth [1 ] + other
189- summed .on_q [1 ] = self .on_q [1 ] + other
190- elif not isinstance (other , DiffractionObject ):
191- raise TypeError ("I only know how to sum two Scattering_object objects" )
192- elif self .on_tth [0 ].all () != other .on_tth [0 ].all ():
193- raise RuntimeError (x_grid_emsg )
194- else :
195- summed .on_tth [1 ] = self .on_tth [1 ] + other .on_tth [1 ]
196- summed .on_q [1 ] = self .on_q [1 ] + other .on_q [1 ]
197- return summed
181+ Parameters
182+ ----------
183+ other : DiffractionObject or int or float
184+ The object to add to the current DiffractionObject. If `other` is a scalar value,
185+ it will be added to all yarray. The length of the yarray must match if `other` is
186+ an instance of DiffractionObject.
187+
188+ Returns
189+ -------
190+ DiffractionObject
191+ The new and deep-copied DiffractionObject instance after adding values to the yarray.
192+
193+ Raises
194+ ------
195+ ValueError
196+ Raised when the length of the yarray of the two DiffractionObject instances do not match.
197+ TypeError
198+ Raised when the type of `other` is not an instance of DiffractionObject, int, or float.
199+
200+ Examples
201+ --------
202+ Add a scalar value to the yarray of the DiffractionObject instance:
203+ >>> new_do = my_do + 10.1
204+ >>> new_do = 10.1 + my_do
205+
206+ Add the yarray of two DiffractionObject instances:
207+ >>> new_do = my_do_1 + my_do_2
208+ """
209+
210+ self ._check_operation_compatibility (other )
211+ summed_do = deepcopy (self )
212+ if isinstance (other , (int , float )):
213+ summed_do ._all_arrays [:, 0 ] += other
214+ if isinstance (other , DiffractionObject ):
215+ summed_do ._all_arrays [:, 0 ] += other .all_arrays [:, 0 ]
216+ return summed_do
217+
218+ __radd__ = __add__
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 )
198228
199229 def __sub__ (self , other ):
200230 subtracted = deepcopy (self )
@@ -204,7 +234,7 @@ def __sub__(self, other):
204234 elif not isinstance (other , DiffractionObject ):
205235 raise TypeError ("I only know how to subtract two Scattering_object objects" )
206236 elif self .on_tth [0 ].all () != other .on_tth [0 ].all ():
207- raise RuntimeError (x_grid_emsg )
237+ raise RuntimeError (y_grid_length_mismatch_emsg )
208238 else :
209239 subtracted .on_tth [1 ] = self .on_tth [1 ] - other .on_tth [1 ]
210240 subtracted .on_q [1 ] = self .on_q [1 ] - other .on_q [1 ]
@@ -218,7 +248,7 @@ def __rsub__(self, other):
218248 elif not isinstance (other , DiffractionObject ):
219249 raise TypeError ("I only know how to subtract two Scattering_object objects" )
220250 elif self .on_tth [0 ].all () != other .on_tth [0 ].all ():
221- raise RuntimeError (x_grid_emsg )
251+ raise RuntimeError (y_grid_length_mismatch_emsg )
222252 else :
223253 subtracted .on_tth [1 ] = other .on_tth [1 ] - self .on_tth [1 ]
224254 subtracted .on_q [1 ] = other .on_q [1 ] - self .on_q [1 ]
@@ -232,7 +262,7 @@ def __mul__(self, other):
232262 elif not isinstance (other , DiffractionObject ):
233263 raise TypeError ("I only know how to multiply two Scattering_object objects" )
234264 elif self .on_tth [0 ].all () != other .on_tth [0 ].all ():
235- raise RuntimeError (x_grid_emsg )
265+ raise RuntimeError (y_grid_length_mismatch_emsg )
236266 else :
237267 multiplied .on_tth [1 ] = self .on_tth [1 ] * other .on_tth [1 ]
238268 multiplied .on_q [1 ] = self .on_q [1 ] * other .on_q [1 ]
@@ -244,7 +274,7 @@ def __rmul__(self, other):
244274 multiplied .on_tth [1 ] = other * self .on_tth [1 ]
245275 multiplied .on_q [1 ] = other * self .on_q [1 ]
246276 elif self .on_tth [0 ].all () != other .on_tth [0 ].all ():
247- raise RuntimeError (x_grid_emsg )
277+ raise RuntimeError (y_grid_length_mismatch_emsg )
248278 else :
249279 multiplied .on_tth [1 ] = self .on_tth [1 ] * other .on_tth [1 ]
250280 multiplied .on_q [1 ] = self .on_q [1 ] * other .on_q [1 ]
@@ -258,7 +288,7 @@ def __truediv__(self, other):
258288 elif not isinstance (other , DiffractionObject ):
259289 raise TypeError ("I only know how to multiply two Scattering_object objects" )
260290 elif self .on_tth [0 ].all () != other .on_tth [0 ].all ():
261- raise RuntimeError (x_grid_emsg )
291+ raise RuntimeError (y_grid_length_mismatch_emsg )
262292 else :
263293 divided .on_tth [1 ] = self .on_tth [1 ] / other .on_tth [1 ]
264294 divided .on_q [1 ] = self .on_q [1 ] / other .on_q [1 ]
@@ -270,7 +300,7 @@ def __rtruediv__(self, other):
270300 divided .on_tth [1 ] = other / self .on_tth [1 ]
271301 divided .on_q [1 ] = other / self .on_q [1 ]
272302 elif self .on_tth [0 ].all () != other .on_tth [0 ].all ():
273- raise RuntimeError (x_grid_emsg )
303+ raise RuntimeError (y_grid_length_mismatch_emsg )
274304 else :
275305 divided .on_tth [1 ] = other .on_tth [1 ] / self .on_tth [1 ]
276306 divided .on_q [1 ] = other .on_q [1 ] / self .on_q [1 ]
0 commit comments