{"id":633,"date":"2015-03-20T23:40:32","date_gmt":"2015-03-20T22:40:32","guid":{"rendered":"http:\/\/numbercrunch.de\/blog\/?p=633"},"modified":"2023-01-18T22:13:30","modified_gmt":"2023-01-18T21:13:30","slug":"c1114-for-scientific-computing-v","status":"publish","type":"post","link":"https:\/\/www.numbercrunch.de\/blog\/2015\/03\/c1114-for-scientific-computing-v\/","title":{"rendered":"C++11\/14 for scientific computing V"},"content":{"rendered":"<h2>$\\boldsymbol{\\lambda}$ functions<\/h2>\n<p style=\"text-align: justify;\">Anonymous functions, often called $\\lambda$ functions, are a common feature of scientific programming languages, e.g., Maple and Matlab. They are particularly useful when functions are employed, which take other functions as arguments.\u00a0 For example, a numerical root finding algorithm requires to specify a function as an input parameter.<\/p>\n<p style=\"text-align: justify;\">C++11 introduces $\\lambda$ functions to C++ and in C++14 they have been extended and become slightly more flexible.\u00a0 $\\lambda$ functions can be assigned to variables similar to <a href=\"http:\/\/en.wikipedia.org\/wiki\/Function_object#In_C_and_C.2B.2B\" target=\"_blank\" rel=\"noopener\">functors<\/a>.\u00a0 The main advantage of $\\lambda$ functions over functors is that they can be defined where needed.\u00a0 Furthermore, $\\lambda$ functions capture variables in the current scope, thus they represent a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Closure_%28computer_programming%29\" target=\"_blank\" rel=\"noopener\">closure<\/a>.<\/p>\n<p>The <a href=\"http:\/\/en.cppreference.com\/w\/cpp\/language\/lambda\" target=\"_blank\" rel=\"noopener\">syntax<\/a> of a $\\lambda$ function has basically the form<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">[ capture-list ] ( params ) -&gt; ret { body }<\/pre>\n<p style=\"text-align: justify;\">The possibly empty capture-list specifies which variables are captured and how they are captured (by value or by reference).\u00a0 Then the arguments and the return type are specified followed by the definition of the function body.\u00a0 The return type may be omitted if the function body consists of nothing but a single return statement with an expression.<\/p>\n<p style=\"text-align: justify;\">The use of $\\lambda$ functions is probably best explained by providing a specific example. In the following, a generic root finding algorithm (<a href=\"http:\/\/en.wikipedia.org\/wiki\/False_position_method\" target=\"_blank\" rel=\"noopener\">regula falsi<\/a>) is implemented, which looks in an interval for a root of some function.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">#include &lt;iostream&gt;\n#include &lt;iomanip&gt;\n#include &lt;cstdlib&gt;\n#include &lt;cmath&gt;\n#include &lt;limits&gt;\n\ntemplate&lt;typename T, typename F&gt;\nT regula_falsi(T s, T t, F f) {\n  typedef enum { none, left, right } side_t;\n  side_t side(none);\n  \/\/ starting values at endpoints of interval \n  T fs=f(s);\n  T ft=f(t);\n  T r;\n  for (int n=0; n&lt;std::numeric_limits&lt;T&gt;::digits+2; ++n) {\n    r=(fs*t - ft*s)\/(fs - ft);\n    if (std::abs(t-s)&lt;std::numeric_limits&lt;T&gt;::epsilon()*std::abs(t+s)) \n      break;\n    T fr=f(r);\n    if (fr*ft&gt;0) { \/\/ fr and ft have same sign, copy r to t\n      t=r; \n      ft=fr;\n      if (side==left) \n    fs\/=2;\n      side=left;\n    } else if (fs*fr&gt;0) { \/\/ fr and fs have same sign, copy r to s\n      s=r; \n      fs=fr;\n      if (side==right) \n    ft\/=2;\n      side=right;\n    } else { \/\/ fr*f_ very small (looks like zero)\n      break;\n    } \n  }\n  return r;\n}\n \nint main() {\n  \/\/ assign lambda function to variable and use afterwards\n  auto f = [](double x){ return std::cos(x)-x*x*x; };\n  double x0=regula_falsi(0., 1., f);\n  \/\/ specify lambda function directly as an argument\n  double x1=regula_falsi(0., 3., [](double x){ return std::cos(x); });\n  std::cout &lt;&lt; std::setprecision(12) \n        &lt;&lt; x0 &lt;&lt; '\\n'\n        &lt;&lt; x1 &lt;&lt; '\\n';\n  return 0;\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>$\\boldsymbol{\\lambda}$ functions Anonymous functions, often called $\\lambda$ functions, are a common feature of scientific programming languages, e.g., Maple and Matlab. They are particularly useful when functions are employed, which take other functions as arguments.\u00a0 For example, a numerical root finding algorithm requires to specify a function as an input parameter. C++11 introduces $\\lambda$ functions to&hellip; <a href=\"https:\/\/www.numbercrunch.de\/blog\/2015\/03\/c1114-for-scientific-computing-v\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">C++11\/14 for scientific computing V<\/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":[16],"tags":[],"class_list":["post-633","post","type-post","status-publish","format-standard","hentry","category-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C++11\/14 for scientific computing V - 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\/2015\/03\/c1114-for-scientific-computing-v\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++11\/14 for scientific computing V - Number Crunch\" \/>\n<meta property=\"og:description\" content=\"$boldsymbol{lambda}$ functions Anonymous functions, often called $lambda$ functions, are a common feature of scientific programming languages, e.g., Maple and Matlab. They are particularly useful when functions are employed, which take other functions as arguments.\u00a0 For example, a numerical root finding algorithm requires to specify a function as an input parameter. C++11 introduces $lambda$ functions to&hellip; Continue reading C++11\/14 for scientific computing V\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.numbercrunch.de\/blog\/2015\/03\/c1114-for-scientific-computing-v\/\" \/>\n<meta property=\"og:site_name\" content=\"Number Crunch\" \/>\n<meta property=\"article:published_time\" content=\"2015-03-20T22:40:32+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-01-18T21:13:30+00:00\" \/>\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=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/2015\/03\/c1114-for-scientific-computing-v\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/2015\/03\/c1114-for-scientific-computing-v\/\"},\"author\":{\"name\":\"Heiko Bauke\",\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/#\/schema\/person\/e73eab65b1721dd0c3d408edb887e413\"},\"headline\":\"C++11\/14 for scientific computing V\",\"datePublished\":\"2015-03-20T22:40:32+00:00\",\"dateModified\":\"2023-01-18T21:13:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/2015\/03\/c1114-for-scientific-computing-v\/\"},\"wordCount\":219,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/#\/schema\/person\/e73eab65b1721dd0c3d408edb887e413\"},\"articleSection\":[\"C++\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.numbercrunch.de\/blog\/2015\/03\/c1114-for-scientific-computing-v\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/2015\/03\/c1114-for-scientific-computing-v\/\",\"url\":\"https:\/\/www.numbercrunch.de\/blog\/2015\/03\/c1114-for-scientific-computing-v\/\",\"name\":\"C++11\/14 for scientific computing V - Number Crunch\",\"isPartOf\":{\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/#website\"},\"datePublished\":\"2015-03-20T22:40:32+00:00\",\"dateModified\":\"2023-01-18T21:13:30+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/2015\/03\/c1114-for-scientific-computing-v\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.numbercrunch.de\/blog\/2015\/03\/c1114-for-scientific-computing-v\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/2015\/03\/c1114-for-scientific-computing-v\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.numbercrunch.de\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++11\/14 for scientific computing V\"}]},{\"@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":"C++11\/14 for scientific computing V - 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\/2015\/03\/c1114-for-scientific-computing-v\/","og_locale":"en_US","og_type":"article","og_title":"C++11\/14 for scientific computing V - Number Crunch","og_description":"$boldsymbol{lambda}$ functions Anonymous functions, often called $lambda$ functions, are a common feature of scientific programming languages, e.g., Maple and Matlab. They are particularly useful when functions are employed, which take other functions as arguments.\u00a0 For example, a numerical root finding algorithm requires to specify a function as an input parameter. C++11 introduces $lambda$ functions to&hellip; Continue reading C++11\/14 for scientific computing V","og_url":"https:\/\/www.numbercrunch.de\/blog\/2015\/03\/c1114-for-scientific-computing-v\/","og_site_name":"Number Crunch","article_published_time":"2015-03-20T22:40:32+00:00","article_modified_time":"2023-01-18T21:13:30+00:00","author":"Heiko Bauke","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Heiko Bauke","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.numbercrunch.de\/blog\/2015\/03\/c1114-for-scientific-computing-v\/#article","isPartOf":{"@id":"https:\/\/www.numbercrunch.de\/blog\/2015\/03\/c1114-for-scientific-computing-v\/"},"author":{"name":"Heiko Bauke","@id":"https:\/\/www.numbercrunch.de\/blog\/#\/schema\/person\/e73eab65b1721dd0c3d408edb887e413"},"headline":"C++11\/14 for scientific computing V","datePublished":"2015-03-20T22:40:32+00:00","dateModified":"2023-01-18T21:13:30+00:00","mainEntityOfPage":{"@id":"https:\/\/www.numbercrunch.de\/blog\/2015\/03\/c1114-for-scientific-computing-v\/"},"wordCount":219,"commentCount":0,"publisher":{"@id":"https:\/\/www.numbercrunch.de\/blog\/#\/schema\/person\/e73eab65b1721dd0c3d408edb887e413"},"articleSection":["C++"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.numbercrunch.de\/blog\/2015\/03\/c1114-for-scientific-computing-v\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.numbercrunch.de\/blog\/2015\/03\/c1114-for-scientific-computing-v\/","url":"https:\/\/www.numbercrunch.de\/blog\/2015\/03\/c1114-for-scientific-computing-v\/","name":"C++11\/14 for scientific computing V - Number Crunch","isPartOf":{"@id":"https:\/\/www.numbercrunch.de\/blog\/#website"},"datePublished":"2015-03-20T22:40:32+00:00","dateModified":"2023-01-18T21:13:30+00:00","breadcrumb":{"@id":"https:\/\/www.numbercrunch.de\/blog\/2015\/03\/c1114-for-scientific-computing-v\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.numbercrunch.de\/blog\/2015\/03\/c1114-for-scientific-computing-v\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.numbercrunch.de\/blog\/2015\/03\/c1114-for-scientific-computing-v\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.numbercrunch.de\/blog\/"},{"@type":"ListItem","position":2,"name":"C++11\/14 for scientific computing V"}]},{"@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\/633"}],"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=633"}],"version-history":[{"count":12,"href":"https:\/\/www.numbercrunch.de\/blog\/wp-json\/wp\/v2\/posts\/633\/revisions"}],"predecessor-version":[{"id":966,"href":"https:\/\/www.numbercrunch.de\/blog\/wp-json\/wp\/v2\/posts\/633\/revisions\/966"}],"wp:attachment":[{"href":"https:\/\/www.numbercrunch.de\/blog\/wp-json\/wp\/v2\/media?parent=633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.numbercrunch.de\/blog\/wp-json\/wp\/v2\/categories?post=633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.numbercrunch.de\/blog\/wp-json\/wp\/v2\/tags?post=633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}