Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
r""" Spike Functions
AUTHORS:
- William Stein (2007-07): initial version
- Karl-Dieter Crisman (2009-09): adding documentation and doctests """ #***************************************************************************** # Copyright (C) 2007 William Stein <wstein@gmail.com> # Copyright (C) 2009 Karl-Dieter Crisman <kcrisman@gmail.com> # # Distributed under the terms of the GNU General Public License (GPL) # as published by the Free Software Foundation; either version 2 of # the License, or (at your option) any later version. # http://www.gnu.org/licenses/ #***************************************************************************** from __future__ import print_function
import math
from sage.plot.all import line from sage.modules.free_module_element import vector from sage.rings.all import RDF
class SpikeFunction: """ Base class for spike functions.
INPUT:
- ``v`` - list of pairs (x, height)
- ``eps`` - parameter that determines approximation to a true spike
OUTPUT:
a function with spikes at each point ``x`` in ``v`` with the given height.
EXAMPLES::
sage: spike_function([(-3,4),(-1,1),(2,3)],0.001) A spike function with spikes at [-3.0, -1.0, 2.0]
Putting the spikes too close together may delete some::
sage: spike_function([(1,1),(1.01,4)],0.1) Some overlapping spikes have been deleted. You might want to use a smaller value for eps. A spike function with spikes at [1.0]
Note this should normally be used indirectly via ``spike_function``, but one can use it directly::
sage: from sage.functions.spike_function import SpikeFunction sage: S = SpikeFunction([(0,1),(1,2),(pi,-5)]) sage: S A spike function with spikes at [0.0, 1.0, 3.141592653589793] sage: S.support [0.0, 1.0, 3.141592653589793] """ def __init__(self, v, eps=0.0000001): """ Initializes base class SpikeFunction.
EXAMPLES::
sage: S = spike_function([(-3,4),(-1,1),(2,3)],0.001); S A spike function with spikes at [-3.0, -1.0, 2.0] sage: S.height [4.0, 1.0, 3.0] sage: S.eps 0.00100000000000000 """ v = [(0,0)]
def __repr__(self): """ String representation of a spike function.
EXAMPLES::
sage: spike_function([(-3,4),(-1,1),(2,3)],0.001) A spike function with spikes at [-3.0, -1.0, 2.0] """
def _eval(self, x): """ Evaluates spike function. Note that when one calls the function within the tolerance, the return value is the full height at that point.
EXAMPLES::
sage: S = spike_function([(0,5)],eps=.001) sage: S(0) 5.0 sage: S(.1) 0.0 sage: S(.01) 0.0 sage: S(.001) 5.0 """
def __call__(self, x): """ Called when spike function is used as callable function.
EXAMPLES::
sage: S = spike_function([(0,5)],eps=.001) sage: S(0) 5.0 sage: S(.1) 0.0 sage: S(.01) 0.0 sage: S(.001) 5.0 """
def plot_fft_abs(self, samples=2**12, xmin=None, xmax=None, **kwds): """ Plot of (absolute values of) Fast Fourier Transform of the spike function with given number of samples.
EXAMPLES::
sage: S = spike_function([(-3,4),(-1,1),(2,3)]); S A spike function with spikes at [-3.0, -1.0, 2.0] sage: P = S.plot_fft_abs(8) sage: p = P[0]; p.ydata [5.0, 5.0, 3.367958691924177, 3.367958691924177, 4.123105625617661, 4.123105625617661, 4.759921664218055, 4.759921664218055] """
def plot_fft_arg(self, samples=2**12, xmin=None, xmax=None, **kwds): """ Plot of (absolute values of) Fast Fourier Transform of the spike function with given number of samples.
EXAMPLES::
sage: S = spike_function([(-3,4),(-1,1),(2,3)]); S A spike function with spikes at [-3.0, -1.0, 2.0] sage: P = S.plot_fft_arg(8) sage: p = P[0]; p.ydata [0.0, 0.0, -0.211524990023434..., -0.211524990023434..., 0.244978663126864..., 0.244978663126864..., -0.149106180027477..., -0.149106180027477...] """
def vector(self, samples=2**16, xmin=None, xmax=None): """ Creates a sampling vector of the spike function in question.
EXAMPLES::
sage: S = spike_function([(-3,4),(-1,1),(2,3)],0.001); S A spike function with spikes at [-3.0, -1.0, 2.0] sage: S.vector(16) (4.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0) """
def _ranges(self, xmin, xmax): """ Quickly find appropriate plotting interval.
EXAMPLES::
sage: S = spike_function([(-1,1),(1,40)]) sage: S._ranges(None,None) (-1.0, 1.0) """ xmax = xmin + 1
def plot(self, xmin=None, xmax=None, **kwds): """ Special fast plot method for spike functions.
EXAMPLES::
sage: S = spike_function([(-1,1),(1,40)]) sage: P = plot(S) sage: P[0] Line defined by 8 points """ else: else: new_x = None for j in range(len(self.support)): if self.support[j] - eps > x: new_x = self.support[j] - eps break if new_x is None: new_x = xmax v.append( (new_x, 0) ) x = new_x
spike_function = SpikeFunction |