### BasicMaxStepGenerator Example Source: https://github.com/pbrod/numdifftools/blob/master/docs/reference/numdifftools.md Demonstrates how to create and iterate over a BasicMaxStepGenerator to get a sequence of decreasing steps. ```python >>> from numdifftools.step_generators import BasicMaxStepGenerator >>> step_gen = BasicMaxStepGenerator(base_step=2.0, step_ratio=2, ... num_steps=4) >>> [s for s in step_gen()] [2.0, 1.0, 0.5, 0.25] ``` -------------------------------- ### LogRule Apply Method Example Source: https://github.com/pbrod/numdifftools/blob/master/docs/reference/numdifftools.md Demonstrates applying the LogRule with the forward method to compute derivatives of an exponential function. It shows the setup for finite differences and verification of the result. ```python >>> step_ratio=2.0 >>> fd_rule = LogRule(n=2, method='forward', order=4) >>> h = 0.002*(1./step_ratio)**np.arange(6) >>> x0 = 1. >>> f = np.exp >>> f_x0 = f(x0) >>> f_del = f(x0+h) - f_x0 # forward difference >>> f_del = fd_rule.diff(f, f_x0, x0, h) # or alternatively >>> fder, h, shape = fd_rule.apply(f_del, h, step_ratio) >>> np.allclose(fder, f(x0)) True ``` -------------------------------- ### Verify Python Installation Source: https://github.com/pbrod/numdifftools/blob/master/docs/tutorials/install.md Check if Python is installed and view its version information. This is a console output example. ```console Python 3.6.3 (64-bit)| (default, Oct 15 2017, 03:27:45) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> ``` -------------------------------- ### Building and Installing Wheel Source: https://github.com/pbrod/numdifftools/blob/master/CHANGELOG.md Updated appveyor.yml to build a wheel and install from a local distribution. ```bash python setup.py bdist_wheel ``` ```bash pip install numdifftools --find-links=dist ``` -------------------------------- ### Installing Setuptools Source: https://github.com/pbrod/numdifftools/blob/master/CHANGELOG.md Added command to upgrade setuptools in appveyor.yml to prevent build errors. ```bash python -m pip install --upgrade setuptools ``` -------------------------------- ### BasicMinStepGenerator Example Source: https://github.com/pbrod/numdifftools/blob/master/docs/reference/numdifftools.md Shows how to instantiate and use BasicMinStepGenerator to produce a series of steps with decreasing magnitudes. ```python >>> from numdifftools.step_generators import BasicMinStepGenerator >>> step_gen = BasicMinStepGenerator(base_step=0.25, step_ratio=2, ... num_steps=4) >>> [s for s in step_gen()] [2.0, 1.0, 0.5, 0.25] ``` -------------------------------- ### Install Sphinx for HTML documentation Source: https://github.com/pbrod/numdifftools/blob/master/docs/tutorials/whatsnext.md Install the Sphinx documentation generator using pip. This is a prerequisite for building local HTML documentation. ```console $ pip install Sphinx ``` -------------------------------- ### LogJacobianRule Forward Method Examples Source: https://github.com/pbrod/numdifftools/blob/master/docs/reference/numdifftools.md Demonstrates the 'forward' method for LogJacobianRule with different orders and step ratios. ```python >>> np.allclose(Rule(n=1, method='forward', order=2).rule(step_ratio=2.), [-1., 4.]) True ``` ```python >>> np.allclose(Rule(n=1, method='forward', order=4).rule(step_ratio=2.), ... [ -0.04761905, 1.33333333, -10.66666667, 24.38095238]) True ``` ```python >>> np.allclose(Rule(n=1, method='forward', order=6).rule(step_ratio=2.), ... [ -1.02406554e-04, 1.26984127e-02, -5.07936508e-01, ... 8.12698413e+00, -5.20126984e+01, 1.07381055e+02]) True ``` -------------------------------- ### Running Doctests Source: https://github.com/pbrod/numdifftools/blob/master/CHANGELOG.md Replaced the command for running doctests in the setup process. ```bash python setup.py doctest ``` -------------------------------- ### LogHessdiagRule Forward Method Examples Source: https://github.com/pbrod/numdifftools/blob/master/docs/reference/numdifftools.md Demonstrates the 'forward' method for LogHessdiagRule with different orders and step ratios. ```python >>> np.allclose(Rule(method='forward', order=2).rule(step_ratio=2.), [ -4., 40., -64.]) True ``` ```python >>> np.allclose(Rule(method='forward', order=4).rule(step_ratio=2.), ... [-1.90476190e-01, 1.10476190e+01, -1.92000000e+02, ... 1.12152381e+03, -1.56038095e+03]) True ``` ```python >>> np.allclose(Rule(method='forward', order=6).rule(step_ratio=2.), ... [-4.09626216e-04, 1.02406554e-01, -8.33015873e+00, 2.76317460e+02, ... -3.84893968e+03, 2.04024004e+04, -2.74895500e+04]) True ``` -------------------------------- ### Start pydoc HTTP Server Source: https://github.com/pbrod/numdifftools/blob/master/docs/tutorials/whatsnext.md Start an HTTP server using pydoc to serve documentation to web browsers. This command also automatically opens a browser to the module index page. ```console $ pydoc -b ``` -------------------------------- ### Install numdifftools Source: https://github.com/pbrod/numdifftools/blob/master/docs/tutorials/install.md Install the latest stable version of numdifftools using pip. This command also automatically installs all required dependencies. ```bash $ pip install numdifftools ``` -------------------------------- ### LogRule Forward Method Examples Source: https://github.com/pbrod/numdifftools/blob/master/docs/reference/numdifftools.md Illustrates the LogRule with the forward method for orders 2 and 4. Verifies the results using np.allclose. ```python >>> np.allclose(LogRule(n=1, method='forward', order=2).rule(step_ratio=2.), [-1., 4.]) True ``` ```python >>> np.allclose(LogRule(n=1, method='forward', order=4).rule(step_ratio=2.), ... [ -0.04761905, 1.33333333, -10.66666667, 24.38095238]) True ``` -------------------------------- ### LogJacobianRule Central Method Examples Source: https://github.com/pbrod/numdifftools/blob/master/docs/reference/numdifftools.md Demonstrates the 'central' method for LogJacobianRule with different orders and step ratios. ```python >>> np.allclose(Rule(n=1, method='central', order=2).rule(step_ratio=2.0), 1) True ``` ```python >>> np.allclose(Rule(n=1, method='central', order=4).rule(step_ratio=2.), ... [-0.33333333, 2.66666667]) True ``` ```python >>> np.allclose(Rule(n=1, method='central', order=6).rule(step_ratio=2.), ... [ 0.02222222, -0.88888889, 5.68888889]) True ``` -------------------------------- ### Check Conda Installation Source: https://github.com/pbrod/numdifftools/blob/master/docs/how-to/create_virtual_env_with_conda.md Verify if conda is installed and accessible in your system's PATH. ```bash $ conda -V conda 4.6.8 ``` -------------------------------- ### Installing Pip Source: https://github.com/pbrod/numdifftools/blob/master/CHANGELOG.md Added command to upgrade pip in appveyor.yml. ```bash python -m pip install --upgrade pip ``` -------------------------------- ### LogRule Forward Method Order 6 Example Source: https://github.com/pbrod/numdifftools/blob/master/docs/reference/numdifftools.md Shows the LogRule with the forward method for order 6, verifying the output with np.allclose. ```python >>> np.allclose(LogRule(n=1, method='forward', order=6).rule(step_ratio=2.), ... [ -1.02406554e-04, 1.26984127e-02, -5.07936508e-01, ... 8.12698413e+00, -5.20126984e+01, 1.07381055e+02]) True ``` -------------------------------- ### LogHessdiagRule Central Method Examples Source: https://github.com/pbrod/numdifftools/blob/master/docs/reference/numdifftools.md Demonstrates the 'central' method for LogHessdiagRule with different orders and step ratios. ```python >>> np.allclose(Rule(method='central', order=2).rule(step_ratio=2.0), 2) True ``` ```python >>> np.allclose(Rule(method='central', order=4).rule(step_ratio=2.), ... [-0.66666667, 10.66666667]) True ``` ```python >>> np.allclose(Rule(method='central', order=6).rule(step_ratio=2.), ... [ 4.44444444e-02, -3.55555556e+00, 4.55111111e+01]) True ``` -------------------------------- ### Upgrade Pip Source: https://github.com/pbrod/numdifftools/blob/master/docs/tutorials/install.md Ensure you have the latest version of pip, the Python package installer, for reliable installations. ```bash $ pip install --upgrade pip ``` -------------------------------- ### Hessian Calculation Example Source: https://github.com/pbrod/numdifftools/blob/master/docs/reference/numdifftools.md Demonstrates how to calculate the Hessian matrix of a function using numdifftools.nd_algopy.Hessian. It includes examples for the Rosenbrock function and cos(x-y). ```APIDOC ## Hessian Calculation Example ### Description Demonstrates how to calculate the Hessian matrix of a function using numdifftools.nd_algopy.Hessian. It includes examples for the Rosenbrock function and cos(x-y). ### Method ```python import numpy as np import numdifftools.nd_algopy as nda # Rosenbrock function, minimized at [1,1] rosen = lambda x : (1.-x[0])**2 + 105*(x[1]-x[0]**2)**2 Hf = nda.Hessian(rosen) h = Hf([1, 1]) # h =[ 842 -420; -420, 210]; print(np.allclose(h, [[ 842., -420.], [-420., 210.]])) # cos(x-y), at (0,0) cos = np.cos fun = lambda xy : cos(xy[0]-xy[1]) Hfun2 = nda.Hessian(fun) h2 = Hfun2([0, 0]) # h2 = [-1 1; 1 -1] print(np.allclose(h2, [[-1., 1.], [ 1., -1.]])) Hfun3 = nda.Hessian(fun, method='reverse') h3 = Hfun3([0, 0]) # h2 = [-1, 1; 1, -1]; print(np.allclose(h3, [[-1., 1.], [ 1., -1.]])) ``` ### SEE ALSO [`Derivative`](#numdifftools.nd_algopy.Derivative), [`Gradient`](#numdifftools.nd_algopy.Gradient), [`Jacobian`](#numdifftools.nd_algopy.Jacobian), [`Hessdiag`](#numdifftools.nd_algopy.Hessdiag) ``` -------------------------------- ### LogRule Central Method Examples Source: https://github.com/pbrod/numdifftools/blob/master/docs/reference/numdifftools.md Demonstrates the use of LogRule with the central method for different orders of differentiation. Shows how to check the computed rule against expected values using np.allclose. ```python >>> from numdifftools.finite_difference import LogRule >>> np.allclose(LogRule(n=1, method='central', order=2).rule(step_ratio=2.0), 1) True >>> np.allclose(LogRule(n=1, method='central', order=4).rule(step_ratio=2.), ... [-0.33333333, 2.66666667]) True >>> np.allclose(LogRule(n=1, method='central', order=6).rule(step_ratio=2.), ... [ 0.02222222, -0.88888889, 5.68888889]) True ``` -------------------------------- ### Installing Pytest Source: https://github.com/pbrod/numdifftools/blob/master/CHANGELOG.md Added command to upgrade pytest in appveyor.yml to resolve package conflicts. ```bash python -m pip install --upgrade pytest ``` -------------------------------- ### LogJacobianRule Apply Method Example Source: https://github.com/pbrod/numdifftools/blob/master/docs/reference/numdifftools.md Applies the LogJacobianRule to the exponential function using forward differences and calculates the derivative estimates. ```python >>> step_ratio=2.0 >>> fd_rule = Rule(n=1, method='forward', order=4) >>> steps = 0.002*(1./step_ratio)**np.arange(6) ``` ```python >>> x0 = np.atleast_1d(1.) >>> f = np.exp >>> f_x0 = f(x0) >>> f_del = [f(x0+h) - f_x0 for h in steps] # forward difference >>> f_del = [fd_rule.diff(f, f_x0, x0, h) for h in steps[:, None]] # or alternatively >>> fder, h, shape = fd_rule.apply(f_del, steps, step_ratio) ``` ```python >>> np.allclose(fder, f(x0)) True ``` -------------------------------- ### Import numdifftools and Check Version Source: https://github.com/pbrod/numdifftools/blob/master/docs/tutorials/install.md Verify that numdifftools is installed and accessible by importing it in a Python interactive session and printing its version. ```python >>> import numdifftools as nd >>> print(nd.__version__) 0.9.42 ``` -------------------------------- ### LogHessdiagRule Apply Method Example Source: https://github.com/pbrod/numdifftools/blob/master/docs/reference/numdifftools.md Applies the LogHessdiagRule to a function using forward differences and calculates the derivative estimates. ```python >>> step_ratio=2.0 >>> fd_rule = Rule(method='forward', order=4) >>> steps = 0.002*(1./step_ratio)**np.arange(6) >>> x0 = np.array([0., 0.]) >>> f = lambda xy : np.cos(xy[0]-xy[1]) >>> f_x0 = f(x0) >>> f_del = [f(x0+h) - f_x0 for h in steps] # forward difference >>> f_del = [fd_rule.diff(f, f_x0, x0, h) for h in steps] # or alternatively >>> fder, h, shape = fd_rule.apply(f_del, steps, step_ratio) ``` ```python >>> np.allclose(fder, [[-1., -1.], [-1., -1.]]) True ``` -------------------------------- ### Import NumPy and Numdifftools Source: https://github.com/pbrod/numdifftools/blob/master/docs/reference/numdifftools.md Standard import statements required for using numdifftools and numpy in Python examples. ```pycon >>> import numpy as np >>> import numdifftools.nd_statsmodels as nd ``` -------------------------------- ### Numdifftools Derivative with full output Source: https://github.com/pbrod/numdifftools/blob/master/docs/src/numerical/derivest.md Calculates the derivative of exp(x) at x=1 using numdifftools with full output to get value, error estimate, and final step size. Demonstrates automatic step size selection for optimal accuracy. ```python import numdifftools as nd from numpy import exp f = nd.Derivative(exp, full_output=True) val, info = f(1) np.allclose(val, 2.71828183) True np.allclose(info.error_estimate, 6.927791673660977e-14) True np.allclose(info.final_step, 0.0078125) True ``` -------------------------------- ### Richardson Extrapolation Example Source: https://github.com/pbrod/numdifftools/blob/master/docs/reference/numdifftools.md Demonstrates using Richardson extrapolation to improve the accuracy of an integral approximation. It calculates an integral using trapezoidal rule with varying step sizes and then applies Richardson extrapolation to refine the result. Checks if the extrapolated value is close to the true value and if the initial approximations were within a certain tolerance. ```python >>> import numpy as np >>> import numdifftools as nd >>> n = 3 >>> Ei = np.zeros((n,1)) >>> h = np.zeros((n,1)) >>> linfun = lambda i : np.linspace(0, np.pi/2., 2**(i+5)+1) >>> for k in np.arange(n): ... x = linfun(k) ... h[k] = x[1] ... Ei[k] = np.trapezoid(np.sin(x),x) >>> En, err, step = nd.Richardson(step=1, order=1)(Ei, h) >>> truErr = np.abs(En-1.) >>> bool(np.all(truErr < err)) True >>> bool(np.all(np.abs(Ei-1)<1e-3)) True >>> np.allclose(En, 1) True ``` -------------------------------- ### DEA3 Extrapolation Example Source: https://github.com/pbrod/numdifftools/blob/master/docs/reference/numdifftools.md Illustrates the use of the dea3 function to extrapolate a sequence obtained from integrating sin(x) from 0 to pi/2. Verifies the accuracy of the extrapolated value and compares it to the initial approximations. ```pycon >>> import numpy as np >>> import numdifftools as nd >>> Ei= np.zeros(3) >>> linfun = lambda i : np.linspace(0, np.pi/2., 2**(i+5)+1) >>> for k in np.arange(3): ... x = linfun(k) ... Ei[k] = np.trapezoid(np.sin(x),x) >>> [En, err] = nd.dea3(Ei[0], Ei[1], Ei[2]) >>> truErr = np.abs(En-1.) >>> bool(np.all(truErr < err)) True >>> np.allclose(En, 1) True >>> bool(np.all(np.abs(Ei-1)<1e-3)) True ``` -------------------------------- ### Install Packages into a Conda Environment Source: https://github.com/pbrod/numdifftools/blob/master/docs/how-to/create_virtual_env_with_conda.md Add new Python packages to a specific conda environment. Ensure you use the '-n yourenvname' flag to avoid installing into the root Python installation. ```bash conda install -n yourenvname [package] ``` -------------------------------- ### epsalg_demo() Source: https://github.com/pbrod/numdifftools/blob/master/docs/reference/numdifftools.md Demonstrates the epsilon algorithm for numerical approximation. It illustrates how the epsilon algorithm refines approximations and reduces error with more panels. ```APIDOC ## epsalg_demo() ### Description Demonstrates the epsilon algorithm for numerical approximation. It illustrates how the epsilon algorithm refines approximations and reduces error with more panels. ### Method Python function call ### Endpoint `numdifftools.extrapolation.epsalg_demo()` ### Parameters None ### Response Example ``` NO. PANELS TRAP. APPROX APPROX W/EA abserr 1 0.78539816 0.78539816 0.21460184 2 0.94805945 0.94805945 0.05194055 4 0.98711580 0.99945672 0.00054328 8 0.99678517 0.99996674 0.00003326 16 0.99919668 0.99999988 0.00000012 32 0.99979919 1.00000000 0.00000000 64 0.99994980 1.00000000 0.00000000 128 0.99998745 1.00000000 0.00000000 256 0.99999686 1.00000000 0.00000000 512 0.99999922 1.00000000 0.00000000 ``` ``` -------------------------------- ### Update Conda Source: https://github.com/pbrod/numdifftools/blob/master/docs/how-to/create_virtual_env_with_conda.md Ensure your conda installation is up to date to access the latest features and bug fixes. ```bash conda update conda ``` -------------------------------- ### Build HTML documentation with Make Source: https://github.com/pbrod/numdifftools/blob/master/docs/tutorials/whatsnext.md Use the included Makefile to convert numdifftools documentation from plain text to HTML. Requires GNU Make. ```console cd path/to/numdifftools/docs $ make html ``` -------------------------------- ### Build HTML documentation with Make on Windows Source: https://github.com/pbrod/numdifftools/blob/master/docs/tutorials/whatsnext.md Use the included batch file to convert numdifftools documentation to HTML on Windows systems. ```bat cd path\to\numdifftools\docs $ make.bat html ``` -------------------------------- ### Epsilon Algorithm Demo Source: https://github.com/pbrod/numdifftools/blob/master/docs/reference/numdifftools.md Demonstrates the epsilon algorithm for numerical approximation. Displays the progression of approximations and absolute errors. ```pycon >>> from numdifftools.extrapolation import epsalg_demo >>> epsalg_demo() NO. PANELS TRAP. APPROX APPROX W/EA abserr 1 0.78539816 0.78539816 0.21460184 2 0.94805945 0.94805945 0.05194055 4 0.98711580 0.99945672 0.00054328 8 0.99678517 0.99996674 0.00003326 16 0.99919668 0.99999988 0.00000012 32 0.99979919 1.00000000 0.00000000 64 0.99994980 1.00000000 0.00000000 128 0.99998745 1.00000000 0.00000000 256 0.99999686 1.00000000 0.00000000 512 0.99999922 1.00000000 0.00000000 ``` -------------------------------- ### Import numdifftools and numpy Source: https://github.com/pbrod/numdifftools/blob/master/docs/reference/numdifftools.md Initializes the necessary libraries for numerical differentiation. ```python >>> import numpy as np >>> import numdifftools.nd_algopy as nda ``` -------------------------------- ### richardson_demo() Source: https://github.com/pbrod/numdifftools/blob/master/docs/reference/numdifftools.md Demonstrates Richardson extrapolation for numerical approximation. It shows the convergence of approximations and reduction in absolute error as the number of panels increases. ```APIDOC ## richardson_demo() ### Description Demonstrates Richardson extrapolation for numerical approximation. It shows the convergence of approximations and reduction in absolute error as the number of panels increases. ### Method Python function call ### Endpoint `numdifftools.extrapolation.richardson_demo()` ### Parameters None ### Response Example ``` NO. PANELS TRAP. APPROX APPROX W/R abserr 1 0.78539816 0.78539816 0.21460184 2 0.94805945 1.11072073 0.11072073 4 0.98711580 0.99798929 0.00201071 8 0.99678517 0.99988201 0.00011799 16 0.99919668 0.99999274 0.00000726 32 0.99979919 0.99999955 0.00000045 64 0.99994980 0.99999997 0.00000003 128 0.99998745 1.00000000 0.00000000 256 0.99999686 1.00000000 0.00000000 512 0.99999922 1.00000000 0.00000000 ``` ``` -------------------------------- ### DEA Demo Source: https://github.com/pbrod/numdifftools/blob/master/docs/reference/numdifftools.md Demonstrates the Deferred Exponential Approximation (DEA) method for numerical approximation. Shows the progression of approximations and absolute errors. ```pycon >>> from numdifftools.extrapolation import dea_demo >>> dea_demo() NO. PANELS TRAP. APPROX APPROX W/EA abserr 1 0.78539816 0.78539816 0.78539816 2 0.94805945 0.94805945 0.97596771 4 0.98711580 0.99945672 0.21405856 8 0.99678517 0.99996674 0.05190729 16 0.99919668 0.99999988 0.00057629 32 0.99979919 1.00000000 0.00057665 64 0.99994980 1.00000000 0.00003338 128 0.99998745 1.00000000 0.00000012 256 0.99999686 1.00000000 0.00000000 512 0.99999922 1.00000000 0.00000000 1024 0.99999980 1.00000000 0.00000000 2048 0.99999995 1.00000000 0.00000000 ``` -------------------------------- ### dea_demo() Source: https://github.com/pbrod/numdifftools/blob/master/docs/reference/numdifftools.md Demonstrates the Damped Exponential Approximation (DEA) method for numerical approximation. It shows the improvement in approximation accuracy with an increasing number of panels. ```APIDOC ## dea_demo() ### Description Demonstrates the Damped Exponential Approximation (DEA) method for numerical approximation. It shows the improvement in approximation accuracy with an increasing number of panels. ### Method Python function call ### Endpoint `numdifftools.extrapolation.dea_demo()` ### Parameters None ### Response Example ``` NO. PANELS TRAP. APPROX APPROX W/EA abserr 1 0.78539816 0.78539816 0.78539816 2 0.94805945 0.94805945 0.97596771 4 0.98711580 0.99945672 0.21405856 8 0.99678517 0.99996674 0.05190729 16 0.99919668 0.99999988 0.00057629 32 0.99979919 1.00000000 0.00057665 64 0.99994980 1.00000000 0.00003338 128 0.99998745 1.00000000 0.00000012 256 0.99999686 1.00000000 0.00000000 512 0.99999922 1.00000000 0.00000000 1024 0.99999980 1.00000000 0.00000000 2048 0.99999995 1.00000000 0.00000000 ``` ``` -------------------------------- ### Richardson Extrapolation Demo Source: https://github.com/pbrod/numdifftools/blob/master/docs/reference/numdifftools.md Demonstrates Richardson extrapolation for numerical approximation. Shows the progression of approximations and absolute errors. ```pycon >>> from numdifftools.extrapolation import richardson_demo >>> richardson_demo() NO. PANELS TRAP. APPROX APPROX W/R abserr 1 0.78539816 0.78539816 0.21460184 2 0.94805945 1.11072073 0.11072073 4 0.98711580 0.99798929 0.00201071 8 0.99678517 0.99988201 0.00011799 16 0.99919668 0.99999274 0.00000726 32 0.99979919 0.99999955 0.00000045 64 0.99994980 0.99999997 0.00000003 128 0.99998745 1.00000000 0.00000000 256 0.99999686 1.00000000 0.00000000 512 0.99999922 1.00000000 0.00000000 ``` -------------------------------- ### Jacobian of a multi-output function Source: https://github.com/pbrod/numdifftools/blob/master/docs/reference/numdifftools.md Define and compute the Jacobian of a function that returns multiple outputs. This example shows how to handle functions with vector outputs. ```python >>> def fun3(x): ... n = int(np.prod(np.shape(x[0]))) ... out = nda.algopy.zeros((2, n), dtype=x) ... out[0] = x[0]*x[1]*x[2]**2 ... out[1] = x[0]*x[1]*x[2] ... return out >>> Jfun3 = nda.Jacobian(fun3) ``` -------------------------------- ### Jacobian of a Linear System Source: https://github.com/pbrod/numdifftools/blob/master/docs/tutorials/getting_started.md Calculate the Jacobian matrix for a linear system, which simplifies to the design matrix. This example shows how to compute it for a linear transformation. ```python >>> A = np.random.rand(5,3) >>> b = np.random.rand(5) >>> fun = lambda x: np.dot(x, A.T) - b >>> x = np.random.rand(3) >>> jac = nd.Jacobian(fun)(x) ``` ```python >>> np.allclose(jac - A, 0) True ``` -------------------------------- ### Calculate Derivatives of sin(x) Source: https://github.com/pbrod/numdifftools/blob/master/docs/tutorials/getting_started.md Demonstrates calculating the first four derivatives of the sine function at x=0 using numdifftools.Derivative with different values for the 'n' parameter. ```python from numpy import sin import numdifftools as nd df = nd.Derivative(sin, n=1) np.allclose(df(0), 1.) True ``` ```python ddf = nd.Derivative(sin, n=2) np.allclose(ddf(0), 0.) True ``` ```python dddf = nd.Derivative(sin, n=3) np.allclose(dddf(0), -1.) True ``` ```python ddddf = nd.Derivative(sin, n=4) np.allclose(ddddf(0), 0.) True ``` -------------------------------- ### Visualize High-Order Derivatives of tanh(x) Source: https://github.com/pbrod/numdifftools/blob/master/docs/tutorials/getting_started.md Visualizes the first 10 derivatives of the hyperbolic tangent function using numdifftools.Derivative and matplotlib. This snippet requires matplotlib to be installed. ```python import numpy as np import matplotlib.pyplot as plt x = np.linspace(-2, 2, 100) for i in range(10): df = nd.Derivative(np.tanh, n=i) y = df(x) h = plt.plot(x, y/np.abs(y).max()) plt.show() ``` -------------------------------- ### Derivatives of sin(x) at x=0 Source: https://github.com/pbrod/numdifftools/blob/master/docs/src/numerical/derivest.md Demonstrates calculating the first four derivatives of the sine function at x=0 using numdifftools.Derivative with different values for the 'n' parameter. ```pycon >>> from numpy import sin >>> import numdifftools as nd >>> df = nd.Derivative(sin, n=1) >>> np.allclose(df(0), 1.) True ``` ```pycon >>> ddf = nd.Derivative(sin, n=2) >>> np.allclose(ddf(0), 0.) True ``` ```pycon >>> dddf = nd.Derivative(sin, n=3) >>> np.allclose(dddf(0), -1.) True ``` ```pycon >>> ddddf = nd.Derivative(sin, n=4) >>> np.allclose(ddddf(0), 0.) True ``` -------------------------------- ### Activate a Conda Virtual Environment Source: https://github.com/pbrod/numdifftools/blob/master/docs/how-to/create_virtual_env_with_conda.md Switch to an existing conda environment to use its isolated Python installation and packages. The command prompt will change to indicate the active environment. ```bash conda activate yourenvname ``` -------------------------------- ### Nonlinear Least Squares Example Source: https://github.com/pbrod/numdifftools/blob/master/docs/reference/numdifftools.md Illustrates using numdifftools for nonlinear least squares problems. It calculates the Jacobian of a cost function defined for fitting exponential data. ```pycon >>> xdata = np.arange(0,1,0.1) >>> ydata = 1+2*np.exp(0.75*xdata) >>> fun = lambda c: (c[0]+c[1]*np.exp(c[2]*xdata) - ydata)**2 >>> np.allclose(fun([1, 2, 0.75]).shape, (10,)) True >>> dfun = nd.Jacobian(fun) >>> np.allclose(dfun([1, 2, 0.75]), np.zeros((10,3))) True ``` -------------------------------- ### Run numdifftools Doctests Source: https://github.com/pbrod/numdifftools/blob/master/docs/tutorials/install.md Execute the doctests for numdifftools to confirm that the toolbox is functioning correctly. No errors should be reported. ```python nd.test('--doctest-module') ``` -------------------------------- ### BasicMinStepGenerator Source: https://github.com/pbrod/numdifftools/blob/master/docs/reference/numdifftools.md Generates a sequence of steps with decreasing magnitude. It is initialized with a base step, a step ratio, and the number of steps. ```APIDOC ### class BasicMinStepGenerator(base_step, step_ratio, num_steps, offset=0) Bases: [`BasicMaxStepGenerator`](#numdifftools.step_generators.BasicMaxStepGenerator) Generates a sequence of steps of decreasing magnitude where : steps = base_step * step_ratio ** (i + offset), i=num_steps-1,… 1, 0. * **Parameters:** * **base_step** (*float* *,* *array-like.*) – Defines the end step, i.e., minimum step * **step_ratio** (*real scalar.*) – Ratio between sequential steps generated. Note: Ratio > 1 * **num_steps** (*scalar integer.*) – defines number of steps generated. * **offset** (*real scalar* *,* *optional* *,* *default 0*) – offset to the base step ### Examples ```pycon >>> from numdifftools.step_generators import BasicMinStepGenerator >>> step_gen = BasicMinStepGenerator(base_step=0.25, step_ratio=2, ... num_steps=4) >>> [s for s in step_gen()] [2.0, 1.0, 0.5, 0.25] ``` ``` -------------------------------- ### Deactivate the Current Conda Environment Source: https://github.com/pbrod/numdifftools/blob/master/docs/how-to/create_virtual_env_with_conda.md Exit the currently active conda environment and return to the system's default Python setup. This command does not require specifying the environment name. ```bash conda deactivate ``` -------------------------------- ### Calculate 1st and 5th Derivative of exp(x) using AD Source: https://github.com/pbrod/numdifftools/blob/master/docs/reference/numdifftools.md Demonstrates how to compute the first and fifth derivatives of the exponential function at x=1 using numdifftools' Derivative class with Algorithmic Differentiation. Ensure numpy and numdifftools.nd_algopy are imported. ```pycon >>> import numpy as np >>> import numdifftools.nd_algopy as nda >>> fd = nda.Derivative(np.exp) # 1'st derivative >>> np.allclose(fd(1), 2.718281828459045) True >>> fd5 = nda.Derivative(np.exp, n=5) # 5'th derivative >>> np.allclose(fd5(1), 2.718281828459045) True ```