Skip to content

Commit 882921f

Browse files
committed
MAINT: refactor integralFunction method
1 parent 10bf4cd commit 882921f

File tree

1 file changed

+18
-31
lines changed

1 file changed

+18
-31
lines changed

rocketpy/Function.py

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2068,46 +2068,33 @@ def derivativeFunction(self):
20682068
else:
20692069
return Function(lambda x: self.differentiate(x))
20702070

2071-
def integralFunction(self, lower=None, upper=None, datapoints=100):
2072-
"""Returns a Function object representing the integral of the Function object.
2071+
def integralFunction(self, lower=None):
2072+
"""Returns a Function object representing the integral of the Function
2073+
object.
20732074
20742075
Parameters
20752076
----------
20762077
lower : scalar, optional
2077-
The lower limit of the interval in which the function is to be
2078-
plotted. If the Function is given by a dataset, the default
2079-
value is the start of the dataset.
2080-
upper : scalar, optional
2081-
The upper limit of the interval in which the function is to be
2082-
plotted. If the Function is given by a dataset, the default
2083-
value is the end of the dataset.
2084-
datapoints : int, optional
2085-
The number of points in which the integral will be evaluated for
2086-
plotting it, which draws lines between each evaluated point.
2087-
The default value is 100.
2078+
The lower integration limit. If the Function is given by a dataset
2079+
of points the default value is the start of the dataset. If the
2080+
Function is defined by a callable, then this parameter must be
2081+
given.
20882082
20892083
Returns
20902084
-------
20912085
result : Function
2092-
The integral of the Function object.
2086+
The integral function of the Function object. Note that the domain
2087+
of the integral function is the same as the domain of the original
2088+
Function object.
20932089
"""
2094-
# Check if lower and upper are given
2095-
if lower is None:
2096-
if isinstance(self.source, np.ndarray):
2097-
lower = self.source[0, 0]
2098-
else:
2099-
raise ValueError("Lower limit must be given if source is a function.")
2100-
if upper is None:
2101-
if isinstance(self.source, np.ndarray):
2102-
upper = self.source[-1, 0]
2103-
else:
2104-
raise ValueError("Upper limit must be given if source is a function.")
2105-
2106-
# Create a new Function object
2107-
xData = np.linspace(lower, upper, datapoints)
2108-
yData = np.zeros(datapoints)
2109-
for i in range(datapoints):
2110-
yData[i] = self.integral(lower, xData[i])
2090+
if callable(self.source):
2091+
return Function(lambda x: self.integral(lower, x))
2092+
2093+
# Not callable, i.e., defined by a dataset of points
2094+
lower = lower if lower is not None else self.source[0, 0]
2095+
xData = self.source[:, 0]
2096+
yData = [self.integral(lower, x) for x in xData]
2097+
21112098
return Function(
21122099
np.concatenate(([xData], [yData])).transpose(),
21132100
inputs=self.__inputs__,

0 commit comments

Comments
 (0)