Skip to content
Merged
45 changes: 32 additions & 13 deletions 05_swap.html
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ <h3>Relative and absolute efficiency</h3>
I still program in C++ because as far as I could ascertain it&rsquo;s the only language which allows me
generality and absolute efficiency.
I can program as general as I like.
I can talk about things like <a href="https://en.wikipedia.org/wiki/Monoid">monoids</a> and <a href="https://en.wikipedia.org/wiki/Semigroup">semi-groups</a>.
I can talk about things like <a href="https://en.wikipedia.org/wiki/Monoid">monoids</a> and <a href="https://en.wikipedia.org/wiki/Semigroup">semi-groups</a><sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a></sup>.
When it compiles I could look at assembly code and see it is good.
It is absolutely efficient.</p>

Expand Down Expand Up @@ -275,7 +275,7 @@ <h2>Swap</h2>
sequence, you swap.
So it is very important practically.
But it also happens to be very important
theoretically, because a long time ago when people were starting group theory<sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a></sup>
theoretically, because a long time ago when people were starting <a href="https://en.wikipedia.org/wiki/Group_theory">group theory</a>
they discovered that any permutation of a sequence could be generated out of swap<sup id="fnref:2"><a href="#fn:2" rel="footnote">2</a></sup>.
Swap is the most primitive operation.
The reason is sequence. And any other
Expand Down Expand Up @@ -482,15 +482,35 @@ <h2>Code</h2>
<hr/>
<ol>
<li id="fn:1">
<p><a href="https://en.wikipedia.org/wiki/Group_theory">Group theory</a>
is one of the main subjects of abstract algebra.
The key idea is that many mathematical structures behave similarly.
You can add and subtract numbers, you can add and subtract vectors and matrices.
Can we study all structures which can add and subtract all together?
It turns out you can, and one such structure is a group.</p>

<p>Alex&rsquo;s ideas about generic programming are inspired
by abstract algebra.</p><a href="#fnref:1" rev="footnote">&#8617;</a></li>
<p>Groups, monoids, and rings are a few of the subjects of abstract algebra,
a field which studies the fundamental properties of mathematical structures.
The key idea is that many different mathematical objects appear to function similarly.
Vectors and matrices can be &ldquo;added&rdquo; and &ldquo;subtracted&rdquo; just like integers.
In what ways are they fundamentally the same?
One explanation is that all of them form a group.
Below is a formal definition:</p>

<p>A <strong>group</strong> is a set <code>G</code> with a binary operation <code>* : G x G -&gt; G</code> such that:</p>

<ol>
<li><code>G</code> contains an identity element <code>e</code> in <code>G</code> such that <code>e * x = x * e = x</code> for all <code>x</code> in <code>G</code>.</li>
<li>The operation <code>*</code> is associative. So <code>((x * y) * z) = (x * (y * z))</code> for all <code>x, y, z</code> in <code>G</code>.</li>
<li>Every element <code>x</code> in <code>G</code> has an inverse element <code>y</code> such that x * y = y * x = e.</li>
</ol>


<p>For example integers are a group with the operation of addition and the identity element 0.</p>

<ol>
<li><code>0 + x = x + 0 = x</code></li>
<li><code>((x + y) + z) = (x + (y + z))</code>.</li>
<li><code>x + (-x) = (-x) + x = 0</code>.</li>
</ol>


