1313returns the integration of function in given limit.
1414"""
1515
16+ from collections .abc import Callable
17+
1618# constants
1719# the more the number of steps the more accurate
1820N_STEPS = 1000
@@ -34,7 +36,7 @@ def f(x: float) -> float:
3436"""
3537
3638
37- def simpson_integration (function , a : float , b : float , precision : int = 4 ) -> float :
39+ def simpson_integration (function : Callable , a : float , b : float , precision : int = 4 ) -> float :
3840 """
3941 Args:
4042 function : the function which's integration is desired
@@ -46,60 +48,65 @@ def simpson_integration(function, a: float, b: float, precision: int = 4) -> flo
4648 result : the value of the approximated integration of function in range a to b
4749
4850 Raises:
49- AssertionError : function is not callable
50- AssertionError : a is not float or integer
51- AssertionError : function should return float or integer
52- AssertionError : b is not float or integer
53- AssertionError : precision is not positive integer
51+ TypeError : function is not callable
52+ TypeError : a is not float or integer
53+ TypeError : function should return float or integer
54+ TypeError : b is not float or integer
55+ ValueError : precision is not positive integer
5456
5557 >>> simpson_integration(lambda x : x*x,1,2,3)
5658 2.333
5759
5860 >>> simpson_integration(lambda x : x*x,'wrong_input',2,3)
5961 Traceback (most recent call last):
6062 ...
61- AssertionError : a should be float or integer your input : wrong_input
63+ TypeError : a should be float or integer your input : wrong_input
6264
6365 >>> simpson_integration(lambda x : x*x,1,'wrong_input',3)
6466 Traceback (most recent call last):
6567 ...
66- AssertionError : b should be float or integer your input : wrong_input
68+ TypeError : b should be float or integer your input : wrong_input
6769
6870 >>> simpson_integration(lambda x : x*x,1,2,'wrong_input')
6971 Traceback (most recent call last):
7072 ...
71- AssertionError : precision should be positive integer your input : wrong_input
73+ ValueError : precision should be positive integer your input : wrong_input
7274 >>> simpson_integration('wrong_input',2,3,4)
7375 Traceback (most recent call last):
7476 ...
75- AssertionError : the function(object) passed should be callable your input : ...
77+ TypeError : the function(object) passed should be callable your input : wrong_input
7678
7779 >>> simpson_integration(lambda x : x*x,3.45,3.2,1)
7880 -2.8
7981
8082 >>> simpson_integration(lambda x : x*x,3.45,3.2,0)
8183 Traceback (most recent call last):
8284 ...
83- AssertionError : precision should be positive integer your input : 0
85+ ValueError : precision should be positive integer your input : 0
8486
8587 >>> simpson_integration(lambda x : x*x,3.45,3.2,-1)
8688 Traceback (most recent call last):
8789 ...
88- AssertionError : precision should be positive integer your input : -1
90+ ValueError : precision should be positive integer your input : -1
8991
9092 """
91- assert callable (function ), (
92- f"the function(object) passed should be callable your input : { function } "
93- )
94- assert isinstance (a , (float , int )), f"a should be float or integer your input : { a } "
95- assert isinstance (function (a ), (float , int )), (
96- "the function should return integer or float return type of your function, "
97- f"{ type (a )} "
98- )
99- assert isinstance (b , (float , int )), f"b should be float or integer your input : { b } "
100- assert isinstance (precision , int ) and precision > 0 , (
101- f"precision should be positive integer your input : { precision } "
102- )
93+ if not callable (function ):
94+ raise TypeError (
95+ f"the function(object) passed should be callable your input : { function } "
96+ )
97+ if not isinstance (a , (float , int )):
98+ raise TypeError (f"a should be float or integer your input : { a } " )
99+ if not isinstance (function (a ), (float , int )):
100+ raise TypeError (
101+ "the function should return integer or float return type of your function, "
102+ f"{ type (function (a ))} "
103+ )
104+ if not isinstance (b , (float , int )):
105+ raise TypeError (f"b should be float or integer your input : { b } " )
106+ if not isinstance (precision , int ) or precision <= 0 :
107+ raise ValueError (
108+ f"precision should be positive integer your input : { precision } "
109+ )
103110
104111 # just applying the formula of simpson for approximate integration written in
105112 # mentioned article in first comment of this file and above this function
0 commit comments