{"id":545,"date":"2014-08-21T22:30:09","date_gmt":"2014-08-21T21:30:09","guid":{"rendered":"http:\/\/numbercrunch.de\/blog\/?p=545"},"modified":"2023-01-18T22:26:23","modified_gmt":"2023-01-18T21:26:23","slug":"calculating-the-hermite-functions","status":"publish","type":"post","link":"https:\/\/www.numbercrunch.de\/blog\/2014\/08\/calculating-the-hermite-functions\/","title":{"rendered":"Calculating the Hermite functions"},"content":{"rendered":"<p><script type=\"text\/x-mathjax-config\">MathJax.Hub.Config({TeX: { equationNumbers: {autoNumber: \"AMS\" } }});<\/script><\/p>\n<p style=\"text-align: justify;\">The <a href=\"http:\/\/en.wikipedia.org\/wiki\/Hermite_polynomials#Hermite_functions\">Hermite functions<\/a> appear as the solutions of the quantum mechanical harmonic oscillator. But they have applications in many other fields and applications, e.g., pseudospectral methods. The Hermite functions $h_n(x)$ are defined as<br \/>\\begin{equation}<br \/>\\label{eq:h}<br \/>h_n(x) = \\frac{1}{\\sqrt{\\sqrt{\\pi} 2^n n!}} \\mathrm{e}^{-x^2\/2} H_n(x) \\,,<br \/>\\end{equation} where $H_n(x)$ denotes the $n$th Hermite polynomial defined via the recurrence relation<br \/>\\begin{equation}<br \/>H_{n}(x) = 2xH_{n-1}(x)-2(n-1)H_{n-2}(x)<br \/>\\end{equation} with the initial values $H_0(x)=1$ and $H_1(x)=2x$. Although the Hermite functions are quite well behaved, they are rather difficult to calculate especially for large $n$ and\/or large $x$.<\/p>\n<p style=\"text-align: justify;\">Calculating the Hermite functions via the definition \\eqref{eq:h} fails easily due to numerical overflow or underflow that is caused by the rapid growth or decrease of the individual factors $\\frac{1}{\\sqrt{\\sqrt{\\pi} 2^n n!}} \\mathrm{e}^{-x^2\/2}$ and $H_n(x)$. A partial solution to this problem is to introduce the modified Hermite polynomials defined via the recurrence relation<br \/>\\begin{equation}<br \/>\\label{eq:h2}<br \/>\\tilde H_{n}(x) = \\sqrt{\\frac{2}{n}}x\\tilde H_{n-1}(x)-\\sqrt{\\frac{n-1}{n}}\\tilde H_{n-2}(x)<br \/>\\end{equation} with the initial values $\\tilde H_0(x)=1\/\\sqrt[4]{\\pi}$ and $\\tilde H_1(x)=\\sqrt{2}\\,x\/\\sqrt[4]{\\pi}$. With these polynomials the Hermite functions become<br \/>\\begin{equation}<br \/>h_n(x) = \\mathrm{e}^{-x^2\/2} \\tilde H_n(x) \\,.<br \/>\\end{equation} Because the modified Hermite polynomials $\\tilde H_{n}(x)$ grow much more slowly than the standard Hermite polynomials we got rid of the normalizing factor $\\frac{1}{\\sqrt{\\sqrt{\\pi} 2^n n!}}$.<\/p>\n<p style=\"text-align: justify;\">But this does not solve all problems of underflow and overflow. For $x&gt;38$ the factor $\\mathrm{e}^{-x^2\/2}$ is smaller than any number that can be represented as a double precision floating point number. A very robust remedy to numerical underflow was sketched in <a href=\"http:\/\/dx.doi.org\/10.1007\/s10543-009-0216-1\">BIT Numerical Mathematics, Vol. 49, pp 281-295<\/a>. The basic idea is to keep the magnitude of the modified Hermite polynomials on the order of one during their calculation via the\u00a0recurrence relation \\eqref{eq:h2} by introducing a suitable normalizing factor. During the calculation one has to keep track of the sum of the logarithms of these normalizing factors. Then the factor\u00a0$\\mathrm{e}^{-x^2\/2}$ has to be modified by this sum when the value of the Hermite function is calculated. A Python implementation of this algorithm is shown below. This algorithm allows to calculate the values of high-order Hermite functions even for quite large arguments.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">from numpy import *\nfrom pylab import *\n\ndef h(n, x):\n    if n==0:\n        return ones_like(x)*pi**(-0.25)*exp(-x**2\/2)\n    if n==1:\n        return sqrt(2.)*x*pi**(-0.25)*exp(-x**2\/2)\n    h_i_2=ones_like(x)*pi**(-0.25)\n    h_i_1=sqrt(2.)*x*pi**(-0.25)\n    sum_log_scale=zeros_like(x)\n    for i in range(2, n+1):\n        h_i=sqrt(2.\/i)*x*h_i_1-sqrt((i-1.)\/i)*h_i_2\n        h_i_2, h_i_1=h_i_1, h_i\n        log_scale=log(abs(h_i)).round()\n        scale=exp(-log_scale)\n        h_i=h_i*scale\n        h_i_1=h_i_1*scale\n        h_i_2=h_i_2*scale\n        sum_log_scale+=log_scale\n    return h_i*exp(-x**2\/2+sum_log_scale)\n<\/pre>\n<p>\u00a0<\/p>\n<p><a href=\"https:\/\/numbercrunch.de\/blog\/wp-content\/uploads\/2014\/08\/h_800.png\"><img decoding=\"async\" class=\"size-full wp-image-564 aligncenter\" src=\"https:\/\/numbercrunch.de\/blog\/wp-content\/uploads\/2014\/08\/h_800.png\" alt=\"&quot;The\" \/><\/a><\/p>\n<p style=\"text-align: justify;\">I would like to thank Randolf Beerwerth for drawing my attention to this problem.<\/p>\n<p>Edit 12.06.2020: A typo in eq. \\eqref{eq:h2} has been fixed.<\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>The Hermite functions appear as the solutions of the quantum mechanical harmonic oscillator. But they have applications in many other fields and applications, e.g., pseudospectral methods. The Hermite functions $h_n(x)$ are defined as\\begin{equation}\\label{eq:h}h_n(x) = \\frac{1}{\\sqrt{\\sqrt{\\pi} 2^n n!}} \\mathrm{e}^{-x^2\/2} H_n(x) \\,,\\end{equation} where $H_n(x)$ denotes the $n$th Hermite polynomial defined via the recurrence relation\\begin{equation}H_{n}(x) = 2xH_{n-1}(x)-2(n-1)H_{n-2}(x)\\end{equation} with&hellip; <a href=\"https:\/\/www.numbercrunch.de\/blog\/2014\/08\/calculating-the-hermite-functions\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Calculating the Hermite functions<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-545","post","type-post","status-publish","format-standard","hentry","category-numerical-methods"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Calculating the Hermite functions - Number Crunch<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.numbercrunch.de\/blog\/2014\/08\/calculating-the-hermite-functions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Calculating the Hermite functions - Number Crunch\" \/>\n<meta property=\"og:description\" content=\"The Hermite functions appear as the solutions of the quantum mechanical harmonic oscillator. But they have applications in many other fields and applications, e.g., pseudospectral methods. The Hermite functions $h_n(x)$ are defined asbegin{equation}label{eq:h}h_n(x) = frac{1}{sqrt{sqrt{pi} 2^n n!}} mathrm{e}^{-x^2\/2} H_n(x) ,,end{equation} where $H_n(x)$ denotes the $n$th Hermite polynomial defined via the recurrence relationbegin{equation}H_{n}(x) = 2xH_{n-1}(x)-2(n-1)H_{n-2}(x)end{equation} with&hellip; Continue reading Calculating the Hermite functions\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.numbercrunch.de\/blog\/2014\/08\/calculating-the-hermite-functions\/\" \/>\n<meta property=\"og:site_name\" content=\"Number Crunch\" \/>\n<meta property=\"article:published_time\" content=\"2014-08-21T21:30:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-01-18T21:26:23+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/numbercrunch.de\/blog\/wp-content\/uploads\/2014\/08\/h_800.png\" \/>\n<meta name=\"author\" content=\"Heiko Bauke\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Heiko Bauke\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/2014\/08\/calculating-the-hermite-functions\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/2014\/08\/calculating-the-hermite-functions\/\"},\"author\":{\"name\":\"Heiko Bauke\",\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/#\/schema\/person\/e73eab65b1721dd0c3d408edb887e413\"},\"headline\":\"Calculating the Hermite functions\",\"datePublished\":\"2014-08-21T21:30:09+00:00\",\"dateModified\":\"2023-01-18T21:26:23+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/2014\/08\/calculating-the-hermite-functions\/\"},\"wordCount\":458,\"commentCount\":4,\"publisher\":{\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/#\/schema\/person\/e73eab65b1721dd0c3d408edb887e413\"},\"image\":{\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/2014\/08\/calculating-the-hermite-functions\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/numbercrunch.de\/blog\/wp-content\/uploads\/2014\/08\/h_800.png\",\"articleSection\":[\"numerical methods\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.numbercrunch.de\/blog\/2014\/08\/calculating-the-hermite-functions\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/2014\/08\/calculating-the-hermite-functions\/\",\"url\":\"https:\/\/www.numbercrunch.de\/blog\/2014\/08\/calculating-the-hermite-functions\/\",\"name\":\"Calculating the Hermite functions - Number Crunch\",\"isPartOf\":{\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/2014\/08\/calculating-the-hermite-functions\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/2014\/08\/calculating-the-hermite-functions\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/numbercrunch.de\/blog\/wp-content\/uploads\/2014\/08\/h_800.png\",\"datePublished\":\"2014-08-21T21:30:09+00:00\",\"dateModified\":\"2023-01-18T21:26:23+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/2014\/08\/calculating-the-hermite-functions\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.numbercrunch.de\/blog\/2014\/08\/calculating-the-hermite-functions\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/2014\/08\/calculating-the-hermite-functions\/#primaryimage\",\"url\":\"https:\/\/numbercrunch.de\/blog\/wp-content\/uploads\/2014\/08\/h_800.png\",\"contentUrl\":\"https:\/\/numbercrunch.de\/blog\/wp-content\/uploads\/2014\/08\/h_800.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/2014\/08\/calculating-the-hermite-functions\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.numbercrunch.de\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Calculating the Hermite functions\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/#website\",\"url\":\"https:\/\/www.numbercrunch.de\/blog\/\",\"name\":\"Number Crunch\",\"description\":\"A computational science blog.\",\"publisher\":{\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/#\/schema\/person\/e73eab65b1721dd0c3d408edb887e413\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.numbercrunch.de\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/#\/schema\/person\/e73eab65b1721dd0c3d408edb887e413\",\"name\":\"Heiko Bauke\",\"logo\":{\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/#\/schema\/person\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Calculating the Hermite functions - Number Crunch","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.numbercrunch.de\/blog\/2014\/08\/calculating-the-hermite-functions\/","og_locale":"en_US","og_type":"article","og_title":"Calculating the Hermite functions - Number Crunch","og_description":"The Hermite functions appear as the solutions of the quantum mechanical harmonic oscillator. But they have applications in many other fields and applications, e.g., pseudospectral methods. The Hermite functions $h_n(x)$ are defined asbegin{equation}label{eq:h}h_n(x) = frac{1}{sqrt{sqrt{pi} 2^n n!}} mathrm{e}^{-x^2\/2} H_n(x) ,,end{equation} where $H_n(x)$ denotes the $n$th Hermite polynomial defined via the recurrence relationbegin{equation}H_{n}(x) = 2xH_{n-1}(x)-2(n-1)H_{n-2}(x)end{equation} with&hellip; Continue reading Calculating the Hermite functions","og_url":"https:\/\/www.numbercrunch.de\/blog\/2014\/08\/calculating-the-hermite-functions\/","og_site_name":"Number Crunch","article_published_time":"2014-08-21T21:30:09+00:00","article_modified_time":"2023-01-18T21:26:23+00:00","og_image":[{"url":"https:\/\/numbercrunch.de\/blog\/wp-content\/uploads\/2014\/08\/h_800.png","type":"","width":"","height":""}],"author":"Heiko Bauke","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Heiko Bauke","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.numbercrunch.de\/blog\/2014\/08\/calculating-the-hermite-functions\/#article","isPartOf":{"@id":"https:\/\/www.numbercrunch.de\/blog\/2014\/08\/calculating-the-hermite-functions\/"},"author":{"name":"Heiko Bauke","@id":"https:\/\/www.numbercrunch.de\/blog\/#\/schema\/person\/e73eab65b1721dd0c3d408edb887e413"},"headline":"Calculating the Hermite functions","datePublished":"2014-08-21T21:30:09+00:00","dateModified":"2023-01-18T21:26:23+00:00","mainEntityOfPage":{"@id":"https:\/\/www.numbercrunch.de\/blog\/2014\/08\/calculating-the-hermite-functions\/"},"wordCount":458,"commentCount":4,"publisher":{"@id":"https:\/\/www.numbercrunch.de\/blog\/#\/schema\/person\/e73eab65b1721dd0c3d408edb887e413"},"image":{"@id":"https:\/\/www.numbercrunch.de\/blog\/2014\/08\/calculating-the-hermite-functions\/#primaryimage"},"thumbnailUrl":"https:\/\/numbercrunch.de\/blog\/wp-content\/uploads\/2014\/08\/h_800.png","articleSection":["numerical methods"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.numbercrunch.de\/blog\/2014\/08\/calculating-the-hermite-functions\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.numbercrunch.de\/blog\/2014\/08\/calculating-the-hermite-functions\/","url":"https:\/\/www.numbercrunch.de\/blog\/2014\/08\/calculating-the-hermite-functions\/","name":"Calculating the Hermite functions - Number Crunch","isPartOf":{"@id":"https:\/\/www.numbercrunch.de\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.numbercrunch.de\/blog\/2014\/08\/calculating-the-hermite-functions\/#primaryimage"},"image":{"@id":"https:\/\/www.numbercrunch.de\/blog\/2014\/08\/calculating-the-hermite-functions\/#primaryimage"},"thumbnailUrl":"https:\/\/numbercrunch.de\/blog\/wp-content\/uploads\/2014\/08\/h_800.png","datePublished":"2014-08-21T21:30:09+00:00","dateModified":"2023-01-18T21:26:23+00:00","breadcrumb":{"@id":"https:\/\/www.numbercrunch.de\/blog\/2014\/08\/calculating-the-hermite-functions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.numbercrunch.de\/blog\/2014\/08\/calculating-the-hermite-functions\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.numbercrunch.de\/blog\/2014\/08\/calculating-the-hermite-functions\/#primaryimage","url":"https:\/\/numbercrunch.de\/blog\/wp-content\/uploads\/2014\/08\/h_800.png","contentUrl":"https:\/\/numbercrunch.de\/blog\/wp-content\/uploads\/2014\/08\/h_800.png"},{"@type":"BreadcrumbList","@id":"https:\/\/www.numbercrunch.de\/blog\/2014\/08\/calculating-the-hermite-functions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.numbercrunch.de\/blog\/"},{"@type":"ListItem","position":2,"name":"Calculating the Hermite functions"}]},{"@type":"WebSite","@id":"https:\/\/www.numbercrunch.de\/blog\/#website","url":"https:\/\/www.numbercrunch.de\/blog\/","name":"Number Crunch","description":"A computational science blog.","publisher":{"@id":"https:\/\/www.numbercrunch.de\/blog\/#\/schema\/person\/e73eab65b1721dd0c3d408edb887e413"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.numbercrunch.de\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.numbercrunch.de\/blog\/#\/schema\/person\/e73eab65b1721dd0c3d408edb887e413","name":"Heiko Bauke","logo":{"@id":"https:\/\/www.numbercrunch.de\/blog\/#\/schema\/person\/image\/"}}]}},"_links":{"self":[{"href":"https:\/\/www.numbercrunch.de\/blog\/wp-json\/wp\/v2\/posts\/545"}],"collection":[{"href":"https:\/\/www.numbercrunch.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.numbercrunch.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.numbercrunch.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.numbercrunch.de\/blog\/wp-json\/wp\/v2\/comments?post=545"}],"version-history":[{"count":42,"href":"https:\/\/www.numbercrunch.de\/blog\/wp-json\/wp\/v2\/posts\/545\/revisions"}],"predecessor-version":[{"id":977,"href":"https:\/\/www.numbercrunch.de\/blog\/wp-json\/wp\/v2\/posts\/545\/revisions\/977"}],"wp:attachment":[{"href":"https:\/\/www.numbercrunch.de\/blog\/wp-json\/wp\/v2\/media?parent=545"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.numbercrunch.de\/blog\/wp-json\/wp\/v2\/categories?post=545"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.numbercrunch.de\/blog\/wp-json\/wp\/v2\/tags?post=545"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}