Source code for function

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Mathematical equations.

:Date: 2019-11-02

.. module:: function
  :platform: *nix, Windows
  :synopsis: Mathematical equations.

.. moduleauthor:: Daniel Weschke <daniel.weschke@directbox.de>
"""
import math
from pylib.mathematics import lcm

[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. :type degree: bool :results: sine wave function of spatial variable x and optional time t 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. :type degree: bool :results: sine wave function of spatial variable x and optional time t .. 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)
# # Parametric equations # roulette #
[docs]def hypotrochoid(R, r, d): r"""Hypotrochoid A point is attached with a distance d from the center of a circle of radius r. The circle is rolling around the inside of a fixed circle of radius R. :param R: radius of the fixed exterior circle :type R: float :param r: radius of the rolling circle inside of the fixed circle :typre r: float :param d: distance from the center of the interior circle :type d: float :results: functions for x of theta and y of theta :rtype: tuple .. math:: x(\theta) = (R - r)\cos\theta + d\cos\left(\frac{R-r}{r}\theta\right) \\ y(\theta) = (R - r)\sin\theta - d\sin\left(\frac{R-r}{r}\theta\right) \\ \theta = \left[0, 2\pi\frac{\mathrm{lcm}(r, R)}{R}\right] :: * * * * R * * * * * * * * * r ** * * .... * * * d * * * ** * * * * * * * * * * * >>> x, y = hyotrochoid(20, 6, 6)[:1] >>> x, y, theta_end = hyotrochoid(20, 6, 6) .. seealso:: :meth:`mathematics.lcm` """ x = lambda theta: (R-r)*math.cos(theta) + d*math.cos((R-r)/r * theta) y = lambda theta: (R-r)*math.sin(theta) - d*math.sin((R-r)/r * theta) theta_end = 2*math.pi*lcm(r, R)/R return x, y, theta_end
[docs]def epitrochoid(R, r, d): r"""Epitrochoid A point is attached with a distance d from the center of a circle of radius r. The circle is rolling around the outside of a fixed circle of radius R. :param R: radius of the fixed interior circle :type R: float :param r: radius of the rolling circle outside of the fixed circle :typre r: float :param d: distance from the center of the exterior circle :type d: float :results: functions for x of theta and y of theta :rtype: tuple .. math:: x(\theta) = (R + r)\cos\theta - d\cos\left(\frac{R+r}{r}\theta\right) \\ y(\theta) = (R + r)\sin\theta - d\sin\left(\frac{R+r}{r}\theta\right) \\ \theta = \left[0, 2\pi\right] :: * * * * R * * * * * * * * * * r * * ** .... * * ** d * * * * * * * * * * * * * * * * >>> x, y = epitrochoid(3, 1, 0.5)[:1] >>> x, y, theta_end = epitrochoid(3, 1, 0.5) """ x = lambda theta: (R+r)*math.cos(theta) - d*math.cos((R+r)/r * theta) y = lambda theta: (R+r)*math.sin(theta) - d*math.sin((R+r)/r * theta) theta_end = 2*math.pi return x, y, theta_end