文件操作 - _finite_differences.cpython-311.pyc
返回文件管理
返回主菜单
删除本文件
文件: /usr/lib/python3/dist-packages/scipy/_lib/__pycache__/_finite_differences.cpython-311.pyc
编辑文件内容
� d�cL � �2 � d dl mZmZmZmZmZ d d�Zd d�ZdS )� )�arange�newaxis�hstack�prod�array� c � � | |dz k rt d� � �| dz dk rt d� � �ddlm} | dz }t | |dz � � }|dd�t f }|d z }t d| � � D ]}t |||z g� � }�t t d|dz � � d� � � |� |� � | z }|S )a� Return weights for an Np-point central derivative. Assumes equally-spaced function points. If weights are in the vector w, then derivative is w[0] * f(x-ho*dx) + ... + w[-1] * f(x+h0*dx) Parameters ---------- Np : int Number of points for the central derivative. ndiv : int, optional Number of divisions. Default is 1. Returns ------- w : ndarray Weights for an Np-point central derivative. Its size is `Np`. Notes ----- Can be inaccurate for a large number of points. Examples -------- We can calculate a derivative value of a function. >>> def f(x): ... return 2 * x**2 + 3 >>> x = 3.0 # derivative point >>> h = 0.1 # differential step >>> Np = 3 # point number for central derivative >>> weights = _central_diff_weights(Np) # weights for first derivative >>> vals = [f(x + (i - Np/2) * h) for i in range(Np)] >>> sum(w * v for (w, v) in zip(weights, vals))/h 11.79999999999998 This value is close to the analytical solution: f'(x) = 4x, so f'(3) = 12 References ---------- .. [1] https://en.wikipedia.org/wiki/Finite_difference r z;Number of points must be at least the derivative order + 1.� r z!The number of points must be odd.)�linalg� �?N� ��axis) � ValueError�scipyr r r �ranger r �inv)�Np�ndivr �ho�x�X�k�ws �@/usr/lib/python3/dist-packages/scipy/_lib/_finite_differences.py�_central_diff_weightsr s� � �^ �D�1�H�}�}��I� � � � �A�v��{�{��<�=�=�=������� �q��B���s�B��H���A� �!�!�!�W�*� �A� �3��A� �1�b�\�\� � ���A�q�!�t�9������V�A�t�a�x� � �q�)�)�)�F�J�J�q�M�M�$�,?�?�A��H� r � � c �& � ||dz k rt d� � �|dz dk rt d� � �|dk r}|dk rt g d�� � dz }n�|d k rt g d �� � dz }n�|dk rt g d �� � dz }n�|dk rt g d�� � dz }n�t |d� � }n�|dk rz|dk rt g d�� � }nr|d k rt g d�� � dz }nW|dk rt g d�� � dz }n<|dk rt g d�� � dz }n!t |d� � }nt ||� � }d}|dz }t |� � D ] } ||| | || |z |z z g|�R � z z }�!|t |f|z d�� � z S )a Find the nth derivative of a function at a point. Given a function, use a central difference formula with spacing `dx` to compute the nth derivative at `x0`. Parameters ---------- func : function Input function. x0 : float The point at which the nth derivative is found. dx : float, optional Spacing. n : int, optional Order of the derivative. Default is 1. args : tuple, optional Arguments order : int, optional Number of points to use, must be odd. Notes ----- Decreasing the step size too small can result in round-off error. Examples -------- >>> def f(x): ... return x**3 + x**2 >>> _derivative(f, 1.0, dx=1e-6) 4.9999999999217337 r zm'order' (the number of points used to compute the derivative), must be at least the derivative order 'n' + 1.r r zJ'order' (the number of points used to compute the derivative) must be odd.r )���r r g @� )r i����r � r! g (@� )r! � i���r �- ���r g N@r% ) r i��� i`���r i� iX���� ���g @�@)r g �r )r! � i��r+ r! )r ���� i���r- r, r g �f@) r'