@@ -187,6 +187,13 @@ def source(x):
187
187
# Do things if domDim is 1
188
188
if self .__domDim__ == 1 :
189
189
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
+
190
197
# Finally set data source as source
191
198
self .source = source
192
199
# Set default interpolation for point source if it hasn't
@@ -197,6 +204,15 @@ def source(x):
197
204
self .setInterpolation (self .__interpolation__ )
198
205
# Do things if function is multivariate
199
206
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
+
200
216
# Finally set data source as source
201
217
self .source = source
202
218
if self .__interpolation__ is None :
@@ -271,9 +287,9 @@ def setGetValueOpt(self):
271
287
self : Function
272
288
"""
273
289
# 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
277
293
if self .__extrapolation__ == "zero" :
278
294
extrapolation = 0 # Extrapolation is zero
279
295
elif self .__extrapolation__ == "natural" :
@@ -563,9 +579,9 @@ def getValue(self, *args):
563
579
if isinstance (args [0 ], (int , float )):
564
580
args = [list (args )]
565
581
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
569
585
coeffs = self .__polynomialCoefficients__
570
586
A = np .zeros ((len (args [0 ]), coeffs .shape [0 ]))
571
587
for i in range (coeffs .shape [0 ]):
@@ -583,10 +599,10 @@ def getValue(self, *args):
583
599
if isinstance (args [0 ], (int , float , complex , np .integer )):
584
600
args = [list (args )]
585
601
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
588
604
xIntervals = np .searchsorted (xData , x )
589
- xmin , xmax = xData [ 0 ], xData [ - 1 ]
605
+ xmin , xmax = self . xmin , self . xmax
590
606
if self .__interpolation__ == "spline" :
591
607
coeffs = self .__splineCoefficients__
592
608
for i in range (len (x )):
@@ -684,9 +700,9 @@ def getValueOpt_deprecated(self, *args):
684
700
685
701
# Interpolated Function
686
702
# 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
690
706
if self .__extrapolation__ == "zero" :
691
707
extrapolation = 0 # Extrapolation is zero
692
708
elif self .__extrapolation__ == "natural" :
@@ -834,8 +850,8 @@ def getValueOpt2(self, *args):
834
850
# Returns value for spline, akima or linear interpolation function type
835
851
elif self .__interpolation__ in ["spline" , "akima" , "linear" ]:
836
852
x = args [0 ]
837
- xData = self .source [:, 0 ]
838
- yData = self .source [:, 1 ]
853
+ xData = self .xArray
854
+ yData = self .yArray
839
855
# Hunt in intervals near the last interval which was used.
840
856
xInterval = self .last_interval
841
857
if xData [xInterval - 1 ] <= x <= xData [xInterval ]:
@@ -844,7 +860,7 @@ def getValueOpt2(self, *args):
844
860
xInterval = np .searchsorted (xData , x )
845
861
self .last_interval = xInterval if xInterval < len (xData ) else 0
846
862
# Interval found... keep going
847
- xmin , xmax = xData [ 0 ], xData [ - 1 ]
863
+ xmin , xmax = self . xmin , self . xmax
848
864
if self .__interpolation__ == "spline" :
849
865
coeffs = self .__splineCoefficients__
850
866
if x == xmin or x == xmax :
@@ -1049,8 +1065,8 @@ def plot1D(
1049
1065
upper = 10 if upper is None else upper
1050
1066
else :
1051
1067
# 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
1054
1070
lower = xmin if lower is None else lower
1055
1071
upper = xmax if upper is None else upper
1056
1072
# Plot data points if forceData = True
@@ -1136,8 +1152,8 @@ def plot2D(
1136
1152
upper = 2 * [upper ] if isinstance (upper , (int , float )) else upper
1137
1153
else :
1138
1154
# Determine boundaries
1139
- xData = self .source [:, 0 ]
1140
- yData = self .source [:, 1 ]
1155
+ xData = self .xArray
1156
+ yData = self .yArray
1141
1157
xMin , xMax = xData .min (), xData .max ()
1142
1158
yMin , yMax = yData .min (), yData .max ()
1143
1159
lower = [xMin , yMin ] if lower is None else lower
@@ -1336,8 +1352,8 @@ def __interpolatePolynomial__(self):
1336
1352
# Find the degree of the polynomial interpolation
1337
1353
degree = self .source .shape [0 ] - 1
1338
1354
# 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
1341
1357
# Check if interpolation requires large numbers
1342
1358
if np .amax (x ) ** degree > 1e308 :
1343
1359
print (
@@ -1356,8 +1372,8 @@ def __interpolatePolynomial__(self):
1356
1372
def __interpolateSpline__ (self ):
1357
1373
"""Calculate natural spline coefficients that fit the data exactly."""
1358
1374
# 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
1361
1377
mdim = len (x )
1362
1378
h = [x [i + 1 ] - x [i ] for i in range (0 , mdim - 1 )]
1363
1379
# Initialize the matrix
@@ -1386,8 +1402,8 @@ def __interpolateSpline__(self):
1386
1402
def __interpolateAkima__ (self ):
1387
1403
"""Calculate akima spline coefficients that fit the data exactly"""
1388
1404
# 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
1391
1407
# Estimate derivatives at each point
1392
1408
d = [0 ] * len (x )
1393
1409
d [0 ] = (y [1 ] - y [0 ]) / (x [1 ] - x [0 ])
@@ -1457,11 +1473,11 @@ def __truediv__(self, other):
1457
1473
and isinstance (self .source , np .ndarray )
1458
1474
and self .__interpolation__ == other .__interpolation__
1459
1475
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
1461
1477
):
1462
1478
# 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
1465
1481
source = np .concatenate (([Xs ], [Ys ])).transpose ()
1466
1482
# Retrieve inputs, outputs and interpolation
1467
1483
inputs = self .__inputs__ [:]
@@ -1478,8 +1494,8 @@ def __truediv__(self, other):
1478
1494
# Check if Function object source is array or callable
1479
1495
if isinstance (self .source , np .ndarray ):
1480
1496
# Operate on grid values
1481
- Ys = self .source [:, 1 ] / other
1482
- Xs = self .source [:, 0 ]
1497
+ Ys = self .yArray / other
1498
+ Xs = self .xArray
1483
1499
source = np .concatenate (([Xs ], [Ys ])).transpose ()
1484
1500
# Retrieve inputs, outputs and interpolation
1485
1501
inputs = self .__inputs__ [:]
@@ -1513,8 +1529,8 @@ def __rtruediv__(self, other):
1513
1529
if isinstance (other , (float , int , complex )):
1514
1530
if isinstance (self .source , np .ndarray ):
1515
1531
# Operate on grid values
1516
- Ys = other / self .source [:, 1 ]
1517
- Xs = self .source [:, 0 ]
1532
+ Ys = other / self .yArray
1533
+ Xs = self .xArray
1518
1534
source = np .concatenate (([Xs ], [Ys ])).transpose ()
1519
1535
# Retrieve inputs, outputs and interpolation
1520
1536
inputs = self .__inputs__ [:]
@@ -1559,11 +1575,11 @@ def __pow__(self, other):
1559
1575
and isinstance (self .source , np .ndarray )
1560
1576
and self .__interpolation__ == other .__interpolation__
1561
1577
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
1563
1579
):
1564
1580
# 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
1567
1583
source = np .concatenate (([Xs ], [Ys ])).transpose ()
1568
1584
# Retrieve inputs, outputs and interpolation
1569
1585
inputs = self .__inputs__ [:]
@@ -1580,8 +1596,8 @@ def __pow__(self, other):
1580
1596
# Check if Function object source is array or callable
1581
1597
if isinstance (self .source , np .ndarray ):
1582
1598
# Operate on grid values
1583
- Ys = self .source [:, 1 ] ** other
1584
- Xs = self .source [:, 0 ]
1599
+ Ys = self .yArray ** other
1600
+ Xs = self .xArray
1585
1601
source = np .concatenate (([Xs ], [Ys ])).transpose ()
1586
1602
# Retrieve inputs, outputs and interpolation
1587
1603
inputs = self .__inputs__ [:]
@@ -1615,8 +1631,8 @@ def __rpow__(self, other):
1615
1631
if isinstance (other , (float , int , complex )):
1616
1632
if isinstance (self .source , np .ndarray ):
1617
1633
# Operate on grid values
1618
- Ys = other ** self .source [:, 1 ]
1619
- Xs = self .source [:, 0 ]
1634
+ Ys = other ** self .yArray
1635
+ Xs = self .xArray
1620
1636
source = np .concatenate (([Xs ], [Ys ])).transpose ()
1621
1637
# Retrieve inputs, outputs and interpolation
1622
1638
inputs = self .__inputs__ [:]
@@ -1661,11 +1677,11 @@ def __mul__(self, other):
1661
1677
and isinstance (self .source , np .ndarray )
1662
1678
and self .__interpolation__ == other .__interpolation__
1663
1679
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
1665
1681
):
1666
1682
# 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
1669
1685
source = np .concatenate (([Xs ], [Ys ])).transpose ()
1670
1686
# Retrieve inputs, outputs and interpolation
1671
1687
inputs = self .__inputs__ [:]
@@ -1682,8 +1698,8 @@ def __mul__(self, other):
1682
1698
# Check if Function object source is array or callable
1683
1699
if isinstance (self .source , np .ndarray ):
1684
1700
# Operate on grid values
1685
- Ys = self .source [:, 1 ] * other
1686
- Xs = self .source [:, 0 ]
1701
+ Ys = self .yArray * other
1702
+ Xs = self .xArray
1687
1703
source = np .concatenate (([Xs ], [Ys ])).transpose ()
1688
1704
# Retrieve inputs, outputs and interpolation
1689
1705
inputs = self .__inputs__ [:]
@@ -1717,8 +1733,8 @@ def __rmul__(self, other):
1717
1733
if isinstance (other , (float , int , complex )):
1718
1734
if isinstance (self .source , np .ndarray ):
1719
1735
# Operate on grid values
1720
- Ys = other * self .source [:, 1 ]
1721
- Xs = self .source [:, 0 ]
1736
+ Ys = other * self .yArray
1737
+ Xs = self .xArray
1722
1738
source = np .concatenate (([Xs ], [Ys ])).transpose ()
1723
1739
# Retrieve inputs, outputs and interpolation
1724
1740
inputs = self .__inputs__ [:]
@@ -1763,11 +1779,11 @@ def __add__(self, other):
1763
1779
and isinstance (self .source , np .ndarray )
1764
1780
and self .__interpolation__ == other .__interpolation__
1765
1781
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
1767
1783
):
1768
1784
# 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
1771
1787
source = np .concatenate (([Xs ], [Ys ])).transpose ()
1772
1788
# Retrieve inputs, outputs and interpolation
1773
1789
inputs = self .__inputs__ [:]
@@ -1784,8 +1800,8 @@ def __add__(self, other):
1784
1800
# Check if Function object source is array or callable
1785
1801
if isinstance (self .source , np .ndarray ):
1786
1802
# Operate on grid values
1787
- Ys = self .source [:, 1 ] + other
1788
- Xs = self .source [:, 0 ]
1803
+ Ys = self .yArray + other
1804
+ Xs = self .xArray
1789
1805
source = np .concatenate (([Xs ], [Ys ])).transpose ()
1790
1806
# Retrieve inputs, outputs and interpolation
1791
1807
inputs = self .__inputs__ [:]
@@ -1819,8 +1835,8 @@ def __radd__(self, other):
1819
1835
if isinstance (other , (float , int , complex )):
1820
1836
if isinstance (self .source , np .ndarray ):
1821
1837
# Operate on grid values
1822
- Ys = other + self .source [:, 1 ]
1823
- Xs = self .source [:, 0 ]
1838
+ Ys = other + self .yArray
1839
+ Xs = self .xArray
1824
1840
source = np .concatenate (([Xs ], [Ys ])).transpose ()
1825
1841
# Retrieve inputs, outputs and interpolation
1826
1842
inputs = self .__inputs__ [:]
@@ -1865,11 +1881,11 @@ def __sub__(self, other):
1865
1881
and isinstance (self .source , np .ndarray )
1866
1882
and self .__interpolation__ == other .__interpolation__
1867
1883
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
1869
1885
):
1870
1886
# 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
1873
1889
source = np .concatenate (([Xs ], [Ys ])).transpose ()
1874
1890
# Retrieve inputs, outputs and interpolation
1875
1891
inputs = self .__inputs__ [:]
@@ -1886,8 +1902,8 @@ def __sub__(self, other):
1886
1902
# Check if Function object source is array or callable
1887
1903
if isinstance (self .source , np .ndarray ):
1888
1904
# Operate on grid values
1889
- Ys = self .source [:, 1 ] - other
1890
- Xs = self .source [:, 0 ]
1905
+ Ys = self .yArray - other
1906
+ Xs = self .xArray
1891
1907
source = np .concatenate (([Xs ], [Ys ])).transpose ()
1892
1908
# Retrieve inputs, outputs and interpolation
1893
1909
inputs = self .__inputs__ [:]
@@ -1921,8 +1937,8 @@ def __rsub__(self, other):
1921
1937
if isinstance (other , (float , int , complex )):
1922
1938
if isinstance (self .source , np .ndarray ):
1923
1939
# Operate on grid values
1924
- Ys = other - self .source [:, 1 ]
1925
- Xs = self .source [:, 0 ]
1940
+ Ys = other - self .yArray
1941
+ Xs = self .xArray
1926
1942
source = np .concatenate (([Xs ], [Ys ])).transpose ()
1927
1943
# Retrieve inputs, outputs and interpolation
1928
1944
inputs = self .__inputs__ [:]
@@ -1961,8 +1977,8 @@ def integral(self, a, b, numerical=False):
1961
1977
"""
1962
1978
if self .__interpolation__ == "spline" and numerical is False :
1963
1979
# Integrate using spline coefficients
1964
- xData = self .source [:, 0 ]
1965
- yData = self .source [:, 1 ]
1980
+ xData = self .xArray
1981
+ yData = self .yArray
1966
1982
coeffs = self .__splineCoefficients__
1967
1983
ans = 0
1968
1984
# Check to see if interval starts before point data
@@ -2021,10 +2037,7 @@ def integral(self, a, b, numerical=False):
2021
2037
# self.__extrapolation__ = 'zero'
2022
2038
pass
2023
2039
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 )
2028
2041
return ans
2029
2042
2030
2043
def differentiate (self , x , dx = 1e-6 ):
0 commit comments