Skip to content

Commit 0c9ef59

Browse files
authored
Refactor diffraction functions to return floats
1 parent 6689388 commit 0c9ef59

1 file changed

Lines changed: 21 additions & 29 deletions

File tree

‎physics/diffraction.py‎

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def check_min_intensity(
1313
wavelength (float): The wavelength of light in nanometers.
1414
1515
Returns:
16-
bool: True if minimum intensity is met, otherwise False.
16+
True if minimum intensity is met, otherwise False.
1717
1818
>>> check_min_intensity(4, 0.25, 300)
1919
False
@@ -33,12 +33,12 @@ def check_max_intensity(
3333
Checks for the condition of maximum intensity in a diffraction pattern.
3434
3535
Args:
36-
slit_width (float): The width of the slit in millimeters.
37-
diff_angle (float): The diffraction angle in radians.
38-
wavelength (float): The wavelength of light in nanometers.
36+
slit_width: The width of the slit in millimeters.
37+
diff_angle: The diffraction angle in radians.
38+
wavelength: The wavelength of light in nanometers.
3939
4040
Returns:
41-
bool: True if maximum intensity is met, otherwise False.
41+
True if maximum intensity is met, otherwise False.
4242
4343
>>> check_max_intensity(1, 0.001, 100)
4444
False
@@ -47,60 +47,52 @@ def check_max_intensity(
4747
"""
4848
wavelength *= 10**-6
4949
n_val = round(((2 * slit_width * diff_angle) - wavelength) / (2 * wavelength), 4)
50-
r_val = n_val - math.floor(n_val) == 0
51-
return r_val
50+
return n_val - math.floor(n_val) == 0
5251

5352

5453
def intensity_single_slit(
5554
slit_width: float = 1.0, diff_angle: float = 0.0, wavelength: float = 100.0
56-
) -> str:
55+
) -> float:
5756
"""
5857
Computes the intensity for a single slit diffraction pattern.
5958
6059
Args:
61-
slit_width (float): The width of the slit in millimeters.
62-
diff_angle (float): The diffraction angle in radians.
63-
wavelength (float): The wavelength of light in nanometers.
60+
slit_width: The width of the slit in millimeters.
61+
diff_angle: The diffraction angle in radians.
62+
wavelength: The wavelength of light in nanometers.
6463
6564
Returns:
66-
str: The intensity of the diffraction pattern.
65+
The intensity of the diffraction pattern.
6766
6867
>>> intensity_single_slit(1, 0.0005, 100)
69-
'0.9999999999177533 I0'
68+
0.9999999999177533
7069
"""
7170
beta = math.pi * slit_width * (math.sin(diff_angle) / wavelength)
72-
i_coeff = (math.sin(beta) / beta) ** 2
73-
return f"{i_coeff} I0"
71+
return (math.sin(beta) / beta) ** 2
7472

7573

76-
def intensity_double_slit(path_diff: int = 0, intensity_max: str | float = "I0") -> str:
74+
def intensity_double_slit(path_diff: float = 0, intensity_max: float = 1.0) -> float:
7775
"""
7876
Computes the intensity for a double slit diffraction pattern.
7977
8078
Args:
81-
path_diff (float): The path difference in the two waves.
82-
intensity_max (str or int): The maximum intensity.
79+
path_diff: The path difference in the two waves.
80+
intensity_max: The maximum intensity.
8381
8482
Returns:
85-
str or float: The intensity of the diffraction pattern.
83+
The intensity of the diffraction pattern.
8684
8785
>>> intensity_double_slit(0, 1)
88-
'4.0'
86+
4.0
8987
>>> intensity_double_slit(0.001, 1)
90-
'3.999999000000084'
88+
3.999999000000084
9189
>>> intensity_double_slit(0)
92-
'4.0 I0'
90+
4.0
9391
"""
94-
r_val = (
95-
str(4 * intensity_max * (math.cos(path_diff / 2)) ** 2)
96-
if (type(intensity_max) is float or type(intensity_max) is int)
97-
else f"{4 * (math.cos(path_diff / 2) ** 2)} I0"
98-
)
99-
return r_val
92+
return 4 * intensity_max * (math.cos(path_diff / 2)) ** 2
10093

10194

10295
if __name__ == "__main__":
10396
import doctest
10497

10598
doctest.testmod()
106-
# pass

0 commit comments

Comments
 (0)