Skip to content

Commit c3c4617

Browse files
committed
enh: added self.xArray and self.yArray
1 parent ecaa167 commit c3c4617

File tree

1 file changed

+79
-66
lines changed

1 file changed

+79
-66
lines changed

rocketpy/Function.py

Lines changed: 79 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,13 @@ def source(x):
187187
# Do things if domDim is 1
188188
if self.__domDim__ == 1:
189189
source = source[source[:, 0].argsort()]
190+
191+
self.xArray = source[:, 0]
192+
self.xmin, self.xmax = self.xArray[0], self.xArray[-1]
193+
194+
self.yArray = source[:, 1]
195+
self.ymin, self.ymax = self.yArray[0], self.yArray[-1]
196+
190197
# Finally set data source as source
191198
self.source = source
192199
# Set default interpolation for point source if it hasn't
@@ -197,6 +204,15 @@ def source(x):
197204
self.setInterpolation(self.__interpolation__)
198205
# Do things if function is multivariate
199206
else:
207+
self.xArray = source[:, 0]
208+
self.xmin, self.xmax = self.xArray[0], self.xArray[-1]
209+
210+
self.yArray = source[:, 1]
211+
self.ymin, self.ymax = self.yArray[0], self.yArray[-1]
212+
213+
self.zArray = source[:, 2]
214+
self.zmin, self.zmax = self.zArray[0], self.zArray[-1]
215+
200216
# Finally set data source as source
201217
self.source = source
202218
if self.__interpolation__ is None:
@@ -271,9 +287,9 @@ def setGetValueOpt(self):
271287
self : Function
272288
"""
273289
# Retrieve general info
274-
xData = self.source[:, 0]
275-
yData = self.source[:, 1]
276-
xmin, xmax = xData[0], xData[-1]
290+
xData = self.xArray
291+
yData = self.yArray
292+
xmin, xmax = self.xmin, self.xmax
277293
if self.__extrapolation__ == "zero":
278294
extrapolation = 0 # Extrapolation is zero
279295
elif self.__extrapolation__ == "natural":
@@ -563,9 +579,9 @@ def getValue(self, *args):
563579
if isinstance(args[0], (int, float)):
564580
args = [list(args)]
565581
x = np.array(args[0])
566-
xData = self.source[:, 0]
567-
yData = self.source[:, 1]
568-
xmin, xmax = xData[0], xData[-1]
582+
xData = self.xArray
583+
yData = self.yArray
584+
xmin, xmax = self.xmin, self.xmax
569585
coeffs = self.__polynomialCoefficients__
570586
A = np.zeros((len(args[0]), coeffs.shape[0]))
571587
for i in range(coeffs.shape[0]):
@@ -583,10 +599,10 @@ def getValue(self, *args):
583599
if isinstance(args[0], (int, float, complex, np.integer)):
584600
args = [list(args)]
585601
x = [arg for arg in args[0]]
586-
xData = self.source[:, 0]
587-
yData = self.source[:, 1]
602+
xData = self.xArray
603+
yData = self.yArray
588604
xIntervals = np.searchsorted(xData, x)
589-
xmin, xmax = xData[0], xData[-1]
605+
xmin, xmax = self.xmin, self.xmax
590606
if self.__interpolation__ == "spline":
591607
coeffs = self.__splineCoefficients__
592608
for i in range(len(x)):
@@ -684,9 +700,9 @@ def getValueOpt_deprecated(self, *args):
684700

685701
# Interpolated Function
686702
# Retrieve general info
687-
xData = self.source[:, 0]
688-
yData = self.source[:, 1]
689-
xmin, xmax = xData[0], xData[-1]
703+
xData = self.xArray
704+
yData = self.yArray
705+
xmin, xmax = self.xmin, self.xmax
690706
if self.__extrapolation__ == "zero":
691707
extrapolation = 0 # Extrapolation is zero
692708
elif self.__extrapolation__ == "natural":
@@ -834,8 +850,8 @@ def getValueOpt2(self, *args):
834850
# Returns value for spline, akima or linear interpolation function type
835851
elif self.__interpolation__ in ["spline", "akima", "linear"]:
836852
x = args[0]
837-
xData = self.source[:, 0]
838-
yData = self.source[:, 1]
853+
xData = self.xArray
854+
yData = self.yArray
839855
# Hunt in intervals near the last interval which was used.
840856
xInterval = self.last_interval
841857
if xData[xInterval - 1] <= x <= xData[xInterval]:
@@ -844,7 +860,7 @@ def getValueOpt2(self, *args):
844860
xInterval = np.searchsorted(xData, x)
845861
self.last_interval = xInterval if xInterval < len(xData) else 0
846862
# Interval found... keep going
847-
xmin, xmax = xData[0], xData[-1]
863+
xmin, xmax = self.xmin, self.xmax
848864
if self.__interpolation__ == "spline":
849865
coeffs = self.__splineCoefficients__
850866
if x == xmin or x == xmax:
@@ -1049,8 +1065,8 @@ def plot1D(
10491065
upper = 10 if upper is None else upper
10501066
else:
10511067
# Determine boundaries
1052-
xData = self.source[:, 0]
1053-
xmin, xmax = xData[0], xData[-1]
1068+
xData = self.xArray
1069+
xmin, xmax = self.xmin, self.xmax
10541070
lower = xmin if lower is None else lower
10551071
upper = xmax if upper is None else upper
10561072
# Plot data points if forceData = True
@@ -1136,8 +1152,8 @@ def plot2D(
11361152
upper = 2 * [upper] if isinstance(upper, (int, float)) else upper
11371153
else:
11381154
# Determine boundaries
1139-
xData = self.source[:, 0]
1140-
yData = self.source[:, 1]
1155+
xData = self.xArray
1156+
yData = self.yArray
11411157
xMin, xMax = xData.min(), xData.max()
11421158
yMin, yMax = yData.min(), yData.max()
11431159
lower = [xMin, yMin] if lower is None else lower
@@ -1336,8 +1352,8 @@ def __interpolatePolynomial__(self):
13361352
# Find the degree of the polynomial interpolation
13371353
degree = self.source.shape[0] - 1
13381354
# Get x and y values for all supplied points.
1339-
x = self.source[:, 0]
1340-
y = self.source[:, 1]
1355+
x = self.xArray
1356+
y = self.yArray
13411357
# Check if interpolation requires large numbers
13421358
if np.amax(x) ** degree > 1e308:
13431359
print(
@@ -1356,8 +1372,8 @@ def __interpolatePolynomial__(self):
13561372
def __interpolateSpline__(self):
13571373
"""Calculate natural spline coefficients that fit the data exactly."""
13581374
# Get x and y values for all supplied points
1359-
x = self.source[:, 0]
1360-
y = self.source[:, 1]
1375+
x = self.xArray
1376+
y = self.yArray
13611377
mdim = len(x)
13621378
h = [x[i + 1] - x[i] for i in range(0, mdim - 1)]
13631379
# Initialize the matrix
@@ -1386,8 +1402,8 @@ def __interpolateSpline__(self):
13861402
def __interpolateAkima__(self):
13871403
"""Calculate akima spline coefficients that fit the data exactly"""
13881404
# Get x and y values for all supplied points
1389-
x = self.source[:, 0]
1390-
y = self.source[:, 1]
1405+
x = self.xArray
1406+
y = self.yArray
13911407
# Estimate derivatives at each point
13921408
d = [0] * len(x)
13931409
d[0] = (y[1] - y[0]) / (x[1] - x[0])
@@ -1457,11 +1473,11 @@ def __truediv__(self, other):
14571473
and isinstance(self.source, np.ndarray)
14581474
and self.__interpolation__ == other.__interpolation__
14591475
and self.__inputs__ == other.__inputs__
1460-
and np.any(self.source[:, 0] - other.source[:, 0]) == False
1476+
and np.any(self.xArray - other.xArray) == False
14611477
):
14621478
# Operate on grid values
1463-
Ys = self.source[:, 1] / other.source[:, 1]
1464-
Xs = self.source[:, 0]
1479+
Ys = self.yArray / other.yArray
1480+
Xs = self.xArray
14651481
source = np.concatenate(([Xs], [Ys])).transpose()
14661482
# Retrieve inputs, outputs and interpolation
14671483
inputs = self.__inputs__[:]
@@ -1478,8 +1494,8 @@ def __truediv__(self, other):
14781494
# Check if Function object source is array or callable
14791495
if isinstance(self.source, np.ndarray):
14801496
# Operate on grid values
1481-
Ys = self.source[:, 1] / other
1482-
Xs = self.source[:, 0]
1497+
Ys = self.yArray / other
1498+
Xs = self.xArray
14831499
source = np.concatenate(([Xs], [Ys])).transpose()
14841500
# Retrieve inputs, outputs and interpolation
14851501
inputs = self.__inputs__[:]
@@ -1513,8 +1529,8 @@ def __rtruediv__(self, other):
15131529
if isinstance(other, (float, int, complex)):
15141530
if isinstance(self.source, np.ndarray):
15151531
# Operate on grid values
1516-
Ys = other / self.source[:, 1]
1517-
Xs = self.source[:, 0]
1532+
Ys = other / self.yArray
1533+
Xs = self.xArray
15181534
source = np.concatenate(([Xs], [Ys])).transpose()
15191535
# Retrieve inputs, outputs and interpolation
15201536
inputs = self.__inputs__[:]
@@ -1559,11 +1575,11 @@ def __pow__(self, other):
15591575
and isinstance(self.source, np.ndarray)
15601576
and self.__interpolation__ == other.__interpolation__
15611577
and self.__inputs__ == other.__inputs__
1562-
and np.any(self.source[:, 0] - other.source[:, 0]) == False
1578+
and np.any(self.xArray - other.xArray) == False
15631579
):
15641580
# Operate on grid values
1565-
Ys = self.source[:, 1] ** other.source[:, 1]
1566-
Xs = self.source[:, 0]
1581+
Ys = self.yArray**other.yArray
1582+
Xs = self.xArray
15671583
source = np.concatenate(([Xs], [Ys])).transpose()
15681584
# Retrieve inputs, outputs and interpolation
15691585
inputs = self.__inputs__[:]
@@ -1580,8 +1596,8 @@ def __pow__(self, other):
15801596
# Check if Function object source is array or callable
15811597
if isinstance(self.source, np.ndarray):
15821598
# Operate on grid values
1583-
Ys = self.source[:, 1] ** other
1584-
Xs = self.source[:, 0]
1599+
Ys = self.yArray**other
1600+
Xs = self.xArray
15851601
source = np.concatenate(([Xs], [Ys])).transpose()
15861602
# Retrieve inputs, outputs and interpolation
15871603
inputs = self.__inputs__[:]
@@ -1615,8 +1631,8 @@ def __rpow__(self, other):
16151631
if isinstance(other, (float, int, complex)):
16161632
if isinstance(self.source, np.ndarray):
16171633
# Operate on grid values
1618-
Ys = other ** self.source[:, 1]
1619-
Xs = self.source[:, 0]
1634+
Ys = other**self.yArray
1635+
Xs = self.xArray
16201636
source = np.concatenate(([Xs], [Ys])).transpose()
16211637
# Retrieve inputs, outputs and interpolation
16221638
inputs = self.__inputs__[:]
@@ -1661,11 +1677,11 @@ def __mul__(self, other):
16611677
and isinstance(self.source, np.ndarray)
16621678
and self.__interpolation__ == other.__interpolation__
16631679
and self.__inputs__ == other.__inputs__
1664-
and np.any(self.source[:, 0] - other.source[:, 0]) == False
1680+
and np.any(self.xArray - other.xArray) == False
16651681
):
16661682
# Operate on grid values
1667-
Ys = self.source[:, 1] * other.source[:, 1]
1668-
Xs = self.source[:, 0]
1683+
Ys = self.yArray * other.yArray
1684+
Xs = self.xArray
16691685
source = np.concatenate(([Xs], [Ys])).transpose()
16701686
# Retrieve inputs, outputs and interpolation
16711687
inputs = self.__inputs__[:]
@@ -1682,8 +1698,8 @@ def __mul__(self, other):
16821698
# Check if Function object source is array or callable
16831699
if isinstance(self.source, np.ndarray):
16841700
# Operate on grid values
1685-
Ys = self.source[:, 1] * other
1686-
Xs = self.source[:, 0]
1701+
Ys = self.yArray * other
1702+
Xs = self.xArray
16871703
source = np.concatenate(([Xs], [Ys])).transpose()
16881704
# Retrieve inputs, outputs and interpolation
16891705
inputs = self.__inputs__[:]
@@ -1717,8 +1733,8 @@ def __rmul__(self, other):
17171733
if isinstance(other, (float, int, complex)):
17181734
if isinstance(self.source, np.ndarray):
17191735
# Operate on grid values
1720-
Ys = other * self.source[:, 1]
1721-
Xs = self.source[:, 0]
1736+
Ys = other * self.yArray
1737+
Xs = self.xArray
17221738
source = np.concatenate(([Xs], [Ys])).transpose()
17231739
# Retrieve inputs, outputs and interpolation
17241740
inputs = self.__inputs__[:]
@@ -1763,11 +1779,11 @@ def __add__(self, other):
17631779
and isinstance(self.source, np.ndarray)
17641780
and self.__interpolation__ == other.__interpolation__
17651781
and self.__inputs__ == other.__inputs__
1766-
and np.any(self.source[:, 0] - other.source[:, 0]) == False
1782+
and np.any(self.xArray - other.xArray) == False
17671783
):
17681784
# Operate on grid values
1769-
Ys = self.source[:, 1] + other.source[:, 1]
1770-
Xs = self.source[:, 0]
1785+
Ys = self.yArray + other.yArray
1786+
Xs = self.xArray
17711787
source = np.concatenate(([Xs], [Ys])).transpose()
17721788
# Retrieve inputs, outputs and interpolation
17731789
inputs = self.__inputs__[:]
@@ -1784,8 +1800,8 @@ def __add__(self, other):
17841800
# Check if Function object source is array or callable
17851801
if isinstance(self.source, np.ndarray):
17861802
# Operate on grid values
1787-
Ys = self.source[:, 1] + other
1788-
Xs = self.source[:, 0]
1803+
Ys = self.yArray + other
1804+
Xs = self.xArray
17891805
source = np.concatenate(([Xs], [Ys])).transpose()
17901806
# Retrieve inputs, outputs and interpolation
17911807
inputs = self.__inputs__[:]
@@ -1819,8 +1835,8 @@ def __radd__(self, other):
18191835
if isinstance(other, (float, int, complex)):
18201836
if isinstance(self.source, np.ndarray):
18211837
# Operate on grid values
1822-
Ys = other + self.source[:, 1]
1823-
Xs = self.source[:, 0]
1838+
Ys = other + self.yArray
1839+
Xs = self.xArray
18241840
source = np.concatenate(([Xs], [Ys])).transpose()
18251841
# Retrieve inputs, outputs and interpolation
18261842
inputs = self.__inputs__[:]
@@ -1865,11 +1881,11 @@ def __sub__(self, other):
18651881
and isinstance(self.source, np.ndarray)
18661882
and self.__interpolation__ == other.__interpolation__
18671883
and self.__inputs__ == other.__inputs__
1868-
and np.any(self.source[:, 0] - other.source[:, 0]) == False
1884+
and np.any(self.xArray - other.xArray) == False
18691885
):
18701886
# Operate on grid values
1871-
Ys = self.source[:, 1] - other.source[:, 1]
1872-
Xs = self.source[:, 0]
1887+
Ys = self.yArray - other.yArray
1888+
Xs = self.xArray
18731889
source = np.concatenate(([Xs], [Ys])).transpose()
18741890
# Retrieve inputs, outputs and interpolation
18751891
inputs = self.__inputs__[:]
@@ -1886,8 +1902,8 @@ def __sub__(self, other):
18861902
# Check if Function object source is array or callable
18871903
if isinstance(self.source, np.ndarray):
18881904
# Operate on grid values
1889-
Ys = self.source[:, 1] - other
1890-
Xs = self.source[:, 0]
1905+
Ys = self.yArray - other
1906+
Xs = self.xArray
18911907
source = np.concatenate(([Xs], [Ys])).transpose()
18921908
# Retrieve inputs, outputs and interpolation
18931909
inputs = self.__inputs__[:]
@@ -1921,8 +1937,8 @@ def __rsub__(self, other):
19211937
if isinstance(other, (float, int, complex)):
19221938
if isinstance(self.source, np.ndarray):
19231939
# Operate on grid values
1924-
Ys = other - self.source[:, 1]
1925-
Xs = self.source[:, 0]
1940+
Ys = other - self.yArray
1941+
Xs = self.xArray
19261942
source = np.concatenate(([Xs], [Ys])).transpose()
19271943
# Retrieve inputs, outputs and interpolation
19281944
inputs = self.__inputs__[:]
@@ -1961,8 +1977,8 @@ def integral(self, a, b, numerical=False):
19611977
"""
19621978
if self.__interpolation__ == "spline" and numerical is False:
19631979
# Integrate using spline coefficients
1964-
xData = self.source[:, 0]
1965-
yData = self.source[:, 1]
1980+
xData = self.xArray
1981+
yData = self.yArray
19661982
coeffs = self.__splineCoefficients__
19671983
ans = 0
19681984
# Check to see if interval starts before point data
@@ -2021,10 +2037,7 @@ def integral(self, a, b, numerical=False):
20212037
# self.__extrapolation__ = 'zero'
20222038
pass
20232039
elif self.__interpolation__ == "linear" and numerical is False:
2024-
return np.trapz(self.source[:, 1], x=self.source[:, 0])
2025-
else:
2026-
# Integrate numerically
2027-
ans, _ = integrate.quad(self, a, b, epsabs=0.1, limit=10000)
2040+
return np.trapz(self.yArray, x=self.xArray)
20282041
return ans
20292042

20302043
def differentiate(self, x, dx=1e-6):

0 commit comments

Comments
 (0)