<commit_before>import scipy as sp
import scipy.fftpack as fftp
import scipy.linalg as la
# from scipy import pi, sin,cos
# import matplotlib.pyplot as plt

__all__ = ["mousai_so",
           "harmonic_deriv",
           "somf"]


def mousai_so(sdfunc, x0, omega, method, *kwargs, num_harmonics=1):
    r"""Harmonic balance solver for second order ODEs.

    Obtains the solution of a second order differential equation under the
    presumption that the solution is harmonic.

    Returns t (time), x (displacement), v (velocity), and a (acceletation)
    response of a second order linear ordinary differential
    equation defined by
    :math:`\ddot{\mathbf{x}}=f(\mathbf{x},\mathbf{v},\omega)`.

    Parameters
    ----------
    sdfunc: str
        name of function that returns second derivative given omega and
        \*kwargs
        :math:`\ddot{\mathbf{x}}=f(\mathbf{x},\mathbf{v},\omega)`
    omega:  float
        assumed fundamental response frequency.
    num_harmonics: int
        number of harmonics to presume. Constant term is always presumed.
    x0: ndarray
        n x m array where n is the number of equations and m is the number of
        values representing the repeating solution.
        It is required that :math:`m = 1 + 2  num_{harmonics}`.
    method: str
        Name of optimization method to be used.

    Returns
    -------
    t, x, v, a : ndarrays
    """

    '''
    a) define a function that returns errors in time domain as vector
    b) define function to obtain velocity and accelerations from displacement
    and frequencies.
    c)
    '''

    return


def harmonic_deriv(omega, r):
    """Derivative of a harmonic function using frequency methods.

    Returns the derivatives of a harmonic function

    Parameters
    ----------
    omega: float
        Fundamendal frequency, in rad/sec, of repeating signal
    r: array
        | Array of rows of time histories to take the derivative of.
        | The 1 axis (each row) corresponds to a time history.
        | The length of the time histories *must be an odd integer*.

    Returns
    -------
    s: array
        array of function derivatives.
        The 1 axis (each row) corresponds to a time history.

    Notes
    -----
    At this time, the length of the time histories *must be an odd integer*.

    Examples
    --------
    >>> import matplotlib.pyplot as plt
    >>> from har_bal import *
    >>> import scipy as sp
    >>> from scipy import pi,sin,cos
    >>> f = 2
    >>> omega = 2.*pi * f
    >>> numsteps = 11
    >>> t = sp.arange(0,1/omega*2*pi,1/omega*2*pi/numsteps)
    >>> x = sp.array([sin(omega*t)])
    >>> v = sp.array([omega*cos(omega*t)])
    >>> states = sp.append(x,v,axis = 0)
    >>> state_derives = harmonic_deriv(omega,states)
    >>> plt.plot(t,states.T,t,state_derives.T,'x')
    [<matplotlib.line...]
    """

    n = r.shape[1]
    omega_half = -sp.arange((n-1)/2+1) * omega * 2j/(n-2)
    omega_whole = sp.append(sp.conj(omega_half[-1:0:-1]), omega_half)
    r_freq = fftp.fft(r)
    s_freq = r_freq * omega_whole
    s = fftp.ifft(s_freq)
    return sp.real(s)


def somf(x, v, M, C, K, F):
    """Acceleration of second order linear matrix system.

    Parameters
    ----------
    x, v, F : arrays
        :math:`n\\times 1` arrays of current displacement, velocity, and Force.
    M, C, K : arrays
        Mass, damping, and stiffness matrices.

    Returns
    -------
    a : array
        :math:`n\\times 1` acceleration vector

    Examples
    --------
    >>> import scipy as sp
    >>> M = sp.array([[2,0],[0,1]])
    >>> K = sp.array([[2,-1],[-1,3]])
    >>> C = 0.01 * M + 0.01 * K
    >>> x = sp.array([[1],[0]])
    >>> v = sp.array([[0],[10]])
    >>> F = v * 0.1
    >>> a = somf(x, v, M, C, K, F)
    >>> print(a)
        [[-0.95]
         [ 1.6 ]]
    """

    return -la.solve(M, C @ v + K @ x - F)


