From 6418a26cb9faa54eb99305acd97aa37eb795c881 Mon Sep 17 00:00:00 2001 From: SparshRastogi <75373475+SparshRastogi@users.noreply.github.com> Date: Thu, 27 Oct 2022 13:10:25 +0530 Subject: [PATCH 1/9] Create malus_law.py Finding the intensity of light transmitted through a polariser using Malus Law and by taking initial intensity and angle between polariser and axis as input --- physics/malus_law.py | 80 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 physics/malus_law.py diff --git a/physics/malus_law.py b/physics/malus_law.py new file mode 100644 index 000000000000..5102ffb2ab3f --- /dev/null +++ b/physics/malus_law.py @@ -0,0 +1,80 @@ +import doctest +import math + +""" +Finding the intensity of light transmitted through a polariser using Malus Law and by taking +initial intensity and angle between polariser and axis as input + +Description : Malus's law, which is named after Étienne-Louis Malus, +says that when a perfect polarizer is placed in a polarized +beam of light, the irradiance, I, of the light that passes +through is given by + I=I'cos²θ +where I' is the initial intensity and θ is the angle between the light's +initial polarization direction and the axis of the polarizer. +A beam of unpolarized light can be thought of as containing a +uniform mixture of linear polarizations at all possible angles. +Since the average value of cos²θ is 1/2, the transmission coefficient becomes +I/I' = 1/2 +In practice, some light is lost in the polarizer and the actual transmission +will be somewhat lower than this, around 38% for Polaroid-type polarizers but +considerably higher (>49.9%) for some birefringent prism types. +If two polarizers are placed one after another (the second polarizer is +generally called an analyzer), the mutual angle between their polarizing axes +gives the value of θ in Malus's law. If the two axes are orthogonal, the +polarizers are crossed and in theory no light is transmitted, though again +practically speaking no polarizer is perfect and the transmission is not exactly +zero (for example, crossed Polaroid sheets appear slightly blue in colour because +their extinction ratio is better in the red). If a transparent object is placed +between the crossed polarizers, any polarization effects present in the sample +(such as birefringence) will be shown as an increase in transmission. +This effect is used in polarimetry to measure the optical activity of a sample. +Real polarizers are also not perfect blockers of the polarization orthogonal to +their polarization axis; the ratio of the transmission of the unwanted component +to the wanted component is called the extinction ratio, and varies from around +1:500 for Polaroid to about 1:106 for Glan–Taylor prism polarizers. + +Reference : "https://en.wikipedia.org/wiki/Polarizer#Malus's_law_and_other_properties" +""" + + +def malus_law(initial_intensity: float, angle: float) -> float: + """ + >>> malus_law(10,45) + 5.0 + >>> malus_law(100,60) + 25.0 + >>> malus_law(50,150) + 37.5 + >>> malus_law(75,270) + 0.0 + >>> malus_law(10,-900) + Traceback (most recent call last): + ... + ValueError: In Malus Law,the angle between the polarizer and the axis always lies between 0 degree and 360 degrees + >>> malus_law(10,900) + Traceback (most recent call last): + ... + ValueError: In Malus Law,the angle between the polarizer and the axis always lies between 0 degree and 360 degrees + >>> malus_law(-100,900) + Traceback (most recent call last): + ... + ValueError: The value of intensity cannot be negative + >>> malus_law(100,180) + 100.0 + >>> malus_law(100,360) + 100.0 + """ + + if initial_intensity < 0: + raise ValueError("The value of intensity cannot be negative") + # handling of negative values of initial intensity + if angle < 0 or angle > 360: + raise ValueError("In Malus Law,the angle between the polarizer and the axis always lies between 0 degree and 360 degrees") + # handling of values out of allowed range + final_intensity = initial_intensity * (math.cos(math.radians(angle)) ** 2) + return round(final_intensity,2) + + +if __name__ == "__main__": + doctest.testmod(name="malus_law") From 769dc85cf571cac87721e5575218105dea27d9ac Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 27 Oct 2022 07:41:59 +0000 Subject: [PATCH 2/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- physics/malus_law.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/physics/malus_law.py b/physics/malus_law.py index 5102ffb2ab3f..4b0e18a5a07b 100644 --- a/physics/malus_law.py +++ b/physics/malus_law.py @@ -70,10 +70,12 @@ def malus_law(initial_intensity: float, angle: float) -> float: raise ValueError("The value of intensity cannot be negative") # handling of negative values of initial intensity if angle < 0 or angle > 360: - raise ValueError("In Malus Law,the angle between the polarizer and the axis always lies between 0 degree and 360 degrees") + raise ValueError( + "In Malus Law,the angle between the polarizer and the axis always lies between 0 degree and 360 degrees" + ) # handling of values out of allowed range final_intensity = initial_intensity * (math.cos(math.radians(angle)) ** 2) - return round(final_intensity,2) + return round(final_intensity, 2) if __name__ == "__main__": From f026fabff36306e5cd3ba077c3f2ad1afb15753a Mon Sep 17 00:00:00 2001 From: SparshRastogi <75373475+SparshRastogi@users.noreply.github.com> Date: Thu, 27 Oct 2022 15:14:25 +0530 Subject: [PATCH 3/9] Update physics/malus_law.py Co-authored-by: Caeden Perelli-Harris --- physics/malus_law.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/physics/malus_law.py b/physics/malus_law.py index 4b0e18a5a07b..10dea50886b0 100644 --- a/physics/malus_law.py +++ b/physics/malus_law.py @@ -71,7 +71,7 @@ def malus_law(initial_intensity: float, angle: float) -> float: # handling of negative values of initial intensity if angle < 0 or angle > 360: raise ValueError( - "In Malus Law,the angle between the polarizer and the axis always lies between 0 degree and 360 degrees" + "In Malus Law, the angle between the polarizer and the axis always lies between 0 degree and 360 degrees" ) # handling of values out of allowed range final_intensity = initial_intensity * (math.cos(math.radians(angle)) ** 2) From 51a0ff1212a877cc696397112319c2b8d7745ecc Mon Sep 17 00:00:00 2001 From: SparshRastogi <75373475+SparshRastogi@users.noreply.github.com> Date: Thu, 27 Oct 2022 15:17:04 +0530 Subject: [PATCH 4/9] Update physics/malus_law.py Co-authored-by: Caeden Perelli-Harris --- physics/malus_law.py | 1 - 1 file changed, 1 deletion(-) diff --git a/physics/malus_law.py b/physics/malus_law.py index 10dea50886b0..bd048923e46f 100644 --- a/physics/malus_law.py +++ b/physics/malus_law.py @@ -1,4 +1,3 @@ -import doctest import math """ From 02819cbd52a96191b08e82ce0ac34e69e269cf21 Mon Sep 17 00:00:00 2001 From: SparshRastogi <75373475+SparshRastogi@users.noreply.github.com> Date: Thu, 27 Oct 2022 15:17:30 +0530 Subject: [PATCH 5/9] Update physics/malus_law.py Co-authored-by: Caeden Perelli-Harris --- physics/malus_law.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/physics/malus_law.py b/physics/malus_law.py index bd048923e46f..4674f400b595 100644 --- a/physics/malus_law.py +++ b/physics/malus_law.py @@ -78,4 +78,6 @@ def malus_law(initial_intensity: float, angle: float) -> float: if __name__ == "__main__": + import doctest + doctest.testmod(name="malus_law") From 6e06127638598337caf818e753279f9324861202 Mon Sep 17 00:00:00 2001 From: SparshRastogi <75373475+SparshRastogi@users.noreply.github.com> Date: Thu, 27 Oct 2022 15:17:50 +0530 Subject: [PATCH 6/9] Update physics/malus_law.py Co-authored-by: Caeden Perelli-Harris --- physics/malus_law.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/physics/malus_law.py b/physics/malus_law.py index 4674f400b595..f73a007bd40b 100644 --- a/physics/malus_law.py +++ b/physics/malus_law.py @@ -73,8 +73,7 @@ def malus_law(initial_intensity: float, angle: float) -> float: "In Malus Law, the angle between the polarizer and the axis always lies between 0 degree and 360 degrees" ) # handling of values out of allowed range - final_intensity = initial_intensity * (math.cos(math.radians(angle)) ** 2) - return round(final_intensity, 2) + return initial_intensity * (math.cos(math.radians(angle)) ** 2) if __name__ == "__main__": From b2d208f607a45d75211a610e5d72c20b5f35ec21 Mon Sep 17 00:00:00 2001 From: SparshRastogi <75373475+SparshRastogi@users.noreply.github.com> Date: Thu, 27 Oct 2022 15:23:20 +0530 Subject: [PATCH 7/9] Update malus_law.py Made some changes in the error messages and the docstring testcases --- physics/malus_law.py | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/physics/malus_law.py b/physics/malus_law.py index f73a007bd40b..47023a80626a 100644 --- a/physics/malus_law.py +++ b/physics/malus_law.py @@ -39,29 +39,29 @@ def malus_law(initial_intensity: float, angle: float) -> float: """ - >>> malus_law(10,45) + >>> round(malus_law(10,45),2) 5.0 - >>> malus_law(100,60) + >>> round(malus_law(100,60),2) 25.0 - >>> malus_law(50,150) + >>> round(malus_law(50,150),2) 37.5 - >>> malus_law(75,270) + >>> round(malus_law(75,270),2) 0.0 - >>> malus_law(10,-900) + >>> round(malus_law(10,-900),2) Traceback (most recent call last): ... - ValueError: In Malus Law,the angle between the polarizer and the axis always lies between 0 degree and 360 degrees - >>> malus_law(10,900) + ValueError: In Malus Law, the angle is in the range 0-360 degrees + >>> round(malus_law(10,900),2) Traceback (most recent call last): ... - ValueError: In Malus Law,the angle between the polarizer and the axis always lies between 0 degree and 360 degrees - >>> malus_law(-100,900) + ValueError: In Malus Law, the angle is in the range 0-360 degrees + >>> round(malus_law(-100,900),2) Traceback (most recent call last): ... ValueError: The value of intensity cannot be negative - >>> malus_law(100,180) + >>> round(malus_law(100,180),2) 100.0 - >>> malus_law(100,360) + >>> round(malus_law(100,360),2) 100.0 """ @@ -69,9 +69,7 @@ def malus_law(initial_intensity: float, angle: float) -> float: raise ValueError("The value of intensity cannot be negative") # handling of negative values of initial intensity if angle < 0 or angle > 360: - raise ValueError( - "In Malus Law, the angle between the polarizer and the axis always lies between 0 degree and 360 degrees" - ) + raise ValueError("In Malus Law, the angle is in the range 0-360 degrees") # handling of values out of allowed range return initial_intensity * (math.cos(math.radians(angle)) ** 2) From 8f1368aeb86697f8be4e3828fea1e06d84b00d03 Mon Sep 17 00:00:00 2001 From: SparshRastogi <75373475+SparshRastogi@users.noreply.github.com> Date: Thu, 27 Oct 2022 15:27:07 +0530 Subject: [PATCH 8/9] Update malus_law.py Made changes for the passing the precommit --- physics/malus_law.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/physics/malus_law.py b/physics/malus_law.py index 47023a80626a..7073cf71fa6b 100644 --- a/physics/malus_law.py +++ b/physics/malus_law.py @@ -1,8 +1,8 @@ import math """ -Finding the intensity of light transmitted through a polariser using Malus Law and by taking -initial intensity and angle between polariser and axis as input +Finding the intensity of light transmitted through a polariser using Malus Law +and by taking initial intensity and angle between polariser and axis as input Description : Malus's law, which is named after Étienne-Louis Malus, says that when a perfect polarizer is placed in a polarized From 4871a2a75a23d87a202b61e4d3c0755ea42bf07f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 27 Oct 2022 09:58:21 +0000 Subject: [PATCH 9/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- physics/malus_law.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/physics/malus_law.py b/physics/malus_law.py index 7073cf71fa6b..ae77d45cf614 100644 --- a/physics/malus_law.py +++ b/physics/malus_law.py @@ -1,7 +1,7 @@ import math """ -Finding the intensity of light transmitted through a polariser using Malus Law +Finding the intensity of light transmitted through a polariser using Malus Law and by taking initial intensity and angle between polariser and axis as input Description : Malus's law, which is named after Étienne-Louis Malus,