{
  "__type": "IngestedDoc",
  "__tag": 4010,
  "_content": {
    "Notes": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "For small n up to 150 a modified version of the Golub-Welsch algorithm is used. Nodes are computed from the eigenvalue problem and improved by one step of a Newton iteration. The weights are computed from the well-known analytical formula."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "For n larger than 150 an optimal asymptotic algorithm is applied which computes nodes and weights in a numerically stable manner. The algorithm has linear runtime making computation for very large n (several thousand or more) feasible."
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Warns": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Raises": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Yields": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Methods": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Returns": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Parameters",
          "__tag": 4026,
          "children": [
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "x",
              "annotation": "ndarray",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "Sample points"
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "w",
              "annotation": "ndarray",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "Weights"
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "mu",
              "annotation": "float",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "Sum of the weights"
                    }
                  ]
                }
              ]
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Summary": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Gauss-Hermite (physicist's) quadrature."
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Receives": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Warnings": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Attributes": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Parameters": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Parameters",
          "__tag": 4026,
          "children": [
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "n",
              "annotation": "int",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "quadrature order"
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "mu",
              "annotation": "bool, optional",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "If True, return the sum of the weights, optional."
                    }
                  ]
                }
              ]
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Extended Summary": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Compute the sample points and weights for Gauss-Hermite quadrature. The sample points are the roots of the nth degree Hermite polynomial, "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "H_n(x)"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ". These sample points and weights correctly integrate polynomials of degree "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "2n - 1"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " or less over the interval "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "[-\\infty, \\infty]"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " with weight function "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "w(x) = e^{-x^2}"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ". See 22.2.14 in "
            },
            {
              "__type": "CitationReference",
              "__tag": 4063,
              "label": "AS"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " for details."
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Other Parameters": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    }
  },
  "_ordered_sections": [
    "Summary",
    "Extended Summary",
    "Parameters",
    "Attributes",
    "Methods",
    "Returns",
    "Yields",
    "Receives",
    "Other Parameters",
    "Raises",
    "Warns",
    "Warnings",
    "Notes"
  ],
  "item_file": "build-install/usr/lib/python3.14/site-packages/scipy/special/_orthogonal.py",
  "item_line": 1115,
  "item_type": "function",
  "aliases": [
    "scipy.special.h_roots"
  ],
  "example_section_data": {
    "__type": "Section",
    "__tag": 4015,
    "children": [
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "from scipy.special import roots_hermite\nroots, weights = roots_hermite(5)\n",
        "execution_status": "success",
        "out": ""
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "roots\nweights\n",
        "execution_status": "failure",
        "out": "array([0.02 , 0.394, 0.945, 0.394, 0.02 ])\n"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nVerify that the values in `roots` are roots of the Hermite polynomial\n:math:`H_5(x)`.\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "from scipy.special import eval_hermite\n",
        "execution_status": "success",
        "out": ""
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "eval_hermite(5, roots)\n",
        "execution_status": "failure",
        "out": "array([ 0.,  0.,  0., -0., -0.])\n"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nThe values of :math:`H_5(x)` evaluated at the roots are indeed rather close\nto zero. The increasing values for larger roots can be explained by the\nincreasing derivative of the Hermite polynomial at the roots.\n\nVerify that the sum of the weights equals the integral from :math:`-\\infty`\nto :math:`\\infty` of :math:`\\exp(-x^2)` which evaluates to :math:`\\sqrt{\\pi}`.\nThere are two ways to obtain the sum of weights, both yielding the expected\nresult within numerical precision.\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "sum(weights)\n",
        "execution_status": "failure",
        "out": "np.float64(1.7724538509055154)\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "roots, weights, sum_of_weights = roots_hermite(5, mu=True)\nsum_of_weights\n",
        "execution_status": "success",
        "out": "np.float64(1.7724538509055159)\n"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "from math import exp, pi, sqrt\nsqrt(pi)\n",
        "execution_status": "success",
        "out": "1.7724538509055159\n"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nRoots and weights obtained from the Hermite polynomial :math:`H_n(x)`\nare used in Gauss-Hermite quadrature where the integral from :math:`-\\infty`\nto :math:`\\infty` of :math:`f(x)\\exp(-x^2)` is evaluated. Roots and weights\nfor order :math:`n` are expected to yield the exact result for polynomials\n:math:`f(x)` of a maximal order of :math:`2n-1`.\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "f = lambda x: x**4\nweights @ f(roots)\n",
        "execution_status": "success",
        "out": "np.float64(1.3293403881791352)\n"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nThis result is indeed very close to the exact value of :math:`3\\sqrt{\\pi}/4`.\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "0.75*sqrt(pi)\n",
        "execution_status": "success",
        "out": "1.329340388179137\n"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nIn general, Gauss-Hermite quadrature will only yield an approximate value of\nthe integral. Consider the integral from :math:`-\\infty` to :math:`\\infty` of\n:math:`\\cos(x)\\exp(-x^2)` which evaluates to :math:`\\sqrt{\\pi}\\exp(-1/4)`.\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "import numpy as np\n",
        "execution_status": "success",
        "out": ""
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "weights @ np.cos(roots)\n",
        "execution_status": "failure",
        "out": "np.float64(1.3803900759356562)\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "sqrt(pi) * exp(-0.25)\n",
        "execution_status": "success",
        "out": "1.380388447043143\n"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nIn order to improve the accuracy obtained from Gauss-Hermite quadrature, a\nlarge number of nodes can be chosen.\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "roots, weights = roots_hermite(50)\nweights @ np.cos(roots)\n",
        "execution_status": "success",
        "out": "np.float64(1.380388447043143)\n"
      }
    ],
    "title": [],
    "level": 0,
    "target": null
  },
  "see_also": [
    {
      "__type": "SeeAlsoItem",
      "__tag": 4028,
      "name": {
        "__type": "CrossRef",
        "__tag": 4002,
        "value": "numpy.polynomial.hermite.hermgauss",
        "reference": {
          "__type": "RefInfo",
          "__tag": 4000,
          "module": "current-module",
          "version": "current-version",
          "kind": "to-resolve",
          "path": "numpy.polynomial.hermite.hermgauss"
        },
        "kind": "module"
      },
      "descriptions": [],
      "type": null
    },
    {
      "__type": "SeeAlsoItem",
      "__tag": 4028,
      "name": {
        "__type": "CrossRef",
        "__tag": 4002,
        "value": "roots_hermitenorm",
        "reference": {
          "__type": "LocalRef",
          "__tag": 4022,
          "kind": "module",
          "path": "scipy.special._orthogonal:roots_hermitenorm"
        },
        "kind": "module"
      },
      "descriptions": [],
      "type": null
    },
    {
      "__type": "SeeAlsoItem",
      "__tag": 4028,
      "name": {
        "__type": "CrossRef",
        "__tag": 4002,
        "value": "scipy.integrate.fixed_quad",
        "reference": {
          "__type": "LocalRef",
          "__tag": 4022,
          "kind": "module",
          "path": "scipy.integrate._quadrature:fixed_quad"
        },
        "kind": "module"
      },
      "descriptions": [],
      "type": null
    }
  ],
  "signature": {
    "__type": "SignatureNode",
    "__tag": 4029,
    "kind": "function",
    "parameters": [
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "n",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "POSITIONAL_OR_KEYWORD",
        "default": {
          "__type": "Empty",
          "__tag": 4031
        }
      },
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "mu",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "POSITIONAL_OR_KEYWORD",
        "default": "False"
      }
    ],
    "return_annotation": {
      "__type": "Empty",
      "__tag": 4031
    },
    "target_name": "roots_hermite"
  },
  "references": [
    ".. [townsend.trogdon.olver-2014]",
    "    Townsend, A. and Trogdon, T. and Olver, S. (2014)",
    "    *Fast computation of Gauss quadrature nodes and",
    "    weights on the whole real line*. :arXiv:`1410.5286`.",
    ".. [townsend.trogdon.olver-2015]",
    "    Townsend, A. and Trogdon, T. and Olver, S. (2015)",
    "    *Fast computation of Gauss quadrature nodes and",
    "    weights on the whole real line*.",
    "    IMA Journal of Numerical Analysis",
    "    :doi:`10.1093/imanum/drv002`.",
    ".. [AS] Milton Abramowitz and Irene A. Stegun, eds.",
    "    Handbook of Mathematical Functions with Formulas,",
    "    Graphs, and Mathematical Tables. New York: Dover, 1972."
  ],
  "qa": "scipy.special._orthogonal:roots_hermite",
  "arbitrary": [],
  "local_refs": [
    "mu",
    "n",
    "w",
    "x"
  ]
}