Skip to content

Commit

Permalink
include signals documentation; remove duplicate docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrik-johansson committed Nov 7, 2022
1 parent 1b476ea commit b7c15d6
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 136 deletions.
1 change: 1 addition & 0 deletions docs/functions/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Mpmath implements the standard functions from Python's ``math`` and ``cmath`` mo
powers
trigonometric
hyperbolic
signals
gamma
expintegrals
bessel
Expand Down
12 changes: 6 additions & 6 deletions mpmath/function_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -10069,7 +10069,7 @@
Computes the square wave function using the definition:
.. math::
x(t) = A(-1)^{\left\lfloor{\frac{2t}{P}}\right\rfloor}
x(t) = A(-1)^{\left\lfloor{2t / P}\right\rfloor}
where `P` is the period of the wave and `A` is the amplitude.
Expand All @@ -10095,9 +10095,9 @@
Computes the triangle wave function using the definition:
.. math::
x(t) = 2A\left(\frac{1}{2}-\left|1-2frac\left(\frac{x}{P}+\frac{1}{4}\right)\right|\right)
x(t) = 2A\left(\frac{1}{2}-\left|1-2 \operatorname{frac}\left(\frac{x}{P}+\frac{1}{4}\right)\right|\right)
where :math:`frac\left(\frac{t}{T}\right) = \frac{t}{T}-\left\lfloor{\frac{t}{T}}\right\rfloor`
where :math:`\operatorname{frac}\left(\frac{t}{T}\right) = \frac{t}{T}-\left\lfloor{\frac{t}{T}}\right\rfloor`
, `P` is the period of the wave, and `A` is the amplitude.
**Examples**
Expand All @@ -10124,10 +10124,10 @@
Computes the sawtooth wave function using the definition:
.. math::
x(t) = Afrac\left(\frac{t}{T}\right)
x(t) = A\operatorname{frac}\left(\frac{t}{T}\right)
where :math:`frac\left(\frac{t}{T}\right) = \frac{t}{T}-\left\lfloor{\frac{t}{T}}\right\rfloor`
, `P` is the period of the wave, and `A` is the amplitude.
where :math:`\operatorname{frac}\left(\frac{t}{T}\right) = \frac{t}{T}-\left\lfloor{\frac{t}{T}}\right\rfloor`,
`P` is the period of the wave, and `A` is the amplitude.
**Examples**
Expand Down
130 changes: 0 additions & 130 deletions mpmath/functions/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,161 +2,31 @@

@defun_wrapped
def squarew(ctx, t, amplitude=1, period=1):
r"""
Computes the square wave function using the definition:
.. math::
x(t) = A(-1)^{\left\lfloor{\frac{2t}{P}}\right\rfloor}
where `P` is the period of the wave and `A` is the amplitude.
**Examples**
Square wave with period = 2, amplitude = 1 ::
>>> from mpmath import *
>>> mp.dps = 25; mp.pretty = True
>>> squarew(0,1,2)
1.0
>>> squarew(0.5,1,2)
1.0
>>> squarew(1,1,2)
-1.0
>>> squarew(1.5,1,2)
-1.0
>>> squarew(2,1,2)
1.0
"""
P = period
A = amplitude
return A*((-1)**ctx.floor(2*t/P))

@defun_wrapped
def trianglew(ctx, t, amplitude=1, period=1):
r"""
Computes the triangle wave function using the definition:
.. math::
x(t) = 2A\left(\frac{1}{2}-\left|1-2frac\left(\frac{x}{P}+\frac{1}{4}\right)\right|\right)
where :math:`frac\left(\frac{t}{T}\right) = \frac{t}{T}-\left\lfloor{\frac{t}{T}}\right\rfloor`
, `P` is the period of the wave, and `A` is the amplitude.
**Examples**
Triangle wave with period = 2, amplitude = 1 ::
>>> from mpmath import *
>>> mp.dps = 25; mp.pretty = True
>>> trianglew(0,1,2)
0.0
>>> trianglew(0.25,1,2)
0.5
>>> trianglew(0.5,1,2)
1.0
>>> trianglew(1,1,2)
0.0
>>> trianglew(1.5,1,2)
-1.0
>>> trianglew(2,1,2)
0.0
"""
A = amplitude
P = period

return 2*A*(0.5 - ctx.fabs(1 - 2*ctx.frac(t/P + 0.25)))

@defun_wrapped
def sawtoothw(ctx, t, amplitude=1, period=1):
r"""
Computes the sawtooth wave function using the definition:
.. math::
x(t) = Afrac\left(\frac{t}{T}\right)
where :math:`frac\left(\frac{t}{T}\right) = \frac{t}{T}-\left\lfloor{\frac{t}{T}}\right\rfloor`
, `P` is the period of the wave, and `A` is the amplitude.
**Examples**
Sawtooth wave with period = 2, amplitude = 1 ::
>>> from mpmath import *
>>> mp.dps = 25; mp.pretty = True
>>> sawtoothw(0,1,2)
0.0
>>> sawtoothw(0.5,1,2)
0.25
>>> sawtoothw(1,1,2)
0.5
>>> sawtoothw(1.5,1,2)
0.75
>>> sawtoothw(2,1,2)
0.0
"""
A = amplitude
P = period
return A*ctx.frac(t/P)

@defun_wrapped
def unit_triangle(t, amplitude=1):
r"""
Computes the unit triangle using the definition:
.. math::
x(t) = A(-\left| t \right| + 1)
where `A` is the amplitude.
**Examples**
Unit triangle with amplitude = 1 ::
>>> from mpmath import *
>>> mp.dps = 25; mp.pretty = True
>>> unit_triangle(-1,1)
0
>>> unit_triangle(-0.5,1)
0.5
>>> unit_triangle(0,1)
1
>>> unit_triangle(0.5,1)
0.5
>>> unit_triangle(1,1)
0
"""
A = amplitude
if t <= -1 or t >= 1:
return ctx.zero
return A*(-abs(t) + 1)

@defun_wrapped
def sigmoid(ctx, t, amplitude=1):
r"""
Computes the sigmoid function using the definition:
.. math::
x(t) = \frac{A}{1 + e^{-t}}
where `A` is the amplitude.
**Examples**
Sigmoid function with amplitude = 1 ::
>>> from mpmath import *
>>> mp.dps = 25; mp.pretty = True
>>> sigmoid(-1,1)
0.2689414213699951207488408
>>> sigmoid(-0.5,1)
0.3775406687981454353610994
>>> sigmoid(0,1)
0.5
>>> sigmoid(0.5,1)
0.6224593312018545646389006
>>> sigmoid(1,1)
0.7310585786300048792511592
"""
A = amplitude
return A / (1 + ctx.exp(-t))

0 comments on commit b7c15d6

Please sign in to comment.