| 
 | 1 | +"""  | 
 | 2 | +This module has functions which calculate focal length of lens, distance of  | 
 | 3 | +image from the lens and distance of object from the lens.  | 
 | 4 | +The above is calculated using the lens formula.  | 
 | 5 | +
  | 
 | 6 | +In optics, the relationship between the distance of the image (v),  | 
 | 7 | +the distance of the object (u), and  | 
 | 8 | +the focal length (f) of the lens is given by the formula known as the Lens formula.  | 
 | 9 | +The Lens formula is applicable for convex as well as concave lenses. The formula  | 
 | 10 | +is given as follows:  | 
 | 11 | +
  | 
 | 12 | +-------------------  | 
 | 13 | +| 1/f = 1/v + 1/u |  | 
 | 14 | +-------------------  | 
 | 15 | +
  | 
 | 16 | +Where  | 
 | 17 | +    f = focal length of the lens in meters.  | 
 | 18 | +    v = distance of the image from the lens in meters.  | 
 | 19 | +    u = distance of the object from the lens in meters.  | 
 | 20 | +
  | 
 | 21 | +To make our calculations easy few assumptions are made while deriving the formula  | 
 | 22 | +which are important to keep in mind before solving this equation.  | 
 | 23 | +The assumptions are as follows:  | 
 | 24 | +    1. The object O is a point object lying somewhere on the principle axis.  | 
 | 25 | +    2. The lens is thin.  | 
 | 26 | +    3. The aperture of the lens taken must be small.  | 
 | 27 | +    4. The angles of incidence and angle of refraction should be small.  | 
 | 28 | +
  | 
 | 29 | +Sign convention is a set of rules to set signs for image distance, object distance,  | 
 | 30 | +focal length, etc  | 
 | 31 | +for mathematical analysis of image formation. According to it:  | 
 | 32 | +    1. Object is always placed to the left of lens.  | 
 | 33 | +    2. All distances are measured from the optical centre of the mirror.  | 
 | 34 | +    3. Distances measured in the direction of the incident ray are positive and  | 
 | 35 | +    the distances measured in the direction opposite  | 
 | 36 | +    to that of the incident rays are negative.  | 
 | 37 | +    4. Distances measured along y-axis above the principal axis are positive and  | 
 | 38 | +    that measured along y-axis below the principal  | 
 | 39 | +    axis are negative.  | 
 | 40 | +
  | 
 | 41 | +Note: Sign convention can be reversed and will still give the correct results.  | 
 | 42 | +
  | 
 | 43 | +Reference for Sign convention:  | 
 | 44 | +https://www.toppr.com/ask/content/concept/sign-convention-for-lenses-210246/  | 
 | 45 | +
  | 
 | 46 | +Reference for assumptions:  | 
 | 47 | +https://testbook.com/physics/derivation-of-lens-maker-formula  | 
 | 48 | +"""  | 
 | 49 | + | 
 | 50 | + | 
 | 51 | +def focal_length_of_lens(  | 
 | 52 | +    object_distance_from_lens: float, image_distance_from_lens: float  | 
 | 53 | +) -> float:  | 
 | 54 | +    """  | 
 | 55 | +    Doctests:  | 
 | 56 | +    >>> from math import isclose  | 
 | 57 | +    >>> isclose(focal_length_of_lens(10,4), 6.666666666666667)  | 
 | 58 | +    True  | 
 | 59 | +    >>> from math import isclose  | 
 | 60 | +    >>> isclose(focal_length_of_lens(2.7,5.8), -5.0516129032258075)  | 
 | 61 | +    True  | 
 | 62 | +    >>> focal_length_of_lens(0, 20)  # doctest: +NORMALIZE_WHITESPACE  | 
 | 63 | +    Traceback (most recent call last):  | 
 | 64 | +        ...  | 
 | 65 | +    ValueError: Invalid inputs. Enter non zero values with respect  | 
 | 66 | +    to the sign convention.  | 
 | 67 | +    """  | 
 | 68 | + | 
 | 69 | +    if object_distance_from_lens == 0 or image_distance_from_lens == 0:  | 
 | 70 | +        raise ValueError(  | 
 | 71 | +            "Invalid inputs. Enter non zero values with respect to the sign convention."  | 
 | 72 | +        )  | 
 | 73 | +    focal_length = 1 / (  | 
 | 74 | +        (1 / image_distance_from_lens) - (1 / object_distance_from_lens)  | 
 | 75 | +    )  | 
 | 76 | +    return focal_length  | 
 | 77 | + | 
 | 78 | + | 
 | 79 | +def object_distance(  | 
 | 80 | +    focal_length_of_lens: float, image_distance_from_lens: float  | 
 | 81 | +) -> float:  | 
 | 82 | +    """  | 
 | 83 | +    Doctests:  | 
 | 84 | +    >>> from math import isclose  | 
 | 85 | +    >>> isclose(object_distance(10,40), -13.333333333333332)  | 
 | 86 | +    True  | 
 | 87 | +
  | 
 | 88 | +    >>> from math import isclose  | 
 | 89 | +    >>> isclose(object_distance(6.2,1.5), 1.9787234042553192)  | 
 | 90 | +    True  | 
 | 91 | +
  | 
 | 92 | +    >>> object_distance(0, 20)  # doctest: +NORMALIZE_WHITESPACE  | 
 | 93 | +    Traceback (most recent call last):  | 
 | 94 | +        ...  | 
 | 95 | +    ValueError: Invalid inputs. Enter non zero values with respect  | 
 | 96 | +    to the sign convention.  | 
 | 97 | +    """  | 
 | 98 | + | 
 | 99 | +    if image_distance_from_lens == 0 or focal_length_of_lens == 0:  | 
 | 100 | +        raise ValueError(  | 
 | 101 | +            "Invalid inputs. Enter non zero values with respect to the sign convention."  | 
 | 102 | +        )  | 
 | 103 | + | 
 | 104 | +    object_distance = 1 / ((1 / image_distance_from_lens) - (1 / focal_length_of_lens))  | 
 | 105 | +    return object_distance  | 
 | 106 | + | 
 | 107 | + | 
 | 108 | +def image_distance(  | 
 | 109 | +    focal_length_of_lens: float, object_distance_from_lens: float  | 
 | 110 | +) -> float:  | 
 | 111 | +    """  | 
 | 112 | +    Doctests:  | 
 | 113 | +    >>> from math import isclose  | 
 | 114 | +    >>> isclose(image_distance(50,40), 22.22222222222222)  | 
 | 115 | +    True  | 
 | 116 | +    >>> from math import isclose  | 
 | 117 | +    >>> isclose(image_distance(5.3,7.9), 3.1719696969696973)  | 
 | 118 | +    True  | 
 | 119 | +
  | 
 | 120 | +    >>> object_distance(0, 20)  # doctest: +NORMALIZE_WHITESPACE  | 
 | 121 | +    Traceback (most recent call last):  | 
 | 122 | +        ...  | 
 | 123 | +    ValueError: Invalid inputs. Enter non zero values with respect  | 
 | 124 | +    to the sign convention.  | 
 | 125 | +    """  | 
 | 126 | +    if object_distance_from_lens == 0 or focal_length_of_lens == 0:  | 
 | 127 | +        raise ValueError(  | 
 | 128 | +            "Invalid inputs. Enter non zero values with respect to the sign convention."  | 
 | 129 | +        )  | 
 | 130 | +    image_distance = 1 / ((1 / object_distance_from_lens) + (1 / focal_length_of_lens))  | 
 | 131 | +    return image_distance  | 
0 commit comments