add erf and erfc functions

This commit is contained in:
wyq 2021-08-25 15:08:16 +08:00
parent 35b80eecd6
commit 6ac0f2dbf7
4 changed files with 109 additions and 7 deletions

View File

@ -1,14 +1,12 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<MeteoInfo File="milconfig.xml" Type="configurefile"> <MeteoInfo File="milconfig.xml" Type="configurefile">
<Path OpenPath="D:\Working\MIScript\Jython\mis\chart\subplot"> <Path OpenPath="D:\Working\MIScript\Jython\mis\common_math\special">
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\satellite\FY"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\satellite"/> <RecentFolder Folder="D:\Working\MIScript\Jython\mis\satellite"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\satellite\calipso"/> <RecentFolder Folder="D:\Working\MIScript\Jython\mis\satellite\calipso"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\map"/> <RecentFolder Folder="D:\Working\MIScript\Jython\mis\map"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\map\topology"/> <RecentFolder Folder="D:\Working\MIScript\Jython\mis\map\topology"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\test"/> <RecentFolder Folder="D:\Working\MIScript\Jython\mis\test"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl"/> <RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\common_math"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\common_math\stats"/> <RecentFolder Folder="D:\Working\MIScript\Jython\mis\common_math\stats"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\array"/> <RecentFolder Folder="D:\Working\MIScript\Jython\mis\array"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types"/> <RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types"/>
@ -16,17 +14,19 @@
<RecentFolder Folder="D:\Working\MIScript\Jython\mis"/> <RecentFolder Folder="D:\Working\MIScript\Jython\mis"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\chart"/> <RecentFolder Folder="D:\Working\MIScript\Jython\mis\chart"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\chart\subplot"/> <RecentFolder Folder="D:\Working\MIScript\Jython\mis\chart\subplot"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\common_math"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\common_math\special"/>
</Path> </Path>
<File> <File>
<OpenedFiles> <OpenedFiles>
<OpenedFile File="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\surf_sombrero.py"/> <OpenedFile File="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\surf_sombrero.py"/>
<OpenedFile File="D:\Working\MIScript\Jython\mis\common_math\stats\kendalltau_1.py"/> <OpenedFile File="D:\Working\MIScript\Jython\mis\common_math\stats\kendalltau_1.py"/>
<OpenedFile File="D:\Working\MIScript\Jython\mis\chart\subplot\subplot_quiver_1.py"/> <OpenedFile File="D:\Working\MIScript\Jython\mis\common_math\special\erfc.py"/>
</OpenedFiles> </OpenedFiles>
<RecentFiles> <RecentFiles>
<RecentFile File="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\surf_sombrero.py"/> <RecentFile File="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\surf_sombrero.py"/>
<RecentFile File="D:\Working\MIScript\Jython\mis\common_math\stats\kendalltau_1.py"/> <RecentFile File="D:\Working\MIScript\Jython\mis\common_math\stats\kendalltau_1.py"/>
<RecentFile File="D:\Working\MIScript\Jython\mis\chart\subplot\subplot_quiver_1.py"/> <RecentFile File="D:\Working\MIScript\Jython\mis\common_math\special\erfc.py"/>
</RecentFiles> </RecentFiles>
</File> </File>
<Font> <Font>
@ -34,5 +34,5 @@
</Font> </Font>
<LookFeel DockWindowDecorated="true" LafDecorated="true" Name="FlatDarkLaf"/> <LookFeel DockWindowDecorated="true" LafDecorated="true" Name="FlatDarkLaf"/>
<Figure DoubleBuffering="true"/> <Figure DoubleBuffering="true"/>
<Startup MainFormLocation="-7,-7" MainFormSize="1293,693"/> <Startup MainFormLocation="-7,0" MainFormSize="1364,813"/>
</MeteoInfo> </MeteoInfo>

View File

@ -1,6 +1,8 @@
from ._gamma import * from ._gamma import *
from ._basic import * from ._basic import *
from ._erf import *
__all__ = [] __all__ = []
__all__.extend(_basic.__all__) __all__.extend(_basic.__all__)
__all__.extend(_gamma.__all__) __all__.extend(_gamma.__all__)
__all__.extend(_erf.__all__)

View File

@ -0,0 +1,47 @@
from org.meteoinfo.math.special import SpecialUtil
from ..core import numeric as np
__all__ = ['erf','erfc']
def erf(x):
"""
Returns the error function of complex argument.
It is defined as 2/sqrt(pi)*integral(exp(-t**2), t=0..z).
:param x: (*array_like*) Input array.
:return: The values of the error function at the given points x.
"""
if isinstance(x, (list, tuple)):
x = np.array(x).asarray()
if isinstance(x, np.NDArray):
x = x.asarray()
r = SpecialUtil.erf(x)
if isinstance(r, float):
return r
else:
return np.array(r)
def erfc(x):
"""
Complementary error function, 1 - erf(x).
:param x: (*array_like*) Input array.
:return: The values of the error function at the given points x.
"""
if isinstance(x, (list, tuple)):
x = np.array(x).asarray()
if isinstance(x, np.NDArray):
x = x.asarray()
r = SpecialUtil.erfc(x)
if isinstance(r, float):
return r
else:
return np.array(r)

View File

@ -1,5 +1,6 @@
package org.meteoinfo.math.special; package org.meteoinfo.math.special;
import org.apache.commons.math3.special.Erf;
import org.apache.commons.math3.special.Gamma; import org.apache.commons.math3.special.Gamma;
import org.apache.commons.math3.util.CombinatoricsUtils; import org.apache.commons.math3.util.CombinatoricsUtils;
import org.meteoinfo.ndarray.Array; import org.meteoinfo.ndarray.Array;
@ -84,4 +85,56 @@ public class SpecialUtil {
return y; return y;
} }
/**
* Returns the error function of complex argument.
* It is defined as 2/sqrt(pi)*integral(exp(-t**2), t=0..z).
* @param x Value
* @return Error function
*/
public static double erf(double x) {
return Erf.erf(x);
}
/**
* Returns the error function of complex argument.
* It is defined as 2/sqrt(pi)*integral(exp(-t**2), t=0..z).
* @param x Value array
* @return Error function
*/
public static Array erf(Array x) {
Array r = Array.factory(DataType.DOUBLE, x.getShape());
IndexIterator xIter = x.getIndexIterator();
IndexIterator rIter = r.getIndexIterator();
while(xIter.hasNext()) {
rIter.setDoubleNext(Erf.erf(xIter.getDoubleNext()));
}
return r;
}
/**
* Complementary error function, 1 - erf(x)
* @param x Value
* @return Complementary error function
*/
public static double erfc(double x) {
return Erf.erfc(x);
}
/**
* Complementary error function, 1 - erf(x)
* @param x Value array
* @return Complementary error function
*/
public static Array erfc(Array x) {
Array r = Array.factory(DataType.DOUBLE, x.getShape());
IndexIterator xIter = x.getIndexIterator();
IndexIterator rIter = r.getIndexIterator();
while(xIter.hasNext()) {
rIter.setDoubleNext(Erf.erfc(xIter.getDoubleNext()));
}
return r;
}
} }