<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>C&#43;&#43; on Klaus K. Holst</title>
    <link>https://holst.it/tags/c&#43;&#43;/</link>
    <description>Recent content in C&#43;&#43; on Klaus K. Holst</description>
    <generator>Hugo -- 0.148.2</generator>
    <language>en</language>
    <lastBuildDate>Fri, 26 Feb 2021 17:33:00 +0100</lastBuildDate>
    <atom:link href="https://holst.it/tags/c++/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>ODEs with the targeted package using external pointers</title>
      <link>https://holst.it/posts/xptr/</link>
      <pubDate>Fri, 26 Feb 2021 17:33:00 +0100</pubDate>
      <guid>https://holst.it/posts/xptr/</guid>
      <description>&lt;p&gt;Mathematical and statistical software often relies on sequential computations.
Examples are &lt;strong&gt;likelihood evaluations&lt;/strong&gt; where it typically is necessary to loop over
the rows of the data, or solving &lt;strong&gt;ordinary differential equations&lt;/strong&gt; where
numerical approximations are based on looping over the evolving time. When using
high-level languages such as &lt;code&gt;R&lt;/code&gt; or &lt;code&gt;python&lt;/code&gt; such calculations can be very slow
unless the algorithms can be vectorized. Fortunately, it is straight-forward to
make the implementations in &lt;code&gt;C/C++&lt;/code&gt; and subsequently make an interface to &lt;code&gt;R&lt;/code&gt; and
&lt;code&gt;python&lt;/code&gt; (&lt;a href=&#34;https://cran.r-project.org/web/packages/Rcpp/index.html&#34; target=&#34;_blank&#34;&gt;Rcpp&lt;/a&gt; and &lt;a href=&#34;https://pybind11.readthedocs.io/en/stable/&#34; target=&#34;_blank&#34;&gt;pybind11&lt;/a&gt;).&lt;/p&gt;</description>
    </item>
    <item>
      <title>A simple ODE Class</title>
      <link>https://holst.it/posts/odesolver1/</link>
      <pubDate>Sun, 03 Nov 2019 07:19:00 +0100</pubDate>
      <guid>https://holst.it/posts/odesolver1/</guid>
      <description>&lt;p&gt;A small illustration on using the &lt;a href=&#34;http://arma.sourceforge.net/&#34; target=&#34;_blank&#34;&gt;&lt;code&gt;armadillo&lt;/code&gt;&lt;/a&gt; C++ linear algebra
library for solving an ordinary differential equation of the form
\[ X&amp;rsquo;(t) = F(t,X(t),U(t)).\]&lt;/p&gt;
&lt;p&gt;The abstract super class &lt;code&gt;Solver&lt;/code&gt;
defines the methods &lt;code&gt;solve&lt;/code&gt; (for approximating the solution in
user-defined time-points) and &lt;code&gt;solveint&lt;/code&gt; (for interpolating user-defined
input functions on a finer grid).  As an illustration a simple
Runge-Kutta solver is derived in the class &lt;code&gt;RK4&lt;/code&gt;.&lt;/p&gt;
&lt;figure&gt;
    &lt;img loading=&#34;lazy&#34; src=&#34;https://holst.it/ox-hugo/dot.svg&#34;/&gt; 
&lt;/figure&gt;

&lt;p&gt;The first step is to define the ODE, here a simple one-dimensional ODE
\(X&amp;rsquo;(t) = \theta\cdot\{U(t)-X(t)\}\) with a single input \(U(t)\):&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-c++&#34; data-lang=&#34;c++&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  rowvec &lt;span style=&#34;color:#0a0&#34;&gt;dX&lt;/span&gt;(&lt;span style=&#34;color:#00a&#34;&gt;const&lt;/span&gt; rowvec &amp;amp;input, // time (first element) and additional input variables
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  	     &lt;span style=&#34;color:#00a&#34;&gt;const&lt;/span&gt; rowvec &amp;amp;x,     &lt;span style=&#34;color:#aaa;font-style:italic&#34;&gt;// state variables
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#aaa;font-style:italic&#34;&gt;&lt;/span&gt;	     &lt;span style=&#34;color:#00a&#34;&gt;const&lt;/span&gt; rowvec &amp;amp;theta) {   &lt;span style=&#34;color:#aaa;font-style:italic&#34;&gt;// parameters
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#aaa;font-style:italic&#34;&gt;&lt;/span&gt;    rowvec res = { theta(&lt;span style=&#34;color:#099&#34;&gt;0&lt;/span&gt;)*theta(&lt;span style=&#34;color:#099&#34;&gt;1&lt;/span&gt;)*(input(&lt;span style=&#34;color:#099&#34;&gt;1&lt;/span&gt;)-x(&lt;span style=&#34;color:#099&#34;&gt;0&lt;/span&gt;)) };
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#00a&#34;&gt;return&lt;/span&gt;( res );
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  }
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The ODE may then be solved using the following syntax&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-c++&#34; data-lang=&#34;c++&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  odesolver::RK4 MyODE(dX);
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  arma::mat res = MyODE.solve(input, init, theta);
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;with the &lt;strong&gt;step size&lt;/strong&gt; defined implicitly by &lt;code&gt;input&lt;/code&gt; (first column is the time variable
and the following columns the optional different input variables) and
&lt;strong&gt;boundary conditions&lt;/strong&gt; defined by &lt;code&gt;init&lt;/code&gt;.&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
