Skip to content

Added Mirror Formulae Equation #9717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@

## Data Structures
* Arrays
* [Median Two Array](data_structures/arrays/median_two_array.py)
* [Permutations](data_structures/arrays/permutations.py)
* [Prefix Sum](data_structures/arrays/prefix_sum.py)
* [Product Sum](data_structures/arrays/product_sum.py)
Expand All @@ -185,6 +186,7 @@
* [Diff Views Of Binary Tree](data_structures/binary_tree/diff_views_of_binary_tree.py)
* [Distribute Coins](data_structures/binary_tree/distribute_coins.py)
* [Fenwick Tree](data_structures/binary_tree/fenwick_tree.py)
* [Flatten Binarytree To Linkedlist](data_structures/binary_tree/flatten_binarytree_to_linkedlist.py)
* [Inorder Tree Traversal 2022](data_structures/binary_tree/inorder_tree_traversal_2022.py)
* [Is Bst](data_structures/binary_tree/is_bst.py)
* [Lazy Segment Tree](data_structures/binary_tree/lazy_segment_tree.py)
Expand Down Expand Up @@ -324,6 +326,7 @@
* [Longest Common Substring](dynamic_programming/longest_common_substring.py)
* [Longest Increasing Subsequence](dynamic_programming/longest_increasing_subsequence.py)
* [Longest Increasing Subsequence O(Nlogn)](dynamic_programming/longest_increasing_subsequence_o(nlogn).py)
* [Longest Palindromic Subsequence](dynamic_programming/longest_palindromic_subsequence.py)
* [Longest Sub Array](dynamic_programming/longest_sub_array.py)
* [Matrix Chain Order](dynamic_programming/matrix_chain_order.py)
* [Max Non Adjacent Sum](dynamic_programming/max_non_adjacent_sum.py)
Expand Down Expand Up @@ -539,6 +542,7 @@
* [Average Mode](maths/average_mode.py)
* [Bailey Borwein Plouffe](maths/bailey_borwein_plouffe.py)
* [Basic Maths](maths/basic_maths.py)
* [Bell Numbers](maths/bell_numbers.py)
* [Binary Exp Mod](maths/binary_exp_mod.py)
* [Binary Exponentiation](maths/binary_exponentiation.py)
* [Binary Exponentiation 3](maths/binary_exponentiation_3.py)
Expand Down Expand Up @@ -690,6 +694,7 @@
* [Matrix Class](matrix/matrix_class.py)
* [Matrix Operation](matrix/matrix_operation.py)
* [Max Area Of Island](matrix/max_area_of_island.py)
* [Median Matrix](matrix/median_matrix.py)
* [Nth Fibonacci Using Matrix Exponentiation](matrix/nth_fibonacci_using_matrix_exponentiation.py)
* [Pascal Triangle](matrix/pascal_triangle.py)
* [Rotate Matrix](matrix/rotate_matrix.py)
Expand All @@ -708,8 +713,8 @@
* Activation Functions
* [Exponential Linear Unit](neural_network/activation_functions/exponential_linear_unit.py)
* [Leaky Rectified Linear Unit](neural_network/activation_functions/leaky_rectified_linear_unit.py)
* [Scaled Exponential Linear Unit](neural_network/activation_functions/scaled_exponential_linear_unit.py)
* [Rectified Linear Unit](neural_network/activation_functions/rectified_linear_unit.py)
* [Scaled Exponential Linear Unit](neural_network/activation_functions/scaled_exponential_linear_unit.py)
* [Back Propagation Neural Network](neural_network/back_propagation_neural_network.py)
* [Convolution Neural Network](neural_network/convolution_neural_network.py)
* [Perceptron](neural_network/perceptron.py)
Expand Down Expand Up @@ -756,9 +761,11 @@
* [Kinetic Energy](physics/kinetic_energy.py)
* [Lorentz Transformation Four Vector](physics/lorentz_transformation_four_vector.py)
* [Malus Law](physics/malus_law.py)
* [Mirror Formulae](physics/mirror_formulae.py)
* [N Body Simulation](physics/n_body_simulation.py)
* [Newtons Law Of Gravitation](physics/newtons_law_of_gravitation.py)
* [Newtons Second Law Of Motion](physics/newtons_second_law_of_motion.py)
* [Photoelectric Effect](physics/photoelectric_effect.py)
* [Potential Energy](physics/potential_energy.py)
* [Rms Speed Of Molecule](physics/rms_speed_of_molecule.py)
* [Shear Stress](physics/shear_stress.py)
Expand Down
127 changes: 127 additions & 0 deletions physics/mirror_formulae.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
"""
This module contains the functions to calculate the focal length, object distance
and image distance of a mirror.

The mirror formula is an equation that relates the object distance (u),
image distance (v), and focal length (f) of a spherical mirror.
It is commonly used in optics to determine the position and characteristics
of an image formed by a mirror. It is expressed using the formulae :

-------------------
| 1/f = 1/v + 1/u |
-------------------

Where,
f = Focal length of the spherical mirror (metre)
v = Image distance from the mirror (metre)
u = Object distance from the mirror (metre)


The signs of the distances are taken with respect to the sign convention.
The sign convention is as follows:
1) Object is always placed to the left of mirror
2) Distances measured in the direction of the incident ray are positive
and the distances measured in the direction opposite to that of the incident
rays are negative.
3) All distances are measured from the pole of the mirror.


There are a few assumptions that are made while using the mirror formulae.
They are as follows:
1) Thin Mirror: The mirror is assumed to be thin, meaning its thickness is
negligible compared to its radius of curvature. This assumption allows
us to treat the mirror as a two-dimensional surface.
2) Spherical Mirror: The mirror is assumed to have a spherical shape. While this
assumption may not hold exactly for all mirrors, it is a reasonable approximation
for most practical purposes.
3) Small Angles: The angles involved in the derivation are assumed to be small.
This assumption allows us to use the small-angle approximation, where the tangent
of a small angle is approximately equal to the angle itself. It simplifies the
calculations and makes the derivation more manageable.
4) Paraxial Rays: The mirror formula is derived using paraxial rays, which are
rays that are close to the principal axis and make small angles with it. This
assumption ensures that the rays are close enough to the principal axis, making the
calculations more accurate.
5) Reflection and Refraction Laws: The derivation assumes that the laws of
reflection and refraction hold.
These laws state that the angle of incidence is equal to the angle of reflection
for reflection, and the incident and refracted rays lie in the same plane and
obey Snell's law for refraction.

(Description and Assumptions adapted from
https://www.collegesearch.in/articles/mirror-formula-derivation)

(Sign Convention adapted from
https://www.toppr.com/ask/content/concept/sign-convention-for-mirrors-210189/)


"""


