<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Digital Ambulation &#187; c</title>
	<atom:link href="http://www.alanbriolat.co.uk/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.alanbriolat.co.uk</link>
	<description>Life, programming and general geekery</description>
	<lastBuildDate>Mon, 09 Jan 2012 12:07:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>C Is Not MATLAB</title>
		<link>http://www.alanbriolat.co.uk/2009/03/c-is-not-matlab/</link>
		<comments>http://www.alanbriolat.co.uk/2009/03/c-is-not-matlab/#comments</comments>
		<pubDate>Mon, 30 Mar 2009 15:04:59 +0000</pubDate>
		<dc:creator>Alan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.alanbriolat.co.uk/?p=255</guid>
		<description><![CDATA[Refactoring some code at work today, I came across a classic example of somebody attempting to use one language like another. The project in question is a port from MATLAB to C, and the purpose of the function is to see if a square matrix is symmetrical within a certain tolerance. The implementation I replaced [...]]]></description>
			<content:encoded><![CDATA[<p>Refactoring some code at work today, I came across a classic example of somebody attempting to use one language like another.  The project in question is a port from MATLAB to C, and the purpose of the function is to see if a square matrix is symmetrical within a certain tolerance.</p>
<p>The implementation I replaced worked somewhat like this:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> check_symmetry<span style="color: #009900;">&#40;</span><span style="color: #993333;">double</span> <span style="color: #339933;">*</span>mtx<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> n<span style="color: #339933;">,</span> <span style="color: #993333;">double</span> tolerance<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #808080; font-style: italic;">/* Allocate memory to work in */</span>
    <span style="color: #993333;">double</span> <span style="color: #339933;">*</span>mtx_trans <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">double</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> malloc<span style="color: #009900;">&#40;</span>n <span style="color: #339933;">*</span> n <span style="color: #339933;">*</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">double</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">double</span> <span style="color: #339933;">*</span>mtx_diff <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">double</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> malloc<span style="color: #009900;">&#40;</span>n <span style="color: #339933;">*</span> n <span style="color: #339933;">*</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">double</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* Matrix transpose */</span>
    matxtrans<span style="color: #009900;">&#40;</span>mtx<span style="color: #339933;">,</span> n<span style="color: #339933;">,</span> n<span style="color: #339933;">,</span> mtx_trans<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #808080; font-style: italic;">/* Matrix subtract */</span>
    matxsub<span style="color: #009900;">&#40;</span>mtx<span style="color: #339933;">,</span> n<span style="color: #339933;">,</span> n<span style="color: #339933;">,</span> mtx_trans<span style="color: #339933;">,</span> n<span style="color: #339933;">,</span> n<span style="color: #339933;">,</span> mtx_sub<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* Assume matrix is symmetric to start with */</span>
    <span style="color: #993333;">int</span> symmetric <span style="color: #339933;">=</span> TRUE<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* Iterate through rows */</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> n<span style="color: #339933;">;</span> <span style="color: #339933;">++</span>i<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #808080; font-style: italic;">/* Iterate through columns */</span>
        <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> j <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> n<span style="color: #339933;">;</span> <span style="color: #339933;">++</span>j<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #808080; font-style: italic;">/* Was the difference greater than the tolerance? */</span>
            <span style="color: #993333;">double</span> val <span style="color: #339933;">=</span> mtx_sub<span style="color: #009900;">&#91;</span>i <span style="color: #339933;">*</span> n <span style="color: #339933;">+</span> j<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>fabs<span style="color: #009900;">&#40;</span>val<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> tolerance<span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #808080; font-style: italic;">/* Exception found: matrix not symmetric
                 * (but notice that we keep looping...) */</span>
                symmetric <span style="color: #339933;">=</span> FALSE<span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* Clean up memory allocations */</span>
    free<span style="color: #009900;">&#40;</span>mtx_trans<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    free<span style="color: #009900;">&#40;</span>mtx_diff<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* ... and return the result */</span>
    <span style="color: #b1b100;">return</span> symmetric<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now, what this is really doing is the equivalent to the following line of MATLAB:</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;">symmetric = <span style="color: #0000FF;">max</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">abs</span><span style="color: #080;">&#40;</span>mtx - mtx'<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> &gt; tol;</pre></div></div>

<p>This in itself highlights the massive differences between MATLAB, where matrix operations are fundamental, and C, where they are not.  While the above C code gets the correct answer, it&#8217;s far from efficient.  What should be obvious is that the programmer translated the code, not the meaning.</p>
<p>To analyse this simple case: a symmetric matrix is reflected in the diagonal&#8212;the lower triangle is the same as the upper.  What we really are trying to find out is if any elements violate this constraint.  More verbosely: &#8220;does any element in the upper triangle differ from it&#8217;s corresponding element in the lower triangle (by more than a certain value)?&#8221;  Once we&#8217;ve figured this out, the operation in C is much, much shorter:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> check_symmetry<span style="color: #009900;">&#40;</span><span style="color: #993333;">double</span> <span style="color: #339933;">*</span>mtx<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> n<span style="color: #339933;">,</span> <span style="color: #993333;">double</span> tolerance<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #808080; font-style: italic;">/* Iterate through upper triangle only */</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> n<span style="color: #339933;">;</span> <span style="color: #339933;">++</span>i<span style="color: #009900;">&#41;</span>
        <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> j <span style="color: #339933;">=</span> i <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> n<span style="color: #339933;">;</span> <span style="color: #339933;">++</span>j<span style="color: #009900;">&#41;</span>
            <span style="color: #808080; font-style: italic;">/* Compare to lower triangle */</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>fabs<span style="color: #009900;">&#40;</span>mtx<span style="color: #009900;">&#91;</span>i <span style="color: #339933;">*</span> n <span style="color: #339933;">+</span> j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">-</span> m<span style="color: #009900;">&#91;</span>j <span style="color: #339933;">*</span> n <span style="color: #339933;">+</span> i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> tolerance<span style="color: #009900;">&#41;</span>
                <span style="color: #808080; font-style: italic;">/* Exception found: matrix not symmetric */</span>
                <span style="color: #b1b100;">return</span> FALSE<span style="color: #339933;">;</span>
    <span style="color: #808080; font-style: italic;">/* No exceptions found: matrix is symmetric */</span>
    <span style="color: #b1b100;">return</span> TRUE<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This achieves the minimum number of iterations and element accesses by ignoring the diagonal, only iterating through the upper triangle and reversing the indices to access the lower triangle.  The only potential downside is that it&#8217;s not as obvious what&#8217;s going on, but <em>that&#8217;s what comments are for!</em></p>
<p>The lesson is that you should always make sure you&#8217;re using the correct method for the tool, and not trying to hammer a screw in with a spanner.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alanbriolat.co.uk/2009/03/c-is-not-matlab/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C linked list macro</title>
		<link>http://www.alanbriolat.co.uk/2008/12/c-linked-list-macro/</link>
		<comments>http://www.alanbriolat.co.uk/2008/12/c-linked-list-macro/#comments</comments>
		<pubDate>Fri, 19 Dec 2008 16:29:00 +0000</pubDate>
		<dc:creator>Alan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[snippets]]></category>

		<guid isPermaLink="false">http://alanbriolat.com/?p=56</guid>
		<description><![CDATA[Here&#8217;s a simple macro I came up with a few weeks ago for easily defining linked list types in C. #define LINKED_LIST(type, name) \ struct name ## _ { \ struct name ## _ * next; \ type data; \ }; \ typedef struct name ## _ name; Usage is simple: LINKED_LIST&#40;int, int_ll&#41;; gives the [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a simple macro I came up with a few weeks ago for easily defining linked list types in C.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#define LINKED_LIST(type, name)     \
    struct name ## _ {              \
        struct name ## _ * next;    \
        type data;                  \
    };                              \
    typedef struct name ## _ name;</span></pre></div></div>

<p>Usage is simple:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">LINKED_LIST<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #339933;">,</span> int_ll<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>gives the same type as</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">struct</span> int_ll_ <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">struct</span> int_ll_ <span style="color: #339933;">*</span> next<span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span> data<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #993333;">typedef</span> int_ll_ int_ll<span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.alanbriolat.co.uk/2008/12/c-linked-list-macro/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

