<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Rod Carvalho</title>
	<atom:link href="http://stochastix.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://stochastix.wordpress.com</link>
	<description></description>
	<lastBuildDate>Fri, 27 Jan 2012 09:11:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='stochastix.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://1.gravatar.com/blavatar/b0e76faf2071260b9a9604a2923d3591?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Rod Carvalho</title>
		<link>http://stochastix.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://stochastix.wordpress.com/osd.xml" title="Rod Carvalho" />
	<atom:link rel='hub' href='http://stochastix.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Cascading systems in Haskell</title>
		<link>http://stochastix.wordpress.com/2012/01/25/cascading-systems-in-haskell/</link>
		<comments>http://stochastix.wordpress.com/2012/01/25/cascading-systems-in-haskell/#comments</comments>
		<pubDate>Wed, 25 Jan 2012 16:22:41 +0000</pubDate>
		<dc:creator>Rod Carvalho</dc:creator>
				<category><![CDATA[Haskell]]></category>
		<category><![CDATA[LTI Systems]]></category>
		<category><![CDATA[Systems Theory]]></category>
		<category><![CDATA[List Processing]]></category>
		<category><![CDATA[Signals & Systems]]></category>

		<guid isPermaLink="false">http://stochastix.wordpress.com/?p=7318</guid>
		<description><![CDATA[Last weekend we learned how to build systems (LTI or otherwise) using Haskell. We now want to construct interconnections of systems. In this post we will study the series interconnection of systems, usually known as cascade interconnection. Parallel and feedback interconnections will be discussed in future posts. __________ Cascading two LTI systems Let us consider [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stochastix.wordpress.com&amp;blog=300626&amp;post=7318&amp;subd=stochastix&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Last weekend we <a href="http://stochastix.wordpress.com/2012/01/21/discrete-time-lti-systems-in-haskell/" target="_blank">learned how</a> <a href="http://stochastix.wordpress.com/2012/01/22/discrete-time-lti-systems-in-haskell-ii/" target="_blank">to build systems</a> (LTI or otherwise) using <a href="http://en.wikipedia.org/wiki/Haskell_%28programming_language%29" target="_blank">Haskell</a>. We now want to construct <em>interconnections</em> of systems. In this post we will study the <em>series</em> interconnection of systems, usually known as <em>cascade</em> interconnection. Parallel and feedback interconnections will be discussed in future posts.</p>
<p style="text-align:center;">__________</p>
<p><strong>Cascading two LTI systems</strong></p>
<p>Let us consider the <em>series</em> interconnection of two causal discrete-time LTI systems, <img src='http://s0.wp.com/latex.php?latex=%5Cmathcal%7BH%7D_1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathcal{H}_1' title='&#92;mathcal{H}_1' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%5Cmathcal%7BH%7D_2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathcal{H}_2' title='&#92;mathcal{H}_2' class='latex' />, as depicted below</p>
<p><a href="http://stochastix.files.wordpress.com/2012/01/cascade-of-h1-and-h2-2.png"><img class="alignnone size-full wp-image-7322" title="Cascade of H1 and H2 (2)" src="http://stochastix.files.wordpress.com/2012/01/cascade-of-h1-and-h2-2.png?w=450&#038;h=115" alt="" width="450" height="115" /></a></p>
<p>where <img src='http://s0.wp.com/latex.php?latex=y+%3D+%5Cmathcal%7BH%7D_1+%28x%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='y = &#92;mathcal{H}_1 (x)' title='y = &#92;mathcal{H}_1 (x)' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=w+%3D%5Cmathcal%7BH%7D_2+%28y%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='w =&#92;mathcal{H}_2 (y)' title='w =&#92;mathcal{H}_2 (y)' class='latex' /> are the outputs of each LTI system in the cascade. Since the output of system <img src='http://s0.wp.com/latex.php?latex=%5Cmathcal%7BH%7D_1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathcal{H}_1' title='&#92;mathcal{H}_1' class='latex' /> is the input of system <img src='http://s0.wp.com/latex.php?latex=%5Cmathcal%7BH%7D_2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathcal{H}_2' title='&#92;mathcal{H}_2' class='latex' />, we have <img src='http://s0.wp.com/latex.php?latex=w+%3D+%28%5Cmathcal%7BH%7D_2+%5Ccirc+%5Cmathcal%7BH%7D_1%29+%28x%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='w = (&#92;mathcal{H}_2 &#92;circ &#92;mathcal{H}_1) (x)' title='w = (&#92;mathcal{H}_2 &#92;circ &#92;mathcal{H}_1) (x)' class='latex' />.</p>
<p>What LTI systems should we consider? Let us choose the simplest ones: the <em>accumulator</em> and the <em>differentiator</em>. Thus, let <img src='http://s0.wp.com/latex.php?latex=%5Cmathcal%7BH%7D_1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathcal{H}_1' title='&#92;mathcal{H}_1' class='latex' /> be an <em>accumulator</em> (also known as &#8220;discrete-time integrator&#8221;), whose input-output relationship is as follows</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=y+%28n%29+%3D+y+%28n-1%29+%2B+x+%28n%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='y (n) = y (n-1) + x (n)' title='y (n) = y (n-1) + x (n)' class='latex' />,</p>
<p>and let <img src='http://s0.wp.com/latex.php?latex=%5Cmathcal%7BH%7D_2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathcal{H}_2' title='&#92;mathcal{H}_2' class='latex' /> be a <em>first difference</em> operator (also known as &#8220;discrete-time differentiator&#8221;), whose input-output relationship is</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=w+%28n%29+%3D+y+%28n%29+-+y+%28n-1%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='w (n) = y (n) - y (n-1)' title='w (n) = y (n) - y (n-1)' class='latex' />.</p>
<p>Note that the output of the cascade of these two LTI systems is thus</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=w+%28n%29+%3D+y+%28n%29+-+y+%28n-1%29+%3D+%28y+%28n-1%29+%2B+x+%28n%29%29+-+y+%28n-1%29+%3D+x+%28n%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='w (n) = y (n) - y (n-1) = (y (n-1) + x (n)) - y (n-1) = x (n)' title='w (n) = y (n) - y (n-1) = (y (n-1) + x (n)) - y (n-1) = x (n)' class='latex' /></p>
<p>and, hence, the cascade is input-output equivalent to the identity operator. Since <img src='http://s0.wp.com/latex.php?latex=%28%5Cmathcal%7BH%7D_2+%5Ccirc+%5Cmathcal%7BH%7D_1%29+%28x%29+%3D+x&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='(&#92;mathcal{H}_2 &#92;circ &#92;mathcal{H}_1) (x) = x' title='(&#92;mathcal{H}_2 &#92;circ &#92;mathcal{H}_1) (x) = x' class='latex' /> for all signals <img src='http://s0.wp.com/latex.php?latex=x&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x' title='x' class='latex' />, we say that <img src='http://s0.wp.com/latex.php?latex=%5Cmathcal%7BH%7D_2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathcal{H}_2' title='&#92;mathcal{H}_2' class='latex' /> is the left-inverse of system <img src='http://s0.wp.com/latex.php?latex=%5Cmathcal%7BH%7D_1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathcal{H}_1' title='&#92;mathcal{H}_1' class='latex' />. Since both systems are LTI, the operators commute, i.e., <img src='http://s0.wp.com/latex.php?latex=%5Cmathcal%7BH%7D_2+%5Ccirc+%5Cmathcal%7BH%7D_1+%3D+%5Cmathcal%7BH%7D_1+%5Ccirc+%5Cmathcal%7BH%7D_2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathcal{H}_2 &#92;circ &#92;mathcal{H}_1 = &#92;mathcal{H}_1 &#92;circ &#92;mathcal{H}_2' title='&#92;mathcal{H}_2 &#92;circ &#92;mathcal{H}_1 = &#92;mathcal{H}_1 &#92;circ &#92;mathcal{H}_2' class='latex' /> and, therefore, <img src='http://s0.wp.com/latex.php?latex=%5Cmathcal%7BH%7D_2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathcal{H}_2' title='&#92;mathcal{H}_2' class='latex' /> is also the right-inverse of system <img src='http://s0.wp.com/latex.php?latex=%5Cmathcal%7BH%7D_1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathcal{H}_1' title='&#92;mathcal{H}_1' class='latex' />. Since <img src='http://s0.wp.com/latex.php?latex=%5Cmathcal%7BH%7D_2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathcal{H}_2' title='&#92;mathcal{H}_2' class='latex' /> is the left- and right-inverse of <img src='http://s0.wp.com/latex.php?latex=%5Cmathcal%7BH%7D_1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathcal{H}_1' title='&#92;mathcal{H}_1' class='latex' />, we say that <img src='http://s0.wp.com/latex.php?latex=%5Cmathcal%7BH%7D_2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathcal{H}_2' title='&#92;mathcal{H}_2' class='latex' /> is the inverse of system <img src='http://s0.wp.com/latex.php?latex=%5Cmathcal%7BH%7D_1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathcal{H}_1' title='&#92;mathcal{H}_1' class='latex' /> (and vice-versa). Do keep in mind, however, that not all systems are <em>invertible</em>. For details, take a look at Oppenheim &amp; Willsky [1].</p>
<p style="text-align:center;">__________</p>
<p><strong>Implementation in Haskell</strong></p>
<p>We can easily find a state-space realization for the accumulator, but that will not be necessary. As in previous posts, let us view discrete-signals as lists. Thus, the accumulator takes a list <img src='http://s0.wp.com/latex.php?latex=%5Bx_0%2C+x_1%2C+x_2%2C+%5Cdots%5D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='[x_0, x_1, x_2, &#92;dots]' title='[x_0, x_1, x_2, &#92;dots]' class='latex' />, and returns the following list</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5By_0%2C+y_1%2C+y_2%2C+%5Cdots%5D+%3D+%5Bx_0%2C+x_0+%2B+x_1%2C+x_0+%2B+x_1+%2B+x_2%2C+%5Cdots%5D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='[y_0, y_1, y_2, &#92;dots] = [x_0, x_0 + x_1, x_0 + x_1 + x_2, &#92;dots]' title='[y_0, y_1, y_2, &#92;dots] = [x_0, x_0 + x_1, x_0 + x_1 + x_2, &#92;dots]' class='latex' /></p>
<p>where we assume that the initial condition of the accumulator is zero (i.e., <img src='http://s0.wp.com/latex.php?latex=y_%7B-1%7D+%3D+0&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='y_{-1} = 0' title='y_{-1} = 0' class='latex' />). Instead of using <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:scanl" target="_blank">scanl</a> yet once again (which would require us to drop the head of the list), let us now use <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:scanl1" target="_blank">scanl1</a> to implement the accumulator:</p>
<pre style="padding-left:30px;background:#EEEEEE;">acc :: Num a =&gt; System a a
acc = scanl1 (+)</pre>
<p>Please note that if the initial condition of the accumulator is not zero, we should use the following code instead:</p>
<pre style="padding-left:30px;background:#EEEEEE;">acc' :: Num a =&gt; System a a
acc' us = tail $ scanl (+) acc_ini us</pre>
<p>where <em>acc_ini</em> is the initial condition of the accumulator (analogous to the <a href="http://en.wikipedia.org/wiki/Constant_of_integration" target="_blank">constant of integration</a> in integral calculus).</p>
<p>The differentiator is not a <em>proper</em> system [2] and, therefore, it has no state-space realization. The differentiator takes a list <img src='http://s0.wp.com/latex.php?latex=%5By_0%2C+y_1%2C+y_2%2C+%5Cdots%5D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='[y_0, y_1, y_2, &#92;dots]' title='[y_0, y_1, y_2, &#92;dots]' class='latex' />, and returns the following list</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Bw_0%2C+w_1%2C+w_2%2C+%5Cdots%5D+%3D+%5By_0%2C+y_1+-+y_0%2C+y_2+-+y_1%2C+%5Cdots%5D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='[w_0, w_1, w_2, &#92;dots] = [y_0, y_1 - y_0, y_2 - y_1, &#92;dots]' title='[w_0, w_1, w_2, &#92;dots] = [y_0, y_1 - y_0, y_2 - y_1, &#92;dots]' class='latex' /></p>
<p>where we again assume that <img src='http://s0.wp.com/latex.php?latex=y_%7B-1%7D+%3D+0&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='y_{-1} = 0' title='y_{-1} = 0' class='latex' />. Note the following</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Bw_0%2C+w_1%2C+w_2%2C+%5Cdots%5D+%3D+%5By_0%2C+y_1%2C+y_2%2C+%5Cdots%5D+-+%5B0%2C+y_0%2C+y_1%2C+%5Cdots%5D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='[w_0, w_1, w_2, &#92;dots] = [y_0, y_1, y_2, &#92;dots] - [0, y_0, y_1, &#92;dots]' title='[w_0, w_1, w_2, &#92;dots] = [y_0, y_1, y_2, &#92;dots] - [0, y_0, y_1, &#92;dots]' class='latex' /></p>
<p>i.e., list <img src='http://s0.wp.com/latex.php?latex=w&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='w' title='w' class='latex' /> is obtained by elementwise subtraction of a right-shifted version of list <img src='http://s0.wp.com/latex.php?latex=y&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='y' title='y' class='latex' /> from list <img src='http://s0.wp.com/latex.php?latex=y&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='y' title='y' class='latex' /> itself. Subtracting two lists elementwise can be implemented using <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zipWith" target="_blank">zipWith</a>:</p>
<pre style="padding-left:30px;background:#EEEEEE;">diff :: Num a =&gt; System a a
diff ys = zipWith (-) ys (0 : ys)</pre>
<p>If the initial condition of the differentiator is not zero, we should instead use the following code:</p>
<pre style="padding-left:30px;background:#EEEEEE;">diff' :: Num a =&gt; System a a
diff' ys = zipWith (-) ys (diff_ini : ys)</pre>
<p>where <em>diff_ini</em> is the initial condition of the differentiator. Piecing it all together, we finally obtain the following <a href="http://en.wikipedia.org/wiki/Haskell_%28programming_language%29" target="_blank">Haskell</a> script:</p>
<pre style="padding-left:30px;background:#EEEEEE;">type Signal a = [a]
type System a b = Signal a -&gt; Signal b

-- accumulator
acc :: Num a =&gt; System a a
acc = scanl1 (+)

-- differentiator
diff :: Num a =&gt; System a a
diff ys = zipWith (-) ys (0 : ys)

-- cascade of the acc. and diff.
sys :: Num a =&gt; System a a
sys = diff . acc</pre>
<p>Take a look at the last line. It says that cascading systems is the same as <a href="http://en.wikipedia.org/wiki/Function_composition" target="_blank">composing</a> systems! Hence, the Haskell implementation is conceptually very close to the mathematical formulation using operators. <a href="http://en.wikipedia.org/wiki/Functional_analysis" target="_blank">Functional analysis</a> meets <a href="http://en.wikipedia.org/wiki/Functional_programming" target="_blank">functional programming</a>&#8230;</p>
<p>We run the script above on GHCi and then play with it:</p>
<pre style="padding-left:30px;background:#FFFFFF;">*Main&gt; -- build unit impulse
*Main&gt; let delta = 1.0 : repeat 0.0 :: Signal Float
*Main&gt; -- output of the accumulator
*Main&gt; let ys = acc delta
*Main&gt; take 20 ys
[1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,
1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0]
*Main&gt; -- output of the differentiator
*Main&gt; let ws = diff ys
*Main&gt; take 20 ws
[1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,
0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0]
*Main&gt; -- impulse response of the cascade
*Main&gt; let hs = sys delta
*Main&gt; take 20 hs
[1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,
0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0]</pre>
<p>The impulse response of the accumulator is the unit step. The impulse response of the cascade is the unit impulse, as we expected. The differentiator is the inverse of the accumulator, and vice-versa.</p>
<p style="text-align:center;">__________</p>
<p><strong>References</strong></p>
<p style="padding-left:30px;">[1] Alan V. Oppenheim, Alan S. Willsky, S. Hamid Nawab, <em>Signals &amp; Systems</em>, 2nd edition, Prentice-Hall, 1997.</p>
<p style="padding-left:30px;">[2] Panos Antsaklis, Anthony Michel, <em><a href="http://books.google.com/books?id=WyWBPV6Cu9QC" target="_blank">A Linear Systems Primer</a></em>, Birkhäuser Boston, 2007.</p>
<br />Filed under: <a href='http://stochastix.wordpress.com/category/programming/haskell/'>Haskell</a>, <a href='http://stochastix.wordpress.com/category/mathematics/systems-theory/lti-systems/'>LTI Systems</a>, <a href='http://stochastix.wordpress.com/category/mathematics/systems-theory/'>Systems Theory</a> Tagged: <a href='http://stochastix.wordpress.com/tag/haskell/'>Haskell</a>, <a href='http://stochastix.wordpress.com/tag/list-processing/'>List Processing</a>, <a href='http://stochastix.wordpress.com/tag/lti-systems/'>LTI Systems</a>, <a href='http://stochastix.wordpress.com/tag/signals-systems/'>Signals &amp; Systems</a>, <a href='http://stochastix.wordpress.com/tag/systems-theory/'>Systems Theory</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/stochastix.wordpress.com/7318/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/stochastix.wordpress.com/7318/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/stochastix.wordpress.com/7318/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/stochastix.wordpress.com/7318/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/stochastix.wordpress.com/7318/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/stochastix.wordpress.com/7318/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/stochastix.wordpress.com/7318/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/stochastix.wordpress.com/7318/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/stochastix.wordpress.com/7318/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/stochastix.wordpress.com/7318/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/stochastix.wordpress.com/7318/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/stochastix.wordpress.com/7318/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/stochastix.wordpress.com/7318/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/stochastix.wordpress.com/7318/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stochastix.wordpress.com&amp;blog=300626&amp;post=7318&amp;subd=stochastix&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://stochastix.wordpress.com/2012/01/25/cascading-systems-in-haskell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">stochastix</media:title>
		</media:content>

		<media:content url="http://stochastix.files.wordpress.com/2012/01/cascade-of-h1-and-h2-2.png" medium="image">
			<media:title type="html">Cascade of H1 and H2 (2)</media:title>
		</media:content>
	</item>
		<item>
		<title>Discrete-time LTI systems in Haskell II</title>
		<link>http://stochastix.wordpress.com/2012/01/22/discrete-time-lti-systems-in-haskell-ii/</link>
		<comments>http://stochastix.wordpress.com/2012/01/22/discrete-time-lti-systems-in-haskell-ii/#comments</comments>
		<pubDate>Sun, 22 Jan 2012 09:14:51 +0000</pubDate>
		<dc:creator>Rod Carvalho</dc:creator>
				<category><![CDATA[Haskell]]></category>
		<category><![CDATA[LTI Systems]]></category>
		<category><![CDATA[Systems Theory]]></category>
		<category><![CDATA[List Processing]]></category>
		<category><![CDATA[Signals & Systems]]></category>

		<guid isPermaLink="false">http://stochastix.wordpress.com/?p=7271</guid>
		<description><![CDATA[Yesterday I wrote a post on how to implement discrete-time LTI systems in Haskell. The code I presented did not quite please my delicate aesthetic sensitivity, as it bundled the declaration of the signals with the declaration of the LTI system under study. This is not practical. Ideally, I would like to define the LTI [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stochastix.wordpress.com&amp;blog=300626&amp;post=7271&amp;subd=stochastix&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Yesterday I wrote a post on how to implement <a href="http://stochastix.wordpress.com/2012/01/21/discrete-time-lti-systems-in-haskell" target="_blank">discrete-time LTI systems in Haskell</a>. The code I presented did not quite please my delicate aesthetic sensitivity, as it bundled the declaration of the signals with the declaration of the LTI system under study. This is not practical. Ideally, I would like to define the LTI system in a <a href="http://en.wikipedia.org/wiki/Haskell_%28programming_language%29" target="_blank">Haskell</a> script, run it on the GHCi interpreter, then create the signals using the GHCi command line. Why? Because I can then compute the response of the system to various test input signals without reloading the script.</p>
<p>In this post, we will again consider the first-order causal discrete-time LTI system described by the difference equation</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=y+%28n%29+-+%5Calpha+y+%28n-1%29+%3D+u+%28n%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='y (n) - &#92;alpha y (n-1) = u (n)' title='y (n) - &#92;alpha y (n-1) = u (n)' class='latex' /></p>
<p>where <img src='http://s0.wp.com/latex.php?latex=%7C%5Calpha%7C+%3C+1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='|&#92;alpha| &lt; 1' title='|&#92;alpha| &lt; 1' class='latex' />. In my previous <a href="http://stochastix.wordpress.com/2012/01/21/discrete-time-lti-systems-in-haskell">post</a>, I obtained a state-space representation of this LTI system. In this post, I will propose a better implementation of the given system in <a href="http://en.wikipedia.org/wiki/Haskell_%28programming_language%29" target="_blank">Haskell</a>.</p>
<p style="text-align:center;">__________</p>
<p><strong>Signal and System type synonyms</strong></p>
<p>What is a discrete-time <em>signal</em>? We can think of a discrete-time signal as a sequence of numbers or symbols. In <a href="http://en.wikipedia.org/wiki/Haskell_%28programming_language%29" target="_blank">Haskell</a>, we often use lists of <a href="http://en.wikipedia.org/wiki/Floating_point" target="_blank">floating-point</a> numbers to represent real-valued discrete-time signals. More generally, we have the type synonym:</p>
<pre style="padding-left:30px;background:#FFFFFF;">type Signal a = [a]</pre>
<p>which says that a signal of type <img src='http://s0.wp.com/latex.php?latex=a&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='a' title='a' class='latex' /> is a list of elements of type <img src='http://s0.wp.com/latex.php?latex=a&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='a' title='a' class='latex' />. We can then have signals of various types: integer, fixed-point, floating-point, bit, binary word, etc. This should be particularly useful in case we want to simulate <a href="http://en.wikipedia.org/wiki/Mixed-signal_integrated_circuit" target="_blank">mixed-signal circuits</a>.</p>
<p>What is a discrete-time <em>system</em>? It is that which maps input discrete-time signals to output discrete-time signals. We will use the type synonym:</p>
<pre style="padding-left:30px;background:#FFFFFF;">type System a b = (Signal a -&gt; Signal b)</pre>
<p>which tells us that a system of type <img src='http://s0.wp.com/latex.php?latex=a+%5C%2C%5C%2C+b&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='a &#92;,&#92;, b' title='a &#92;,&#92;, b' class='latex' /> is a function that maps an input signal of type <img src='http://s0.wp.com/latex.php?latex=a&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='a' title='a' class='latex' /> to an output signal of type <img src='http://s0.wp.com/latex.php?latex=b&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='b' title='b' class='latex' />. In other words, it takes a list whose elements are of type <img src='http://s0.wp.com/latex.php?latex=a&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='a' title='a' class='latex' />, and it returns a list whose elements are of type <img src='http://s0.wp.com/latex.php?latex=b&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='b' title='b' class='latex' />.</p>
<p style="text-align:center;">__________</p>
<p><strong>Implementing the LTI system in Haskell</strong></p>
<p>In my previous <a href="http://stochastix.wordpress.com/2012/01/21/discrete-time-lti-systems-in-haskell" target="_blank">post</a>, I created the state and output sequences using</p>
<pre style="padding-left:30px;background:#FFFFFF;">xs = scanl f x0 us
ys = zipWith g xs us</pre>
<p>Combining the two lines above into a single line, we obtain</p>
<pre style="padding-left:30px;background:#FFFFFF;">ys = zipWith g (scanl f x0 us) us</pre>
<p>which returns the output sequence when given the input sequence. Note that the state sequence is not created. Assuming zero initial condition, we can then create a <em>system</em> (i.e., a function that takes a list and returns a list) as follows</p>
<pre style="padding-left:30px;background:#FFFFFF;">sys :: Floating a =&gt; System a a
sys us = zipWith g (scanl f 0.0 us) us</pre>
<p>which takes signals of type <img src='http://s0.wp.com/latex.php?latex=a&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='a' title='a' class='latex' /> and returns signals of the same type, where <img src='http://s0.wp.com/latex.php?latex=a&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='a' title='a' class='latex' /> is a <a href="http://en.wikipedia.org/wiki/Floating_point" target="_blank">floating-point</a> type (either <a href="http://hackage.haskell.org/packages/archive/base/3.0.3.2/doc/html/Prelude.html#t%3AFloat" target="_blank">Float</a> or <a href="http://hackage.haskell.org/packages/archive/base/3.0.3.2/doc/html/Prelude.html#t%3ADouble" target="_blank">Double</a>).</p>
<p>Putting it all together in a script, we finally obtain:</p>
<pre style="padding-left:30px;background:#EEEEEE;">type Signal a = [a]

type System a b = (Signal a -&gt; Signal b)

-- state-space model
f,g :: Floating a =&gt; a -&gt; a -&gt; a
f x u = (0.5 * x) + u  	-- state-transition
g x u = f x u		-- output

-- define LTI system
sys :: Floating a =&gt; System a a
sys us = zipWith g (scanl f 0.0 us) us</pre>
<p>which defines the LTI system under study with <img src='http://s0.wp.com/latex.php?latex=%5Calpha+%3D+1%2F2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;alpha = 1/2' title='&#92;alpha = 1/2' class='latex' />. For this choice of <img src='http://s0.wp.com/latex.php?latex=%5Calpha&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;alpha' title='&#92;alpha' class='latex' />, the system is a <a href="http://en.wikipedia.org/wiki/Low-pass_filter" target="_blank">lowpass filter</a>.</p>
<p style="text-align:center;">__________</p>
<p><strong>Example: lowpass-filtering a square wave</strong></p>
<p>We run the script above on GHCi to create the desired LTI system. Let us now create an input signal, a <a href="http://en.wikipedia.org/wiki/Square_wave" target="_blank">square wave</a> of period equal to 16 and <a href="http://en.wikipedia.org/wiki/Duty_cycle" target="_blank">duty cycle</a> equal to 50%, and lowpass-filter it using the LTI system we created:</p>
<pre style="padding-left:30px;background:#FFFFFF;">*Main&gt; -- create square wave
*Main&gt; let ones  = take 8 $ repeat 1.0 :: Signal Float
*Main&gt; let zeros = take 8 $ repeat 0.0 :: Signal Float
*Main&gt; let us = cycle (ones ++ zeros) :: Signal Float
*Main&gt; -- create output sequence
*Main&gt; let ys = sys us
*Main&gt; -- check types
*Main&gt; :type us
us :: Signal Float
*Main&gt; :type ys
ys :: Signal Float
*Main&gt; :type sys
sys :: Floating a =&gt; System a a</pre>
<p>Finally, we compute the first 64 samples of the input and output signals using function <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:take" target="_blank">take</a>, and plot the signals using MATLAB:</p>
<p><a href="http://stochastix.files.wordpress.com/2012/01/filtered-square-wave.png"><img class="alignnone size-full wp-image-7291" title="Filtered square wave" src="http://stochastix.files.wordpress.com/2012/01/filtered-square-wave.png?w=450&#038;h=412" alt="" width="450" height="412" /></a></p>
<p>In case you are wondering why the peak amplitude of the output signal is twice that of the input signal, compute the transfer function of the LTI system and note that the DC gain is equal to <img src='http://s0.wp.com/latex.php?latex=%5Cfrac%7B1%7D%7B1+-+%5Calpha%7D+%3D+2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;frac{1}{1 - &#92;alpha} = 2' title='&#92;frac{1}{1 - &#92;alpha} = 2' class='latex' />.</p>
<br />Filed under: <a href='http://stochastix.wordpress.com/category/programming/haskell/'>Haskell</a>, <a href='http://stochastix.wordpress.com/category/mathematics/systems-theory/lti-systems/'>LTI Systems</a>, <a href='http://stochastix.wordpress.com/category/mathematics/systems-theory/'>Systems Theory</a> Tagged: <a href='http://stochastix.wordpress.com/tag/haskell/'>Haskell</a>, <a href='http://stochastix.wordpress.com/tag/list-processing/'>List Processing</a>, <a href='http://stochastix.wordpress.com/tag/lti-systems/'>LTI Systems</a>, <a href='http://stochastix.wordpress.com/tag/signals-systems/'>Signals &amp; Systems</a>, <a href='http://stochastix.wordpress.com/tag/systems-theory/'>Systems Theory</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/stochastix.wordpress.com/7271/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/stochastix.wordpress.com/7271/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/stochastix.wordpress.com/7271/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/stochastix.wordpress.com/7271/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/stochastix.wordpress.com/7271/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/stochastix.wordpress.com/7271/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/stochastix.wordpress.com/7271/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/stochastix.wordpress.com/7271/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/stochastix.wordpress.com/7271/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/stochastix.wordpress.com/7271/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/stochastix.wordpress.com/7271/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/stochastix.wordpress.com/7271/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/stochastix.wordpress.com/7271/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/stochastix.wordpress.com/7271/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stochastix.wordpress.com&amp;blog=300626&amp;post=7271&amp;subd=stochastix&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://stochastix.wordpress.com/2012/01/22/discrete-time-lti-systems-in-haskell-ii/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">stochastix</media:title>
		</media:content>

		<media:content url="http://stochastix.files.wordpress.com/2012/01/filtered-square-wave.png" medium="image">
			<media:title type="html">Filtered square wave</media:title>
		</media:content>
	</item>
		<item>
		<title>Discrete-time LTI systems in Haskell</title>
		<link>http://stochastix.wordpress.com/2012/01/21/discrete-time-lti-systems-in-haskell/</link>
		<comments>http://stochastix.wordpress.com/2012/01/21/discrete-time-lti-systems-in-haskell/#comments</comments>
		<pubDate>Sat, 21 Jan 2012 12:57:48 +0000</pubDate>
		<dc:creator>Rod Carvalho</dc:creator>
				<category><![CDATA[Haskell]]></category>
		<category><![CDATA[LTI Systems]]></category>
		<category><![CDATA[Systems Theory]]></category>
		<category><![CDATA[List Processing]]></category>
		<category><![CDATA[Signals & Systems]]></category>
		<category><![CDATA[State-Space Models]]></category>

		<guid isPermaLink="false">http://stochastix.wordpress.com/?p=7222</guid>
		<description><![CDATA[Consider the first-order causal discrete-time LTI system described by the following difference equation where . This system can be represented by the block diagram where is a unit-delay block, the output of which is . We now introduce state variable denoting the output of the unit-delay block, i.e., . Thus, and, since , we obtain [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stochastix.wordpress.com&amp;blog=300626&amp;post=7222&amp;subd=stochastix&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Consider the first-order causal <a href="http://stochastix.wordpress.com/2010/08/14/on-discrete-time-lti-systems" target="_blank">discrete-time LTI system</a> described by the following difference equation</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=y+%28n%29+-+%5Calpha+y+%28n-1%29+%3D+u+%28n%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='y (n) - &#92;alpha y (n-1) = u (n)' title='y (n) - &#92;alpha y (n-1) = u (n)' class='latex' /></p>
<p>where <img src='http://s0.wp.com/latex.php?latex=%7C%5Calpha%7C+%3C+1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='|&#92;alpha| &lt; 1' title='|&#92;alpha| &lt; 1' class='latex' />. This system can be represented by the block diagram</p>
<p><a href="http://stochastix.files.wordpress.com/2012/01/1st-order-lti-system.png"><img class="alignnone size-full wp-image-7229" title="1st order LTI system" src="http://stochastix.files.wordpress.com/2012/01/1st-order-lti-system.png?w=450&#038;h=158" alt="" width="450" height="158" /></a></p>
<p>where <img src='http://s0.wp.com/latex.php?latex=D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='D' title='D' class='latex' /> is a unit-delay block, the output of which is <img src='http://s0.wp.com/latex.php?latex=y+%28n-1%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='y (n-1)' title='y (n-1)' class='latex' />. We now introduce state variable <img src='http://s0.wp.com/latex.php?latex=x+%28n%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x (n)' title='x (n)' class='latex' /> denoting the output of the unit-delay block, i.e., <img src='http://s0.wp.com/latex.php?latex=x%28n%29+%3D+y%28n-1%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x(n) = y(n-1)' title='x(n) = y(n-1)' class='latex' />. Thus, <img src='http://s0.wp.com/latex.php?latex=x+%28n%2B1%29+%3D+y%28n%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x (n+1) = y(n)' title='x (n+1) = y(n)' class='latex' /> and, since <img src='http://s0.wp.com/latex.php?latex=y+%28n%29+%3D+%5Calpha+y+%28n-1%29+%2B+u+%28n%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='y (n) = &#92;alpha y (n-1) + u (n)' title='y (n) = &#92;alpha y (n-1) + u (n)' class='latex' />, we obtain the following <em>state-transition</em> equation</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=x+%28n%2B1%29+%3D+%5Calpha+x+%28n%29+%2B+u+%28n%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x (n+1) = &#92;alpha x (n) + u (n)' title='x (n+1) = &#92;alpha x (n) + u (n)' class='latex' />.</p>
<p>The initial condition is <img src='http://s0.wp.com/latex.php?latex=x%280%29+%3D+y%28-1%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x(0) = y(-1)' title='x(0) = y(-1)' class='latex' />, which we assume to be zero. Note that the output is <img src='http://s0.wp.com/latex.php?latex=y+%28n%29+%3D+x%28n%2B1%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='y (n) = x(n+1)' title='y (n) = x(n+1)' class='latex' /> and, thus, the <em>output</em> equation is</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=y+%28n%29+%3D+%5Calpha+x+%28n%29+%2B+u+%28n%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='y (n) = &#92;alpha x (n) + u (n)' title='y (n) = &#92;alpha x (n) + u (n)' class='latex' />.</p>
<p>Lastly, we define <img src='http://s0.wp.com/latex.php?latex=f+%28x%2Cu%29+%3D+g+%28x%2Cu%29+%3A%3D+%5Calpha+x+%2B+u&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='f (x,u) = g (x,u) := &#92;alpha x + u' title='f (x,u) = g (x,u) := &#92;alpha x + u' class='latex' />, which allows us to write the state-transition and output equations as follows</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Cbegin%7Barray%7D%7Brl%7D+x+%28n%2B1%29+%26%3D+f+%28x%28n%29%2C+u%28n%29%29%5C%5C+y+%28n%29+%26%3D+g+%28x%28n%29%2C+u%28n%29%29%5C%5C%5Cend%7Barray%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;begin{array}{rl} x (n+1) &amp;= f (x(n), u(n))&#92;&#92; y (n) &amp;= g (x(n), u(n))&#92;&#92;&#92;end{array}' title='&#92;begin{array}{rl} x (n+1) &amp;= f (x(n), u(n))&#92;&#92; y (n) &amp;= g (x(n), u(n))&#92;&#92;&#92;end{array}' class='latex' /></p>
<p>We can now implement this state-space model in <a href="http://en.wikipedia.org/wiki/Haskell_%28programming_language%29" target="_blank">Haskell</a>!</p>
<p style="text-align:center;">__________</p>
<p><strong>Implementation in <strong>Haskell</strong><br />
</strong></p>
<p>Given an initial state <img src='http://s0.wp.com/latex.php?latex=x_0&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x_0' title='x_0' class='latex' /> and an input sequence <img src='http://s0.wp.com/latex.php?latex=u+%3D+%5Bu_0%2C+u_1%2C+%5Cdots%5D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='u = [u_0, u_1, &#92;dots]' title='u = [u_0, u_1, &#92;dots]' class='latex' />, we can compute the state sequence using the <a href="http://stochastix.wordpress.com/2012/01/15/computing-state-trajectories-using-scans" target="_blank">scanl trick</a> I wrote about last weekend. Once we have obtained the state sequence <img src='http://s0.wp.com/latex.php?latex=x+%3D+%5Bx_0%2C+x_1%2C+%5Cdots%5D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x = [x_0, x_1, &#92;dots]' title='x = [x_0, x_1, &#92;dots]' class='latex' />, the output sequence is</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=y+%3D+%5By_0%2C+y_1%2C+%5Cdots%5D+%3D+%5Bg+%28x_0%2C+u_0%29%2C+g+%28x_1%2C+u_1%29%2C+%5Cdots%5D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='y = [y_0, y_1, &#92;dots] = [g (x_0, u_0), g (x_1, u_1), &#92;dots]' title='y = [y_0, y_1, &#92;dots] = [g (x_0, u_0), g (x_1, u_1), &#92;dots]' class='latex' />.</p>
<p>Do you recognize the pattern? We are zipping lists <img src='http://s0.wp.com/latex.php?latex=x&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x' title='x' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=u&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='u' title='u' class='latex' /> using function <img src='http://s0.wp.com/latex.php?latex=g&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='g' title='g' class='latex' />!! We thus use (higher-order) function <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zipWith" target="_blank">zipWith</a> to compute the output sequence.</p>
<p>For <img src='http://s0.wp.com/latex.php?latex=%5Calpha+%3D+7%2F8&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;alpha = 7/8' title='&#92;alpha = 7/8' class='latex' /> and a constant input sequence, the following Haskell code computes the state and output trajectories:</p>
<pre style="padding-left:30px;background:#EEEEEE;">-- constant input sequence
us :: Floating a =&gt; [a]
us = repeat 1.0

-- parameter alpha
alpha :: Floating a =&gt; a
alpha = 7/8

-- state-transition function
f :: Floating a =&gt; a -&gt; a -&gt; a
f x u = (alpha * x) + u

-- output function
g :: Floating a =&gt; a -&gt; a -&gt; a
g x u = f x u

-- initial condition
x0 :: Floating a =&gt; a
x0 = 0.0

-- state sequence
xs :: Floating a =&gt; [a]
xs = scanl f x0 us

-- output sequence
ys :: Floating a =&gt; [a]
ys = zipWith g xs us</pre>
<p>Not quite. I am still stuck in the <a href="http://en.wikipedia.org/wiki/Imperative_programming" target="_blank">imperative</a> paradigm&#8230; and need to be corrected. The code above does not compute anything. It does <em>declare</em> what the input, state and output sequences are. After running the script above in GHCi, we compute 30 samples of the input and output sequences using function <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:take" target="_blank">take</a>:</p>
<pre style="padding-left:30px;background:#FFFFFF;">*Main&gt; take 30 us
[1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,
1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,
1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0]
*Main&gt; take 30 ys
[1.0,1.875,2.640625,3.310546875,3.896728515625,
4.409637451171875,4.858432769775391,5.251128673553467,
5.5947375893592834,5.895395390689373,6.158470966853201,
6.388662095996551,6.590079333996982,6.7663194172473595,
6.92052949009144,7.05546330383001,7.173530390851258,
7.276839091994852,7.367234205495495,7.446329929808559,
7.515538688582489,7.576096352509678,7.629084308445968,
7.675448769890222,7.716017673653944,7.751515464447201,
7.782576031391301,7.809754027467388,7.833534774033964,
7.854342927279719]</pre>
<p>We can plot these sequences in MATLAB:</p>
<p><a href="http://stochastix.files.wordpress.com/2012/01/plot-input-and-output-signals.png"><img class="alignnone size-full wp-image-7264" title="Plot - input and output signals" src="http://stochastix.files.wordpress.com/2012/01/plot-input-and-output-signals.png?w=450&#038;h=337" alt="" width="450" height="337" /></a></p>
<p>We now know how to simulate discrete-time LTI systems using Haskell. Note that our framework is based on state-space models, not on transfer functions. Hence, it should be possible to simulate discrete-time nonlinear systems in the same manner: using <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:scanl" target="_blank">scanl</a> to compute the state sequence, and <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zipWith" target="_blank">zipWith</a> to compute the output sequence. All we need is a state-space representation of the system under study.</p>
<br />Filed under: <a href='http://stochastix.wordpress.com/category/programming/haskell/'>Haskell</a>, <a href='http://stochastix.wordpress.com/category/mathematics/systems-theory/lti-systems/'>LTI Systems</a>, <a href='http://stochastix.wordpress.com/category/mathematics/systems-theory/'>Systems Theory</a> Tagged: <a href='http://stochastix.wordpress.com/tag/haskell/'>Haskell</a>, <a href='http://stochastix.wordpress.com/tag/list-processing/'>List Processing</a>, <a href='http://stochastix.wordpress.com/tag/lti-systems/'>LTI Systems</a>, <a href='http://stochastix.wordpress.com/tag/signals-systems/'>Signals &amp; Systems</a>, <a href='http://stochastix.wordpress.com/tag/state-space-models/'>State-Space Models</a>, <a href='http://stochastix.wordpress.com/tag/systems-theory/'>Systems Theory</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/stochastix.wordpress.com/7222/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/stochastix.wordpress.com/7222/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/stochastix.wordpress.com/7222/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/stochastix.wordpress.com/7222/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/stochastix.wordpress.com/7222/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/stochastix.wordpress.com/7222/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/stochastix.wordpress.com/7222/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/stochastix.wordpress.com/7222/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/stochastix.wordpress.com/7222/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/stochastix.wordpress.com/7222/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/stochastix.wordpress.com/7222/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/stochastix.wordpress.com/7222/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/stochastix.wordpress.com/7222/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/stochastix.wordpress.com/7222/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stochastix.wordpress.com&amp;blog=300626&amp;post=7222&amp;subd=stochastix&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://stochastix.wordpress.com/2012/01/21/discrete-time-lti-systems-in-haskell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">stochastix</media:title>
		</media:content>

		<media:content url="http://stochastix.files.wordpress.com/2012/01/1st-order-lti-system.png" medium="image">
			<media:title type="html">1st order LTI system</media:title>
		</media:content>

		<media:content url="http://stochastix.files.wordpress.com/2012/01/plot-input-and-output-signals.png" medium="image">
			<media:title type="html">Plot - input and output signals</media:title>
		</media:content>
	</item>
		<item>
		<title>Communicating with half-words</title>
		<link>http://stochastix.wordpress.com/2012/01/16/communicating-with-half-words/</link>
		<comments>http://stochastix.wordpress.com/2012/01/16/communicating-with-half-words/#comments</comments>
		<pubDate>Mon, 16 Jan 2012 12:04:25 +0000</pubDate>
		<dc:creator>Rod Carvalho</dc:creator>
				<category><![CDATA[Thoughts]]></category>
		<category><![CDATA[Russian Culture]]></category>
		<category><![CDATA[Svetlana Boym]]></category>

		<guid isPermaLink="false">http://stochastix.wordpress.com/?p=7179</guid>
		<description><![CDATA[Svetlana Boym on communicating with &#8220;half-words&#8221;: There used to be a saying among Soviet intelligentsia&#8212;&#8221;to understand each other with half-words.&#8221; What is shared is silence, tone of voice, nuance of intonation. To say a full word is to say too much; communication on the level of words is already excessive, banal, almost kitschy. This peculiar [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stochastix.wordpress.com&amp;blog=300626&amp;post=7179&amp;subd=stochastix&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://en.wikipedia.org/wiki/Svetlana_Boym" target="_blank">Svetlana Boym</a> on communicating with &#8220;half-words&#8221;:</p>
<p style="padding-left:30px;"><em>There used to be a saying among Soviet intelligentsia&#8212;&#8221;to understand each other with half-words.&#8221; What is shared is silence, tone of voice, nuance of intonation. To say a full word is to say too much; communication on the level of words is already excessive, banal, almost kitschy. This peculiar form of communication &#8220;with half-words&#8221; is a mark of belonging to an imagined community that exists on the margin of the official public sphere. Hence the American metaphors for being sincere and authentic&#8212;&#8221;saying what you mean,&#8221; &#8220;going public,&#8221; and &#8220;being straightforward&#8221;&#8212;do not translate properly into the Soviet and Russian contexts. &#8220;Saying what you mean&#8221; could be interpreted as being stupid, naïve, or not streetwise. Such a profession of sincerity could be seen, at best, as a sign of foreign theatrical behavior; at worst, as a cunning provocation. There is no word for authenticity in Russian, but there are two words for truth, pravda and istina. It is possible to tell the truth (pravda), but istina&#8212;the word that, according to Vladimir Nabokov, does not rhyme with anything&#8212;must remain unarticulated.<br />
</em></p>
<p>In Russian cyrillic: <em>istina</em> = истина, <em>pravda</em> = правда. I would be delighted if any native Russians would care to comment on or critique this passage.</p>
<p style="text-align:center;">__________</p>
<p>Source:</p>
<p style="padding-left:30px;">Svetlana Boym, <em><a href="http://books.google.com/books?id=B7_uBFThxD4C" target="_blank">Common places: mythologies of everyday life in Russia</a></em>, Harvard University Press, 1994.</p>
<br />Filed under: <a href='http://stochastix.wordpress.com/category/thoughts/'>Thoughts</a> Tagged: <a href='http://stochastix.wordpress.com/tag/russian-culture/'>Russian Culture</a>, <a href='http://stochastix.wordpress.com/tag/svetlana-boym/'>Svetlana Boym</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/stochastix.wordpress.com/7179/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/stochastix.wordpress.com/7179/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/stochastix.wordpress.com/7179/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/stochastix.wordpress.com/7179/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/stochastix.wordpress.com/7179/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/stochastix.wordpress.com/7179/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/stochastix.wordpress.com/7179/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/stochastix.wordpress.com/7179/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/stochastix.wordpress.com/7179/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/stochastix.wordpress.com/7179/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/stochastix.wordpress.com/7179/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/stochastix.wordpress.com/7179/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/stochastix.wordpress.com/7179/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/stochastix.wordpress.com/7179/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stochastix.wordpress.com&amp;blog=300626&amp;post=7179&amp;subd=stochastix&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://stochastix.wordpress.com/2012/01/16/communicating-with-half-words/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">stochastix</media:title>
		</media:content>
	</item>
		<item>
		<title>Computing state trajectories using scans</title>
		<link>http://stochastix.wordpress.com/2012/01/15/computing-state-trajectories-using-scans/</link>
		<comments>http://stochastix.wordpress.com/2012/01/15/computing-state-trajectories-using-scans/#comments</comments>
		<pubDate>Mon, 16 Jan 2012 06:17:30 +0000</pubDate>
		<dc:creator>Rod Carvalho</dc:creator>
				<category><![CDATA[Dynamical Systems]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Chaotic Maps]]></category>
		<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[Higher-Order Functions]]></category>
		<category><![CDATA[List Processing]]></category>
		<category><![CDATA[Logistic Map]]></category>

		<guid isPermaLink="false">http://stochastix.wordpress.com/?p=7036</guid>
		<description><![CDATA[Consider a class of dynamical systems of the form where is the state, is the input, and is the state-transition function, where and  are the state set and input set, respectively. Given an initial state and a (possibly infinite) input sequence , we would like to compute the state trajectory . Iterating, we obtain and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stochastix.wordpress.com&amp;blog=300626&amp;post=7036&amp;subd=stochastix&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Consider a class of dynamical systems of the form</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=x_%7Bk%2B1%7D+%3D+f+%28x_k%2C+u_k+%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x_{k+1} = f (x_k, u_k )' title='x_{k+1} = f (x_k, u_k )' class='latex' /></p>
<p>where <img src='http://s0.wp.com/latex.php?latex=x_k&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x_k' title='x_k' class='latex' /> is the <em>state</em>, <img src='http://s0.wp.com/latex.php?latex=u_k&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='u_k' title='u_k' class='latex' /> is the <em>input</em>, and <img src='http://s0.wp.com/latex.php?latex=f+%3A+%5Cmathcal%7BX%7D+%5Ctimes+%5Cmathcal%7BU%7D+%5Cto+%5Cmathcal%7BX%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='f : &#92;mathcal{X} &#92;times &#92;mathcal{U} &#92;to &#92;mathcal{X}' title='f : &#92;mathcal{X} &#92;times &#92;mathcal{U} &#92;to &#92;mathcal{X}' class='latex' /> is the <em>state-transition</em> function, where <img src='http://s0.wp.com/latex.php?latex=%5Cmathcal%7BX%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathcal{X}' title='&#92;mathcal{X}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%5Cmathcal%7BU%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathcal{U}' title='&#92;mathcal{U}' class='latex' /> are the <em>state set</em> and <em>input set</em>, respectively.</p>
<p>Given an <em>initial</em> state <img src='http://s0.wp.com/latex.php?latex=x_0&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x_0' title='x_0' class='latex' /> and a (possibly infinite) <em>input sequence</em> <img src='http://s0.wp.com/latex.php?latex=%28u_0%2C+u_1%2C+u_2%2C+%5Cdots%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='(u_0, u_1, u_2, &#92;dots)' title='(u_0, u_1, u_2, &#92;dots)' class='latex' />, we would like to compute the <em>state trajectory</em> <img src='http://s0.wp.com/latex.php?latex=%28x_0%2C+x_1%2C+x_2%2C+x_3%2C+%5Cdots%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='(x_0, x_1, x_2, x_3, &#92;dots)' title='(x_0, x_1, x_2, x_3, &#92;dots)' class='latex' />. Iterating, we obtain</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Cbegin%7Barray%7D%7Brl%7D+x_1+%26%3D+f+%28x_0%2C+u_0%29%5C%5C+x_2+%26%3D+f+%28x_1%2C+u_1%29+%3D+f+%28f+%28x_0%2C+u_0%29%2C+u_1%29%5C%5C+x_3+%26%3D+f+%28x_2%2C+u_2%29+%3D+f+%28f+%28x_1%2C+u_1%29%2C+u_2%29+%3D+f+%28f+%28f+%28x_0%2C+u_0%29%2C+u_1%29%2C+u_2%29%5Cend%7Barray%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;begin{array}{rl} x_1 &amp;= f (x_0, u_0)&#92;&#92; x_2 &amp;= f (x_1, u_1) = f (f (x_0, u_0), u_1)&#92;&#92; x_3 &amp;= f (x_2, u_2) = f (f (x_1, u_1), u_2) = f (f (f (x_0, u_0), u_1), u_2)&#92;end{array}' title='&#92;begin{array}{rl} x_1 &amp;= f (x_0, u_0)&#92;&#92; x_2 &amp;= f (x_1, u_1) = f (f (x_0, u_0), u_1)&#92;&#92; x_3 &amp;= f (x_2, u_2) = f (f (x_1, u_1), u_2) = f (f (f (x_0, u_0), u_1), u_2)&#92;end{array}' class='latex' /></p>
<p>and so on. Let us pause to take a look at the following equation</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=x_3+%3D+f+%28f+%28f+%28x_0%2C+u_0%29%2C+u_1%29%2C+u_2%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x_3 = f (f (f (x_0, u_0), u_1), u_2)' title='x_3 = f (f (f (x_0, u_0), u_1), u_2)' class='latex' />.</p>
<p>If you happen to be acquainted with <a href="http://en.wikipedia.org/wiki/Functional_programming" target="_blank">functional programming</a>, you will probably recognize this <em>pattern</em>. It is a left <a href="http://en.wikipedia.org/wiki/Fold_%28higher-order_function%29" target="_blank">fold</a>!! Hence, for every integer <img src='http://s0.wp.com/latex.php?latex=N+%5Cgeq+1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='N &#92;geq 1' title='N &#92;geq 1' class='latex' />, we can compute <img src='http://s0.wp.com/latex.php?latex=x_N&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x_N' title='x_N' class='latex' /> in <a href="http://en.wikipedia.org/wiki/Haskell_%28programming_language%29" target="_blank">Haskell</a> using function <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl" target="_blank">foldl</a>, as follows</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Ctext%7Bfoldl%7D+%5C%2C+f+%5C%2C+x_0%5C%2C+%5Bu_0%2C+u_1%2C+%5Cdots%2C+u_%7BN-1%7D%5D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;text{foldl} &#92;, f &#92;, x_0&#92;, [u_0, u_1, &#92;dots, u_{N-1}]' title='&#92;text{foldl} &#92;, f &#92;, x_0&#92;, [u_0, u_1, &#92;dots, u_{N-1}]' class='latex' />.</p>
<p>However, we would like to obtain the intermediate states <img src='http://s0.wp.com/latex.php?latex=x_1%2C+x_2%2C+%5Cdots%2C+x_%7BN-1%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x_1, x_2, &#92;dots, x_{N-1}' title='x_1, x_2, &#92;dots, x_{N-1}' class='latex' /> as well. We can compute the entire state trajectory using function <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:scanl" target="_blank">scanl</a> instead</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Ctext%7Bscanl%7D+%5C%2C+f+%5C%2C+x_0%5C%2C+%5Bu_0%2C+u_1%2C+%5Cdots%2C+u_%7BN-1%7D%5D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;text{scanl} &#92;, f &#92;, x_0&#92;, [u_0, u_1, &#92;dots, u_{N-1}]' title='&#92;text{scanl} &#92;, f &#92;, x_0&#92;, [u_0, u_1, &#92;dots, u_{N-1}]' class='latex' /></p>
<p>which returns a (finite) list of states <img src='http://s0.wp.com/latex.php?latex=%5B+x_0%2C+x_1%2C+%5Cdots%2C+x_N%5D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='[ x_0, x_1, &#92;dots, x_N]' title='[ x_0, x_1, &#92;dots, x_N]' class='latex' />. Amazingly (or perhaps not), since <a href="http://en.wikipedia.org/wiki/Haskell_%28programming_language%29" target="_blank">Haskell</a> is <a href="http://en.wikipedia.org/wiki/Lazy_evaluation" target="_blank">lazy</a>, we can obtain a state trajectory of infinite length corresponding to a given initial state and a given input sequence of infinite length!</p>
<p style="text-align:center;">__________</p>
<p><strong>Example</strong></p>
<p>Consider the <a href="http://en.wikipedia.org/wiki/Logistic_map" target="_blank">logistic map</a> with a control input</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=x_%7Bk%2B1%7D+%3D+r+x_k+%281+-+x_k%29+%2B+u_k&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x_{k+1} = r x_k (1 - x_k) + u_k' title='x_{k+1} = r x_k (1 - x_k) + u_k' class='latex' /></p>
<p>where we will use <img src='http://s0.wp.com/latex.php?latex=r+%3D+3.8&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='r = 3.8' title='r = 3.8' class='latex' />, for which the dynamics (of the unforced system) are chaotic. Let the initial state be <img src='http://s0.wp.com/latex.php?latex=x_0+%3D+0&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x_0 = 0' title='x_0 = 0' class='latex' /> (note that <img src='http://s0.wp.com/latex.php?latex=x+%3D+0&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x = 0' title='x = 0' class='latex' /> is a fixed-point of the unforced logistic map), and let the input sequence be a discrete-time sinusoid of infinite length</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=u_k+%3D+%5Cdisplaystyle%5Cfrac%7B1%7D%7B5%7D+%5Csin+%5Cleft%28%5Cfrac%7B%5Cpi%7D%7B10%7D+k%5Cright%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='u_k = &#92;displaystyle&#92;frac{1}{5} &#92;sin &#92;left(&#92;frac{&#92;pi}{10} k&#92;right)' title='u_k = &#92;displaystyle&#92;frac{1}{5} &#92;sin &#92;left(&#92;frac{&#92;pi}{10} k&#92;right)' class='latex' /></p>
<p>whose period is <img src='http://s0.wp.com/latex.php?latex=20&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='20' title='20' class='latex' />. The following <a href="http://en.wikipedia.org/wiki/Haskell_%28programming_language%29" target="_blank">Haskell</a> program computes the infinite-length state trajectory using the aforementioned <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:scanl" target="_blank">scanl</a> trick:</p>
<pre style="padding-left:30px;background:#EEEEEE;">-- define logistic map with control input
f :: Floating a =&gt; a -&gt; a -&gt; a
f x u = (3.8 * x * (1 - x)) + u

-- frequency of the input sinusoid
omega :: Floating a =&gt; a
omega = pi / 10

-- generate sinusoidal control input
us :: Floating a =&gt; [a]
us = map ((0.2*) . sin . (omega*) . fromIntegral) [0..]

-- initial state
x0 :: Floating a =&gt; a
x0 = 0.0

-- compute the state trajectory
xs :: Floating a =&gt; [a]
xs = scanl f x0 us</pre>
<p>where we used <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map" target="_blank">map</a>, <a href="http://en.wikipedia.org/wiki/Function_composition" target="_blank">function composition</a>, and an infinite list of non-negative integers to generate the sinusoidal input.</p>
<p>To be more precise, the program above does not quite compute the state trajectory, it merely &#8220;promises&#8221; to compute it when needed. After running the program in <a href="http://www.haskell.org/ghc/docs/latest/html/users_guide/ghci.html" target="_blank">GHCi</a>, we can obtain the first 20 values of the input sequence and the first 21 values of the state sequence using function <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:take" target="_blank">take</a>, as follows</p>
<pre style="padding-left:30px;background:#FFFFFF;">*Main&gt; take 20 us
[0.0,6.180339887498948e-,0.11755705045849463,
0.1618033988749895,0.1902113032590307,0.2,
0.19021130325903074,0.1618033988749895,
0.11755705045849466, 6.180339887498951e-2,
2.4492127076447546e17,-6.180339887498946e2,
-0.11755705045849461,-0.16180339887498948,
-0.1902113032590307,-0.2,-0.19021130325903074,
-0.1618033988749895,-0.11755705045849468,
-6.180339887498953e-2]
*Main&gt; take 21 xs
[0.0,0.0,6.180339887498948e-2,0.33789525775595064,
1.0119471985345525,0.14426955372699996,
0.6691322284587664,1.031509602586003,
3.8294059838691885e-2,0.2575020247735924,
0.7883433805171414,0.6340607606653986,
0.8199019084343063,0.44356147166584237,
0.7760924326990134,0.4701259774450641,
0.7466086625502713,0.5286885334506017,
0.7850690797091344,0.5236383047578965,
0.8860732772080671]</pre>
<p>In other words, the <em>input</em> and <em>state</em> sequences are only computed when the expressions with <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:take" target="_blank">take</a> are evaluated by the interpreter. We can plot these sequences in MATLAB:</p>
<p><a href="http://stochastix.files.wordpress.com/2012/01/logistic-map-plot-of-input-and-state.png"><img class="alignnone size-full wp-image-7194" title="Logistic Map - plot of input and state" src="http://stochastix.files.wordpress.com/2012/01/logistic-map-plot-of-input-and-state.png?w=450&#038;h=337" alt="" width="450" height="337" /></a></p>
<p>Last but not least, here is the MATLAB code that generates the plot:</p>
<p><pre class="brush: matlabkey;">
figure; hold on;
stem([0:1:19],u,'b');
stem([0:1:20],x,'r');
legend('input','state');
xlabel('k (time)'); ylabel('amplitude');
axis([0 21 -0.25 1.20]);
</pre></p>
<p>where (row) vectors <img src='http://s0.wp.com/latex.php?latex=u&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='u' title='u' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=x&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x' title='x' class='latex' /> are the lists produced by GHCi.</p>
<p style="text-align:center;">__________</p>
<p>Lastly, please do note that the class of dynamical systems we have considered in this post is extremely broad. It encompasses difference equations obtained from the time-discretization of differential equations, IIR filters, finite and infinite automata, etc.</p>
<br />Filed under: <a href='http://stochastix.wordpress.com/category/mathematics/dynamical-systems-mathematics/'>Dynamical Systems</a>, <a href='http://stochastix.wordpress.com/category/programming/haskell/'>Haskell</a> Tagged: <a href='http://stochastix.wordpress.com/tag/chaotic-maps/'>Chaotic Maps</a>, <a href='http://stochastix.wordpress.com/tag/dynamical-systems/'>Dynamical Systems</a>, <a href='http://stochastix.wordpress.com/tag/functional-programming/'>Functional Programming</a>, <a href='http://stochastix.wordpress.com/tag/haskell/'>Haskell</a>, <a href='http://stochastix.wordpress.com/tag/higher-order-functions/'>Higher-Order Functions</a>, <a href='http://stochastix.wordpress.com/tag/list-processing/'>List Processing</a>, <a href='http://stochastix.wordpress.com/tag/logistic-map/'>Logistic Map</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/stochastix.wordpress.com/7036/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/stochastix.wordpress.com/7036/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/stochastix.wordpress.com/7036/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/stochastix.wordpress.com/7036/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/stochastix.wordpress.com/7036/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/stochastix.wordpress.com/7036/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/stochastix.wordpress.com/7036/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/stochastix.wordpress.com/7036/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/stochastix.wordpress.com/7036/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/stochastix.wordpress.com/7036/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/stochastix.wordpress.com/7036/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/stochastix.wordpress.com/7036/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/stochastix.wordpress.com/7036/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/stochastix.wordpress.com/7036/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stochastix.wordpress.com&amp;blog=300626&amp;post=7036&amp;subd=stochastix&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://stochastix.wordpress.com/2012/01/15/computing-state-trajectories-using-scans/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">stochastix</media:title>
		</media:content>

		<media:content url="http://stochastix.files.wordpress.com/2012/01/logistic-map-plot-of-input-and-state.png" medium="image">
			<media:title type="html">Logistic Map - plot of input and state</media:title>
		</media:content>
	</item>
		<item>
		<title>Not all birds can fly</title>
		<link>http://stochastix.wordpress.com/2012/01/01/not-all-birds-can-fly/</link>
		<comments>http://stochastix.wordpress.com/2012/01/01/not-all-birds-can-fly/#comments</comments>
		<pubDate>Mon, 02 Jan 2012 05:59:59 +0000</pubDate>
		<dc:creator>Rod Carvalho</dc:creator>
				<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Logic]]></category>
		<category><![CDATA[Predicate Logic]]></category>

		<guid isPermaLink="false">http://stochastix.wordpress.com/?p=7071</guid>
		<description><![CDATA[Consider the following declarative sentence Not all birds can fly. This sentence can be paraphrased as It is not the case that all things which are birds can fly. Let be the set of all things of interest (i.e., the universe under discussion). We now introduce predicates and , defined by and Finally, using (unary) [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stochastix.wordpress.com&amp;blog=300626&amp;post=7071&amp;subd=stochastix&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Consider the following declarative sentence</p>
<p style="text-align:center;"><em>Not all birds can fly.</em></p>
<p>This sentence can be paraphrased as</p>
<p style="text-align:center;"><em>It is not the case that all things which are birds can fly.</em></p>
<p>Let <img src='http://s0.wp.com/latex.php?latex=%5Cmathcal%7BX%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathcal{X}' title='&#92;mathcal{X}' class='latex' /> be the set of all things of interest (i.e., the universe under discussion). We now introduce <a href="http://en.wikipedia.org/wiki/Predicate_%28mathematical_logic%29" target="_blank">predicates</a> <img src='http://s0.wp.com/latex.php?latex=B+%3A+%5Cmathcal%7BX%7D+%5Cto+%5C%7B%5Ctext%7BTrue%7D%2C+%5Ctext%7BFalse%7D%5C%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='B : &#92;mathcal{X} &#92;to &#92;{&#92;text{True}, &#92;text{False}&#92;}' title='B : &#92;mathcal{X} &#92;to &#92;{&#92;text{True}, &#92;text{False}&#92;}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=F+%3A+%5Cmathcal%7BX%7D+%5Cto+%5C%7B%5Ctext%7BTrue%7D%2C+%5Ctext%7BFalse%7D%5C%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='F : &#92;mathcal{X} &#92;to &#92;{&#92;text{True}, &#92;text{False}&#92;}' title='F : &#92;mathcal{X} &#92;to &#92;{&#92;text{True}, &#92;text{False}&#92;}' class='latex' />, defined by</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=B+%28x%29+%3D+%5Cbegin%7Bcases%7D+%5Ctext%7BTrue%7D%2C+%26+x+%5Ctextrm%7B+is+a+bird%7D%5C%5C+%5Ctext%7BFalse%7D%2C+%26+x+%5Ctextrm%7B+is+not+a+bird%7D%5Cend%7Bcases%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='B (x) = &#92;begin{cases} &#92;text{True}, &amp; x &#92;textrm{ is a bird}&#92;&#92; &#92;text{False}, &amp; x &#92;textrm{ is not a bird}&#92;end{cases}' title='B (x) = &#92;begin{cases} &#92;text{True}, &amp; x &#92;textrm{ is a bird}&#92;&#92; &#92;text{False}, &amp; x &#92;textrm{ is not a bird}&#92;end{cases}' class='latex' /></p>
<p>and</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=F+%28x%29+%3D+%5Cbegin%7Bcases%7D+%5Ctext%7BTrue%7D%2C+%26+x+%5Ctextrm%7B+can+fly%7D%5C%5C+%5Ctext%7BFalse%7D%2C+%26+x+%5Ctextrm%7B+cannot+fly%7D%5Cend%7Bcases%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='F (x) = &#92;begin{cases} &#92;text{True}, &amp; x &#92;textrm{ can fly}&#92;&#92; &#92;text{False}, &amp; x &#92;textrm{ cannot fly}&#92;end{cases}' title='F (x) = &#92;begin{cases} &#92;text{True}, &amp; x &#92;textrm{ can fly}&#92;&#92; &#92;text{False}, &amp; x &#92;textrm{ cannot fly}&#92;end{cases}' class='latex' /></p>
<p>Finally, using (unary) predicates <img src='http://s0.wp.com/latex.php?latex=B&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='B' title='B' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=F&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='F' title='F' class='latex' />, the declarative sentence <em>&#8220;</em>Not all birds can fly&#8221; can be encoded as follows</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Cneg+%5Cleft%28+%5Cforall+x+%5C%2C+%28+B+%28x%29+%5Crightarrow+F+%28x%29+%29+%5Cright%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;neg &#92;left( &#92;forall x &#92;, ( B (x) &#92;rightarrow F (x) ) &#92;right)' title='&#92;neg &#92;left( &#92;forall x &#92;, ( B (x) &#92;rightarrow F (x) ) &#92;right)' class='latex' /></p>
<p>where we used <a href="http://en.wikipedia.org/wiki/Negation" target="_blank">negation</a> (<img src='http://s0.wp.com/latex.php?latex=%5Cneg&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;neg' title='&#92;neg' class='latex' />), <a href="http://en.wikipedia.org/wiki/Universal_quantification" target="_blank">universal quantification</a> (<img src='http://s0.wp.com/latex.php?latex=%5Cforall&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;forall' title='&#92;forall' class='latex' />), and <a href="http://en.wikipedia.org/wiki/Material_conditional" target="_blank">material implication</a> (<img src='http://s0.wp.com/latex.php?latex=%5Crightarrow&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;rightarrow' title='&#92;rightarrow' class='latex' />). The (well-formed) <a href="http://en.wikipedia.org/wiki/Well-formed_formula" target="_blank">formula</a> above should be read as: &#8220;it is not the case that for all <img src='http://s0.wp.com/latex.php?latex=x+%5Cin+%5Cmathcal%7BX%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x &#92;in &#92;mathcal{X}' title='x &#92;in &#92;mathcal{X}' class='latex' />, if <img src='http://s0.wp.com/latex.php?latex=x&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x' title='x' class='latex' /> is a bird, then <img src='http://s0.wp.com/latex.php?latex=x&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x' title='x' class='latex' /> can fly&#8221;. Whether the (universally quantified) formula evaluates to <img src='http://s0.wp.com/latex.php?latex=%5Ctext%7BTrue%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;text{True}' title='&#92;text{True}' class='latex' /> or <img src='http://s0.wp.com/latex.php?latex=%5Ctext%7BFalse%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;text{False}' title='&#92;text{False}' class='latex' /> depends on the set <img src='http://s0.wp.com/latex.php?latex=%5Cmathcal%7BX%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathcal{X}' title='&#92;mathcal{X}' class='latex' /> we consider. For example, if <img src='http://s0.wp.com/latex.php?latex=%5Cmathcal%7BX%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathcal{X}' title='&#92;mathcal{X}' class='latex' /> is the set of all living things on Earth on January 1, 2012, then the formula evaluates to <img src='http://s0.wp.com/latex.php?latex=%5Ctext%7BTrue%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;text{True}' title='&#92;text{True}' class='latex' /> because penguins are birds which cannot fly. However, if we spend the next century eradicating all birds that cannot fly, then on January 1, 2112, the formula might evaluate to <img src='http://s0.wp.com/latex.php?latex=%5Ctext%7BFalse%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;text{False}' title='&#92;text{False}' class='latex' />.</p>
<p style="text-align:center;">__________</p>
<p><strong>Technicalities</strong></p>
<p>Let us consider the formula <img src='http://s0.wp.com/latex.php?latex=%5Cneg%5Cleft%28%5Cforall+x+%5C%2C+%5Cvarphi+%28x%29%5Cright%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;neg&#92;left(&#92;forall x &#92;, &#92;varphi (x)&#92;right)' title='&#92;neg&#92;left(&#92;forall x &#92;, &#92;varphi (x)&#92;right)' class='latex' />, where <img src='http://s0.wp.com/latex.php?latex=%5Cvarphi&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;varphi' title='&#92;varphi' class='latex' /> is a predicate. If this formula evaluates to <img src='http://s0.wp.com/latex.php?latex=%5Ctext%7BTrue%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;text{True}' title='&#92;text{True}' class='latex' />, then it is not the case that for all <img src='http://s0.wp.com/latex.php?latex=x+%5Cin+%5Cmathcal%7BX%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x &#92;in &#92;mathcal{X}' title='x &#92;in &#92;mathcal{X}' class='latex' /> we have that <img src='http://s0.wp.com/latex.php?latex=%5Cvarphi+%28x%29+%3D+%5Ctext%7BTrue%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;varphi (x) = &#92;text{True}' title='&#92;varphi (x) = &#92;text{True}' class='latex' />, which is the same as saying that there exists a <img src='http://s0.wp.com/latex.php?latex=x+%5Cin+%5Cmathcal%7BX%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x &#92;in &#92;mathcal{X}' title='x &#92;in &#92;mathcal{X}' class='latex' /> for which <img src='http://s0.wp.com/latex.php?latex=%5Cvarphi+%28x%29+%3D+%5Ctext%7BFalse%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;varphi (x) = &#92;text{False}' title='&#92;varphi (x) = &#92;text{False}' class='latex' /> or, in other words, for which <img src='http://s0.wp.com/latex.php?latex=%5Cneg+%5Cvarphi+%28x%29+%3D+%5Ctext%7BTrue%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;neg &#92;varphi (x) = &#92;text{True}' title='&#92;neg &#92;varphi (x) = &#92;text{True}' class='latex' />. Hence, formula <img src='http://s0.wp.com/latex.php?latex=%5Cneg%5Cleft%28%5Cforall+x+%5C%2C+%5Cvarphi+%28x%29%5Cright%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;neg&#92;left(&#92;forall x &#92;, &#92;varphi (x)&#92;right)' title='&#92;neg&#92;left(&#92;forall x &#92;, &#92;varphi (x)&#92;right)' class='latex' /> is <em>equivalent</em> (in some sense) to the formula <img src='http://s0.wp.com/latex.php?latex=%5Cexists+x+%5C%2C+%5Cneg+%5Cvarphi+%28x%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;exists x &#92;, &#92;neg &#92;varphi (x)' title='&#92;exists x &#92;, &#92;neg &#92;varphi (x)' class='latex' />, where <img src='http://s0.wp.com/latex.php?latex=%5Cexists&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;exists' title='&#92;exists' class='latex' /> is the <a href="http://en.wikipedia.org/wiki/Existential_quantification" target="_blank">existential quantifier</a>. We write <img src='http://s0.wp.com/latex.php?latex=%5Cneg%5Cleft%28%5Cforall+x+%5C%2C+%5Cvarphi+%28x%29%5Cright%29+%5Cequiv+%5Cexists+x+%5C%2C+%5Cneg+%5Cvarphi+%28x%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;neg&#92;left(&#92;forall x &#92;, &#92;varphi (x)&#92;right) &#92;equiv &#92;exists x &#92;, &#92;neg &#92;varphi (x)' title='&#92;neg&#92;left(&#92;forall x &#92;, &#92;varphi (x)&#92;right) &#92;equiv &#92;exists x &#92;, &#92;neg &#92;varphi (x)' class='latex' /> to denote this equivalence. Hence, we conclude that</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Cneg+%5Cleft%28+%5Cforall+x+%5C%2C+%28+B+%28x%29+%5Crightarrow+F+%28x%29+%29+%5Cright%29+%5Cequiv+%5Cexists+x+%5C%2C+%5Cneg+%28+B+%28x%29+%5Crightarrow+F+%28x%29+%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;neg &#92;left( &#92;forall x &#92;, ( B (x) &#92;rightarrow F (x) ) &#92;right) &#92;equiv &#92;exists x &#92;, &#92;neg ( B (x) &#92;rightarrow F (x) )' title='&#92;neg &#92;left( &#92;forall x &#92;, ( B (x) &#92;rightarrow F (x) ) &#92;right) &#92;equiv &#92;exists x &#92;, &#92;neg ( B (x) &#92;rightarrow F (x) )' class='latex' />.</p>
<p>Given propositional atoms <img src='http://s0.wp.com/latex.php?latex=p&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='p' title='p' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=q&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='q' title='q' class='latex' />, we have that the formula <img src='http://s0.wp.com/latex.php?latex=p+%5Crightarrow+q&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='p &#92;rightarrow q' title='p &#92;rightarrow q' class='latex' /> is <em>semantically equivalent</em> [1] to the formula <img src='http://s0.wp.com/latex.php?latex=%5Cneg+p+%5Clor+q&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;neg p &#92;lor q' title='&#92;neg p &#92;lor q' class='latex' />. We write <img src='http://s0.wp.com/latex.php?latex=p+%5Crightarrow+q+%5Cequiv+%5Cneg+p+%5Clor+q&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='p &#92;rightarrow q &#92;equiv &#92;neg p &#92;lor q' title='p &#92;rightarrow q &#92;equiv &#92;neg p &#92;lor q' class='latex' /> to denote this equivalence. Thus, we have that</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=B+%28x%29+%5Crightarrow+F+%28x%29+%5Cequiv+%5Cneg+B+%28x%29+%5Clor+F+%28x%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='B (x) &#92;rightarrow F (x) &#92;equiv &#92;neg B (x) &#92;lor F (x)' title='B (x) &#92;rightarrow F (x) &#92;equiv &#92;neg B (x) &#92;lor F (x)' class='latex' /></p>
<p>which allows us to conclude that</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Cexists+x+%5C%2C+%5Cneg+%28+B+%28x%29+%5Crightarrow+F+%28x%29+%29+%5Cequiv+%5Cexists+x+%5C%2C+%5Cneg+%28+%5Cneg+B+%28x%29+%5Clor+F+%28x%29+%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;exists x &#92;, &#92;neg ( B (x) &#92;rightarrow F (x) ) &#92;equiv &#92;exists x &#92;, &#92;neg ( &#92;neg B (x) &#92;lor F (x) )' title='&#92;exists x &#92;, &#92;neg ( B (x) &#92;rightarrow F (x) ) &#92;equiv &#92;exists x &#92;, &#92;neg ( &#92;neg B (x) &#92;lor F (x) )' class='latex' />.</p>
<p>Using one of <a href="http://en.wikipedia.org/wiki/De_Morgan%27s_laws" target="_blank">De Morgan&#8217;s laws</a>, namely, <img src='http://s0.wp.com/latex.php?latex=%5Cneg+%28p+%5Clor+q%29+%5Cequiv+%5Cneg+p+%5Cland+%5Cneg+q&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;neg (p &#92;lor q) &#92;equiv &#92;neg p &#92;land &#92;neg q' title='&#92;neg (p &#92;lor q) &#92;equiv &#92;neg p &#92;land &#92;neg q' class='latex' />, we conclude that <img src='http://s0.wp.com/latex.php?latex=%5Cneg+%28+%5Cneg+B+%28x%29+%5Clor+F+%28x%29+%29+%5Cequiv+%5Cneg+%5Cneg+B+%28x%29+%5Cland+%5Cneg+F+%28x%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;neg ( &#92;neg B (x) &#92;lor F (x) ) &#92;equiv &#92;neg &#92;neg B (x) &#92;land &#92;neg F (x)' title='&#92;neg ( &#92;neg B (x) &#92;lor F (x) ) &#92;equiv &#92;neg &#92;neg B (x) &#92;land &#92;neg F (x)' class='latex' />. Eliminating the <a href="http://en.wikipedia.org/wiki/Double_negation_elimination" target="_blank">double negation</a> (we can do that in Classical Logic, but not in <a href="http://en.wikipedia.org/wiki/Intuitionistic_logic" target="_blank">Intuitionistic Logic</a>), we obtain <img src='http://s0.wp.com/latex.php?latex=%5Cneg+%28+%5Cneg+B+%28x%29+%5Clor+F+%28x%29+%29+%5Cequiv+B+%28x%29+%5Cland+%5Cneg+F+%28x%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;neg ( &#92;neg B (x) &#92;lor F (x) ) &#92;equiv B (x) &#92;land &#92;neg F (x)' title='&#92;neg ( &#92;neg B (x) &#92;lor F (x) ) &#92;equiv B (x) &#92;land &#92;neg F (x)' class='latex' />. Without further ado, we have that</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Cexists+x+%5C%2C+%5Cneg+%28+%5Cneg+B+%28x%29+%5Clor+F+%28x%29+%29+%5Cequiv+%5Cexists+x+%5C%2C+%28+B+%28x%29+%5Cland+%5Cneg+F+%28x%29+%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;exists x &#92;, &#92;neg ( &#92;neg B (x) &#92;lor F (x) ) &#92;equiv &#92;exists x &#92;, ( B (x) &#92;land &#92;neg F (x) )' title='&#92;exists x &#92;, &#92;neg ( &#92;neg B (x) &#92;lor F (x) ) &#92;equiv &#92;exists x &#92;, ( B (x) &#92;land &#92;neg F (x) )' class='latex' />.</p>
<p>From all these equivalences, we finally conclude that</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Cneg+%5Cleft%28+%5Cforall+x+%5C%2C+%28+B+%28x%29+%5Crightarrow+F+%28x%29+%29+%5Cright%29+%5Cequiv+%5Cexists+x+%5C%2C+%28+B+%28x%29+%5Cland+%5Cneg+F+%28x%29+%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;neg &#92;left( &#92;forall x &#92;, ( B (x) &#92;rightarrow F (x) ) &#92;right) &#92;equiv &#92;exists x &#92;, ( B (x) &#92;land &#92;neg F (x) )' title='&#92;neg &#92;left( &#92;forall x &#92;, ( B (x) &#92;rightarrow F (x) ) &#92;right) &#92;equiv &#92;exists x &#92;, ( B (x) &#92;land &#92;neg F (x) )' class='latex' />.</p>
<p>What does this equivalence say? It tells us that the declarative sentence &#8220;Not all birds can fly&#8221; can be paraphrased as follows</p>
<p style="text-align:center;"><em>There exists a thing which is a bird and which cannot fly.</em></p>
<p>That &#8220;thing&#8221; could be, for example, a penguin. For the sake of convenience, let us introduce predicates <img src='http://s0.wp.com/latex.php?latex=%5Cvarphi&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;varphi' title='&#92;varphi' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%5Cpsi&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;psi' title='&#92;psi' class='latex' />, defined by</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Cvarphi+%28x%29+%3A%3D+B+%28x%29+%5Crightarrow+F+%28x%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;varphi (x) := B (x) &#92;rightarrow F (x)' title='&#92;varphi (x) := B (x) &#92;rightarrow F (x)' class='latex' /></p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Cpsi+%28x%29+%3A%3D+B+%28x%29+%5Cland+%5Cneg+F+%28x%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;psi (x) := B (x) &#92;land &#92;neg F (x)' title='&#92;psi (x) := B (x) &#92;land &#92;neg F (x)' class='latex' /></p>
<p>so that the last equivalence can be written more compactly as</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Cneg+%5Cleft%28+%5Cforall+x+%5C%2C+%5Cvarphi+%28x%29+%5Cright%29+%5Cequiv+%5Cexists+x+%5C%2C+%5Cpsi+%28x%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;neg &#92;left( &#92;forall x &#92;, &#92;varphi (x) &#92;right) &#92;equiv &#92;exists x &#92;, &#92;psi (x)' title='&#92;neg &#92;left( &#92;forall x &#92;, &#92;varphi (x) &#92;right) &#92;equiv &#92;exists x &#92;, &#92;psi (x)' class='latex' /></p>
<p>where <img src='http://s0.wp.com/latex.php?latex=%5Cvarphi+%28x%29+%5Cequiv+%5Cneg+B+%28x%29+%5Clor+F+%28x%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;varphi (x) &#92;equiv &#92;neg B (x) &#92;lor F (x)' title='&#92;varphi (x) &#92;equiv &#92;neg B (x) &#92;lor F (x)' class='latex' />. This will be useful later on.</p>
<p style="text-align:center;">__________</p>
<p><strong>Creating our universe</strong></p>
<p>Let us suppose that we do not know that penguins are birds and that they cannot fly. Evaluating the formula <img src='http://s0.wp.com/latex.php?latex=%5Cexists+x+%5C%2C+%28+B+%28x%29+%5Cland+%5Cneg+F+%28x%29+%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;exists x &#92;, ( B (x) &#92;land &#92;neg F (x) )' title='&#92;exists x &#92;, ( B (x) &#92;land &#92;neg F (x) )' class='latex' /> using planet Earth&#8217;s <a href="http://en.wikipedia.org/wiki/Fauna" target="_blank">fauna</a> as the set of all things of interest would require us to consult quite a few Zoology books. However, it is Sunday night and the library is closed for the Winter break. And, to make matters worse, we are somewhat lazy.</p>
<p>Instead of bothering with the actual universe, let us create our own imaginary universe in which there are only three classes of animals: birds, cats, and dogs. Each class contains only three species each.</p>
<p>We construct this imaginary universe in <a href="http://en.wikipedia.org/wiki/Haskell_%28programming_language%29" target="_blank">Haskell</a>:</p>
<pre style="padding-left:30px;background:#EEEEEE;">-- create Animal data type
data Animal = Bird String |
              Cat String |
              Dog String deriving (Show, Eq)

-- create list of animals
animals = [Bird "Dove", Bird "Hawk", Bird "Penguin",
           Cat "Burmese", Cat "Persian", Cat "Siamese",
           Dog "Dalmatian", Dog "Husky", Dog "Terrier"]

-- define isBird predicate
isBird :: Animal -&gt; Bool
isBird (Bird s) = True
isBird _ = False

-- define canFly predicate
canFly :: Animal -&gt; Bool
canFly (Bird "Dove") = True
canFly (Bird "Hawk") = True
canFly _ = False

-- define phi predicate
phi :: Animal -&gt; Bool
phi x = ((not . isBird) x) || (canFly x) 

-- define psi predicate
psi :: Animal -&gt; Bool
psi x = (isBird x) &amp;&amp; ((not . canFly) x)</pre>
<p>Let us now play with these objects and predicates and evaluate the quantified formulas using the <a href="http://www.haskell.org/ghc/docs/latest/html/users_guide/ghci.html" target="_blank">GHCi</a> interpreter:</p>
<pre style="padding-left:30px;background:#FFFFFF;">*Main&gt; -- print list of animals
*Main&gt; animals
[Bird "Dove",Bird "Hawk",Bird "Penguin",Cat "Burmese",Cat "Persian",
Cat "Siamese",Dog "Dalmatian",Dog "Husky",Dog "Terrier"]
*Main&gt; -- test isBird predicate
*Main&gt; map isBird animals
[True,True,True,False,False,False,False,False,False]
*Main&gt; -- test canFly predicate
*Main&gt; map canFly animals
[True,True,False,False,False,False,False,False,False]
*Main&gt; -- test phi predicate
*Main&gt; map phi animals
[True,True,False,True,True,True,True,True,True]
*Main&gt; -- test psi predicate
*Main&gt; map psi animals
[False,False,True,False,False,False,False,False,False]
*Main&gt; -- evaluate universally quantified formula
*Main&gt; not (all phi animals)
True
*Main&gt; -- evaluate existentially quantified formula
*Main&gt; any psi animals
True</pre>
<p>where we used functions <a href="http://hackage.haskell.org/packages/archive/base/latest/doc/html/Prelude.html#v:all" target="_blank">all</a> and <a href="http://hackage.haskell.org/packages/archive/base/latest/doc/html/Prelude.html#v:any" target="_blank">any</a> to emulate the universal and existential quantifiers, respectively. Note that both formulas evaluate to <img src='http://s0.wp.com/latex.php?latex=%5Ctext%7BTrue%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;text{True}' title='&#92;text{True}' class='latex' />, as expected.</p>
<p style="text-align:center;">__________</p>
<p><span style="text-decoration:underline;">Caution</span>: do not confuse the arrow used in the definition of the predicates with the arrow used in material implication. They look the same, but they are different <img src='http://s0.wp.com/latex.php?latex=%5CLaTeX&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;LaTeX' title='&#92;LaTeX' class='latex' /> symbols and context should allow one to distinguish the two.</p>
<p><span style="text-decoration:underline;">Remark</span>: this post is based on an example in chapter 2 of Huth &amp; Ryan [1].</p>
<p style="text-align:center;">__________</p>
<p><strong>References</strong></p>
<p style="padding-left:30px;">[1] Michael Huth, Mark Ryan, <a href="http://www.cs.bham.ac.uk/research/projects/lics/" target="_blank"><em>Logic in Computer Science: Modelling and Reasoning about Systems</em></a>, 2nd ed., Cambridge University Press, June 2004.</p>
<br />Filed under: <a href='http://stochastix.wordpress.com/category/programming/haskell/'>Haskell</a>, <a href='http://stochastix.wordpress.com/category/logic/'>Logic</a> Tagged: <a href='http://stochastix.wordpress.com/tag/haskell/'>Haskell</a>, <a href='http://stochastix.wordpress.com/tag/logic/'>Logic</a>, <a href='http://stochastix.wordpress.com/tag/predicate-logic/'>Predicate Logic</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/stochastix.wordpress.com/7071/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/stochastix.wordpress.com/7071/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/stochastix.wordpress.com/7071/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/stochastix.wordpress.com/7071/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/stochastix.wordpress.com/7071/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/stochastix.wordpress.com/7071/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/stochastix.wordpress.com/7071/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/stochastix.wordpress.com/7071/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/stochastix.wordpress.com/7071/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/stochastix.wordpress.com/7071/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/stochastix.wordpress.com/7071/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/stochastix.wordpress.com/7071/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/stochastix.wordpress.com/7071/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/stochastix.wordpress.com/7071/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stochastix.wordpress.com&amp;blog=300626&amp;post=7071&amp;subd=stochastix&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://stochastix.wordpress.com/2012/01/01/not-all-birds-can-fly/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">stochastix</media:title>
		</media:content>
	</item>
		<item>
		<title>Gell-Mann on the evolution of human languages</title>
		<link>http://stochastix.wordpress.com/2012/01/01/gell-mann-on-the-evolution-of-human-languages/</link>
		<comments>http://stochastix.wordpress.com/2012/01/01/gell-mann-on-the-evolution-of-human-languages/#comments</comments>
		<pubDate>Sun, 01 Jan 2012 08:10:46 +0000</pubDate>
		<dc:creator>Rod Carvalho</dc:creator>
				<category><![CDATA[Linguistics]]></category>
		<category><![CDATA[Murray Gell-Mann]]></category>

		<guid isPermaLink="false">http://stochastix.wordpress.com/?p=7066</guid>
		<description><![CDATA[Murray Gell-Mann gave a very interesting talk on evolutionary linguistics at CERN: Hat tip: Luboš Motl Filed under: Linguistics Tagged: Linguistics, Murray Gell-Mann<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stochastix.wordpress.com&amp;blog=300626&amp;post=7066&amp;subd=stochastix&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://en.wikipedia.org/wiki/Murray_Gell-Mann" target="_blank">Murray Gell-Mann</a> gave a very interesting talk on evolutionary linguistics at CERN:</p>
<span class='embed-youtube' style='text-align:center; display: block;'><iframe class='youtube-player' type='text/html' width='450' height='284' src='http://www.youtube.com/embed/ayipu3D_15M?version=3&amp;rel=1&amp;fs=1&amp;showsearch=0&amp;showinfo=1&amp;iv_load_policy=1&amp;wmode=transparent' frameborder='0'></iframe></span>
<p><span style="text-decoration:underline;">Hat tip</span>: <a href="http://motls.blogspot.com/2012/01/gell-mann-on-evolutionary-linguistics.html" target="_blank">Luboš Motl</a></p>
<br />Filed under: <a href='http://stochastix.wordpress.com/category/linguistics/'>Linguistics</a> Tagged: <a href='http://stochastix.wordpress.com/tag/linguistics/'>Linguistics</a>, <a href='http://stochastix.wordpress.com/tag/murray-gell-mann/'>Murray Gell-Mann</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/stochastix.wordpress.com/7066/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/stochastix.wordpress.com/7066/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/stochastix.wordpress.com/7066/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/stochastix.wordpress.com/7066/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/stochastix.wordpress.com/7066/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/stochastix.wordpress.com/7066/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/stochastix.wordpress.com/7066/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/stochastix.wordpress.com/7066/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/stochastix.wordpress.com/7066/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/stochastix.wordpress.com/7066/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/stochastix.wordpress.com/7066/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/stochastix.wordpress.com/7066/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/stochastix.wordpress.com/7066/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/stochastix.wordpress.com/7066/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stochastix.wordpress.com&amp;blog=300626&amp;post=7066&amp;subd=stochastix&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://stochastix.wordpress.com/2012/01/01/gell-mann-on-the-evolution-of-human-languages/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">stochastix</media:title>
		</media:content>
	</item>
		<item>
		<title>Circular convolution in Haskell</title>
		<link>http://stochastix.wordpress.com/2011/09/05/circular-convolution-in-haskell/</link>
		<comments>http://stochastix.wordpress.com/2011/09/05/circular-convolution-in-haskell/#comments</comments>
		<pubDate>Tue, 06 Sep 2011 06:51:15 +0000</pubDate>
		<dc:creator>Rod Carvalho</dc:creator>
				<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Signal Processing]]></category>
		<category><![CDATA[Circular Convolution]]></category>
		<category><![CDATA[Digital Signal Processing]]></category>
		<category><![CDATA[Discrete-Time Signal Processing]]></category>
		<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[Higher-Order Functions]]></category>
		<category><![CDATA[List Processing]]></category>

		<guid isPermaLink="false">http://stochastix.wordpress.com/?p=6947</guid>
		<description><![CDATA[Given two finite sequences, and , of length , their -point circular convolution can be written in matrix form [1] as follows where is also a sequence of length . Note that the matrix above is a circulant matrix, as each row is a circularly right-shifted version of the previous one. Each is obtained by [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stochastix.wordpress.com&amp;blog=300626&amp;post=6947&amp;subd=stochastix&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Given two finite sequences, <img src='http://s0.wp.com/latex.php?latex=x&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x' title='x' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=y&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='y' title='y' class='latex' />, of length <img src='http://s0.wp.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='n' title='n' class='latex' />, their <img src='http://s0.wp.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='n' title='n' class='latex' />-point <em>circular convolution</em> can be written in matrix form [1] as follows</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Cleft%5B%5Cbegin%7Barray%7D%7Bc%7D+z_0%5C%5C+z_1%5C%5C+z_2%5C%5C+%5Cvdots%5C%5C+z_%7Bn-2%7D%5C%5C+z_%7Bn-1%7D%5Cend%7Barray%7D%5Cright%5D+%3D+%5Cleft%5B%5Cbegin%7Barray%7D%7Bcccccc%7D+y_0+%26+y_%7Bn-1%7D+%26+y_%7Bn-2%7D+%26+%5Cldots+%26+y_2+%26+y_1%5C%5C+y_1+%26+y_0+%26+y_%7Bn-1%7D+%26+%5Cldots+%26+y_3+%26+y_2%5C%5C+y_2+%26+y_1+%26+y_0+%26+%5Cldots+%26+y_4+%26+y_3%5C%5C+%5Cvdots+%26+%5Cvdots+%26+%5Cvdots+%26+%5Cddots+%26+%5Cvdots+%26+%5Cvdots%5C%5C+y_%7Bn-2%7D+%26+y_%7Bn-3%7D+%26+y_%7Bn-4%7D+%26+%5Cldots+%26+y_0+%26+y_%7Bn-1%7D%5C%5C+y_%7Bn-1%7D+%26+y_%7Bn-2%7D+%26+y_%7Bn-3%7D+%26+%5Cldots+%26+y_1+%26+y_0%5Cend%7Barray%7D%5Cright%5D+%5Cleft%5B%5Cbegin%7Barray%7D%7Bc%7D+x_0%5C%5C+x_1%5C%5C+x_2%5C%5C+%5Cvdots%5C%5C+x_%7Bn-2%7D%5C%5C+x_%7Bn-1%7D%5Cend%7Barray%7D%5Cright%5D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;left[&#92;begin{array}{c} z_0&#92;&#92; z_1&#92;&#92; z_2&#92;&#92; &#92;vdots&#92;&#92; z_{n-2}&#92;&#92; z_{n-1}&#92;end{array}&#92;right] = &#92;left[&#92;begin{array}{cccccc} y_0 &amp; y_{n-1} &amp; y_{n-2} &amp; &#92;ldots &amp; y_2 &amp; y_1&#92;&#92; y_1 &amp; y_0 &amp; y_{n-1} &amp; &#92;ldots &amp; y_3 &amp; y_2&#92;&#92; y_2 &amp; y_1 &amp; y_0 &amp; &#92;ldots &amp; y_4 &amp; y_3&#92;&#92; &#92;vdots &amp; &#92;vdots &amp; &#92;vdots &amp; &#92;ddots &amp; &#92;vdots &amp; &#92;vdots&#92;&#92; y_{n-2} &amp; y_{n-3} &amp; y_{n-4} &amp; &#92;ldots &amp; y_0 &amp; y_{n-1}&#92;&#92; y_{n-1} &amp; y_{n-2} &amp; y_{n-3} &amp; &#92;ldots &amp; y_1 &amp; y_0&#92;end{array}&#92;right] &#92;left[&#92;begin{array}{c} x_0&#92;&#92; x_1&#92;&#92; x_2&#92;&#92; &#92;vdots&#92;&#92; x_{n-2}&#92;&#92; x_{n-1}&#92;end{array}&#92;right]' title='&#92;left[&#92;begin{array}{c} z_0&#92;&#92; z_1&#92;&#92; z_2&#92;&#92; &#92;vdots&#92;&#92; z_{n-2}&#92;&#92; z_{n-1}&#92;end{array}&#92;right] = &#92;left[&#92;begin{array}{cccccc} y_0 &amp; y_{n-1} &amp; y_{n-2} &amp; &#92;ldots &amp; y_2 &amp; y_1&#92;&#92; y_1 &amp; y_0 &amp; y_{n-1} &amp; &#92;ldots &amp; y_3 &amp; y_2&#92;&#92; y_2 &amp; y_1 &amp; y_0 &amp; &#92;ldots &amp; y_4 &amp; y_3&#92;&#92; &#92;vdots &amp; &#92;vdots &amp; &#92;vdots &amp; &#92;ddots &amp; &#92;vdots &amp; &#92;vdots&#92;&#92; y_{n-2} &amp; y_{n-3} &amp; y_{n-4} &amp; &#92;ldots &amp; y_0 &amp; y_{n-1}&#92;&#92; y_{n-1} &amp; y_{n-2} &amp; y_{n-3} &amp; &#92;ldots &amp; y_1 &amp; y_0&#92;end{array}&#92;right] &#92;left[&#92;begin{array}{c} x_0&#92;&#92; x_1&#92;&#92; x_2&#92;&#92; &#92;vdots&#92;&#92; x_{n-2}&#92;&#92; x_{n-1}&#92;end{array}&#92;right]' class='latex' /></p>
<p>where <img src='http://s0.wp.com/latex.php?latex=z&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='z' title='z' class='latex' /> is also a sequence of length <img src='http://s0.wp.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='n' title='n' class='latex' />. Note that the <img src='http://s0.wp.com/latex.php?latex=n+%5Ctimes+n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='n &#92;times n' title='n &#92;times n' class='latex' /> matrix above is a <a href="http://en.wikipedia.org/wiki/Circulant_matrix" target="_blank">circulant matrix</a>, as each row is a circularly right-shifted version of the previous one. Each <img src='http://s0.wp.com/latex.php?latex=z_k&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='z_k' title='z_k' class='latex' /> is obtained by taking the inner product of the <img src='http://s0.wp.com/latex.php?latex=k&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='k' title='k' class='latex' />-th row of the matrix with the <img src='http://s0.wp.com/latex.php?latex=x&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x' title='x' class='latex' /> vector.</p>
<p>In <a href="http://en.wikipedia.org/wiki/Digital_signal_processing" target="_blank">Digital Signal Processing</a> (DSP), we often think of finite sequences as <em>vectors</em>, which allows us to compute convolutions using matrix operations. Taking a more Computer Science-ish approach, one can think of finite sequences as finite <em>lists</em>, and compute convolutions using list operations. The matrix above can be thought of as a <em>list of lists</em> (where each row is a list). We know how to multiply a matrix by a vector, but how do we &#8220;multiply&#8221; a list of lists by a list?</p>
<p>I started teaching myself <a href="http://en.wikipedia.org/wiki/Haskell_%28programming_language%29" target="_blank">Haskell</a> this Summer, and I am (obviously) looking for interesting problems at which to throw some <a href="http://en.wikipedia.org/wiki/Functional_programming" target="_blank">functional</a> firepower. Solutions looking for a problem. But, first, let us &#8220;begin at the beginning&#8221;&#8230;</p>
<p style="text-align:center;">__________</p>
<p><strong>Reverse</strong></p>
<p>Take another look at the matrix above and note that its last row, when viewed as a list, is the reversal of list <img src='http://s0.wp.com/latex.php?latex=y&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='y' title='y' class='latex' />, which means that we need a function that reverses lists. Fortunately, we do not need to implement such a function, as there is the function <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse" target="_blank">reverse</a> in <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html" target="_blank">Prelude</a> that uses <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl" target="_blank">foldl</a> and <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:flip" target="_blank">flip</a>:</p>
<pre style="padding-left:30px;background:#EEEEEE;">reverse :: [a] -&gt; [a]
reverse = foldl (flip (:)) []</pre>
<p style="text-align:center;">__________</p>
<p><strong>Circular shifts</strong></p>
<p>Yet once again, let us look at the matrix at the top of this post. Note that the 2nd row is a circularly right-shifted version of the 1st one, and that the 3rd row is a circularly right-shifted version of the 2nd one, <em>et cetera</em>. Also, note that the <img src='http://s0.wp.com/latex.php?latex=%28n-1%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='(n-1)' title='(n-1)' class='latex' />-th row is a circularly left-shifted version of the <img src='http://s0.wp.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='n' title='n' class='latex' />-th row, and so and so on. Thus, we need to implement functions that perform circular shifts.</p>
<p>The following function circularly right-shifts a list:</p>
<pre style="padding-left:30px;background:#EEEEEE;">circShiftR :: [a] -&gt; [a]
circShiftR [] = []
circShiftR x = last x : init x</pre>
<p>This function merely takes the last element of the list and moves it to the beginning. We can circularly left-shift a list using the function:</p>
<pre style="padding-left:30px;background:#EEEEEE;">circShiftL :: [a] -&gt; [a]
circShiftL [] = []
circShiftL xs = (tail xs) ++ [head xs]</pre>
<p>which moves the first element of a list to the end. Let us now test these functions on some simple lists using the <a href="http://www.haskell.org/ghc/docs/latest/html/users_guide/ghci.html" target="_blank">GHCi</a> interpreter:</p>
<pre style="padding-left:30px;background:#FFFFFF;">*Main&gt; circShiftR [0..9]
[9,0,1,2,3,4,5,6,7,8]
*Main&gt; circShiftR (circShiftR [0..9])
[8,9,0,1,2,3,4,5,6,7]
*Main&gt; (circShiftR . circShiftR) [0..9]
[8,9,0,1,2,3,4,5,6,7]
*Main&gt; circShiftL [0..9]
[1,2,3,4,5,6,7,8,9,0]
*Main&gt; (circShiftL . circShiftL) [0..9]
[2,3,4,5,6,7,8,9,0,1]</pre>
<p>Note that in the 3rd and 5th inputs we <a href="http://en.wikipedia.org/wiki/Function_composition" target="_blank">composed</a> the circular shifting functions to perform double right- and left-shifts.</p>
<p style="text-align:center;">__________</p>
<p><strong>Iterate</strong></p>
<p>Since each row of the matrix is a right-shifted version of the previous one or a left-shifted version of the next one, we can generate the matrix from its first or last rows. Putting it in terms of lists, we can generate a <em>list of lists</em> that represents the matrix from a single list using the <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:iterate" target="_blank">iterate</a> function in <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html" target="_blank">Prelude</a>:</p>
<pre style="padding-left:30px;background:#EEEEEE;">iterate :: (a -&gt; a) -&gt; a -&gt; [a]
iterate f x =  x : iterate f (f x)</pre>
<p>and the circular shifting functions we implemented before. To make things concrete, we can generate all four circular right-shifts of a list of length 4, as follows:</p>
<pre style="padding-left:30px;background:#FFFFFF;">*Main&gt; take 4 (iterate circShiftR [0..3])
[[0,1,2,3],[3,0,1,2],[2,3,0,1],[1,2,3,0]]</pre>
<p>A word of caution is in order. Note that we <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:take" target="_blank">take</a> the first four lists, as</p>
<pre style="padding-left:30px;">iterate f x</pre>
<p>does generate an <em>infinite</em> list containing <img src='http://s0.wp.com/latex.php?latex=x&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x' title='x' class='latex' />, <img src='http://s0.wp.com/latex.php?latex=f+%28x%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='f (x)' title='f (x)' class='latex' />, <img src='http://s0.wp.com/latex.php?latex=%28f+%5Ccirc+f+%29+%28x%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='(f &#92;circ f ) (x)' title='(f &#92;circ f ) (x)' class='latex' />, <img src='http://s0.wp.com/latex.php?latex=%28f+%5Ccirc+f+%5Ccirc+f+%29+%28x%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='(f &#92;circ f &#92;circ f ) (x)' title='(f &#92;circ f &#92;circ f ) (x)' class='latex' />, <em>et cetera</em>. We do not need an infinite list of lists.</p>
<p>We now know how to generate a <em>list of lists</em> representing the matrix.</p>
<p style="text-align:center;">__________</p>
<p><strong>Inner product</strong></p>
<p>To obtain the circular convolution of <img src='http://s0.wp.com/latex.php?latex=x&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x' title='x' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=y&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='y' title='y' class='latex' />, we multiply a matrix by a (column) vector, which consists of computing <img src='http://s0.wp.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='n' title='n' class='latex' /> inner products. However, in this post we are thinking in terms of lists. We thus need to implement a function that computes the inner product of two (finite) lists, as follows:</p>
<pre style="padding-left:30px;background:#EEEEEE;">innerProd :: Num a =&gt; [a] -&gt; [a] -&gt; a
innerProd xs ys = sum(zipWith (*) xs ys)</pre>
<p>where we first use <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zipWith" target="_blank">zipWith</a> to obtain a list of the <img src='http://s0.wp.com/latex.php?latex=x_i+y_i&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x_i y_i' title='x_i y_i' class='latex' /> products, and then use <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sum" target="_blank">sum</a> to compute the sum <img src='http://s0.wp.com/latex.php?latex=%5Csum_i+x_i+y_i&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;sum_i x_i y_i' title='&#92;sum_i x_i y_i' class='latex' />. Let us test the inner product function:</p>
<pre style="padding-left:30px;background:#FFFFFF;">*Main&gt; let xs = [1..4]
*Main&gt; let ys = [0..3]
*Main&gt; zipWith (*) xs ys
[0,2,6,12]
*Main&gt; sum(zipWith (*) xs ys)
20</pre>
<p style="text-align:center;">__________</p>
<p><strong>Circular convolution</strong></p>
<p>We finally have all we need to compute the circular convolution.</p>
<p>For example, suppose we want to compute the circular convolution of two 4-point sequences, say, <img src='http://s0.wp.com/latex.php?latex=x+%3D+%281%2C1%2C1%2C1%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x = (1,1,1,1)' title='x = (1,1,1,1)' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=y+%3D+%280%2C1%2C2%2C3%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='y = (0,1,2,3)' title='y = (0,1,2,3)' class='latex' />. Using matrix-vector multiplication, we obtain:</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Cleft%5B%5Cbegin%7Barray%7D%7Bcccc%7D+0+%26+3+%26+2+%26+1%5C%5C+1+%26+0+%26+3+%26+2%5C%5C+2+%26+1+%26+0+%26+3%5C%5C+3+%26+2+%26+1+%26+0%5Cend%7Barray%7D%5Cright%5D+%5Cleft%5B%5Cbegin%7Barray%7D%7Bc%7D+1%5C%5C+1%5C%5C+1%5C%5C+1%5Cend%7Barray%7D%5Cright%5D+%3D+%5Cleft%5B%5Cbegin%7Barray%7D%7Bc%7D+6%5C%5C+6%5C%5C+6%5C%5C+6%5Cend%7Barray%7D%5Cright%5D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;left[&#92;begin{array}{cccc} 0 &amp; 3 &amp; 2 &amp; 1&#92;&#92; 1 &amp; 0 &amp; 3 &amp; 2&#92;&#92; 2 &amp; 1 &amp; 0 &amp; 3&#92;&#92; 3 &amp; 2 &amp; 1 &amp; 0&#92;end{array}&#92;right] &#92;left[&#92;begin{array}{c} 1&#92;&#92; 1&#92;&#92; 1&#92;&#92; 1&#92;end{array}&#92;right] = &#92;left[&#92;begin{array}{c} 6&#92;&#92; 6&#92;&#92; 6&#92;&#92; 6&#92;end{array}&#92;right]' title='&#92;left[&#92;begin{array}{cccc} 0 &amp; 3 &amp; 2 &amp; 1&#92;&#92; 1 &amp; 0 &amp; 3 &amp; 2&#92;&#92; 2 &amp; 1 &amp; 0 &amp; 3&#92;&#92; 3 &amp; 2 &amp; 1 &amp; 0&#92;end{array}&#92;right] &#92;left[&#92;begin{array}{c} 1&#92;&#92; 1&#92;&#92; 1&#92;&#92; 1&#92;end{array}&#92;right] = &#92;left[&#92;begin{array}{c} 6&#92;&#92; 6&#92;&#92; 6&#92;&#92; 6&#92;end{array}&#92;right]' class='latex' /></p>
<p>Alternatively, we can compute the circular convolution by representing sequences by lists and using the functions we have implemented before:</p>
<pre style="padding-left:30px;background:#FFFFFF;">*Main&gt; -- define lists
*Main&gt; let xs = [1,1,1,1]
*Main&gt; let ys = [0,1,2,3]
*Main&gt; -- reverse and right-shift ys
*Main&gt; let ys' = (circShiftR . reverse) ys
*Main&gt; ys'
[0,3,2,1]
*Main&gt; -- compute list of lists representing the matrix
*Main&gt; let yss = take 4 (iterate circShiftR ys')
*Main&gt; yss
[[0,3,2,1],[1,0,3,2],[2,1,0,3],[3,2,1,0]]
*Main&gt; -- compute inner product of xs with each list in yss
*Main&gt; map (innerProd xs) yss
[6,6,6,6]</pre>
<p>Note that I wrote comments on the GHCi command line in order to improve readability. We are now ready to implement the function that computes the circular convolution:</p>
<pre style="padding-left:30px;background:#EEEEEE;">circConv :: Num a =&gt; [a] -&gt; [a] -&gt; [a]
circConv xs ys = map (innerProd xs) yss
		 where
	     	   n   = length xs
	           ys' = (circShiftR . reverse) ys
		   yss = take n (iterate circShiftR ys')</pre>
<p>How does it work? We first take sequence <img src='http://s0.wp.com/latex.php?latex=y&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='y' title='y' class='latex' />, reverse and right-shift it (to obtain the 1st row of the matrix), generate the remaining rows by right-shifting the previous ones, and finally compute the inner product of each row with sequence <img src='http://s0.wp.com/latex.php?latex=x&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x' title='x' class='latex' /> using <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map" target="_blank">map</a>. Note that</p>
<pre style="padding-left:30px;">innerProd xs</pre>
<p>is a <a href="http://en.wikipedia.org/wiki/Partial_application" target="_blank">partial application</a> of the inner product function, as we provide only the first argument. We could define a function <img src='http://s0.wp.com/latex.php?latex=g&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='g' title='g' class='latex' /> as follows:</p>
<pre style="padding-left:30px;">*Main&gt; let g = innerProd [1,1,1,1]
*Main&gt; :type g
g :: [Integer] -&gt; Integer
*Main&gt; g [0..3]
6</pre>
<p>Note that <img src='http://s0.wp.com/latex.php?latex=g&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='g' title='g' class='latex' /> takes a list of integers and returns its inner product with the list of four ones. Putting it all together in a .hs file, we have:</p>
<pre style="padding-left:30px;background:#EEEEEE;">-- circular right-shift
circShiftR :: [a] -&gt; [a]
circShiftR [] = []
circShiftR x = last x : init x

-- inner product of two lists
innerProd :: Num a =&gt; [a] -&gt; [a] -&gt; a
innerProd xs ys = sum(zipWith (*) xs ys)

-- circular convolution of two lists
circConv :: Num a =&gt; [a] -&gt; [a] -&gt; [a]
circConv xs ys = map (innerProd xs) yss
		 where
	     	    n   = length xs
	            ys' = (circShiftR . reverse) ys
		    yss = take n (iterate circShiftR ys')</pre>
<p>We use several <a href="http://en.wikipedia.org/wiki/Higher-order_function" target="_blank">higher-order functions</a> in this implementation: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map" target="_blank">map</a>, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:iterate" target="_blank">iterate</a>, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zipWith" target="_blank">zipWith</a>. We also use <a href="http://en.wikipedia.org/wiki/Function_composition" target="_blank">function composition</a> and <a href="http://en.wikipedia.org/wiki/Partial_application" target="_blank">partial function application</a>. A lot of beautiful ideas in only a dozen lines of code!</p>
<p>I am a Haskell neophyte, and I make no claims that the code above cannot be improved. If you have any constructive suggestions, I would be happy to hear them. Please use the HTML tags &lt;pre&gt; and &lt;/pre&gt; to post code in the comments.</p>
<p style="text-align:center;">__________</p>
<p><strong>Testing</strong></p>
<p>Last but not least, let us carry out a couple of tests using examples from Mitra&#8217;s book [1]:</p>
<pre style="padding-left:30px;">*Main&gt; -- problem 5.2 b)
*Main&gt; let xs = [-1,5,3,0,3]
*Main&gt; let hs = [-2,0,5,3,-2]
*Main&gt; circConv xs hs
[1,-1,-2,16,26]
*Main&gt; -- problem 5.45 b)
*Main&gt; let gs = [2,-1,3,0]
*Main&gt; let hs = [-2,4,2,-1]
*Main&gt; circConv gs hs
[3,7,-6,8]</pre>
<p>It appears to be working! Please let me know if you find any bugs.</p>
<p style="text-align:center;">__________</p>
<p><strong>References</strong></p>
<p style="padding-left:30px;">[1] Sanjit K. Mitra, <a href="http://books.google.com/books?id=okdqQgAACAAJ" target="_blank"><em>Digital Signal Processing: a computer-based approach</em></a>, 3rd edition, McGraw-Hill, 2005.</p>
<br />Filed under: <a href='http://stochastix.wordpress.com/category/programming/haskell/'>Haskell</a>, <a href='http://stochastix.wordpress.com/category/mathematics/signal-processing/'>Signal Processing</a> Tagged: <a href='http://stochastix.wordpress.com/tag/circular-convolution/'>Circular Convolution</a>, <a href='http://stochastix.wordpress.com/tag/digital-signal-processing/'>Digital Signal Processing</a>, <a href='http://stochastix.wordpress.com/tag/discrete-time-signal-processing/'>Discrete-Time Signal Processing</a>, <a href='http://stochastix.wordpress.com/tag/functional-programming/'>Functional Programming</a>, <a href='http://stochastix.wordpress.com/tag/haskell/'>Haskell</a>, <a href='http://stochastix.wordpress.com/tag/higher-order-functions/'>Higher-Order Functions</a>, <a href='http://stochastix.wordpress.com/tag/list-processing/'>List Processing</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/stochastix.wordpress.com/6947/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/stochastix.wordpress.com/6947/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/stochastix.wordpress.com/6947/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/stochastix.wordpress.com/6947/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/stochastix.wordpress.com/6947/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/stochastix.wordpress.com/6947/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/stochastix.wordpress.com/6947/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/stochastix.wordpress.com/6947/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/stochastix.wordpress.com/6947/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/stochastix.wordpress.com/6947/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/stochastix.wordpress.com/6947/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/stochastix.wordpress.com/6947/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/stochastix.wordpress.com/6947/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/stochastix.wordpress.com/6947/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stochastix.wordpress.com&amp;blog=300626&amp;post=6947&amp;subd=stochastix&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://stochastix.wordpress.com/2011/09/05/circular-convolution-in-haskell/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">stochastix</media:title>
		</media:content>
	</item>
		<item>
		<title>Deciding 2-colorability via quantifier elimination</title>
		<link>http://stochastix.wordpress.com/2011/09/01/deciding-2-colorability-of-graphs/</link>
		<comments>http://stochastix.wordpress.com/2011/09/01/deciding-2-colorability-of-graphs/#comments</comments>
		<pubDate>Fri, 02 Sep 2011 06:26:55 +0000</pubDate>
		<dc:creator>Rod Carvalho</dc:creator>
				<category><![CDATA[Algebraic Geometry]]></category>
		<category><![CDATA[Combinatorics]]></category>
		<category><![CDATA[2-Colorability]]></category>
		<category><![CDATA[Algebraic Sets]]></category>
		<category><![CDATA[Decision Problems]]></category>
		<category><![CDATA[Feasibility]]></category>
		<category><![CDATA[Graph Coloring]]></category>
		<category><![CDATA[Graph Theory]]></category>
		<category><![CDATA[Quantifier Elimination]]></category>
		<category><![CDATA[Real Algebraic Geometry]]></category>
		<category><![CDATA[REDLOG]]></category>
		<category><![CDATA[REDUCE]]></category>
		<category><![CDATA[Vertex Coloring]]></category>

		<guid isPermaLink="false">http://stochastix.wordpress.com/?p=6887</guid>
		<description><![CDATA[A vertex coloring of a graph is a map that assigns a color to each vertex of . The coloring is proper if adjacent vertices are assigned distinct colors. The graph is 2-colorable if there exists a proper coloring whose color set has two elements. For instance, consider a graph whose vertex set is and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stochastix.wordpress.com&amp;blog=300626&amp;post=6887&amp;subd=stochastix&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>A <em>vertex coloring</em> of a graph <img src='http://s0.wp.com/latex.php?latex=G+%3D+%28V%2CE%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='G = (V,E)' title='G = (V,E)' class='latex' /> is a map <img src='http://s0.wp.com/latex.php?latex=%5Ckappa+%3A+V+%5Cto+C&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;kappa : V &#92;to C' title='&#92;kappa : V &#92;to C' class='latex' /> that assigns a color to each vertex of <img src='http://s0.wp.com/latex.php?latex=G&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='G' title='G' class='latex' />. The coloring is <em>proper</em> if adjacent vertices are assigned distinct colors. The graph is 2-colorable if there exists a proper coloring whose <em>color set</em> <img src='http://s0.wp.com/latex.php?latex=C&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='C' title='C' class='latex' /> has two elements.</p>
<p>For instance, consider a graph <img src='http://s0.wp.com/latex.php?latex=G+%3D+%28V%2CE%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='G = (V,E)' title='G = (V,E)' class='latex' /> whose <em>vertex set</em> is <img src='http://s0.wp.com/latex.php?latex=V+%3A%3D+%5C%7B1%2C+2%2C+3%5C%7D+&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='V := &#92;{1, 2, 3&#92;} ' title='V := &#92;{1, 2, 3&#92;} ' class='latex' /> and whose <em>edge set</em> is <img src='http://s0.wp.com/latex.php?latex=E+%3A%3D+%5C%7B%5C%7B1%2C2%5C%7D%2C+%5C%7B1%2C3%5C%7D%2C+%5C%7B2%2C3%5C%7D%5C%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='E := &#92;{&#92;{1,2&#92;}, &#92;{1,3&#92;}, &#92;{2,3&#92;}&#92;}' title='E := &#92;{&#92;{1,2&#92;}, &#92;{1,3&#92;}, &#92;{2,3&#92;}&#92;}' class='latex' />. A pictorial representation of this graph is depicted below</p>
<p><a href="http://stochastix.files.wordpress.com/2011/09/complete-graph-with-3-vertices.png"><img class="size-full wp-image-6903 aligncenter" title="Complete graph with 3 vertices" src="http://stochastix.files.wordpress.com/2011/09/complete-graph-with-3-vertices.png?w=450" alt=""   /></a>Note that this graph is <a href="http://en.wikipedia.org/wiki/Complete_graph" target="_blank">complete</a>. We can assign two distinct colors to any two vertices, but we cannot assign a color to the third vertex that will yield a <em>proper</em> coloring. Therefore, the graph is not 2-colorable. To illustrate, we use the color set <img src='http://s0.wp.com/latex.php?latex=C+%3A%3D+%5C%7B%5Ctext%7Bpink%7D%2C+%5Ctext%7Bblue%7D%5C%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='C := &#92;{&#92;text{pink}, &#92;text{blue}&#92;}' title='C := &#92;{&#92;text{pink}, &#92;text{blue}&#92;}' class='latex' /> and generate all <img src='http://s0.wp.com/latex.php?latex=2%5E3+%3D+8&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='2^3 = 8' title='2^3 = 8' class='latex' /> possible vertex 2-colorings, as depicted below</p>
<p><a href="http://stochastix.files.wordpress.com/2011/09/8-vertex-2-colorings.png"><img class="size-full wp-image-6906 aligncenter" title="8 vertex 2-colorings" src="http://stochastix.files.wordpress.com/2011/09/8-vertex-2-colorings.png?w=450" alt=""   /></a>Notice that none of these 2-colorings is <em>proper</em>, as expected.</p>
<p>As discussed by <a href="http://www.math.ucdavis.edu/~deloera/" target="_blank">De Loera</a> <em>et alia</em> [1], the <a href="http://en.wikipedia.org/wiki/Graph_coloring" target="_blank">graph vertex-coloring</a> problem can be modeled using polynomial equations. We introduce variables <img src='http://s0.wp.com/latex.php?latex=x_1%2C+x_2%2C+x_3&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x_1, x_2, x_3' title='x_1, x_2, x_3' class='latex' />, where <img src='http://s0.wp.com/latex.php?latex=x_i+%3D+-1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x_i = -1' title='x_i = -1' class='latex' /> if vertex <img src='http://s0.wp.com/latex.php?latex=i&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='i' title='i' class='latex' /> is assigned the color <img src='http://s0.wp.com/latex.php?latex=%5Ctext%7Bpink%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;text{pink}' title='&#92;text{pink}' class='latex' />, and <img src='http://s0.wp.com/latex.php?latex=x_i+%3D+1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x_i = 1' title='x_i = 1' class='latex' /> if vertex <img src='http://s0.wp.com/latex.php?latex=i&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='i' title='i' class='latex' /> is assigned the color <img src='http://s0.wp.com/latex.php?latex=%5Ctext%7Bblue%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;text{blue}' title='&#92;text{blue}' class='latex' />. Given an edge <img src='http://s0.wp.com/latex.php?latex=%5C%7Bi%2Cj%5C%7D+%5Cin+E&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;{i,j&#92;} &#92;in E' title='&#92;{i,j&#92;} &#92;in E' class='latex' />, we have that:</p>
<ul>
<li>if both vertices are pink, we get <img src='http://s0.wp.com/latex.php?latex=x_i+%2B+x_j+%3D+-2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x_i + x_j = -2' title='x_i + x_j = -2' class='latex' />.</li>
</ul>
<ul>
<li>if both vertices are blue, we get <img src='http://s0.wp.com/latex.php?latex=x_i+%2B+x_j+%3D+2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x_i + x_j = 2' title='x_i + x_j = 2' class='latex' />.</li>
</ul>
<ul>
<li>if the vertices have distinct colors, we get <img src='http://s0.wp.com/latex.php?latex=x_i+%2B+x_j+%3D+0&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x_i + x_j = 0' title='x_i + x_j = 0' class='latex' />.</li>
</ul>
<p>Thus, stating that adjacent vertices must be assigned distinct colors is equivalent to imposing the constraints <img src='http://s0.wp.com/latex.php?latex=x_i+%2B+x_j+%3D+0&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x_i + x_j = 0' title='x_i + x_j = 0' class='latex' /> for all <img src='http://s0.wp.com/latex.php?latex=%5C%7Bi%2Cj%5C%7D+%5Cin+E&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;{i,j&#92;} &#92;in E' title='&#92;{i,j&#92;} &#92;in E' class='latex' />. We thus obtain a system of three equations</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Cbegin%7Barray%7D%7Brl%7D+x_1+%2B+x_2+%26%3D+0%5C%5C+x_1+%2B+x_3+%26%3D+0%5C%5C+x_2+%2B+x_3+%26%3D+0%5Cend%7Barray%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;begin{array}{rl} x_1 + x_2 &amp;= 0&#92;&#92; x_1 + x_3 &amp;= 0&#92;&#92; x_2 + x_3 &amp;= 0&#92;end{array}' title='&#92;begin{array}{rl} x_1 + x_2 &amp;= 0&#92;&#92; x_1 + x_3 &amp;= 0&#92;&#92; x_2 + x_3 &amp;= 0&#92;end{array}' class='latex' /></p>
<p>where <img src='http://s0.wp.com/latex.php?latex=x_1%2C+x_2%2C+x_3+%5Cin+%5C%7B-1%2C1%5C%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x_1, x_2, x_3 &#92;in &#92;{-1,1&#92;}' title='x_1, x_2, x_3 &#92;in &#92;{-1,1&#92;}' class='latex' />. This is still a combinatorial problem, though. We can transform it into an algebraic-geometric problem. Here is a truly beautiful trick [1]: the constraint <img src='http://s0.wp.com/latex.php?latex=x_i+%5Cin+%5C%7B-1%2C1%5C%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x_i &#92;in &#92;{-1,1&#92;}' title='x_i &#92;in &#92;{-1,1&#92;}' class='latex' /> can be encoded as <img src='http://s0.wp.com/latex.php?latex=x_i%5E2+%3D+1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x_i^2 = 1' title='x_i^2 = 1' class='latex' /> with <img src='http://s0.wp.com/latex.php?latex=x_i+%5Cin+%5Cmathbb%7BR%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x_i &#92;in &#92;mathbb{R}' title='x_i &#92;in &#92;mathbb{R}' class='latex' />. How quaint!! We then obtain a system of six equations with polynomials in <img src='http://s0.wp.com/latex.php?latex=%5Cmathbb%7BR%7D%5Bx_1%2C+x_2%2C+x_3%5D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbb{R}[x_1, x_2, x_3]' title='&#92;mathbb{R}[x_1, x_2, x_3]' class='latex' /></p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Cbegin%7Barray%7D%7Brl%7D+x_1+%2B+x_2+%26%3D+0%5C%5C+x_1+%2B+x_3+%26%3D+0%5C%5C+x_2+%2B+x_3+%26%3D+0%5C%5C+x_1%5E2+-+1+%26%3D+0%5C%5C+x_2%5E2+-+1+%26%3D+0%5C%5C+x_3%5E2+-1+%26%3D+0%5Cend%7Barray%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;begin{array}{rl} x_1 + x_2 &amp;= 0&#92;&#92; x_1 + x_3 &amp;= 0&#92;&#92; x_2 + x_3 &amp;= 0&#92;&#92; x_1^2 - 1 &amp;= 0&#92;&#92; x_2^2 - 1 &amp;= 0&#92;&#92; x_3^2 -1 &amp;= 0&#92;end{array}' title='&#92;begin{array}{rl} x_1 + x_2 &amp;= 0&#92;&#92; x_1 + x_3 &amp;= 0&#92;&#92; x_2 + x_3 &amp;= 0&#92;&#92; x_1^2 - 1 &amp;= 0&#92;&#92; x_2^2 - 1 &amp;= 0&#92;&#92; x_3^2 -1 &amp;= 0&#92;end{array}' class='latex' /></p>
<p>whose solution set, which we denote by <img src='http://s0.wp.com/latex.php?latex=S&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='S' title='S' class='latex' />, is <a href="http://en.wikipedia.org/wiki/Algebraic_set" target="_blank">algebraic</a> [2]. The given graph is 2-<em>colorable</em> if and only if the system of polynomial equations above is <em>feasible</em>, i.e., if set <img src='http://s0.wp.com/latex.php?latex=S&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='S' title='S' class='latex' /> is <em>nonempty</em> [1]. The following <a href="http://redlog.dolzmann.de/" target="_blank">REDUCE + REDLOG</a> script uses <a href="http://en.wikipedia.org/wiki/Quantifier_elimination" target="_blank">quantifier elimination</a> to decide if <img src='http://s0.wp.com/latex.php?latex=S&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='S' title='S' class='latex' /> is nonempty:</p>
<pre style="padding-left:30px;background:#EEEEEE;">% feasibility via quantifier elimination

load_package redlog;
rlset ofsf;

% define polynomial functions
f1 := x1+x2;
f2 := x1+x3;
f3 := x2+x3;
f4 := x1^2-1;
f5 := x2^2-1;
f6 := x3^2-1;

% define existential formula
phi := ex({x1,x2,x3}, f1=0 and f2=0 and f3=0 and
                      f4=0 and f5=0 and f6=0);

% perform quantifier elimination
rlqe phi;

end;</pre>
<p>This script produces the truth value <em>false</em>. Thus, the solution set <img src='http://s0.wp.com/latex.php?latex=S&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='S' title='S' class='latex' /> is empty, which means that the system of polynomial equations is <em>infeasible</em>. We conclude that the given graph is not 2-colorable.</p>
<p style="text-align:center;">__________</p>
<p><strong>References</strong></p>
<p style="padding-left:30px;">[1] Jesús De Loera, Jon Lee, Susan Margulies, Shmuel Onn, <a href="http://arxiv.org/abs/0706.0578" target="_blank"><em>Expressing Combinatorial Optimization Problems by Systems of Polynomial Equations and the Nullstellensatz</em></a>, 2007.</p>
<p style="padding-left:30px;">[2] Jacek Bochnak, Michel Coste, Marie-Françoise Roy, <a href="http://books.google.com/books?id=m8Er5OAnT5cC" target="_blank"><em>Real Algebraic Geometry</em></a>, Springer, 1998.</p>
<br />Filed under: <a href='http://stochastix.wordpress.com/category/mathematics/geometry-mathematics/algebraic-geometry/'>Algebraic Geometry</a>, <a href='http://stochastix.wordpress.com/category/mathematics/combinatorics/'>Combinatorics</a> Tagged: <a href='http://stochastix.wordpress.com/tag/2-colorability/'>2-Colorability</a>, <a href='http://stochastix.wordpress.com/tag/algebraic-sets/'>Algebraic Sets</a>, <a href='http://stochastix.wordpress.com/tag/decision-problems/'>Decision Problems</a>, <a href='http://stochastix.wordpress.com/tag/feasibility/'>Feasibility</a>, <a href='http://stochastix.wordpress.com/tag/graph-coloring/'>Graph Coloring</a>, <a href='http://stochastix.wordpress.com/tag/graph-theory/'>Graph Theory</a>, <a href='http://stochastix.wordpress.com/tag/quantifier-elimination/'>Quantifier Elimination</a>, <a href='http://stochastix.wordpress.com/tag/real-algebraic-geometry/'>Real Algebraic Geometry</a>, <a href='http://stochastix.wordpress.com/tag/redlog/'>REDLOG</a>, <a href='http://stochastix.wordpress.com/tag/reduce/'>REDUCE</a>, <a href='http://stochastix.wordpress.com/tag/vertex-coloring/'>Vertex Coloring</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/stochastix.wordpress.com/6887/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/stochastix.wordpress.com/6887/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/stochastix.wordpress.com/6887/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/stochastix.wordpress.com/6887/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/stochastix.wordpress.com/6887/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/stochastix.wordpress.com/6887/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/stochastix.wordpress.com/6887/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/stochastix.wordpress.com/6887/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/stochastix.wordpress.com/6887/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/stochastix.wordpress.com/6887/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/stochastix.wordpress.com/6887/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/stochastix.wordpress.com/6887/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/stochastix.wordpress.com/6887/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/stochastix.wordpress.com/6887/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stochastix.wordpress.com&amp;blog=300626&amp;post=6887&amp;subd=stochastix&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://stochastix.wordpress.com/2011/09/01/deciding-2-colorability-of-graphs/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">stochastix</media:title>
		</media:content>

		<media:content url="http://stochastix.files.wordpress.com/2011/09/complete-graph-with-3-vertices.png" medium="image">
			<media:title type="html">Complete graph with 3 vertices</media:title>
		</media:content>

		<media:content url="http://stochastix.files.wordpress.com/2011/09/8-vertex-2-colorings.png" medium="image">
			<media:title type="html">8 vertex 2-colorings</media:title>
		</media:content>
	</item>
		<item>
		<title>Feasibility via quantifier elimination II</title>
		<link>http://stochastix.wordpress.com/2011/08/29/feasibility-via-quantifier-elimination-ii/</link>
		<comments>http://stochastix.wordpress.com/2011/08/29/feasibility-via-quantifier-elimination-ii/#comments</comments>
		<pubDate>Mon, 29 Aug 2011 08:47:46 +0000</pubDate>
		<dc:creator>Rod Carvalho</dc:creator>
				<category><![CDATA[Algebraic Geometry]]></category>
		<category><![CDATA[Decision Problems]]></category>
		<category><![CDATA[Feasibility]]></category>
		<category><![CDATA[Quantifier Elimination]]></category>
		<category><![CDATA[Real Algebraic Geometry]]></category>
		<category><![CDATA[REDLOG]]></category>
		<category><![CDATA[REDUCE]]></category>
		<category><![CDATA[Semialgebraic Sets]]></category>

		<guid isPermaLink="false">http://stochastix.wordpress.com/?p=6859</guid>
		<description><![CDATA[Suppose we want to decide whether the following semialgebraic set is nonempty. This is the same as determining whether the following system of polynomial inequalities is feasible. We now define , and . Set is depicted below and set is depicted below Note that . We depict this intersection below Visual inspection of the plot [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stochastix.wordpress.com&amp;blog=300626&amp;post=6859&amp;subd=stochastix&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Suppose we want to decide whether the following <a href="http://en.wikipedia.org/wiki/Semialgebraic_set" target="_blank">semialgebraic</a> set</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=S+%3A%3D+%5C%7B%28x%2Cy%29+%5Cin+%5Cmathbb%7BR%7D%5E2+%5Cmid+x+-+y%5E2+%2B+1+%5Cgeq+0+%5Cland+-x+-+y%5E2+%2B+1+%5Cgeq+0%5C%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='S := &#92;{(x,y) &#92;in &#92;mathbb{R}^2 &#92;mid x - y^2 + 1 &#92;geq 0 &#92;land -x - y^2 + 1 &#92;geq 0&#92;}' title='S := &#92;{(x,y) &#92;in &#92;mathbb{R}^2 &#92;mid x - y^2 + 1 &#92;geq 0 &#92;land -x - y^2 + 1 &#92;geq 0&#92;}' class='latex' /></p>
<p>is <em>nonempty</em>. This is the same as determining whether the following system of polynomial inequalities</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Cbegin%7Barray%7D%7Brrl%7D+g_1+%28x%2C+y%29+%3A%3D%26+x+-+y%5E2+%2B+1+%26%5Cgeq+0%5C%5C+g_2+%28x%2C+y%29+%3A%3D%26+-x+-+y%5E2+%2B1+%26%5Cgeq+0%5Cend%7Barray%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;begin{array}{rrl} g_1 (x, y) :=&amp; x - y^2 + 1 &amp;&#92;geq 0&#92;&#92; g_2 (x, y) :=&amp; -x - y^2 +1 &amp;&#92;geq 0&#92;end{array}' title='&#92;begin{array}{rrl} g_1 (x, y) :=&amp; x - y^2 + 1 &amp;&#92;geq 0&#92;&#92; g_2 (x, y) :=&amp; -x - y^2 +1 &amp;&#92;geq 0&#92;end{array}' class='latex' /></p>
<p>is <em>feasible</em>. We now define <img src='http://s0.wp.com/latex.php?latex=S_1+%3A%3D+%5C%7B%28x%2Cy%29+%5Cin+%5Cmathbb%7BR%7D%5E2+%5Cmid+g_1+%28x%2C+y%29+%5Cgeq+0%5C%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='S_1 := &#92;{(x,y) &#92;in &#92;mathbb{R}^2 &#92;mid g_1 (x, y) &#92;geq 0&#92;}' title='S_1 := &#92;{(x,y) &#92;in &#92;mathbb{R}^2 &#92;mid g_1 (x, y) &#92;geq 0&#92;}' class='latex' />, and <img src='http://s0.wp.com/latex.php?latex=S_2+%3A%3D+%5C%7B%28x%2Cy%29+%5Cin+%5Cmathbb%7BR%7D%5E2+%5Cmid+g_2+%28x%2C+y%29+%5Cgeq+0%5C%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='S_2 := &#92;{(x,y) &#92;in &#92;mathbb{R}^2 &#92;mid g_2 (x, y) &#92;geq 0&#92;}' title='S_2 := &#92;{(x,y) &#92;in &#92;mathbb{R}^2 &#92;mid g_2 (x, y) &#92;geq 0&#92;}' class='latex' />. Set <img src='http://s0.wp.com/latex.php?latex=S_1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='S_1' title='S_1' class='latex' /> is depicted below</p>
<p><a href="http://stochastix.files.wordpress.com/2011/08/wa-plot-x-y21-gt-0.gif"><img class="size-full wp-image-6868 aligncenter" title="WA plot x-y^2+1 gt 0" src="http://stochastix.files.wordpress.com/2011/08/wa-plot-x-y21-gt-0.gif?w=450" alt=""   /></a>and set <img src='http://s0.wp.com/latex.php?latex=S_2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='S_2' title='S_2' class='latex' /> is depicted below</p>
<p><a href="http://stochastix.files.wordpress.com/2011/08/wa-plot-x-y21-gt-01.gif"><img class="size-full wp-image-6869 aligncenter" title="WA plot -x-y^2+1 gt 0" src="http://stochastix.files.wordpress.com/2011/08/wa-plot-x-y21-gt-01.gif?w=450" alt=""   /></a>Note that <img src='http://s0.wp.com/latex.php?latex=S+%3D+S_1+%5Ccap+S_2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='S = S_1 &#92;cap S_2' title='S = S_1 &#92;cap S_2' class='latex' />. We depict this intersection below</p>
<p><a href="http://stochastix.files.wordpress.com/2011/08/wa-plot-x-y21-gt-0-and-x-y21-gt-0.gif"><img class="size-full wp-image-6870 aligncenter" title="WA plot x-y^2+1 gt 0 and -x-y^2+1 gt 0" src="http://stochastix.files.wordpress.com/2011/08/wa-plot-x-y21-gt-0-and-x-y21-gt-0.gif?w=450" alt=""   /></a>Visual inspection of the plot of the intersection <img src='http://s0.wp.com/latex.php?latex=S+%3D+S_1+%5Ccap+S_2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='S = S_1 &#92;cap S_2' title='S = S_1 &#92;cap S_2' class='latex' /> allows us to conclude that <img src='http://s0.wp.com/latex.php?latex=S&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='S' title='S' class='latex' /> in <em>nonempty</em>. Asserting that <img src='http://s0.wp.com/latex.php?latex=S&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='S' title='S' class='latex' /> is nonempty is the same as stating that the proposition</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Cexists%7Bx%7D%5Cexists%7By%7D+%5Cleft%28g_1+%28x%2Cy%29+%5Cgeq+0+%5Cland+g_2+%28x%2Cy%29+%5Cgeq+0%5Cright%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;exists{x}&#92;exists{y} &#92;left(g_1 (x,y) &#92;geq 0 &#92;land g_2 (x,y) &#92;geq 0&#92;right)' title='&#92;exists{x}&#92;exists{y} &#92;left(g_1 (x,y) &#92;geq 0 &#92;land g_2 (x,y) &#92;geq 0&#92;right)' class='latex' /></p>
<p>is <em>true</em>. We can determine the truth value of this proposition via <a href="http://en.wikipedia.org/wiki/Quantifier_elimination" target="_blank">quantifier elimination</a> using the following <a href="http://redlog.dolzmann.de/" target="_blank">REDUCE + REDLOG</a> script:</p>
<pre style="padding-left:30px;background:#EEEEEE;">% feasibility via quantifier elimination

load_package redlog;
rlset ofsf;

% define polynomial functions
g1 :=  x-y^2+1;
g2 := -x-y^2+1;

% define existential formula
phi := ex({x,y}, g1&gt;=0 and g2&gt;=0);

% perform quantifier elimination
rlqe phi;

end;</pre>
<p>which produces the truth value<em> true</em>. This means that there exists a <img src='http://s0.wp.com/latex.php?latex=%28x%2Cy%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='(x,y)' title='(x,y)' class='latex' /> that satisfies the polynomial inequalities, i.e., <img src='http://s0.wp.com/latex.php?latex=S&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='S' title='S' class='latex' /> is nonempty.</p>
<p style="text-align:center;">__________</p>
<p><strong>Remark:</strong> the plots in this post were generated by <a href="http://www.wolframalpha.com" target="_blank">Wolfram Alpha</a>.</p>
<br />Filed under: <a href='http://stochastix.wordpress.com/category/mathematics/geometry-mathematics/algebraic-geometry/'>Algebraic Geometry</a> Tagged: <a href='http://stochastix.wordpress.com/tag/decision-problems/'>Decision Problems</a>, <a href='http://stochastix.wordpress.com/tag/feasibility/'>Feasibility</a>, <a href='http://stochastix.wordpress.com/tag/quantifier-elimination/'>Quantifier Elimination</a>, <a href='http://stochastix.wordpress.com/tag/real-algebraic-geometry/'>Real Algebraic Geometry</a>, <a href='http://stochastix.wordpress.com/tag/redlog/'>REDLOG</a>, <a href='http://stochastix.wordpress.com/tag/reduce/'>REDUCE</a>, <a href='http://stochastix.wordpress.com/tag/semialgebraic-sets/'>Semialgebraic Sets</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/stochastix.wordpress.com/6859/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/stochastix.wordpress.com/6859/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/stochastix.wordpress.com/6859/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/stochastix.wordpress.com/6859/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/stochastix.wordpress.com/6859/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/stochastix.wordpress.com/6859/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/stochastix.wordpress.com/6859/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/stochastix.wordpress.com/6859/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/stochastix.wordpress.com/6859/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/stochastix.wordpress.com/6859/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/stochastix.wordpress.com/6859/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/stochastix.wordpress.com/6859/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/stochastix.wordpress.com/6859/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/stochastix.wordpress.com/6859/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stochastix.wordpress.com&amp;blog=300626&amp;post=6859&amp;subd=stochastix&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://stochastix.wordpress.com/2011/08/29/feasibility-via-quantifier-elimination-ii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">stochastix</media:title>
		</media:content>

		<media:content url="http://stochastix.files.wordpress.com/2011/08/wa-plot-x-y21-gt-0.gif" medium="image">
			<media:title type="html">WA plot x-y^2+1 gt 0</media:title>
		</media:content>

		<media:content url="http://stochastix.files.wordpress.com/2011/08/wa-plot-x-y21-gt-01.gif" medium="image">
			<media:title type="html">WA plot -x-y^2+1 gt 0</media:title>
		</media:content>

		<media:content url="http://stochastix.files.wordpress.com/2011/08/wa-plot-x-y21-gt-0-and-x-y21-gt-0.gif" medium="image">
			<media:title type="html">WA plot x-y^2+1 gt 0 and -x-y^2+1 gt 0</media:title>
		</media:content>
	</item>
	</channel>
</rss>
