Functions

This page contains the definitions of the Function and the AnalyticFunction classes.

class siegpy.functions.Function(grid, values)[source]

Bases: object

Class defining a 1-dimendional (1D) function from its grid and the corresponding values.

A function can be plotted, and different types of operations can be performed (e.g., scalar product with another function, addition of another function). It is also possible to compute its norm, and return its conjugate and its absolute value as another function.

Parameters:
  • grid (list or set or numpy array) – Discretization grid.
  • values (list or set or numpy array) – Values of the function evaluated on the grid points.
Raises:
  • ValueError – If the grid is not made of reals or if the grid and values arrays have incoherent lengths.

  • Example

  • Note that both grid and values are converted to numpy

  • arrays:

  • >>> f = Function([-1, 0, 1], [1, 2, 3])
    
  • >>> f.grid
    
  • array([-1, 0, 1])

  • >>> f.values
    
  • array([1, 2, 3])

grid
Returns:
  • numpy array – Grid of a Function instance.
  • .. warning:: – The grid of a Function instance cannot be modified:
    >>> f = Function([-1, 0, 1], [1, 2, 3])
    >>> f.grid = [-1, 1]
    Traceback (most recent call last):
    AttributeError: can't set attribute
    
values
Returns:
  • numpy array – Values of a Function instance.
  • .. warning:: – The values of a Function instance cannot be modified:
    >>> f = Function([-1, 0, 1], [1, 2, 3])
    >>> f.values = [3, 0, 1]
    Traceback (most recent call last):
    AttributeError: can't set attribute
    
__eq__(other)[source]

Two functions are equal if they have the same grid and values.

Parameters:other (object) – Another object.
Returns:
  • boolTrue if other is a Function instance with the same grid and values.
  • Examples
  • >>> f = Function([1, 2, 3], [1, 1, 1])
  • >>> Function([1, 2, 3], [1, 1, 1]) == f
  • True
  • >>> Function([1, 2, 3], [-1, 0, 1]) == f
  • False
  • >>> Function([-1, 0, 1], [1, 1, 1]) == f
  • False
  • >>> f == 1
  • False
__add__(other)[source]

Add two functions, if both grids are the same.

Parameters:

other (Function) – Another function.

Returns:

Sum of both functions.

Return type:

Function

Raises:
  • ValueError – If both the grid of both functions differ.

  • Examples

  • Two functions can be added:

  • >>> f1 = Function([1, 2, 3], [1, 1, 1])
    
  • >>> f2 = Function([1, 2, 3], [0, -1, 0])
    
  • >>> f = f1 + f2
    
  • The grid of the new function is the same, and its values are

  • the sum of both values:

  • >>> f.grid
    
  • array([1, 2, 3])

  • >>> f.values
    
  • array([1, 0, 1])

  • It leaves the other functions unchanged:

  • >>> f1.values
    
  • array([1, 1, 1])

  • >>> f2.values
    
  • array([ 0, -1, 0])

is_even
Returns:
  • boolTrue if the function is even, False if not.
  • Examples
  • >>> Function([1, 2, 3], [1j, 1j, 1j]).is_even
  • False
  • >>> Function([-1, 0, 1], [1j, 1j, 1j]).is_even
  • True
is_odd
Returns:
  • boolTrue if the function is odd, False if not.
  • Examples
  • >>> Function([-1, 0, 1], [-1j, 0j, 1j]).is_odd
  • True
  • >>> Function([1, 2, 3], [-1j, 0j, 1j]).is_odd
  • False
plot(xlim=None, ylim=None, title=None, file_save=None)[source]

Plot the real and imaginary parts of the function.

Parameters:
  • xlim (tuple(float or int, float or int)) – Range of the x axis of the plot (optional).
  • ylim (tuple(float or int, float or int)) – Range of the y axis of the plot (optional).
  • title (str) – Title for the plot.
  • file_save (str) – Filename of the plot to be saved (optional).
abs()[source]
Returns:
  • Function – Absolute value of the function.
  • Example
  • Applying the abs() method to a Function instance
  • returns a new Function instance (i.e., the initial
  • one is unchanged)
  • >>> f = Function([1, 2, 3], [1j, 1j, 1j])
  • >>> f.abs().values
  • array([ 1., 1., 1.])
  • >>> f.values
  • array([ 0.+1.j, 0.+1.j, 0.+1.j])
conjugate()[source]
Returns:
  • Function – Conjugate of the function.
  • Example
  • Applying the conjugate() method to a Function
  • instance returns a new Function instance (i.e. the
  • initial one is unchanged)
  • >>> f = Function([1, 2, 3], [1j, 1j, 1j])
  • >>> f.conjugate().values
  • array([ 0.-1.j, 0.-1.j, 0.-1.j])
  • >>> f.values
  • array([ 0.+1.j, 0.+1.j, 0.+1.j])
scal_prod(other, xlim=None)[source]

Evaluate the usual scalar product of two functions f and g:

\(\langle f | g \rangle = \int f^*(x) g(x) \text{d}x\)

where * represents the conjugation.

Note

  • The trapezoidal integration rule is used.
Parameters:
  • other (Function) – Another function.
  • xlim (tuple(float or int, float or int)) – Range of the x-axis for the integration (optional).
Returns:

Value of the scalar product

Return type:

float

Raises:
  • ValueError – If the grid of both functions are different or if the interval given by xlim is not inside the one defined by the discretization grid.

  • Example

  • >>> grid = [-1, 0, 1]
    
  • >>> f = Function(grid, [1, 0, 1])
    
  • >>> g = Function(grid, np.ones_like(grid))
    
  • >>> f.scal_prod(g)
    
  • 1.0

norm()[source]
Returns:
  • float – Norm of the function.
  • Example
  • >>> Function([-1, 0, 1], [2j, 0, 2j]).norm()
  • (4+0j)
class siegpy.functions.AnalyticFunction(grid=None)[source]

Bases: siegpy.functions.Function

Note

This is an abstract class. A child class must implement the _compute_values class. Examples of such child classes are:

The main change with respect to the siegpy.functions.Function class is that the grid is an optional parameter, so that, if it is modified, then the values of the function are updated accordingly.

Parameters:grid (list or set or numpy array) – Discretization grid (optional).
evaluate(grid)[source]

Wrapper for the _compute_values() to evaluate the analytic function either for a grid of points or a single point in the 1-dimensional space.

Parameters:grid (int or float or numpy array) – Discretization grid.
Returns:Values of the function for all the grid points.
Return type:float or complex or numpy array
_compute_values(grid)[source]

Note

This is an abstract method.

Compute the values of the function given a discretization grid that has been converted to a numpy array by the evaluate() method.

Parameters:grid (numpy array) – Discretization grid.
Returns:Values of the analytic function over the provided grid.
Return type:numpy array
grid

grid still is an attribute, but it is more powerful than for a Function instance: when the grid of an AnalyticFunction instance is updated, so are its values.

__add__(other)[source]
Parameters:other (Function, or any class inheriting from it) – Another Function.
Returns:Sum of an AnalyticFunction instance with another function. It requires that at least one of both functions has a grid that is not None.
Return type:Function
Raises:ValueError – If both functions have grids set to None.