@@ -1986,7 +1986,11 @@ def integral(self, a, b, numerical=False):
1986
1986
ans : float
1987
1987
Evaluated integral.
1988
1988
"""
1989
- if self .__interpolation__ == "spline" and numerical is False :
1989
+ if self .__interpolation__ == "linear" and not numerical :
1990
+ Xs = np .linspace (a , b , int ((b - a ) * 5 ))
1991
+ Ys = self .getValue (Xs )
1992
+ ans = np .trapz (Ys , x = Xs )
1993
+ elif self .__interpolation__ == "spline" and not numerical :
1990
1994
# Integrate using spline coefficients
1991
1995
xData = self .xArray
1992
1996
yData = self .yArray
@@ -2047,8 +2051,9 @@ def integral(self, a, b, numerical=False):
2047
2051
else :
2048
2052
# self.__extrapolation__ = 'zero'
2049
2053
pass
2050
- elif self .__interpolation__ == "linear" and numerical is False :
2051
- return np .trapz (self .yArray , x = self .xArray )
2054
+ else :
2055
+ # Integrate numerically
2056
+ ans , _ = integrate .quad (self , a , b , epsabs = 0.01 , limit = 10000 )
2052
2057
return ans
2053
2058
2054
2059
def differentiate (self , x , dx = 1e-6 ):
@@ -2080,18 +2085,21 @@ def derivativeFunction(self):
2080
2085
# Check if Function object source is array
2081
2086
if isinstance (self .source , np .ndarray ):
2082
2087
# Operate on grid values
2083
- Ys = np .diff (self .source [:, 1 ] ) / np .diff (self .source [:, 0 ] )
2084
- Xs = self .source [:- 1 , 0 ] + np .diff (self .source [:, 0 ] ) / 2
2085
- source = np .concatenate (([ Xs ], [ Ys ])). transpose ( )
2088
+ Ys = np .diff (self .yArray ) / np .diff (self .xArray )
2089
+ Xs = self .source [:- 1 , 0 ] + np .diff (self .xArray ) / 2
2090
+ source = np .column_stack (( Xs , Ys ) )
2086
2091
# Retrieve inputs, outputs and interpolation
2087
2092
inputs = self .__inputs__ [:]
2088
- outputs = "d(" + self .__outputs__ [0 ] + ")/d(" + inputs [0 ] + ")"
2089
- outputs = "(" + outputs + ")"
2093
+ outputs = f"d({ self .__outputs__ [0 ]} )/d({ inputs [0 ]} )"
2090
2094
interpolation = "linear"
2091
- # Create new Function object
2092
- return Function (source , inputs , outputs , interpolation )
2093
2095
else :
2094
- return Function (lambda x : self .differentiate (x ))
2096
+ source = lambda x : self .differentiate (x )
2097
+ inputs = self .__inputs__ [:]
2098
+ outputs = f"d({ self .__outputs__ [0 ]} )/d({ inputs [0 ]} )"
2099
+ interpolation = "linear"
2100
+
2101
+ # Create new Function object
2102
+ return Function (source , inputs , outputs , interpolation )
2095
2103
2096
2104
def integralFunction (self , lower = None , upper = None , datapoints = 100 ):
2097
2105
"""Returns a Function object representing the integral of the Function object.
@@ -2124,7 +2132,7 @@ def integralFunction(self, lower=None, upper=None, datapoints=100):
2124
2132
for i in range (datapoints ):
2125
2133
yData [i ] = self .integral (lower , xData [i ])
2126
2134
return Function (
2127
- np .concatenate (([ xData ], [ yData ])). transpose ( ),
2135
+ np .column_stack (( xData , yData ) ),
2128
2136
inputs = self .__inputs__ ,
2129
2137
outputs = [o + " Integral" for o in self .__outputs__ ],
2130
2138
)
@@ -2155,27 +2163,18 @@ def inverseFunction(self, approxFunc=None, tol=1e-4):
2155
2163
"""
2156
2164
if isinstance (self .source , np .ndarray ):
2157
2165
# Swap the columns
2158
- source = np .concatenate (
2159
- ([self .source [:, 1 ]], [self .source [:, 0 ]])
2160
- ).transpose ()
2161
-
2162
- return Function (
2163
- source ,
2164
- inputs = self .__outputs__ ,
2165
- outputs = self .__inputs__ ,
2166
- interpolation = self .__interpolation__ ,
2167
- )
2166
+ source = np .flip (self .source , axis = 1 )
2168
2167
else :
2169
2168
if approxFunc :
2170
- source = lambda x : self .findInput (x , approxFunc (x ), tol )
2169
+ source = lambda x : self .findInput (x , approxFunc (x ), tol = tol )
2171
2170
else :
2172
2171
source = lambda x : self .findInput (x , tol = tol )
2173
- return Function (
2174
- source ,
2175
- inputs = self .__outputs__ ,
2176
- outputs = self .__inputs__ ,
2177
- interpolation = self .__interpolation__ ,
2178
- )
2172
+ return Function (
2173
+ source ,
2174
+ inputs = self .__outputs__ ,
2175
+ outputs = self .__inputs__ ,
2176
+ interpolation = self .__interpolation__ ,
2177
+ )
2179
2178
2180
2179
def findInput (self , val , start = 0 , tol = 1e-4 ):
2181
2180
"""
@@ -2195,7 +2194,7 @@ def findInput(self, val, start=0, tol=1e-4):
2195
2194
lambda x : self .getValue (x ) - val ,
2196
2195
start ,
2197
2196
tol = tol ,
2198
- ).x
2197
+ ).x [ 0 ]
2199
2198
2200
2199
def average (self , lower , upper ):
2201
2200
"""
@@ -2215,7 +2214,8 @@ def averageFunction(self, lower=None):
2215
2214
Parameters
2216
2215
----------
2217
2216
lower : float
2218
- Lower limit of the new domain. Only required if the Function's source is a callable instead of a list of points.
2217
+ Lower limit of the new domain. Only required if the Function's source
2218
+ is a callable instead of a list of points.
2219
2219
2220
2220
Returns
2221
2221
-------
0 commit comments