def focal_length(distance_of_object: float, distance_of_image: float) -> float:
"""
>>> from math import isclose
>>> isclose(focal_length(10, 20), 6.66666666666666)
True
>>> from math import isclose
>>> isclose(focal_length(9.5, 6.7), 3.929012346)
True
>>> focal_length(0, 20)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

>>> focal_length(0, 20)  # doctest: +NORMALIZE_WHITESPACE

Traceback (most recent call last):
...
ValueError: Invalid inputs. Enter non zero values with respect
to the sign convention.
"""

if distance_of_object == 0 or distance_of_image == 0:
raise ValueError(
"Invalid inputs. Enter non zero values with respect to the sign convention."
)
focal_length = 1 / ((1 / distance_of_object) + (1 / distance_of_image))
return focal_length


def object_distance(focal_length: float, distance_of_image: float) -> float:
"""
>>> from math import isclose
>>> isclose(object_distance(30, 20), -60.0)
True
>>> from math import isclose
>>> isclose(object_distance(10.5, 11.7), 102.375)
True
Copy link
Member

@cclauss cclauss Oct 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think we need is_close() here. Let’s only use it where we need to.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When running the doctests locally, there were issues when used without the isclose, should i still remove them or leave it as it is ?
image

image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. We need is_close()

Copy link
Contributor Author

@vipinkarthic vipinkarthic Oct 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there anything else I should add/change ?, or just have to wait till PR is merged ?

>>> object_distance(90, 0)
Copy link
Member

@cclauss cclauss Oct 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# doctest: +NORMALIZE_WHITESPACE

Traceback (most recent call last):
...
ValueError: Invalid inputs. Enter non zero values with respect
to the sign convention.
"""

if distance_of_image == 0 or focal_length == 0:
raise ValueError(
"Invalid inputs. Enter non zero values with respect to the sign convention."
)
object_distance = 1 / ((1 / focal_length) - (1 / distance_of_image))
return object_distance


def image_distance(focal_length: float, distance_of_object: float) -> float:
"""
>>> from math import isclose
>>> isclose(image_distance(10, 40), 13.33333333)
True
>>> from math import isclose
>>> isclose(image_distance(1.5, 6.7), 1.932692308)
True
>>> image_distance(0, 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# doctest: +NORMALIZE_WHITESPACE

Traceback (most recent call last):
...
ValueError: Invalid inputs. Enter non zero values with respect
to the sign convention.
"""

if distance_of_object == 0 or focal_length == 0:
raise ValueError(
"Invalid inputs. Enter non zero values with respect to the sign convention."
)
image_distance = 1 / ((1 / focal_length) - (1 / distance_of_object))
return image_distance