<?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>Programming on Side Effects</title><link>https://marcinklimek.github.io/tags/programming/</link><description>Recent content in Programming on Side Effects</description><generator>Hugo -- gohugo.io</generator><language>en</language><lastBuildDate>Mon, 26 Feb 2024 16:33:54 +0100</lastBuildDate><atom:link href="https://marcinklimek.github.io/tags/programming/index.xml" rel="self" type="application/rss+xml"/><item><title>Gaussian Elimination</title><link>https://marcinklimek.github.io/posts/2024-02-26-gaussian-elimination/</link><pubDate>Mon, 26 Feb 2024 16:33:54 +0100</pubDate><guid>https://marcinklimek.github.io/posts/2024-02-26-gaussian-elimination/</guid><description>&lt;p>&lt;a href="https://injuly.in/blog/gaussian-elimination/index.html">A visual explanation of Gaussian Elimination&lt;/a>&lt;/p>
&lt;p>This post delves into the intuition behind Gaussian Elimination, a cornerstone algorithm in linear algebra for solving systems of equations. It revisits the algorithm through a simple 2D example, emphasizing matrix representation and permissible operations to simplify systems. The post uniquely visualizes the process, explaining how transformations parallel to coordinate axes help deduce solutions, extending the concept from 2D lines to 3D planes and N-dimensional hyperplanes. It also explores the method&amp;rsquo;s application in higher dimensions, aiming to demystify the mathematical logic behind this widely used technique.&lt;/p></description><content:encoded><![CDATA[<p><a href="https://injuly.in/blog/gaussian-elimination/index.html">A visual explanation of Gaussian Elimination</a></p>
<p>This post delves into the intuition behind Gaussian Elimination, a cornerstone algorithm in linear algebra for solving systems of equations. It revisits the algorithm through a simple 2D example, emphasizing matrix representation and permissible operations to simplify systems. The post uniquely visualizes the process, explaining how transformations parallel to coordinate axes help deduce solutions, extending the concept from 2D lines to 3D planes and N-dimensional hyperplanes. It also explores the method&rsquo;s application in higher dimensions, aiming to demystify the mathematical logic behind this widely used technique.</p>
]]></content:encoded></item><item><title>Contact</title><link>https://marcinklimek.github.io/contact/</link><pubDate>Thu, 28 Feb 2019 00:00:00 +0000</pubDate><guid>https://marcinklimek.github.io/contact/</guid><description>Contact</description><content:encoded>&lt;p>&lt;a href="https://github.com/marcinklimek">github.com/marcinklimek&lt;/a>&lt;/p>
</content:encoded></item><item><title/><link>https://marcinklimek.github.io/posts/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://marcinklimek.github.io/posts/</guid><description/><content:encoded></content:encoded></item><item><title>Unordered erase</title><link>https://marcinklimek.github.io/posts/unordered-erase/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://marcinklimek.github.io/posts/unordered-erase/</guid><description>&lt;h3 id="unordered-erase">Unordered erase&lt;/h3>
&lt;p>I believe the most frequently used collection in C++ is &lt;code>vector&lt;/code>. We know that a &lt;code>vector&lt;/code> is great for everything except inserting or deleting items from random parts of the collection. This takes &lt;code>O(n)&lt;/code> time, which is why I always feel a bit sad deleting something from the middle of a vector—since it has to shuffle about half of its contents just to shift slightly to the left.&lt;/p></description><content:encoded><![CDATA[<h3 id="unordered-erase">Unordered erase</h3>
<p>I believe the most frequently used collection in C++ is <code>vector</code>. We know that a <code>vector</code> is great for everything except inserting or deleting items from random parts of the collection. This takes <code>O(n)</code> time, which is why I always feel a bit sad deleting something from the middle of a vector—since it has to shuffle about half of its contents just to shift slightly to the left.</p>
<p>There&rsquo;s an idiomatic trick that can turn <code>O(n)</code> into <code>O(1)</code>, but it comes at the cost of losing the element order in the vector. So, if you&rsquo;re ready to pay the price, this simple trick is definitely the way to go:</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span><span class="lnt">9
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">std::vector&lt;int&gt; v {
</span></span><span class="line"><span class="cl">  17, -2, 1084, 1, 17, 40, -11
</span></span><span class="line"><span class="cl">};
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">// we delete 1 from the vector
</span></span><span class="line"><span class="cl">std::swap(v[3], v.back()); 
</span></span><span class="line"><span class="cl">v.pop_back();
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">// we get [17, -2, 1084, -11, 17, 40]
</span></span></code></pre></td></tr></table>
</div>
</div><p>What did we do? First, we replaced the last element of the vector with the one marked for deletion, and then simply discarded it. Both operations are super cheap—it&rsquo;s simple and beautiful.</p>
<p>Why the vector interface doesn&rsquo;t have such a simple alternative to the usual <code>erase</code> method is unclear. In Rust, for example, <a href="https://doc.rust-lang.org/std/vec/struct.Vec.html#method.swap_remove">it exists</a>.</p>
<p>Well, we&rsquo;ll have to create our own helper function for the code base:</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">template&lt;typename T&gt;
</span></span><span class="line"><span class="cl">void unorderedErase(std::vector&lt;T&gt;&amp; v, int index)
</span></span><span class="line"><span class="cl">{
</span></span><span class="line"><span class="cl">  std::swap(v[index], v.back());
</span></span><span class="line"><span class="cl">  v.pop_back();
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></td></tr></table>
</div>
</div><p><em>Source: <a href="https://pvs-studio.com/">PVS‑Studio</a></em></p>
]]></content:encoded></item></channel></rss>