<p>The process of discovering and applying generic concepts is very similar.
Alex introduces the basics of abstract algebra, from a programmers perspective,
in his book &ldquo;From Mathematics to Generic Programming&rdquo;.</p><a href="#fnref:1" rev="footnote">&#8617;</a></li>
<li id="fn:2">
<p>A <a href="https://en.wikipedia.org/wiki/Permutation_group">permutation</a>
is a bijection (1-1, onto) map from a set to itself.
Expand Down Expand Up @@ -537,8 +557,7 @@ <h2>Code</h2>
<li id="fn:4">
Alex himself uses Tropical semi-rings to describe
several algorithms in his book &ldquo;From Mathematics to Generic Programming&rdquo; (See chapter 8.6).
So his issue here is not algebraic abstractions, but pursuing abstraction
with enormous cost.<a href="#fnref:4" rev="footnote">&#8617;</a></li>
So his issue here is not abstraction itself, rather that it can become too costly.<a href="#fnref:4" rev="footnote">&#8617;</a></li>
<li id="fn:5">
<p>The <code>^</code> symbol is bitwise <a href="https://en.wikipedia.org/wiki/Exclusive_or">exclusive or</a>.
The expression <code>a ^ b</code> means <code>a</code> is true, or <code>b</code> is true, but not both.
Expand Down
38 changes: 26 additions & 12 deletions 05_swap.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ After all, I didn't start with C++.
I still program in C++ because as far as I could ascertain it's the only language which allows me
generality and absolute efficiency.
I can program as general as I like.
I can talk about things like [monoids][monoid] and [semi-groups][semi-group].
I can talk about things like [monoids][monoid] and [semi-groups][semi-group][^group-theory].
When it compiles I could look at assembly code and see it is good.
It is absolutely efficient.

Expand Down Expand Up @@ -146,7 +146,7 @@ Basically if you do something with a
sequence, you swap.
So it is very important practically.
But it also happens to be very important
theoretically, because a long time ago when people were starting group theory[^group-theory]
theoretically, because a long time ago when people were starting [group theory][group-theory]
they discovered that any permutation of a sequence could be generated out of swap[^permutation].
Swap is the most primitive operation.
The reason is sequence. And any other
Expand All @@ -159,16 +159,31 @@ the greatest language was great, and since it couldn't do swap,
what do you do?
You deny the utility of swap.

[^group-theory]: [Group theory](https://en.wikipedia.org/wiki/Group_theory)
is one of the main subjects of abstract algebra.
The key idea is that many mathematical structures behave similarly.
You can add and subtract numbers, you can add and subtract vectors and matrices.
Can we study all structures which can add and subtract all together?
It turns out you can, and one such structure is a group.
[group-theory]: https://en.wikipedia.org/wiki/Group_theory

Alex's ideas about generic programming are inspired
by abstract algebra.
[^group-theory]: Groups, monoids, and rings are a few of the subjects of abstract algebra,
a field which studies the fundamental properties of mathematical structures.
The key idea is that many different mathematical objects appear to function similarly.
Vectors and matrices can be "added" and "subtracted" just like integers.
In what ways are they fundamentally the same?
One explanation is that all of them form a group.
Below is a formal definition:

A **group** is a set `G` with a binary operation `* : G x G -> G` such that:

1. `G` contains an identity element `e` in `G` such that `e * x = x * e = x` for all `x` in `G`.
2. The operation `*` is associative. So `((x * y) * z) = (x * (y * z))` for all `x, y, z` in `G`.
3. Every element `x` in `G` has an inverse element `y` such that x * y = y * x = e.

For example integers are a group with the operation of addition and the identity element 0.

1. `0 + x = x + 0 = x`
2. `((x + y) + z) = (x + (y + z))`.
3. `x + (-x) = (-x) + x = 0`.

The process of discovering and applying generic concepts is very similar.
Alex introduces the basics of abstract algebra, from a programmers perspective,
in his book "From Mathematics to Generic Programming".

[^permutation]: A [permutation](https://en.wikipedia.org/wiki/Permutation_group)
is a bijection (1-1, onto) map from a set to itself.
Expand Down Expand Up @@ -261,8 +276,7 @@ and the `T` parameter is still generic.

[^tropical]: Alex himself uses Tropical semi-rings to describe
several algorithms in his book "From Mathematics to Generic Programming" (See chapter 8.6).
So his issue here is not algebraic abstractions, but pursuing abstraction
with enormous cost.
So his issue here is not abstraction itself, rather that it can become too costly.

[^move]:
Since C++11 this issue has been addressed by [move semantics](https://en.cppreference.com/w/cpp/language/move_constructor)
Expand Down
Loading