From 1427fe34f0bf364f860b9f3c4ecd811215e0cdcb Mon Sep 17 00:00:00 2001 From: Baron105 Date: Tue, 10 Oct 2023 19:22:41 +0530 Subject: [PATCH 01/33] avg and mps speed formulae added --- ...avg_and_mps_speeds_of_gaseous_molecules.py | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 physics/avg_and_mps_speeds_of_gaseous_molecules.py diff --git a/physics/avg_and_mps_speeds_of_gaseous_molecules.py b/physics/avg_and_mps_speeds_of_gaseous_molecules.py new file mode 100644 index 000000000000..70f189b91912 --- /dev/null +++ b/physics/avg_and_mps_speeds_of_gaseous_molecules.py @@ -0,0 +1,79 @@ +""" +The root-mean-square, average and most probable speeds are derived from the Maxwell-Boltzmann distribution. The Maxwell-Boltzmann distribution is a probability distribution that describes the distribution of speeds for particles in a gas. The distribution is given by the following equation: + + ------------------------------------------------- + | f(v) = (M/2πRT)^(3/2) * 4πv^2 * e^(-Mv^2/2RT) | + ------------------------------------------------- + +where: + f(v) is the fraction of molecules with a speed v + M is the molar mass of the gas in kg/mol + R is the gas constant + T is the absolute temperature + +More information about the Maxwell-Boltzmann distribution can be found here: https://en.wikipedia.org/wiki/Maxwell%E2%80%93Boltzmann_distribution + +The average speed can be calculated by integrating the Maxwell-Boltzmann distribution from 0 to infinity and dividing by the total number of molecules. The result is: + + --------------------- + | vavg = √8RT/πM | + --------------------- + +The most probable speed is the speed at which the Maxwell-Boltzmann distribution is at its maximum. This can be found by differentiating the Maxwell-Boltzmann distribution with respect to v and setting the result equal to zero. The result is: + + --------------------- + | vmp = √2RT/M | + --------------------- + +The root-mean-square speed is another measure of the average speed of the molecules in a gas. It is calculated by taking the square root of the average of the squares of the speeds of the molecules. The result is: + + --------------------- + | vrms = √3RT/m | + --------------------- + +Here we have defined functions to calculate the average and most probable speeds of molecules in a gas given the temperature and molar mass of the gas. +""" + +# necessary constants +PI = 3.1415926535 # pi +R = 8.3144626181 # gas constant + +def avg_speed_of_molecule(temperature: float, molar_mass: float) -> float: + """ + Takes the temperature (in K) and molar mass (in kg/mol) of a gas and returns the average speed of a molecule in the gas (in m/s). + + Examples: + >>> avg_speed_of_molecule(273, 0.028) # nitrogen at 273 K + 454.34887551126405 + >>> avg_speed_of_molecule(300, 0.032) # oxygen at 300 K + 445.5257273482451 + """ + if temperature < 0: + raise Exception("Absolute temperature cannot be less than 0 K") + if molar_mass <= 0: + raise Exception("Molar mass should be greater than 0 kg/mol") + else: + return (8 * R * temperature / (PI * molar_mass)) ** 0.5 + +def mps_speed_of_molecule(temperature: float, molar_mass: float) -> float: + """ + Takes the temperature (in K) and molar mass (in kg/mol) of a gas and returns the most probable speed of a molecule in the gas (in m/s). + + Examples: + >>> mps_speed_of_molecule(273, 0.028) # nitrogen at 273 K + 402.6562070215111 + >>> mps_speed_of_molecule(300, 0.032) # oxygen at 300 K + 394.83689555229637 + """ + if temperature < 0: + raise Exception("Absolute temperature cannot be less than 0 K") + if molar_mass <= 0: + raise Exception("Molar mass should be greater than 0 kg/mol") + else: + return (2 * R * temperature / molar_mass) ** 0.5 + +if __name__ == "__main__": + import doctest + + doctest.testmod() + \ No newline at end of file From 0608c40a89a1cba314551633795318f140466825 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 10 Oct 2023 14:00:21 +0000 Subject: [PATCH 02/33] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- ...avg_and_mps_speeds_of_gaseous_molecules.py | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/physics/avg_and_mps_speeds_of_gaseous_molecules.py b/physics/avg_and_mps_speeds_of_gaseous_molecules.py index 70f189b91912..b851ae3e2542 100644 --- a/physics/avg_and_mps_speeds_of_gaseous_molecules.py +++ b/physics/avg_and_mps_speeds_of_gaseous_molecules.py @@ -1,10 +1,10 @@ """ The root-mean-square, average and most probable speeds are derived from the Maxwell-Boltzmann distribution. The Maxwell-Boltzmann distribution is a probability distribution that describes the distribution of speeds for particles in a gas. The distribution is given by the following equation: - + ------------------------------------------------- | f(v) = (M/2πRT)^(3/2) * 4πv^2 * e^(-Mv^2/2RT) | ------------------------------------------------- - + where: f(v) is the fraction of molecules with a speed v M is the molar mass of the gas in kg/mol @@ -12,19 +12,19 @@ T is the absolute temperature More information about the Maxwell-Boltzmann distribution can be found here: https://en.wikipedia.org/wiki/Maxwell%E2%80%93Boltzmann_distribution - + The average speed can be calculated by integrating the Maxwell-Boltzmann distribution from 0 to infinity and dividing by the total number of molecules. The result is: - + --------------------- | vavg = √8RT/πM | --------------------- - + The most probable speed is the speed at which the Maxwell-Boltzmann distribution is at its maximum. This can be found by differentiating the Maxwell-Boltzmann distribution with respect to v and setting the result equal to zero. The result is: --------------------- | vmp = √2RT/M | --------------------- - + The root-mean-square speed is another measure of the average speed of the molecules in a gas. It is calculated by taking the square root of the average of the squares of the speeds of the molecules. The result is: --------------------- @@ -35,13 +35,14 @@ """ # necessary constants -PI = 3.1415926535 # pi -R = 8.3144626181 # gas constant +PI = 3.1415926535 # pi +R = 8.3144626181 # gas constant + def avg_speed_of_molecule(temperature: float, molar_mass: float) -> float: """ Takes the temperature (in K) and molar mass (in kg/mol) of a gas and returns the average speed of a molecule in the gas (in m/s). - + Examples: >>> avg_speed_of_molecule(273, 0.028) # nitrogen at 273 K 454.34887551126405 @@ -54,11 +55,12 @@ def avg_speed_of_molecule(temperature: float, molar_mass: float) -> float: raise Exception("Molar mass should be greater than 0 kg/mol") else: return (8 * R * temperature / (PI * molar_mass)) ** 0.5 - + + def mps_speed_of_molecule(temperature: float, molar_mass: float) -> float: """ Takes the temperature (in K) and molar mass (in kg/mol) of a gas and returns the most probable speed of a molecule in the gas (in m/s). - + Examples: >>> mps_speed_of_molecule(273, 0.028) # nitrogen at 273 K 402.6562070215111 @@ -71,9 +73,9 @@ def mps_speed_of_molecule(temperature: float, molar_mass: float) -> float: raise Exception("Molar mass should be greater than 0 kg/mol") else: return (2 * R * temperature / molar_mass) ** 0.5 - + + if __name__ == "__main__": import doctest doctest.testmod() - \ No newline at end of file From 7457b25d709d242468b8d31b29307dee3914771d Mon Sep 17 00:00:00 2001 From: Baron105 Date: Tue, 10 Oct 2023 19:33:39 +0530 Subject: [PATCH 03/33] avg and mps speed formulae added --- ...avg_and_mps_speeds_of_gaseous_molecules.py | 37 ++++++++++++------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/physics/avg_and_mps_speeds_of_gaseous_molecules.py b/physics/avg_and_mps_speeds_of_gaseous_molecules.py index 70f189b91912..8ee71c56f903 100644 --- a/physics/avg_and_mps_speeds_of_gaseous_molecules.py +++ b/physics/avg_and_mps_speeds_of_gaseous_molecules.py @@ -1,5 +1,8 @@ """ -The root-mean-square, average and most probable speeds are derived from the Maxwell-Boltzmann distribution. The Maxwell-Boltzmann distribution is a probability distribution that describes the distribution of speeds for particles in a gas. The distribution is given by the following equation: +The root-mean-square, average and most probable speeds are derived from +the Maxwell-Boltzmann distribution. The Maxwell-Boltzmann distribution is a +probability distribution that describes the distribution of speeds for particles in a gas. +The distribution is given by the following equation: ------------------------------------------------- | f(v) = (M/2πRT)^(3/2) * 4πv^2 * e^(-Mv^2/2RT) | @@ -11,27 +14,34 @@ R is the gas constant T is the absolute temperature -More information about the Maxwell-Boltzmann distribution can be found here: https://en.wikipedia.org/wiki/Maxwell%E2%80%93Boltzmann_distribution +More information about the Maxwell-Boltzmann distribution can be found here: +https://en.wikipedia.org/wiki/Maxwell%E2%80%93Boltzmann_distribution -The average speed can be calculated by integrating the Maxwell-Boltzmann distribution from 0 to infinity and dividing by the total number of molecules. The result is: +The average speed can be calculated by integrating the Maxwell-Boltzmann distribution +from 0 to infinity and dividing by the total number of molecules. The result is: --------------------- | vavg = √8RT/πM | --------------------- -The most probable speed is the speed at which the Maxwell-Boltzmann distribution is at its maximum. This can be found by differentiating the Maxwell-Boltzmann distribution with respect to v and setting the result equal to zero. The result is: +The most probable speed is the speed at which the Maxwell-Boltzmann distribution is at its maximum. +This can be found by differentiating the Maxwell-Boltzmann distribution with respect to v +and setting the result equal to zero. The result is: --------------------- | vmp = √2RT/M | --------------------- -The root-mean-square speed is another measure of the average speed of the molecules in a gas. It is calculated by taking the square root of the average of the squares of the speeds of the molecules. The result is: +The root-mean-square speed is another measure of the average speed of the molecules in a gas. +It is calculated by taking the square root of the average of the +squares of the speeds of the molecules. The result is: --------------------- | vrms = √3RT/m | --------------------- -Here we have defined functions to calculate the average and most probable speeds of molecules in a gas given the temperature and molar mass of the gas. +Here we have defined functions to calculate the average and +most probable speeds of molecules in a gas given the temperature and molar mass of the gas. """ # necessary constants @@ -40,8 +50,9 @@ def avg_speed_of_molecule(temperature: float, molar_mass: float) -> float: """ - Takes the temperature (in K) and molar mass (in kg/mol) of a gas and returns the average speed of a molecule in the gas (in m/s). - + Takes the temperature (in K) and molar mass (in kg/mol) of a gas + and returns the average speed of a molecule in the gas (in m/s). + Examples: >>> avg_speed_of_molecule(273, 0.028) # nitrogen at 273 K 454.34887551126405 @@ -54,11 +65,12 @@ def avg_speed_of_molecule(temperature: float, molar_mass: float) -> float: raise Exception("Molar mass should be greater than 0 kg/mol") else: return (8 * R * temperature / (PI * molar_mass)) ** 0.5 - + def mps_speed_of_molecule(temperature: float, molar_mass: float) -> float: """ - Takes the temperature (in K) and molar mass (in kg/mol) of a gas and returns the most probable speed of a molecule in the gas (in m/s). - + Takes the temperature (in K) and molar mass (in kg/mol) of a gas + and returns the most probable speed of a molecule in the gas (in m/s). + Examples: >>> mps_speed_of_molecule(273, 0.028) # nitrogen at 273 K 402.6562070215111 @@ -71,9 +83,8 @@ def mps_speed_of_molecule(temperature: float, molar_mass: float) -> float: raise Exception("Molar mass should be greater than 0 kg/mol") else: return (2 * R * temperature / molar_mass) ** 0.5 - + if __name__ == "__main__": import doctest doctest.testmod() - \ No newline at end of file From b1ef5a36fbec003505091eda2731e3bb8aca4955 Mon Sep 17 00:00:00 2001 From: Baron105 Date: Tue, 10 Oct 2023 19:39:08 +0530 Subject: [PATCH 04/33] fixed_spacing --- physics/avg_and_mps_speeds_of_gaseous_molecules.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/physics/avg_and_mps_speeds_of_gaseous_molecules.py b/physics/avg_and_mps_speeds_of_gaseous_molecules.py index 8ee71c56f903..20baff29082e 100644 --- a/physics/avg_and_mps_speeds_of_gaseous_molecules.py +++ b/physics/avg_and_mps_speeds_of_gaseous_molecules.py @@ -3,11 +3,11 @@ the Maxwell-Boltzmann distribution. The Maxwell-Boltzmann distribution is a probability distribution that describes the distribution of speeds for particles in a gas. The distribution is given by the following equation: - + ------------------------------------------------- | f(v) = (M/2πRT)^(3/2) * 4πv^2 * e^(-Mv^2/2RT) | ------------------------------------------------- - + where: f(v) is the fraction of molecules with a speed v M is the molar mass of the gas in kg/mol @@ -16,14 +16,14 @@ More information about the Maxwell-Boltzmann distribution can be found here: https://en.wikipedia.org/wiki/Maxwell%E2%80%93Boltzmann_distribution - + The average speed can be calculated by integrating the Maxwell-Boltzmann distribution from 0 to infinity and dividing by the total number of molecules. The result is: - + --------------------- | vavg = √8RT/πM | --------------------- - + The most probable speed is the speed at which the Maxwell-Boltzmann distribution is at its maximum. This can be found by differentiating the Maxwell-Boltzmann distribution with respect to v and setting the result equal to zero. The result is: @@ -31,7 +31,7 @@ --------------------- | vmp = √2RT/M | --------------------- - + The root-mean-square speed is another measure of the average speed of the molecules in a gas. It is calculated by taking the square root of the average of the squares of the speeds of the molecules. The result is: From 07502c875b13e28836d1e1769613d192ce87bf74 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 10 Oct 2023 14:11:38 +0000 Subject: [PATCH 05/33] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- ...avg_and_mps_speeds_of_gaseous_molecules.py | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/physics/avg_and_mps_speeds_of_gaseous_molecules.py b/physics/avg_and_mps_speeds_of_gaseous_molecules.py index f861520d334b..8dfaa9bd9944 100644 --- a/physics/avg_and_mps_speeds_of_gaseous_molecules.py +++ b/physics/avg_and_mps_speeds_of_gaseous_molecules.py @@ -1,46 +1,46 @@ """ -The root-mean-square, average and most probable speeds are derived from -the Maxwell-Boltzmann distribution. The Maxwell-Boltzmann distribution is a +The root-mean-square, average and most probable speeds are derived from +the Maxwell-Boltzmann distribution. The Maxwell-Boltzmann distribution is a probability distribution that describes the distribution of speeds for particles in a gas. The distribution is given by the following equation: ------------------------------------------------- | f(v) = (M/2πRT)^(3/2) * 4πv^2 * e^(-Mv^2/2RT) | ------------------------------------------------- - + where: f(v) is the fraction of molecules with a speed v M is the molar mass of the gas in kg/mol R is the gas constant T is the absolute temperature -More information about the Maxwell-Boltzmann distribution can be found here: +More information about the Maxwell-Boltzmann distribution can be found here: https://en.wikipedia.org/wiki/Maxwell%E2%80%93Boltzmann_distribution -The average speed can be calculated by integrating the Maxwell-Boltzmann distribution +The average speed can be calculated by integrating the Maxwell-Boltzmann distribution from 0 to infinity and dividing by the total number of molecules. The result is: - + --------------------- | vavg = √8RT/πM | --------------------- -The most probable speed is the speed at which the Maxwell-Boltzmann distribution is at its maximum. -This can be found by differentiating the Maxwell-Boltzmann distribution with respect to v +The most probable speed is the speed at which the Maxwell-Boltzmann distribution is at its maximum. +This can be found by differentiating the Maxwell-Boltzmann distribution with respect to v and setting the result equal to zero. The result is: --------------------- | vmp = √2RT/M | --------------------- -The root-mean-square speed is another measure of the average speed of the molecules in a gas. -It is calculated by taking the square root of the average of the +The root-mean-square speed is another measure of the average speed of the molecules in a gas. +It is calculated by taking the square root of the average of the squares of the speeds of the molecules. The result is: --------------------- | vrms = √3RT/m | --------------------- -Here we have defined functions to calculate the average and +Here we have defined functions to calculate the average and most probable speeds of molecules in a gas given the temperature and molar mass of the gas. """ @@ -51,7 +51,7 @@ def avg_speed_of_molecule(temperature: float, molar_mass: float) -> float: """ - Takes the temperature (in K) and molar mass (in kg/mol) of a gas + Takes the temperature (in K) and molar mass (in kg/mol) of a gas and returns the average speed of a molecule in the gas (in m/s). Examples: @@ -67,9 +67,10 @@ def avg_speed_of_molecule(temperature: float, molar_mass: float) -> float: else: return (8 * R * temperature / (PI * molar_mass)) ** 0.5 + def mps_speed_of_molecule(temperature: float, molar_mass: float) -> float: """ - Takes the temperature (in K) and molar mass (in kg/mol) of a gas + Takes the temperature (in K) and molar mass (in kg/mol) of a gas and returns the most probable speed of a molecule in the gas (in m/s). Examples: @@ -85,8 +86,8 @@ def mps_speed_of_molecule(temperature: float, molar_mass: float) -> float: else: return (2 * R * temperature / molar_mass) ** 0.5 + if __name__ == "__main__": import doctest doctest.testmod() - From ead4f47af540c24a9575fa1eda95897bec18a76b Mon Sep 17 00:00:00 2001 From: Baron105 Date: Tue, 10 Oct 2023 19:44:07 +0530 Subject: [PATCH 06/33] spacing --- .../avg_and_mps_speeds_of_gaseous_molecules.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/physics/avg_and_mps_speeds_of_gaseous_molecules.py b/physics/avg_and_mps_speeds_of_gaseous_molecules.py index f861520d334b..3b7e622184e7 100644 --- a/physics/avg_and_mps_speeds_of_gaseous_molecules.py +++ b/physics/avg_and_mps_speeds_of_gaseous_molecules.py @@ -1,7 +1,8 @@ """ The root-mean-square, average and most probable speeds are derived from the Maxwell-Boltzmann distribution. The Maxwell-Boltzmann distribution is a -probability distribution that describes the distribution of speeds for particles in a gas. +probability distribution that describes the distribution of speeds for particles +in a gas. The distribution is given by the following equation: ------------------------------------------------- @@ -24,24 +25,25 @@ | vavg = √8RT/πM | --------------------- -The most probable speed is the speed at which the Maxwell-Boltzmann distribution is at its maximum. -This can be found by differentiating the Maxwell-Boltzmann distribution with respect to v -and setting the result equal to zero. The result is: +The most probable speed is the speed at which the Maxwell-Boltzmann distribution +is at its maximum. This can be found by differentiating the Maxwell-Boltzmann +distribution with respect to v and setting the result equal to zero. The result is: --------------------- | vmp = √2RT/M | --------------------- -The root-mean-square speed is another measure of the average speed of the molecules in a gas. -It is calculated by taking the square root of the average of the -squares of the speeds of the molecules. The result is: +The root-mean-square speed is another measure of the average speed +of the molecules in a gas. It is calculated by taking the square root +of the average of the squares of the speeds of the molecules. The result is: --------------------- | vrms = √3RT/m | --------------------- Here we have defined functions to calculate the average and -most probable speeds of molecules in a gas given the temperature and molar mass of the gas. +most probable speeds of molecules in a gas given the +temperature and molar mass of the gas. """ # necessary constants From cb006c383bbba43063a51f079f47a528900230a8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 10 Oct 2023 14:19:02 +0000 Subject: [PATCH 07/33] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../avg_and_mps_speeds_of_gaseous_molecules.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/physics/avg_and_mps_speeds_of_gaseous_molecules.py b/physics/avg_and_mps_speeds_of_gaseous_molecules.py index 2a7fd542289d..3157741208e9 100644 --- a/physics/avg_and_mps_speeds_of_gaseous_molecules.py +++ b/physics/avg_and_mps_speeds_of_gaseous_molecules.py @@ -1,7 +1,7 @@ """ -The root-mean-square, average and most probable speeds are derived from -the Maxwell-Boltzmann distribution. The Maxwell-Boltzmann distribution is a -probability distribution that describes the distribution of speeds for particles +The root-mean-square, average and most probable speeds are derived from +the Maxwell-Boltzmann distribution. The Maxwell-Boltzmann distribution is a +probability distribution that describes the distribution of speeds for particles in a gas. The distribution is given by the following equation: @@ -25,24 +25,24 @@ | vavg = √8RT/πM | --------------------- -The most probable speed is the speed at which the Maxwell-Boltzmann distribution -is at its maximum. This can be found by differentiating the Maxwell-Boltzmann +The most probable speed is the speed at which the Maxwell-Boltzmann distribution +is at its maximum. This can be found by differentiating the Maxwell-Boltzmann distribution with respect to v and setting the result equal to zero. The result is: --------------------- | vmp = √2RT/M | --------------------- -The root-mean-square speed is another measure of the average speed -of the molecules in a gas. It is calculated by taking the square root +The root-mean-square speed is another measure of the average speed +of the molecules in a gas. It is calculated by taking the square root of the average of the squares of the speeds of the molecules. The result is: --------------------- | vrms = √3RT/m | --------------------- -Here we have defined functions to calculate the average and -most probable speeds of molecules in a gas given the +Here we have defined functions to calculate the average and +most probable speeds of molecules in a gas given the temperature and molar mass of the gas. """ From ab04734aa2ec0ef0af14fc5e233d3067da27eb3c Mon Sep 17 00:00:00 2001 From: Baron105 Date: Tue, 10 Oct 2023 19:50:16 +0530 Subject: [PATCH 08/33] ws --- .../avg_and_mps_speeds_of_gaseous_molecules.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/physics/avg_and_mps_speeds_of_gaseous_molecules.py b/physics/avg_and_mps_speeds_of_gaseous_molecules.py index 2a7fd542289d..3157741208e9 100644 --- a/physics/avg_and_mps_speeds_of_gaseous_molecules.py +++ b/physics/avg_and_mps_speeds_of_gaseous_molecules.py @@ -1,7 +1,7 @@ """ -The root-mean-square, average and most probable speeds are derived from -the Maxwell-Boltzmann distribution. The Maxwell-Boltzmann distribution is a -probability distribution that describes the distribution of speeds for particles +The root-mean-square, average and most probable speeds are derived from +the Maxwell-Boltzmann distribution. The Maxwell-Boltzmann distribution is a +probability distribution that describes the distribution of speeds for particles in a gas. The distribution is given by the following equation: @@ -25,24 +25,24 @@ | vavg = √8RT/πM | --------------------- -The most probable speed is the speed at which the Maxwell-Boltzmann distribution -is at its maximum. This can be found by differentiating the Maxwell-Boltzmann +The most probable speed is the speed at which the Maxwell-Boltzmann distribution +is at its maximum. This can be found by differentiating the Maxwell-Boltzmann distribution with respect to v and setting the result equal to zero. The result is: --------------------- | vmp = √2RT/M | --------------------- -The root-mean-square speed is another measure of the average speed -of the molecules in a gas. It is calculated by taking the square root +The root-mean-square speed is another measure of the average speed +of the molecules in a gas. It is calculated by taking the square root of the average of the squares of the speeds of the molecules. The result is: --------------------- | vrms = √3RT/m | --------------------- -Here we have defined functions to calculate the average and -most probable speeds of molecules in a gas given the +Here we have defined functions to calculate the average and +most probable speeds of molecules in a gas given the temperature and molar mass of the gas. """ From 6a741dc63f7ba3140c24e2cf7bde364b9ae4afa2 Mon Sep 17 00:00:00 2001 From: Baron105 Date: Tue, 10 Oct 2023 20:37:45 +0530 Subject: [PATCH 09/33] added amicable numbers --- maths/amicable_pair.py | 65 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 maths/amicable_pair.py diff --git a/maths/amicable_pair.py b/maths/amicable_pair.py new file mode 100644 index 000000000000..d596cfc9884b --- /dev/null +++ b/maths/amicable_pair.py @@ -0,0 +1,65 @@ +""" +Amicable numbers are two different natural numbers such that the sum +of the proper divisors of each is equal to the other number. +That is, s(a)=b and s(b)=a, where s(n) is equal to +the sum of positive divisors of n except n itself. +Here, a and b form a pair of amicable numbers. + +More information about amicable numbers can be found here: +https://en.wikipedia.org/wiki/Amicable_numbers + +Here, we have defined a function to check if two numbers are amicable. +We have also defined an auxiliary function, +to find the sum of the proper divisors of a number. +""" + +def sum_of_divisors(n: int) -> int: + """ + Find the sum of the proper divisors of a number. + + Examples: + >>> sum_of_divisors(220) + 284 + >>> sum_of_divisors(284) + 220 + """ + sum = 0 + for i in range(1, n): + if n % i == 0: + sum += i + + return sum + +def is_amicable_pair(a: int, b: int) -> bool: + """ + Check if two numbers (a and b) are amicable numbers. + Arguments must be positive integers. + + Examples: + >>> is_amicable_pair(220, 284) + True + >>> is_amicable_pair(1184, 1210) + True + >>> is_amicable_pair(127, 729) + False + >>> is_amicable_pair(7, 13) + False + >>> is_amicable_pair(0, 12) + Traceback (most recent call last): + ... + ValueError: Numbers must be positive integers. + >>> is_amicable_pair(12, -1) + Traceback (most recent call last): + ... + ValueError: Numbers must be positive integers. + >>> is_amicable_pair(42, 42) + False + """ + if a <= 0 or b <= 0: + raise ValueError("Numbers must be positive integers.") + + return sum_of_divisors(a) == b and sum_of_divisors(b) == a + +if __name__ == "__main__": + import doctest + doctest.testmod() \ No newline at end of file From d13c09d2ca2399abd3126a3a63584dc5f308d582 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 10 Oct 2023 15:08:24 +0000 Subject: [PATCH 10/33] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/amicable_pair.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/maths/amicable_pair.py b/maths/amicable_pair.py index d596cfc9884b..a94dcab32fee 100644 --- a/maths/amicable_pair.py +++ b/maths/amicable_pair.py @@ -13,10 +13,11 @@ to find the sum of the proper divisors of a number. """ + def sum_of_divisors(n: int) -> int: """ Find the sum of the proper divisors of a number. - + Examples: >>> sum_of_divisors(220) 284 @@ -27,14 +28,15 @@ def sum_of_divisors(n: int) -> int: for i in range(1, n): if n % i == 0: sum += i - + return sum + def is_amicable_pair(a: int, b: int) -> bool: """ Check if two numbers (a and b) are amicable numbers. Arguments must be positive integers. - + Examples: >>> is_amicable_pair(220, 284) True @@ -60,6 +62,8 @@ def is_amicable_pair(a: int, b: int) -> bool: return sum_of_divisors(a) == b and sum_of_divisors(b) == a + if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + + doctest.testmod() From 63734b9d97e34ec9d31f34d196f4d24099911ad4 Mon Sep 17 00:00:00 2001 From: Baron105 Date: Tue, 10 Oct 2023 20:41:56 +0530 Subject: [PATCH 11/33] added amicable numbers --- maths/amicable_pair.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/maths/amicable_pair.py b/maths/amicable_pair.py index d596cfc9884b..144d7e2a42ae 100644 --- a/maths/amicable_pair.py +++ b/maths/amicable_pair.py @@ -13,7 +13,7 @@ to find the sum of the proper divisors of a number. """ -def sum_of_divisors(n: int) -> int: +def sum_of_divisors(number: int) -> int: """ Find the sum of the proper divisors of a number. @@ -24,15 +24,15 @@ def sum_of_divisors(n: int) -> int: 220 """ sum = 0 - for i in range(1, n): - if n % i == 0: + for i in range(1, number): + if number % i == 0: sum += i return sum -def is_amicable_pair(a: int, b: int) -> bool: +def is_amicable_pair(number_1: int, number_2: int) -> bool: """ - Check if two numbers (a and b) are amicable numbers. + Check if two numbers (number_1 and number_2) are amicable. Arguments must be positive integers. Examples: @@ -55,10 +55,10 @@ def is_amicable_pair(a: int, b: int) -> bool: >>> is_amicable_pair(42, 42) False """ - if a <= 0 or b <= 0: + if number_1 <= 0 or number_2 <= 0: raise ValueError("Numbers must be positive integers.") - - return sum_of_divisors(a) == b and sum_of_divisors(b) == a + + return sum_of_divisors(number_1) == number_2 and sum_of_divisors(number_2) == number_1 if __name__ == "__main__": import doctest From 9779c1a623a84c5729bc568302c330047ad1d42c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 10 Oct 2023 15:13:42 +0000 Subject: [PATCH 12/33] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/amicable_pair.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/maths/amicable_pair.py b/maths/amicable_pair.py index ac50168f7deb..a15a0a87dcf5 100644 --- a/maths/amicable_pair.py +++ b/maths/amicable_pair.py @@ -13,6 +13,7 @@ to find the sum of the proper divisors of a number. """ + def sum_of_divisors(number: int) -> int: """ Find the sum of the proper divisors of a number. @@ -30,6 +31,7 @@ def sum_of_divisors(number: int) -> int: return sum + def is_amicable_pair(number_1: int, number_2: int) -> bool: """ Check if two numbers (number_1 and number_2) are amicable. @@ -57,8 +59,11 @@ def is_amicable_pair(number_1: int, number_2: int) -> bool: """ if number_1 <= 0 or number_2 <= 0: raise ValueError("Numbers must be positive integers.") - - return sum_of_divisors(number_1) == number_2 and sum_of_divisors(number_2) == number_1 + + return ( + sum_of_divisors(number_1) == number_2 and sum_of_divisors(number_2) == number_1 + ) + if __name__ == "__main__": import doctest From aa4d4e8b89cecd6252dffe72b0b6d5850bea9d96 Mon Sep 17 00:00:00 2001 From: Baron105 Date: Tue, 10 Oct 2023 20:45:55 +0530 Subject: [PATCH 13/33] spacing --- maths/amicable_pair.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/maths/amicable_pair.py b/maths/amicable_pair.py index ac50168f7deb..c392671db352 100644 --- a/maths/amicable_pair.py +++ b/maths/amicable_pair.py @@ -2,7 +2,7 @@ Amicable numbers are two different natural numbers such that the sum of the proper divisors of each is equal to the other number. That is, s(a)=b and s(b)=a, where s(n) is equal to -the sum of positive divisors of n except n itself. +the 'sum' of positive divisors of n except n itself. Here, a and b form a pair of amicable numbers. More information about amicable numbers can be found here: @@ -10,7 +10,7 @@ Here, we have defined a function to check if two numbers are amicable. We have also defined an auxiliary function, -to find the sum of the proper divisors of a number. +to find the 'sum' of the proper divisors of a number. """ def sum_of_divisors(number: int) -> int: @@ -57,8 +57,11 @@ def is_amicable_pair(number_1: int, number_2: int) -> bool: """ if number_1 <= 0 or number_2 <= 0: raise ValueError("Numbers must be positive integers.") - - return sum_of_divisors(number_1) == number_2 and sum_of_divisors(number_2) == number_1 + + if sum_of_divisors(number_1) == number_2 and sum_of_divisors(number_2) == number_1: + return True + else: + return False if __name__ == "__main__": import doctest From 52c2cb82d7210d704229ea9ef2deeb45a397e267 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 10 Oct 2023 15:17:23 +0000 Subject: [PATCH 14/33] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/amicable_pair.py | 1 + 1 file changed, 1 insertion(+) diff --git a/maths/amicable_pair.py b/maths/amicable_pair.py index 0bc287d744da..ed8f9184353d 100644 --- a/maths/amicable_pair.py +++ b/maths/amicable_pair.py @@ -65,6 +65,7 @@ def is_amicable_pair(number_1: int, number_2: int) -> bool: else: return False + if __name__ == "__main__": import doctest From 643958b1f429bf9afdb7ebdab235a1fad183bf1f Mon Sep 17 00:00:00 2001 From: Baron105 Date: Tue, 10 Oct 2023 20:48:27 +0530 Subject: [PATCH 15/33] removed --- maths/amicable_pair.py | 71 ------------------------------------------ 1 file changed, 71 deletions(-) delete mode 100644 maths/amicable_pair.py diff --git a/maths/amicable_pair.py b/maths/amicable_pair.py deleted file mode 100644 index 0bc287d744da..000000000000 --- a/maths/amicable_pair.py +++ /dev/null @@ -1,71 +0,0 @@ -""" -Amicable numbers are two different natural numbers such that the sum -of the proper divisors of each is equal to the other number. -That is, s(a)=b and s(b)=a, where s(n) is equal to -the 'sum' of positive divisors of n except n itself. -Here, a and b form a pair of amicable numbers. - -More information about amicable numbers can be found here: -https://en.wikipedia.org/wiki/Amicable_numbers - -Here, we have defined a function to check if two numbers are amicable. -We have also defined an auxiliary function, -to find the 'sum' of the proper divisors of a number. -""" - - -def sum_of_divisors(number: int) -> int: - """ - Find the sum of the proper divisors of a number. - - Examples: - >>> sum_of_divisors(220) - 284 - >>> sum_of_divisors(284) - 220 - """ - sum = 0 - for i in range(1, number): - if number % i == 0: - sum += i - - return sum - - -def is_amicable_pair(number_1: int, number_2: int) -> bool: - """ - Check if two numbers (number_1 and number_2) are amicable. - Arguments must be positive integers. - - Examples: - >>> is_amicable_pair(220, 284) - True - >>> is_amicable_pair(1184, 1210) - True - >>> is_amicable_pair(127, 729) - False - >>> is_amicable_pair(7, 13) - False - >>> is_amicable_pair(0, 12) - Traceback (most recent call last): - ... - ValueError: Numbers must be positive integers. - >>> is_amicable_pair(12, -1) - Traceback (most recent call last): - ... - ValueError: Numbers must be positive integers. - >>> is_amicable_pair(42, 42) - False - """ - if number_1 <= 0 or number_2 <= 0: - raise ValueError("Numbers must be positive integers.") - - if sum_of_divisors(number_1) == number_2 and sum_of_divisors(number_2) == number_1: - return True - else: - return False - -if __name__ == "__main__": - import doctest - - doctest.testmod() From c05ed7cc8a6821315b477abb6161dd8e993d8a29 Mon Sep 17 00:00:00 2001 From: Baron105 Date: Thu, 12 Oct 2023 21:54:51 +0530 Subject: [PATCH 16/33] changed name of file and added code improvements --- ...olecules.py => speeds_of_gas_molecules.py} | 38 +++++++++++++------ 1 file changed, 26 insertions(+), 12 deletions(-) rename physics/{avg_and_mps_speeds_of_gaseous_molecules.py => speeds_of_gas_molecules.py} (75%) diff --git a/physics/avg_and_mps_speeds_of_gaseous_molecules.py b/physics/speeds_of_gas_molecules.py similarity index 75% rename from physics/avg_and_mps_speeds_of_gaseous_molecules.py rename to physics/speeds_of_gas_molecules.py index 3157741208e9..004670148530 100644 --- a/physics/avg_and_mps_speeds_of_gaseous_molecules.py +++ b/physics/speeds_of_gas_molecules.py @@ -46,10 +46,8 @@ temperature and molar mass of the gas. """ -# necessary constants -PI = 3.1415926535 # pi -R = 8.3144626181 # gas constant - +# import the constants R and PI from the scipy.constants library +from scipy.constants import R, pi as PI def avg_speed_of_molecule(temperature: float, molar_mass: float) -> float: """ @@ -58,16 +56,24 @@ def avg_speed_of_molecule(temperature: float, molar_mass: float) -> float: Examples: >>> avg_speed_of_molecule(273, 0.028) # nitrogen at 273 K - 454.34887551126405 + 454.3488755020387 >>> avg_speed_of_molecule(300, 0.032) # oxygen at 300 K - 445.5257273482451 + 445.52572733919885 + >>> avg_speed_of_molecule(-273, 0.028) # invalid temperature + Traceback (most recent call last): + ... + Exception: Absolute temperature cannot be less than 0 K + >>> avg_speed_of_molecule(273, 0) # invalid molar mass + Traceback (most recent call last): + ... + Exception: Molar mass should be greater than 0 kg/mol """ + if temperature < 0: raise Exception("Absolute temperature cannot be less than 0 K") if molar_mass <= 0: raise Exception("Molar mass should be greater than 0 kg/mol") - else: - return (8 * R * temperature / (PI * molar_mass)) ** 0.5 + return (8 * R * temperature / (PI * molar_mass)) ** 0.5 def mps_speed_of_molecule(temperature: float, molar_mass: float) -> float: @@ -77,16 +83,24 @@ def mps_speed_of_molecule(temperature: float, molar_mass: float) -> float: Examples: >>> mps_speed_of_molecule(273, 0.028) # nitrogen at 273 K - 402.6562070215111 + 402.65620701908966 >>> mps_speed_of_molecule(300, 0.032) # oxygen at 300 K - 394.83689555229637 + 394.836895549922 + >>> mps_speed_of_molecule(-273, 0.028) # invalid temperature + Traceback (most recent call last): + ... + Exception: Absolute temperature cannot be less than 0 K + >>> mps_speed_of_molecule(273, 0) # invalid molar mass + Traceback (most recent call last): + ... + Exception: Molar mass should be greater than 0 kg/mol """ + if temperature < 0: raise Exception("Absolute temperature cannot be less than 0 K") if molar_mass <= 0: raise Exception("Molar mass should be greater than 0 kg/mol") - else: - return (2 * R * temperature / molar_mass) ** 0.5 + return (2 * R * temperature / molar_mass) ** 0.5 if __name__ == "__main__": From 886fb5d37f6290c2c51d0db8846d38e4c2befe53 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 12 Oct 2023 16:25:28 +0000 Subject: [PATCH 17/33] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- physics/speeds_of_gas_molecules.py | 1 + 1 file changed, 1 insertion(+) diff --git a/physics/speeds_of_gas_molecules.py b/physics/speeds_of_gas_molecules.py index 004670148530..18de1f466cc1 100644 --- a/physics/speeds_of_gas_molecules.py +++ b/physics/speeds_of_gas_molecules.py @@ -49,6 +49,7 @@ # import the constants R and PI from the scipy.constants library from scipy.constants import R, pi as PI + def avg_speed_of_molecule(temperature: float, molar_mass: float) -> float: """ Takes the temperature (in K) and molar mass (in kg/mol) of a gas From 97b89cdfdfe4024afcc29eeda08bdce268fffe28 Mon Sep 17 00:00:00 2001 From: Baron105 Date: Thu, 12 Oct 2023 21:57:34 +0530 Subject: [PATCH 18/33] issues fixed due to pi --- physics/speeds_of_gas_molecules.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/physics/speeds_of_gas_molecules.py b/physics/speeds_of_gas_molecules.py index 004670148530..c73201767001 100644 --- a/physics/speeds_of_gas_molecules.py +++ b/physics/speeds_of_gas_molecules.py @@ -46,8 +46,8 @@ temperature and molar mass of the gas. """ -# import the constants R and PI from the scipy.constants library -from scipy.constants import R, pi as PI +# import the constants R and pi from the scipy.constants library +from scipy.constants import R, pi def avg_speed_of_molecule(temperature: float, molar_mass: float) -> float: """ @@ -73,7 +73,7 @@ def avg_speed_of_molecule(temperature: float, molar_mass: float) -> float: raise Exception("Absolute temperature cannot be less than 0 K") if molar_mass <= 0: raise Exception("Molar mass should be greater than 0 kg/mol") - return (8 * R * temperature / (PI * molar_mass)) ** 0.5 + return (8 * R * temperature / (pi * molar_mass)) ** 0.5 def mps_speed_of_molecule(temperature: float, molar_mass: float) -> float: From 1c2b7b2e5f905a8257725cfe39806f87936e2594 Mon Sep 17 00:00:00 2001 From: Baron105 Date: Thu, 12 Oct 2023 22:24:17 +0530 Subject: [PATCH 19/33] requested changes added --- physics/speeds_of_gas_molecules.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/physics/speeds_of_gas_molecules.py b/physics/speeds_of_gas_molecules.py index 92cc607da9cd..a50d1c0f6d76 100644 --- a/physics/speeds_of_gas_molecules.py +++ b/physics/speeds_of_gas_molecules.py @@ -1,8 +1,9 @@ """ -The root-mean-square, average and most probable speeds are derived from -the Maxwell-Boltzmann distribution. The Maxwell-Boltzmann distribution is a -probability distribution that describes the distribution of speeds for particles -in a gas. +The root-mean-square, average and most probable speeds of gas molecules are +derived from the Maxwell-Boltzmann distribution. The Maxwell-Boltzmann +distribution is a probability distribution that describes the distribution of +speeds of particles in an ideal gas. + The distribution is given by the following equation: ------------------------------------------------- @@ -22,7 +23,7 @@ from 0 to infinity and dividing by the total number of molecules. The result is: --------------------- - | vavg = √8RT/πM | + | vavg = √(8RT/πM) | --------------------- The most probable speed is the speed at which the Maxwell-Boltzmann distribution @@ -30,7 +31,7 @@ distribution with respect to v and setting the result equal to zero. The result is: --------------------- - | vmp = √2RT/M | + | vmp = √(2RT/M) | --------------------- The root-mean-square speed is another measure of the average speed @@ -38,7 +39,7 @@ of the average of the squares of the speeds of the molecules. The result is: --------------------- - | vrms = √3RT/m | + | vrms = √(3RT/M) | --------------------- Here we have defined functions to calculate the average and From 2052af0b32f67561b8f414c3a6b7edb17dddcf30 Mon Sep 17 00:00:00 2001 From: Baron105 <76466796+Baron105@users.noreply.github.com> Date: Sat, 14 Oct 2023 10:52:38 +0530 Subject: [PATCH 20/33] Created doppler_effect_of_sound.py --- physics/doppler_effect_of_sound.py | 99 ++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 physics/doppler_effect_of_sound.py diff --git a/physics/doppler_effect_of_sound.py b/physics/doppler_effect_of_sound.py new file mode 100644 index 000000000000..bb8eb399f5e8 --- /dev/null +++ b/physics/doppler_effect_of_sound.py @@ -0,0 +1,99 @@ +""" +Doppler's effect + +The Doppler effect (also Doppler shift) is the change in the frequency of a wave +in relation to an observer who is moving relative to the source of the wave. +The Doppler effect is named after the physicist Christian Doppler. +A common example of Doppler shift is the change of pitch heard when +a vehicle sounding a horn approaches and recedes from an observer. + +The reason for the Doppler effect is that when the source of the waves +is moving towards the observer, each successive wave crest is emitted from a position +closer to the observer than the crest of the previous wave. +Therefore, each wave takes slightly less time to reach the observer +than the previous wave. Hence, the time between the arrivals of successive +wave crests at the observer is reduced, causing an increase in the frequency. +Similarly, if the source of waves is moving away from the observer, +each wave is emitted from a position farther from the observer than the previous wave, +so the arrival time between successive waves is increased, reducing the frequency. + +Now, if the source of waves is stationary but the observer is moving with respect +to the source, the transmission velocity of the waves changes +(ie the rate at which the observer receives waves) even if the wavelength +and frequency emitted from the source remain constant. + +All these results are summarized by the Doppler formula: + + f = (f0 * (v + v0)) / (v - vs) + +where: + f: frequency of the wave + f0: frequency of the wave when the source is stationary + v: velocity of the wave in the medium + v0: velocity of the observer, positive if the observer is moving towards the source + vs: velocity of the source, positive if the source is moving towards the observer + +Doppler's effect has many applications in physics and engineering, +such as radar, astronomy, medical imaging and seismology. + +References: +https://en.wikipedia.org/wiki/Doppler_effect + +Now, we will implement a function that calculates the frequency of a wave as a function +of the frequency of the wave when the source is stationary, the velocity of the wave +in the medium, the velocity of the observer and the velocity of the source. +""" + +def doppler_effect(f0: float, v: float, v0: float, vs: float) -> float: + """ + Input Parameters: + ----------------- + f0: frequency of the wave when the source is stationary + v: velocity of the wave in the medium + v0: velocity of the observer, positive if the observer is moving towards the source + vs: velocity of the source, positive if the source is moving towards the observer + + Returns: + -------- + f: frequency of the wave as perceived by the observer + + Docstring Tests: + >>> doppler_effect(100, 330, 10, 0) #observer moving towards the source + 103.03030303030303 + >>> doppler_effect(100, 330, -10, 0) #observer moving away from the source + 96.96969696969697 + >>> doppler_effect(100, 330, 0, 10) #source moving towards the observer + 103.125 + >>> doppler_effect(100, 330, 0, -10) #source moving away from the observer + 97.05882352941177 + >>> doppler_effect(100, 330, 10, 10) #observer and source moving towards each other + 106.25 + >>> doppler_effect(100, 330, -10, -10) #observer and source moving away + 94.11764705882354 + >>> doppler_effect(100, 330, 10, 330) #source moving at the same speed as the wave + Error: Division by zero + Infinite frequency implies vs = v and observer infront of the source + >>> doppler_effect(100, 330, 10, 340) #source moving faster than the wave + Error: Negative frequency + Negative frequency implies vs > v or v0 > v (in opposite direction) + >>> doppler_effect(100, 330, -340, 10) #observer moving faster than the wave + Error: Negative frequency + Negative frequency implies vs > v or v0 > v (in opposite direction) + """ + + try: + f = (f0 * (v + v0)) / (v - vs) + except ZeroDivisionError: + print("Error: Division by zero") + print("Infinite frequency implies vs = v and observer infront of the source") + return None + if f < 0: + print("Error: Negative frequency") + print("Negative frequency implies vs > v or v0 > v (in opposite direction)") + return None + return f + +if __name__ == "__main__": + import doctest + + doctest.testmod() From efe1055739c6f144e276aee23fa9d55c8e76189f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 14 Oct 2023 05:27:04 +0000 Subject: [PATCH 21/33] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- physics/doppler_effect_of_sound.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/physics/doppler_effect_of_sound.py b/physics/doppler_effect_of_sound.py index bb8eb399f5e8..3e148f519d2f 100644 --- a/physics/doppler_effect_of_sound.py +++ b/physics/doppler_effect_of_sound.py @@ -25,14 +25,14 @@ All these results are summarized by the Doppler formula: f = (f0 * (v + v0)) / (v - vs) - + where: f: frequency of the wave f0: frequency of the wave when the source is stationary v: velocity of the wave in the medium v0: velocity of the observer, positive if the observer is moving towards the source vs: velocity of the source, positive if the source is moving towards the observer - + Doppler's effect has many applications in physics and engineering, such as radar, astronomy, medical imaging and seismology. @@ -44,6 +44,7 @@ in the medium, the velocity of the observer and the velocity of the source. """ + def doppler_effect(f0: float, v: float, v0: float, vs: float) -> float: """ Input Parameters: @@ -93,6 +94,7 @@ def doppler_effect(f0: float, v: float, v0: float, vs: float) -> float: return None return f + if __name__ == "__main__": import doctest From 03e3c0c97e03ec1f2f55a9f0da0f028987a9964f Mon Sep 17 00:00:00 2001 From: Baron105 <76466796+Baron105@users.noreply.github.com> Date: Sat, 14 Oct 2023 10:59:10 +0530 Subject: [PATCH 22/33] Updated doppler_effect_of_sound.py --- physics/doppler_effect_of_sound.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/physics/doppler_effect_of_sound.py b/physics/doppler_effect_of_sound.py index 3e148f519d2f..a776b80dd5a2 100644 --- a/physics/doppler_effect_of_sound.py +++ b/physics/doppler_effect_of_sound.py @@ -44,7 +44,6 @@ in the medium, the velocity of the observer and the velocity of the source. """ - def doppler_effect(f0: float, v: float, v0: float, vs: float) -> float: """ Input Parameters: @@ -94,7 +93,6 @@ def doppler_effect(f0: float, v: float, v0: float, vs: float) -> float: return None return f - if __name__ == "__main__": import doctest From 664095f72148dfe7085896065a293c92de46ee84 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 14 Oct 2023 05:29:44 +0000 Subject: [PATCH 23/33] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- physics/doppler_effect_of_sound.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/physics/doppler_effect_of_sound.py b/physics/doppler_effect_of_sound.py index a776b80dd5a2..3e148f519d2f 100644 --- a/physics/doppler_effect_of_sound.py +++ b/physics/doppler_effect_of_sound.py @@ -44,6 +44,7 @@ in the medium, the velocity of the observer and the velocity of the source. """ + def doppler_effect(f0: float, v: float, v0: float, vs: float) -> float: """ Input Parameters: @@ -93,6 +94,7 @@ def doppler_effect(f0: float, v: float, v0: float, vs: float) -> float: return None return f + if __name__ == "__main__": import doctest From 728a37891a46c62599d0927313bbc716e3463e83 Mon Sep 17 00:00:00 2001 From: Baron105 <76466796+Baron105@users.noreply.github.com> Date: Sat, 14 Oct 2023 11:12:41 +0530 Subject: [PATCH 24/33] added desc names --- physics/doppler_effect_of_sound.py | 40 ++++++++++++++++-------------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/physics/doppler_effect_of_sound.py b/physics/doppler_effect_of_sound.py index 3e148f519d2f..550ca3122bde 100644 --- a/physics/doppler_effect_of_sound.py +++ b/physics/doppler_effect_of_sound.py @@ -44,15 +44,17 @@ in the medium, the velocity of the observer and the velocity of the source. """ - -def doppler_effect(f0: float, v: float, v0: float, vs: float) -> float: +def doppler_effect(org_freq: float, + wave_vel: float, + obs_vel: float, + src_vel: float) -> float: """ Input Parameters: ----------------- - f0: frequency of the wave when the source is stationary - v: velocity of the wave in the medium - v0: velocity of the observer, positive if the observer is moving towards the source - vs: velocity of the source, positive if the source is moving towards the observer + org_freq: frequency of the wave when the source is stationary + wave_vel: velocity of the wave in the medium + obs_vel: velocity of the observer, positive if the observer is moving towards the source + src_vel: velocity of the source, positive if the source is moving towards the observer Returns: -------- @@ -74,26 +76,28 @@ def doppler_effect(f0: float, v: float, v0: float, vs: float) -> float: >>> doppler_effect(100, 330, 10, 330) #source moving at the same speed as the wave Error: Division by zero Infinite frequency implies vs = v and observer infront of the source + 0.0 >>> doppler_effect(100, 330, 10, 340) #source moving faster than the wave - Error: Negative frequency - Negative frequency implies vs > v or v0 > v (in opposite direction) + Error: Non-positive frequency + Non-positive frequency implies vs > v or v0 > v(in opposite direction) + 0.0 >>> doppler_effect(100, 330, -340, 10) #observer moving faster than the wave - Error: Negative frequency - Negative frequency implies vs > v or v0 > v (in opposite direction) + Error: Non-positive frequency + Non-positive frequency implies vs > v or v0 > v(in opposite direction) + 0.0 """ try: - f = (f0 * (v + v0)) / (v - vs) + doppler_freq = (org_freq * (wave_vel + obs_vel)) / (wave_vel - src_vel) except ZeroDivisionError: print("Error: Division by zero") print("Infinite frequency implies vs = v and observer infront of the source") - return None - if f < 0: - print("Error: Negative frequency") - print("Negative frequency implies vs > v or v0 > v (in opposite direction)") - return None - return f - + return 0.0 + if doppler_freq <= 0: + print("Error: Non-positive frequency") + print("Non-positive frequency implies vs > v or v0 > v(in opposite direction)") + return 0.0 + return doppler_freq if __name__ == "__main__": import doctest From a56688b2ccfae647bb269686274af694cb44cda5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 14 Oct 2023 05:43:14 +0000 Subject: [PATCH 25/33] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- physics/doppler_effect_of_sound.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/physics/doppler_effect_of_sound.py b/physics/doppler_effect_of_sound.py index 550ca3122bde..e54bc7b7031a 100644 --- a/physics/doppler_effect_of_sound.py +++ b/physics/doppler_effect_of_sound.py @@ -44,10 +44,10 @@ in the medium, the velocity of the observer and the velocity of the source. """ -def doppler_effect(org_freq: float, - wave_vel: float, - obs_vel: float, - src_vel: float) -> float: + +def doppler_effect( + org_freq: float, wave_vel: float, obs_vel: float, src_vel: float +) -> float: """ Input Parameters: ----------------- @@ -99,6 +99,7 @@ def doppler_effect(org_freq: float, return 0.0 return doppler_freq + if __name__ == "__main__": import doctest From 006761f55ea5edb947803dafb2ecb0d125460b2e Mon Sep 17 00:00:00 2001 From: Baron105 <76466796+Baron105@users.noreply.github.com> Date: Sat, 14 Oct 2023 11:14:23 +0530 Subject: [PATCH 26/33] fixed spacing --- physics/doppler_effect_of_sound.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/physics/doppler_effect_of_sound.py b/physics/doppler_effect_of_sound.py index e54bc7b7031a..1f69d72cc017 100644 --- a/physics/doppler_effect_of_sound.py +++ b/physics/doppler_effect_of_sound.py @@ -44,17 +44,17 @@ in the medium, the velocity of the observer and the velocity of the source. """ - -def doppler_effect( - org_freq: float, wave_vel: float, obs_vel: float, src_vel: float -) -> float: +def doppler_effect(org_freq: float, + wave_vel: float, + obs_vel: float, + src_vel: float) -> float: """ Input Parameters: ----------------- org_freq: frequency of the wave when the source is stationary wave_vel: velocity of the wave in the medium - obs_vel: velocity of the observer, positive if the observer is moving towards the source - src_vel: velocity of the source, positive if the source is moving towards the observer + obs_vel: velocity of the observer, +ve if the observer is moving towards the source + src_vel: velocity of the source, +ve if the source is moving towards the observer Returns: -------- @@ -99,7 +99,6 @@ def doppler_effect( return 0.0 return doppler_freq - if __name__ == "__main__": import doctest From ba5a96bc6617ac4ec68f88636f19376da7bce7bd Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 14 Oct 2023 05:44:59 +0000 Subject: [PATCH 27/33] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- physics/doppler_effect_of_sound.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/physics/doppler_effect_of_sound.py b/physics/doppler_effect_of_sound.py index 1f69d72cc017..03c8db1804c4 100644 --- a/physics/doppler_effect_of_sound.py +++ b/physics/doppler_effect_of_sound.py @@ -44,10 +44,10 @@ in the medium, the velocity of the observer and the velocity of the source. """ -def doppler_effect(org_freq: float, - wave_vel: float, - obs_vel: float, - src_vel: float) -> float: + +def doppler_effect( + org_freq: float, wave_vel: float, obs_vel: float, src_vel: float +) -> float: """ Input Parameters: ----------------- @@ -99,6 +99,7 @@ def doppler_effect(org_freq: float, return 0.0 return doppler_freq + if __name__ == "__main__": import doctest From 620b03f1200c396317c6a4512d63d4414c430752 Mon Sep 17 00:00:00 2001 From: Barun Parua <76466796+Baron105@users.noreply.github.com> Date: Sat, 21 Oct 2023 20:56:29 +0530 Subject: [PATCH 28/33] renamed doppler_effect_of_sound.py to doppler_frequency.py --- physics/{doppler_effect_of_sound.py => doppler_frequency.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename physics/{doppler_effect_of_sound.py => doppler_frequency.py} (100%) diff --git a/physics/doppler_effect_of_sound.py b/physics/doppler_frequency.py similarity index 100% rename from physics/doppler_effect_of_sound.py rename to physics/doppler_frequency.py From 8947a2ab72acd53b5609108afa37b64c807cc3a5 Mon Sep 17 00:00:00 2001 From: Baron105 Date: Sat, 21 Oct 2023 21:37:05 +0530 Subject: [PATCH 29/33] used expection handling rather than print statements --- physics/doppler_frequency.py | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/physics/doppler_frequency.py b/physics/doppler_frequency.py index 03c8db1804c4..707ac74e0a44 100644 --- a/physics/doppler_frequency.py +++ b/physics/doppler_frequency.py @@ -74,29 +74,28 @@ def doppler_effect( >>> doppler_effect(100, 330, -10, -10) #observer and source moving away 94.11764705882354 >>> doppler_effect(100, 330, 10, 330) #source moving at the same speed as the wave - Error: Division by zero - Infinite frequency implies vs = v and observer infront of the source - 0.0 + Traceback (most recent call last): + ... + ZeroDivisionError: Division by zero implies vs = v and observer infront of the source >>> doppler_effect(100, 330, 10, 340) #source moving faster than the wave - Error: Non-positive frequency - Non-positive frequency implies vs > v or v0 > v(in opposite direction) - 0.0 + Traceback (most recent call last): + ... + ValueError: Non-positive frequency implies vs > v or v0 > v(in opposite direction) >>> doppler_effect(100, 330, -340, 10) #observer moving faster than the wave - Error: Non-positive frequency - Non-positive frequency implies vs > v or v0 > v(in opposite direction) - 0.0 + Traceback (most recent call last): + ... + ValueError: Non-positive frequency implies vs > v or v0 > v(in opposite direction) """ - try: - doppler_freq = (org_freq * (wave_vel + obs_vel)) / (wave_vel - src_vel) - except ZeroDivisionError: - print("Error: Division by zero") - print("Infinite frequency implies vs = v and observer infront of the source") - return 0.0 + if wave_vel == src_vel: + raise ZeroDivisionError( + "Division by zero implies vs = v and observer infront of the source" + ) + doppler_freq = (org_freq * (wave_vel + obs_vel)) / (wave_vel - src_vel) if doppler_freq <= 0: - print("Error: Non-positive frequency") - print("Non-positive frequency implies vs > v or v0 > v(in opposite direction)") - return 0.0 + raise ValueError( + "Non-positive frequency implies vs > v or v0 > v(in opposite direction)" + ) return doppler_freq From 72988ae6bc6a1928e197f431921692c0594f8c50 Mon Sep 17 00:00:00 2001 From: Baron105 Date: Sat, 21 Oct 2023 21:39:32 +0530 Subject: [PATCH 30/33] fixed spacing for ruff --- physics/doppler_frequency.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/physics/doppler_frequency.py b/physics/doppler_frequency.py index 707ac74e0a44..089d86060955 100644 --- a/physics/doppler_frequency.py +++ b/physics/doppler_frequency.py @@ -76,25 +76,25 @@ def doppler_effect( >>> doppler_effect(100, 330, 10, 330) #source moving at the same speed as the wave Traceback (most recent call last): ... - ZeroDivisionError: Division by zero implies vs = v and observer infront of the source + ZeroDivisionError: Division by zero implies vs=v and observer infront of the source >>> doppler_effect(100, 330, 10, 340) #source moving faster than the wave Traceback (most recent call last): ... - ValueError: Non-positive frequency implies vs > v or v0 > v(in opposite direction) + ValueError: Non-positive frequency implies vs>v or v0>v(in opposite direction) >>> doppler_effect(100, 330, -340, 10) #observer moving faster than the wave Traceback (most recent call last): ... - ValueError: Non-positive frequency implies vs > v or v0 > v(in opposite direction) + ValueError: Non-positive frequency implies vs>v or v0>v(in opposite direction) """ if wave_vel == src_vel: raise ZeroDivisionError( - "Division by zero implies vs = v and observer infront of the source" + "Division by zero implies vs=v and observer infront of the source" ) doppler_freq = (org_freq * (wave_vel + obs_vel)) / (wave_vel - src_vel) if doppler_freq <= 0: raise ValueError( - "Non-positive frequency implies vs > v or v0 > v(in opposite direction)" + "Non-positive frequency implies vs>v or v0>v(in opposite direction)" ) return doppler_freq From 36909a159d8c890f47c0138ea08863db1db1765e Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 22 Oct 2023 00:20:52 +0200 Subject: [PATCH 31/33] Update doppler_frequency.py This is super slick! Well done. --- physics/doppler_frequency.py | 67 ++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 34 deletions(-) diff --git a/physics/doppler_frequency.py b/physics/doppler_frequency.py index 089d86060955..2a761c72d9b8 100644 --- a/physics/doppler_frequency.py +++ b/physics/doppler_frequency.py @@ -1,28 +1,27 @@ """ Doppler's effect -The Doppler effect (also Doppler shift) is the change in the frequency of a wave -in relation to an observer who is moving relative to the source of the wave. -The Doppler effect is named after the physicist Christian Doppler. -A common example of Doppler shift is the change of pitch heard when -a vehicle sounding a horn approaches and recedes from an observer. - -The reason for the Doppler effect is that when the source of the waves -is moving towards the observer, each successive wave crest is emitted from a position -closer to the observer than the crest of the previous wave. -Therefore, each wave takes slightly less time to reach the observer -than the previous wave. Hence, the time between the arrivals of successive -wave crests at the observer is reduced, causing an increase in the frequency. -Similarly, if the source of waves is moving away from the observer, +The Doppler effect (also Doppler shift) is the change in the frequency of a wave in +relation to an observer who is moving relative to the source of the wave. The Doppler +effect is named after the physicist Christian Doppler. A common example of Doppler +shift is the change of pitch heard when a vehicle sounding a horn approaches and +recedes from an observer. + +The reason for the Doppler effect is that when the source of the waves is moving +towards the observer, each successive wave crest is emitted from a position closer to +the observer than the crest of the previous wave. Therefore, each wave takes slightly +less time to reach the observer than the previous wave. Hence, the time between the +arrivals of successive wave crests at the observer is reduced, causing an increase in +the frequency. Similarly, if the source of waves is moving away from the observer, each wave is emitted from a position farther from the observer than the previous wave, so the arrival time between successive waves is increased, reducing the frequency. -Now, if the source of waves is stationary but the observer is moving with respect -to the source, the transmission velocity of the waves changes -(ie the rate at which the observer receives waves) even if the wavelength -and frequency emitted from the source remain constant. +If the source of waves is stationary but the observer is moving with respect to the +source, the transmission velocity of the waves changes (ie the rate at which the +observer receives waves) even if the wavelength and frequency emitted from the source +remain constant. -All these results are summarized by the Doppler formula: +These results are all summarized by the Doppler formula: f = (f0 * (v + v0)) / (v - vs) @@ -33,8 +32,8 @@ v0: velocity of the observer, positive if the observer is moving towards the source vs: velocity of the source, positive if the source is moving towards the observer -Doppler's effect has many applications in physics and engineering, -such as radar, astronomy, medical imaging and seismology. +Doppler's effect has many applications in physics and engineering, such as radar, +astronomy, medical imaging, and seismology. References: https://en.wikipedia.org/wiki/Doppler_effect @@ -61,40 +60,40 @@ def doppler_effect( f: frequency of the wave as perceived by the observer Docstring Tests: - >>> doppler_effect(100, 330, 10, 0) #observer moving towards the source + >>> doppler_effect(100, 330, 10, 0) # observer moving towards the source 103.03030303030303 - >>> doppler_effect(100, 330, -10, 0) #observer moving away from the source + >>> doppler_effect(100, 330, -10, 0) # observer moving away from the source 96.96969696969697 - >>> doppler_effect(100, 330, 0, 10) #source moving towards the observer + >>> doppler_effect(100, 330, 0, 10) # source moving towards the observer 103.125 - >>> doppler_effect(100, 330, 0, -10) #source moving away from the observer + >>> doppler_effect(100, 330, 0, -10) # source moving away from the observer 97.05882352941177 - >>> doppler_effect(100, 330, 10, 10) #observer and source moving towards each other + >>> doppler_effect(100, 330, 10, 10) # source & observer moving towards each other 106.25 - >>> doppler_effect(100, 330, -10, -10) #observer and source moving away + >>> doppler_effect(100, 330, -10, -10) # source and observer moving away 94.11764705882354 - >>> doppler_effect(100, 330, 10, 330) #source moving at the same speed as the wave + >>> doppler_effect(100, 330, 10, 330) # source moving at same speed as the wave Traceback (most recent call last): ... - ZeroDivisionError: Division by zero implies vs=v and observer infront of the source - >>> doppler_effect(100, 330, 10, 340) #source moving faster than the wave + ZeroDivisionError: Division by zero implies vs=v and observer in front of the source + >>> doppler_effect(100, 330, 10, 340) # source moving faster than the wave Traceback (most recent call last): ... - ValueError: Non-positive frequency implies vs>v or v0>v(in opposite direction) - >>> doppler_effect(100, 330, -340, 10) #observer moving faster than the wave + ValueError: Non-positive frequency implies vs>v or v0>v (in the opposite direction) + >>> doppler_effect(100, 330, -340, 10) # observer moving faster than the wave Traceback (most recent call last): ... - ValueError: Non-positive frequency implies vs>v or v0>v(in opposite direction) + ValueError: Non-positive frequency implies vs>v or v0>v (in the opposite direction) """ if wave_vel == src_vel: raise ZeroDivisionError( - "Division by zero implies vs=v and observer infront of the source" + "Division by zero implies vs=v and observer in front of the source" ) doppler_freq = (org_freq * (wave_vel + obs_vel)) / (wave_vel - src_vel) if doppler_freq <= 0: raise ValueError( - "Non-positive frequency implies vs>v or v0>v(in opposite direction)" + "Non-positive frequency implies vs>v or v0>v (in the opposite direction)" ) return doppler_freq From 7a01a8a6dfe5060a6a284dffd0d02c2645b25068 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 22 Oct 2023 00:29:40 +0200 Subject: [PATCH 32/33] Update perfect_number.py --- maths/special_numbers/perfect_number.py | 53 ++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/maths/special_numbers/perfect_number.py b/maths/special_numbers/perfect_number.py index 148e988fb4c5..488d95d211d4 100644 --- a/maths/special_numbers/perfect_number.py +++ b/maths/special_numbers/perfect_number.py @@ -14,21 +14,64 @@ def perfect(number: int) -> bool: """ + Check if a number is a perfect number. + + A perfect number is a positive integer that is equal to the sum of its proper + divisors (excluding itself). + + Args: + number: The number to be checked. + + Returns: + True if the number is a perfect number otherwise, False. + Start from 1 because dividing by 0 will raise ZeroDivisionError. + A number at most can be divisible by the half of the number except the number + itself. For example, 6 is at most can be divisible by 3 except by 6 itself. + Examples: >>> perfect(27) False >>> perfect(28) True >>> perfect(29) False - - Start from 1 because dividing by 0 will raise ZeroDivisionError. - A number at most can be divisible by the half of the number except the number - itself. For example, 6 is at most can be divisible by 3 except by 6 itself. + >>> perfect(6) + True + >>> perfect(12) + False + >>> perfect(496) + True + >>> perfect(8128) + True + >>> perfect(0) + False + >>> perfect(-1) + False + >>> perfect(12.34) + Traceback (most recent call last): + ... + ValueError: number must be an integer + >>> perfect("Hello") + Traceback (most recent call last): + ... + ValueError: number must be an integer """ + if not isinstance(number, int): + raise ValueError("number must be an integer") + if number <= 0: + return False return sum(i for i in range(1, number // 2 + 1) if number % i == 0) == number if __name__ == "__main__": + from doctest import testmod + + testmod() print("Program to check whether a number is a Perfect number or not...") - number = int(input("Enter number: ").strip()) + try: + number = int(input("Enter a positive integer: ").strip()) + except ValueError: + msg = "number must be an integer" + print(msg) + raise ValueError(msg) + print(f"{number} is {'' if perfect(number) else 'not '}a Perfect Number.") From f29ded98fad9b3b7846fa2ec5834e9edd2fb217d Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 22 Oct 2023 00:30:34 +0200 Subject: [PATCH 33/33] Rename perceptron.py to perceptron.py.DISABLED --- neural_network/{perceptron.py => perceptron.py.DISABLED} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename neural_network/{perceptron.py => perceptron.py.DISABLED} (100%) diff --git a/neural_network/perceptron.py b/neural_network/perceptron.py.DISABLED similarity index 100% rename from neural_network/perceptron.py rename to neural_network/perceptron.py.DISABLED