From e4e9358a353dbe26ddd860089066a1f897081c24 Mon Sep 17 00:00:00 2001 From: nikhitha79 <115790230+nikhitha79@users.noreply.github.com> Date: Thu, 17 Oct 2024 23:00:47 +0530 Subject: [PATCH 1/2] Create de_broglie de_broglie wavelength --- physics/de_broglie | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 physics/de_broglie diff --git a/physics/de_broglie b/physics/de_broglie new file mode 100644 index 000000000000..e3c0b8ebef97 --- /dev/null +++ b/physics/de_broglie @@ -0,0 +1,46 @@ +PLANCK_CONSTANT_JS = 6.62607015e-34 # Planck's constant in Joule-seconds +PLANCK_CONSTANT_EVS = 4.135667696e-15 # Planck's constant in eV-seconds + +def de_broglie_wavelength(momentum: float, in_ev: bool = False) -> str: + """ + Calculates the de Broglie wavelength of a particle using the given momentum. + + Parameters: + momentum (float): Momentum of the particle. + in_ev (bool, optional): True if momentum is in eV·s. + If False, momentum is in kg·m/s. + + Returns: + str: The calculated de Broglie wavelength of the particle in meters, + formatted in scientific notation. + + Raises: + ValueError: If the momentum is zero or negative. + + Usage example: + >>> de_broglie_wavelength(1e-24) + '6.62607015e-10' + >>> de_broglie_wavelength(1e-24, True) + '4.13566770e+09' + >>> de_broglie_wavelength(0) + Traceback (most recent call last): + ... + ValueError: Momentum can't be zero or negative. + >>> de_broglie_wavelength(-1e-24) + Traceback (most recent call last): + ... + ValueError: Momentum can't be zero or negative. + >>> de_broglie_wavelength(5e-20) + '1.32521403e-14' + """ + + if momentum <= 0: + raise ValueError("Momentum can't be zero or negative.") + + + wavelength = PLANCK_CONSTANT_EVS / momentum if in_ev else PLANCK_CONSTANT_JS / momentum + return f"{wavelength:.8e}" + +if __name__ == "__main__": + import doctest + doctest.testmod() From 7fa920adfc92ea55db2feede56d01de152218703 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 17 Oct 2024 17:32:29 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- physics/de_broglie | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/physics/de_broglie b/physics/de_broglie index e3c0b8ebef97..fa329e636700 100644 --- a/physics/de_broglie +++ b/physics/de_broglie @@ -7,11 +7,11 @@ def de_broglie_wavelength(momentum: float, in_ev: bool = False) -> str: Parameters: momentum (float): Momentum of the particle. - in_ev (bool, optional): True if momentum is in eV·s. + in_ev (bool, optional): True if momentum is in eV·s. If False, momentum is in kg·m/s. Returns: - str: The calculated de Broglie wavelength of the particle in meters, + str: The calculated de Broglie wavelength of the particle in meters, formatted in scientific notation. Raises: @@ -37,7 +37,7 @@ def de_broglie_wavelength(momentum: float, in_ev: bool = False) -> str: if momentum <= 0: raise ValueError("Momentum can't be zero or negative.") - + wavelength = PLANCK_CONSTANT_EVS / momentum if in_ev else PLANCK_CONSTANT_JS / momentum return f"{wavelength:.8e}"