From b566c643dbfa16b8851e0f5363e3ae35386cd19f Mon Sep 17 00:00:00 2001 From: Daniel Weschke Date: Sun, 27 Oct 2019 15:02:44 +0100 Subject: [PATCH] add mathematical functions module update docs and clean up code --- docs/build/html/_modules/data.html | 5 +- docs/build/html/_modules/date.html | 1 - docs/build/html/_modules/function.html | 199 ++++++++++++++++++ docs/build/html/_modules/index.html | 1 + docs/build/html/_modules/numerical/fit.html | 6 +- .../html/_modules/numerical/integration.html | 56 ++--- docs/build/html/_modules/time_of_day.html | 2 - docs/build/html/_sources/function.rst.txt | 7 + docs/build/html/_sources/modules.rst.txt | 1 + docs/build/html/function.html | 190 +++++++++++++++++ docs/build/html/genindex.html | 12 +- docs/build/html/modules.html | 1 + docs/build/html/numerical.html | 5 + docs/build/html/objects.inv | Bin 794 -> 828 bytes docs/build/html/py-modindex.html | 5 + docs/build/html/searchindex.js | 2 +- docs/source/function.rst | 7 + docs/source/modules.rst | 1 + pylib/function.py | 100 +++++++++ pylib/numerical/fit.py | 6 +- pylib/numerical/integration.py | 56 ++--- 21 files changed, 592 insertions(+), 71 deletions(-) create mode 100644 docs/build/html/_modules/function.html create mode 100644 docs/build/html/_sources/function.rst.txt create mode 100644 docs/build/html/function.html create mode 100644 docs/source/function.rst create mode 100644 pylib/function.py diff --git a/docs/build/html/_modules/data.html b/docs/build/html/_modules/data.html index f562ec7..3eb5069 100644 --- a/docs/build/html/_modules/data.html +++ b/docs/build/html/_modules/data.html @@ -45,7 +45,6 @@ .. moduleauthor:: Daniel Weschke <daniel.weschke@directbox.de> """ -from __future__ import print_function import math import pickle @@ -79,7 +78,6 @@ y = [] for row in file: fields = re.split(r'\s+', row.strip()) - #print(filds) x.append(float(fields[x_column])) y.append(float(fields[y_column])) file.close() @@ -192,7 +190,6 @@ exponent = max(start_exp, step_exp) # no stop because it is an open bound n = int(math.ceil((stop - start)/float(step))) - #n = int((stop - start)/float(step)) # in py3 math.ceil returns int lst = [] if n > 0: lst = [round(start + step*i, exponent) for i in range(n)] @@ -227,7 +224,7 @@ :returns: full id :rtype: str or int """ - # we know it is a unique ending + # take first element, because we know it is a unique ending return [idi for idi in ids if idi.endswith(uide)][0] diff --git a/docs/build/html/_modules/date.html b/docs/build/html/_modules/date.html index 54d8410..a4598bc 100644 --- a/docs/build/html/_modules/date.html +++ b/docs/build/html/_modules/date.html @@ -45,7 +45,6 @@ .. moduleauthor:: Daniel Weschke <daniel.weschke@directbox.de> """ -from __future__ import division, print_function, unicode_literals
[docs]def gaußsche_osterformel(year): """Gaußsche Osterformel. diff --git a/docs/build/html/_modules/function.html b/docs/build/html/_modules/function.html new file mode 100644 index 0000000..e4b29a0 --- /dev/null +++ b/docs/build/html/_modules/function.html @@ -0,0 +1,199 @@ + + + + + + + function — pylib 2019.5.19 documentation + + + + + + + + + + + + + + + + + + + + +
+
+
+ + +
+ +

Source code for function

+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+"""Mathematical functions.
+
+:Date: 2019-10-27
+
+.. module:: function
+  :platform: *nix, Windows
+  :synopsis: Mathematical functions.
+
+.. moduleauthor:: Daniel Weschke <daniel.weschke@directbox.de>
+"""
+import math
+
+
[docs]def sine_wave(A=1, k=1, f=1, phi=0, D=0, degree=False): + r"""A sine wave or sinusoid is a mathematical curve that describes a + smooth periodic oscillation. + + :param A: amplitude + :type A: float or int + :param k: (angular) wave number + :type k: float or int + :param f: ordinary frequency + :type f: float or int + :param phi: phase + :type phi: float or int + :param D: non-zero center amplitude + :type D: float or int + :param degree: boolean to switch between radians and degree. If + False phi is interpreted in radians and if True then phi is + interpreted in degrees. + + In general, the function is: + + .. math:: + y(x,t) = A\sin(kx + 2\pi f t + \varphi) + D \\ + y(x,t) = A\sin(kx + \omega t + \varphi) + D + + where: + + * A, amplitude, the peak deviation of the function from zero. + * f, ordinary frequency, the number of oscillations (cycles) that + occur each second of time. + * ω = 2πf, angular frequency, the rate of change of the function + argument in units of radians per second. If ω < 0 the wave is + moving to the right, if ω > 0 the wave is moving to the left. + * φ, phase, specifies (in radians) where in its cycle the + oscillation is at t = 0. + * x, spatial variable that represents the position on the + dimension on which the wave propagates. + * k, characteristic parameter called wave number (or angular wave + number), which represents the proportionality between the + angular frequency ω and the linear speed (speed of propagation) + ν. + * D, non-zero center amplitude. + + The wavenumber is related to the angular frequency by: + + .. math:: + k={\omega \over v}={2\pi f \over v}={2\pi \over \lambda } + + where λ (lambda) is the wavelength, f is the frequency, and v is the + linear speed. + + .. seealso:: + :meth:`function_cosine_wave_degree` + """ + if degree: + phi = math.radians(phi) + return lambda x, t=0: A*math.sin(k*x + 2*math.pi*f*t + phi) + D
+ +
[docs]def cosine_wave(A=1, k=1, f=1, phi=0, D=0, degree=False): + r"""A cosine wave is said to be sinusoidal, because, + :math:`\cos(x) = \sin(x + \pi/2)`, which is also a sine wave with a + phase-shift of π/2 radians. Because of this head start, it is often + said that the cosine function leads the sine function or the sine + lags the cosine. + + :param A: amplitude + :type A: float or int + :param k: (angular) wave number + :type k: float or int + :param f: ordinary frequency + :type f: float or int + :param phi: phase + :type phi: float or int + :param D: non-zero center amplitude + :type D: float or int + :param degree: boolean to switch between radians and degree. If + False phi is interpreted in radians and if True then phi is + interpreted in degrees. + + .. seealso:: + :meth:`function_sine_wave_degree` + """ + if degree: + phi = phi + 90 + else: + phi = phi + math.pi/2 + return sine_wave(A=A, k=k, f=f, phi=phi, D=D, degree=degree)
+
+ +
+ +
+
+ +
+
+ + + + + + + \ No newline at end of file diff --git a/docs/build/html/_modules/index.html b/docs/build/html/_modules/index.html index 1c1bbd2..a67f9f9 100644 --- a/docs/build/html/_modules/index.html +++ b/docs/build/html/_modules/index.html @@ -35,6 +35,7 @@

All modules for which code is available

if __name__ == "__main__": - True + pass diff --git a/docs/build/html/_modules/numerical/integration.html b/docs/build/html/_modules/numerical/integration.html index 787ca4b..cb6b130 100644 --- a/docs/build/html/_modules/numerical/integration.html +++ b/docs/build/html/_modules/numerical/integration.html @@ -51,7 +51,7 @@ from numpy import linspace, trapz, zeros
[docs]def trapez(f, a=0, b=1, N=10, x=None, verbose=False, - save_values=False): + save_values=False): r""" Integration of :math:`f(x)` using the trapezoidal rule (Simpson's rule, Kepler's rule). @@ -122,14 +122,14 @@ f(x) &= x^2 \\ a &= 0 \\ b &= 1 - + analytical solution .. math:: I = \int\limits_{0}^{1} x^2 \,\mathrm{d}x = \left. \frac{1}{3} x^3 \right\vert_0^1 = \frac{1}{3} - + numerical solution >>> f = lambda(x): x**2 @@ -142,30 +142,30 @@ """ N = int(N) # f is function or list - if hasattr(f, '__call__'): - # h width of each subinterval - h = (b-a)/N + if callable(f): + # h width of each subinterval + h = (b-a)/N - # x variable of integration - x = linspace(a, b, N+1) - if save_values: - # ff contribution from the points - ff = zeros((N+1)) - for n in linspace(0, N, N+1): - ff[n] = f(x[n]) - T = (ff[0]/2.+sum(ff[1:N])+ff[N]/2.)*h - else: - TL = f(x[0]) - TR = f(x[N]) - TI = 0 - for n in range(1, N): - TI = TI + f(x[n]) - T = (TL/2.+TI+TR/2.)*h + # x variable of integration + x = linspace(a, b, N+1) + if save_values: + # ff contribution from the points + ff = zeros((N+1)) + for n in linspace(0, N, N+1): + ff[n] = f(x[n]) + T = (ff[0]/2.+sum(ff[1:N])+ff[N]/2.)*h + else: + TL = f(x[0]) + TR = f(x[N]) + TI = 0 + for n in range(1, N): + TI = TI + f(x[n]) + T = (TL/2.+TI+TR/2.)*h else: - N = len(f)-1 - T = 0 - for n in range(N): - T = T + (x[n+1]-x[n])/2*(f[n+1]+f[n]) + N = len(f)-1 + T = 0 + for n in range(N): + T = T + (x[n+1]-x[n])/2*(f[n+1]+f[n]) if verbose: print(T) @@ -178,12 +178,12 @@ trapez(func, 0, 1, 1e6, verbose=True) #print(trapz(func, linspace(0,1,10))) - trapez([0,1,4,9], x=[0,1,2,3], verbose=True) + trapez([0, 1, 4, 9], x=[0, 1, 2, 3], verbose=True) #print(trapz([0,1,4,9])) - trapez([2,2], x=[-1,1], verbose=True) + trapez([2, 2], x=[-1, 1], verbose=True) - trapez([-1,1,1,-1], x=[-1,-1,1,1], verbose=True) + trapez([-1, 1, 1, -1], x=[-1, -1, 1, 1], verbose=True)
diff --git a/docs/build/html/_modules/time_of_day.html b/docs/build/html/_modules/time_of_day.html index 6a35136..7074044 100644 --- a/docs/build/html/_modules/time_of_day.html +++ b/docs/build/html/_modules/time_of_day.html @@ -45,10 +45,8 @@ .. moduleauthor:: Daniel Weschke <daniel.weschke@directbox.de> """ -from __future__ import division, print_function, unicode_literals from time import struct_time, mktime -
[docs]def in_seconds(time): """If time is `time.struct_time` convert to float seconds. diff --git a/docs/build/html/_sources/function.rst.txt b/docs/build/html/_sources/function.rst.txt new file mode 100644 index 0000000..0e0123f --- /dev/null +++ b/docs/build/html/_sources/function.rst.txt @@ -0,0 +1,7 @@ +function module +=============== + +.. automodule:: function + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/build/html/_sources/modules.rst.txt b/docs/build/html/_sources/modules.rst.txt index cc2cd40..31ce243 100644 --- a/docs/build/html/_sources/modules.rst.txt +++ b/docs/build/html/_sources/modules.rst.txt @@ -6,6 +6,7 @@ pylib data date + function geometry geometry_plot numerical diff --git a/docs/build/html/function.html b/docs/build/html/function.html new file mode 100644 index 0000000..f68fc8a --- /dev/null +++ b/docs/build/html/function.html @@ -0,0 +1,190 @@ + + + + + + + function module — pylib 2019.5.19 documentation + + + + + + + + + + + + + + + + + + + + +
+
+
+ + +
+ +
+

function module

+

Mathematical functions.

+
+
Date
+

2019-10-27

+
+
+
+
+cosine_wave(A=1, k=1, f=1, phi=0, D=0, degree=False)[source]
+

A cosine wave is said to be sinusoidal, because, +\(\cos(x) = \sin(x + \pi/2)\), which is also a sine wave with a +phase-shift of π/2 radians. Because of this head start, it is often +said that the cosine function leads the sine function or the sine +lags the cosine.

+
+
Parameters
+
    +
  • A (float or int) – amplitude

  • +
  • k (float or int) – (angular) wave number

  • +
  • f (float or int) – ordinary frequency

  • +
  • phi (float or int) – phase

  • +
  • D (float or int) – non-zero center amplitude

  • +
  • degree – boolean to switch between radians and degree. If +False phi is interpreted in radians and if True then phi is +interpreted in degrees.

  • +
+
+
+
+

See also

+

function_sine_wave_degree()

+
+
+ +
+
+sine_wave(A=1, k=1, f=1, phi=0, D=0, degree=False)[source]
+

A sine wave or sinusoid is a mathematical curve that describes a +smooth periodic oscillation.

+
+
Parameters
+
    +
  • A (float or int) – amplitude

  • +
  • k (float or int) – (angular) wave number

  • +
  • f (float or int) – ordinary frequency

  • +
  • phi (float or int) – phase

  • +
  • D (float or int) – non-zero center amplitude

  • +
  • degree – boolean to switch between radians and degree. If +False phi is interpreted in radians and if True then phi is +interpreted in degrees.

  • +
+
+
+

In general, the function is:

+
+\[\begin{split}y(x,t) = A\sin(kx + 2\pi f t + \varphi) + D \\ +y(x,t) = A\sin(kx + \omega t + \varphi) + D\end{split}\]
+

where:

+
+
    +
  • A, amplitude, the peak deviation of the function from zero.

  • +
  • f, ordinary frequency, the number of oscillations (cycles) that +occur each second of time.

  • +
  • ω = 2πf, angular frequency, the rate of change of the function +argument in units of radians per second. If ω < 0 the wave is +moving to the right, if ω > 0 the wave is moving to the left.

  • +
  • φ, phase, specifies (in radians) where in its cycle the +oscillation is at t = 0.

  • +
  • x, spatial variable that represents the position on the +dimension on which the wave propagates.

  • +
  • k, characteristic parameter called wave number (or angular wave +number), which represents the proportionality between the +angular frequency ω and the linear speed (speed of propagation) +ν.

  • +
  • D, non-zero center amplitude.

  • +
+
+

The wavenumber is related to the angular frequency by:

+
+\[k={\omega \over v}={2\pi f \over v}={2\pi \over \lambda }\]
+

where λ (lambda) is the wavelength, f is the frequency, and v is the +linear speed.

+
+

See also

+

function_cosine_wave_degree()

+
+
+ +
+ + +
+ +
+
+ +
+
+ + + + + + + \ No newline at end of file diff --git a/docs/build/html/genindex.html b/docs/build/html/genindex.html index 9eff453..7a8d0aa 100644 --- a/docs/build/html/genindex.html +++ b/docs/build/html/genindex.html @@ -72,6 +72,8 @@

C

@@ -132,11 +134,13 @@ - +
@@ -287,10 +291,12 @@
  • seconds() (in module time_of_day)
  • seconds_norm() (in module time_of_day) +
  • +
  • seq() (in module data)