Skip to content

Commit

Permalink
fix for new numpy
Browse files Browse the repository at this point in the history
  • Loading branch information
VanyaBelyaev committed Sep 26, 2024
1 parent 0312b28 commit 8ce716b
Showing 1 changed file with 39 additions and 24 deletions.
63 changes: 39 additions & 24 deletions ostap/math/param.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def legendre_sum ( func , N , xmin , xmax , **kwargs ) :
# @author Vanya Belyaev [email protected]
# @date 2015-07-26
def chebyshev_sum ( func , N , xmin , xmax ) :
"""Make a function representation in terms of Chebyshev polynomials
""" Make a function representation in terms of Chebyshev polynomials
>>> func = lambda x : x * x
>>> fsum = chebyshev_sum ( func , 4 , -1 , 1 )
>>> print fsum
Expand Down Expand Up @@ -239,9 +239,8 @@ def chebyshev_sum ( func , N , xmin , xmax ) :

return csum


# =============================================================================
try :
try : # =======================================================================
# =========================================================================
import numpy
# =========================================================================
Expand All @@ -257,7 +256,7 @@ def chebyshev_sum ( func , N , xmin , xmax ) :
# @author Vanya Belyaev [email protected]
# @date 2015-07-26
def fourier_sum ( func , N , xmin , xmax , fejer = False ) :
"""Make a function/histiogram representation in terms of Fourier series
""" Make a function/histogram representation in terms of Fourier series
>>> func = lambda x : x * x
>>> fsum = fourier_sum ( func , 4 , -1 , 1 )
>>> print fsum
Expand All @@ -268,9 +267,15 @@ def fourier_sum ( func , N , xmin , xmax , fejer = False ) :

xmin , xmax = _get_xminmax_ ( func , xmin , xmax , 'fourier_sum' )

## 0) check the type of return value in the mid point
xmid = 0.5 * ( xmin + xmax )
fval = func ( xmid )
#
if isinstance ( fval , num_types ) : the_fun = func
else : the_fun = lambda x : float ( func ( x ) )

## 1) vectorize the function
vfunc = numpy.vectorize ( func )
vfunc = numpy.vectorize ( the_fun )

## prepare sampling
f_sample = 2 * N
Expand All @@ -284,14 +289,14 @@ def fourier_sum ( func , N , xmin , xmax , fejer = False ) :
## decode the results:
#

a0 = y[0].real
a0 = y[0 ].real
a = y[1:-1].real
b = y[1:-1].imag

#
## prepare the output
#
fsum = Ostap.Math.FourierSum ( N, xmin , xmax , fejer )
fsum = Ostap.Math.FourierSum ( N , xmin , xmax , fejer )

#
## fill it!
Expand All @@ -311,19 +316,20 @@ def fourier_sum ( func , N , xmin , xmax , fejer = False ) :
__all__ = __all__ + (
'fourier_sum' , ## Fourier sum for the given function/object
)

except ImportError :

# =========================================================================
except ImportError : # ========================================================
# =========================================================================
numpy = None
pass


# =============================================================================
try :
try : # =======================================================================
# =========================================================================
import numpy
import scipy
import scipy.fftpack
# =========================================================================

# =============================================================================
## make a function representation in terms of cosine Fourier series
# @code
# func = lambda x : x * x
Expand All @@ -336,7 +342,7 @@ def fourier_sum ( func , N , xmin , xmax , fejer = False ) :
# @author Vanya Belyaev [email protected]
# @date 2015-07-26
def cosine_sum ( func , N , xmin , xmax , fejer = False ) :
"""Make a function/histiogram representation in terms of Fourier series
""" Make a function/histiogram representation in terms of Fourier series
>>> func = lambda x : x * x
>>> fsum = fourier_sum ( func , 4 , -1 , 1 )
>>> print fsum
Expand All @@ -347,12 +353,18 @@ def cosine_sum ( func , N , xmin , xmax , fejer = False ) :

xmin,xmax = _get_xminmax_ ( func , xmin , xmax , 'cosine_sum' )

# 0) check the functon type at the misdpoint
xmid = 0.5 * ( xmin + xmax )
fval = func ( xmid )

if isinstance ( fval , num_types ) : the_fun = func
else : the_fun = lambda x : float ( func ( x ) )

## 1) prepare sampling
t = numpy.linspace ( xmin , xmax , N + 1 , endpoint=True )

## 2) vectorize the function
vfunc = numpy.vectorize ( lambda x : float ( func ( x ) ) )
vfunc = numpy.vectorize ( the_fun )

## make cosine fourier transform
r = scipy.fftpack.dct ( vfunc ( t ) , 1 ) / N
Expand All @@ -368,10 +380,13 @@ def cosine_sum ( func , N , xmin , xmax , fejer = False ) :
__all__ = __all__ + (
'cosine_sum' , ## Cosine Fourier sum for the given function/object
)

except ImportError :
pass


# =========================================================================
except ImportError : # ========================================================
# =========================================================================
scipy = None
numpy = None

# =============================================================================
## make a function representation in terms of Bezier sum
# (sum over Bernstein polynomials)
Expand All @@ -390,7 +405,7 @@ def cosine_sum ( func , N , xmin , xmax , fejer = False ) :
# @author Vanya Belyaev [email protected]
# @date 2016-07-03
def bezier_sum ( func , N , xmin , xmax , **kwargs ) :
"""Make a function/histiogram representation in terms of Bezier sum
""" Make a function/histiogram representation in terms of Bezier sum
(sum of Bernstein Polynomials)
>>> func = lambda x : x * x
>>> fsum = bezier_sum ( func , 4 , 0 , 1 )
Expand Down Expand Up @@ -580,9 +595,9 @@ def rational_fun ( func , n , d , xmin , xmax , **kwargs ) :
docme ( __name__ , logger = logger )

if not 'fourier_sum' in __all__ :
logger.warning ("Since numpy is not available, fourier_sum is disabled")
if not 'cosine_sum' in __all__ :
logger.warning ("Since scipy is not available, cosine_sum is disabled")
logger.warning ( "Since numpy is not available, fourier_sum is disabled")
if not 'cosine_sum' in __all__ :
logger.warning ( "Since scipy is not available, cosine_sum is disabled")

# =============================================================================
## The END
Expand Down

0 comments on commit 8ce716b

Please sign in to comment.