def hb_so_err(x, **kwargs):
    """Array (vector) of hamonic balance second order algebraic errors.

    Given a set of second order equations<commit_msg>fix to may pytest work<commit_after>import scipy as sp
import scipy.fftpack as fftp
import scipy.linalg as la
# from scipy import pi, sin,cos
# import matplotlib.pyplot as plt

__all__ = ["hb_so",
           "harmonic_deriv",
           "solmf"]


def hb_so(sdfunc, x0, omega, method, *kwargs, num_harmonics=1):
    r"""Harmonic balance solver for second order ODEs.

    Obtains the solution of a second order differential equation under the
    presumption that the solution is harmonic.

    Returns t (time), x (displacement), v (velocity), and a (acceleration)
    response of a second order linear ordinary differential
    equation defined by
    :math:`\ddot{\mathbf{x}}=f(\mathbf{x},\mathbf{v},\omega)`.

    Parameters
    ----------
    sdfunc: str
        name of function that returns second derivative given omega and
        \*kwargs
        :math:`\ddot{\mathbf{x}}=f(\mathbf{x},\mathbf{v},\omega)`
    omega:  float
        assumed fundamental response frequency.
    num_harmonics: int
        number of harmonics to presume. Constant term is always presumed.
    x0: ndarray
        n x m array where n is the number of equations and m is the number of
        values representing the repeating solution.
        It is required that :math:`m = 1 + 2  num_{harmonics}`.
    method: str
        Name of optimization method to be used.

    Returns
    -------
    t, x, v, a : ndarrays
    """

    '''
    a) define a function that returns errors in time domain as vector
    b) define function to obtain velocity and accelerations from displacement
    and frequencies.
    c)
    '''

    return


def harmonic_deriv(omega, r):
    """Derivative of a harmonic function using frequency methods.

    Returns the derivatives of a harmonic function

    Parameters
    ----------
    omega: float
        Fundamendal frequency, in rad/sec, of repeating signal
    r: array
        | Array of rows of time histories to take the derivative of.
        | The 1 axis (each row) corresponds to a time history.
        | The length of the time histories *must be an odd integer*.

    Returns
    -------
    s: array
        array of function derivatives.
        The 1 axis (each row) corresponds to a time history.

    Notes
    -----
    At this time, the length of the time histories *must be an odd integer*.

    Examples
    --------
    >>> import matplotlib.pyplot as plt
    >>> from mousai import *
    >>> import scipy as sp
    >>> from scipy import pi,sin,cos
    >>> f = 2
    >>> omega = 2.*pi * f
    >>> numsteps = 11
    >>> t = sp.arange(0,1/omega*2*pi,1/omega*2*pi/numsteps)
    >>> x = sp.array([sin(omega*t)])
    >>> v = sp.array([omega*cos(omega*t)])
    >>> states = sp.append(x,v,axis = 0)
    >>> state_derives = harmonic_deriv(omega,states)
    >>> plt.plot(t,states.T,t,state_derives.T,'x')
    [<matplotlib.line...]
    """

    n = r.shape[1]
    omega_half = -sp.arange((n-1)/2+1) * omega * 2j/(n-2)
    omega_whole = sp.append(sp.conj(omega_half[-1:0:-1]), omega_half)
    r_freq = fftp.fft(r)
    s_freq = r_freq * omega_whole
    s = fftp.ifft(s_freq)
    return sp.real(s)


def solmf(x, v, M, C, K, F):
    """Acceleration of second order linear matrix system.

    Parameters
    ----------
    x, v, F : arrays
        :math:`n\\times 1` arrays of current displacement, velocity, and Force.
    M, C, K : arrays
        Mass, damping, and stiffness matrices.

    Returns
    -------
    a : array
        :math:`n\\times 1` acceleration vector

    Examples
    --------
    >>> import scipy as sp
    >>> M = sp.array([[2,0],[0,1]])
    >>> K = sp.array([[2,-1],[-1,3]])
    >>> C = 0.01 * M + 0.01 * K
    >>> x = sp.array([[1],[0]])
    >>> v = sp.array([[0],[10]])
    >>> F = v * 0.1
    >>> a = solmf(x, v, M, C, K, F)
    >>> print(a)
        [[-0.95]
         [ 1.6 ]]
    """

    return -la.solve(M, C @ v + K @ x - F)


def hb_so_err(x, **kwargs):
    """Array (vector) of hamonic balance second order algebraic errors.

    Given a set of second order equations
{'fraction_non_alphanumeric': 0.13911315364550988, 'fraction_numerical': 0.01341448267295988, 'mean_word_length': 2.2905598692276254, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 135, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2437649', 'n_tokens_mistral': 2724, 'n_tokens_neox': 2551, 'n_words': 869}