<?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>Numerical Ruby</title>
	<atom:link href="http://numericalruby.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://numericalruby.wordpress.com</link>
	<description>why should I be stuck with FORTRAN?</description>
	<lastBuildDate>Wed, 28 Nov 2007 05:43:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='numericalruby.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Numerical Ruby</title>
		<link>http://numericalruby.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://numericalruby.wordpress.com/osd.xml" title="Numerical Ruby" />
	<atom:link rel='hub' href='http://numericalruby.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Quick numerical benchmark of Ruby 1.9</title>
		<link>http://numericalruby.wordpress.com/2007/11/28/quick-numerical-benchmark-of-ruby-19/</link>
		<comments>http://numericalruby.wordpress.com/2007/11/28/quick-numerical-benchmark-of-ruby-19/#comments</comments>
		<pubDate>Wed, 28 Nov 2007 05:40:56 +0000</pubDate>
		<dc:creator>Brian Broom</dc:creator>
				<category><![CDATA[benchmarks]]></category>
		<category><![CDATA[numerical]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://numericalruby.com/2007/11/28/quick-numerical-benchmark-of-ruby-19/</guid>
		<description><![CDATA[I was looking for something to run to try to put the current Ruby 1.9 through its paces. The first code I found was this total = 0.0 1.0.step(2000.0,0.0001) do &#124;x&#124; result = (5.4*x**5 - 3.211*x**4 + 100.3*x**2 - 100 + 20*Math.sin(x) - Math.log(x)) * 20*Math.exp(-x/100.3) total += result / 0.0001 end puts total Its [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=numericalruby.wordpress.com&amp;blog=1091501&amp;post=23&amp;subd=numericalruby&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<a class="DiggThisButton DiggMedium" href="http://digg.com/submit?url=http%3A%2F%2Fnumericalruby.wordpress.com%2F2007%2F11%2F28%2Fquick-numerical-benchmark-of-ruby-19%2F&amp;title=Quick+numerical+benchmark+of+Ruby%26nbsp%3B1.9"></a>
<p>I was looking for something to run to try to put the <a href="http://www.rubyinside.com/how-to-start-playing-with-ruby-19-right-now-661.html" title="How To Start Playing With Ruby 1.9 Right Now!">current Ruby 1.9</a> through its paces.  The first code I found was this</p>
<pre>total = 0.0

1.0.step(2000.0,0.0001) do |x|

  result = (5.4*x**5 - 3.211*x**4 + 100.3*x**2 - 100 +
    20*Math.sin(x) - Math.log(x)) * 20*Math.exp(-x/100.3)
  total += result / 0.0001

end

puts total</pre>
<p>Its heavy on the floating point that&#8217;s what I mostly use; &#8216;normal&#8217; use is more likely integer math. Its a nonsensical calculation that I should rework as a real integration problem&#8230;</p>
<p>Timings (intel iMac)</p>
<pre>imac:$ time ruby pure_ruby.rb
1.31784023450574e+24

real    1m43.283s
user    1m38.615s
sys     0m1.213s
imac:$ time /usr/local/ruby1.9/bin/ruby pure_ruby.rb
1.31784023450574e+24

real    1m1.352s
user    0m58.550s
sys     0m0.749s</pre>
<p>Which is 1.7x faster.  Not bad for no additional work. These numbers are very preliminary, and shouldn&#8217;t be mistaken for real benchmarks.  I have a series of tests I did with pair correlation function calculations on a simulated liquid that I want to rerun and see how 1.9 holds up.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/numericalruby.wordpress.com/23/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/numericalruby.wordpress.com/23/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/numericalruby.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/numericalruby.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/numericalruby.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/numericalruby.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/numericalruby.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/numericalruby.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/numericalruby.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/numericalruby.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/numericalruby.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/numericalruby.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/numericalruby.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/numericalruby.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/numericalruby.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/numericalruby.wordpress.com/23/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=numericalruby.wordpress.com&amp;blog=1091501&amp;post=23&amp;subd=numericalruby&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://numericalruby.wordpress.com/2007/11/28/quick-numerical-benchmark-of-ruby-19/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/72ddd6509156a7dd3c301b87a46c5219?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bcbroom</media:title>
		</media:content>
	</item>
		<item>
		<title>Performance in Ruby 1.9</title>
		<link>http://numericalruby.wordpress.com/2007/11/27/performance-in-ruby-19/</link>
		<comments>http://numericalruby.wordpress.com/2007/11/27/performance-in-ruby-19/#comments</comments>
		<pubDate>Wed, 28 Nov 2007 01:44:34 +0000</pubDate>
		<dc:creator>Brian Broom</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://numericalruby.com/2007/11/27/performance-in-ruby-19/</guid>
		<description><![CDATA[I have seen some rumblings about Ruby 1.9 being almost ready (here&#8217;s hoping that it turns out better than this years OS upgrades) and faster than before. I haven&#8217;t looked at 1.9 since I ran some of my early benchmarks. Resources How To Start Playing With Ruby 1.9 Right Now! More Real World Performance Data [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=numericalruby.wordpress.com&amp;blog=1091501&amp;post=21&amp;subd=numericalruby&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://numericalruby.files.wordpress.com/2007/11/ruby.jpg" title="Ruby logo"><img src="http://numericalruby.files.wordpress.com/2007/11/ruby.thumbnail.jpg?w=80&#038;h=80" alt="Ruby logo" align="left" height="80" width="80" /></a><a class="DiggThisButton DiggMedium" href="http://digg.com/submit?url=http%3A%2F%2Fnumericalruby.wordpress.com%2F2007%2F11%2F27%2Fperformance-in-ruby-19%2F&amp;title=Performance+in+Ruby%26nbsp%3B1.9"></a>I have seen some rumblings about Ruby 1.9 being almost ready (here&#8217;s hoping that it turns out better than this years OS upgrades) and faster than before.  I haven&#8217;t looked at 1.9 since I ran some of my early benchmarks.</p>
<p><strong>Resources</strong><br />
<a href="http://www.rubyinside.com/how-to-start-playing-with-ruby-19-right-now-661.html">How To Start Playing With Ruby 1.9 Right Now!</a></p>
<p><a href="http://on-ruby.blogspot.com/2007/04/more-real-world-performance-data.html"> More Real World Performance Data</a></p>
<p><a href="http://eigenclass.org/hiki.rb?Changes+in+Ruby+1.9">Changes in Ruby 1.9</a></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/numericalruby.wordpress.com/21/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/numericalruby.wordpress.com/21/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/numericalruby.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/numericalruby.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/numericalruby.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/numericalruby.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/numericalruby.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/numericalruby.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/numericalruby.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/numericalruby.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/numericalruby.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/numericalruby.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/numericalruby.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/numericalruby.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/numericalruby.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/numericalruby.wordpress.com/21/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=numericalruby.wordpress.com&amp;blog=1091501&amp;post=21&amp;subd=numericalruby&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://numericalruby.wordpress.com/2007/11/27/performance-in-ruby-19/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/72ddd6509156a7dd3c301b87a46c5219?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bcbroom</media:title>
		</media:content>

		<media:content url="http://numericalruby.files.wordpress.com/2007/11/ruby.thumbnail.jpg" medium="image">
			<media:title type="html">Ruby logo</media:title>
		</media:content>
	</item>
		<item>
		<title>Shortcomings in Scientific Computing</title>
		<link>http://numericalruby.wordpress.com/2007/07/16/shortcomings-in-scientific-computing/</link>
		<comments>http://numericalruby.wordpress.com/2007/07/16/shortcomings-in-scientific-computing/#comments</comments>
		<pubDate>Mon, 16 Jul 2007 17:04:56 +0000</pubDate>
		<dc:creator>Brian Broom</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[scientific]]></category>

		<guid isPermaLink="false">http://numericalruby.com/2007/07/16/shortcomings-in-scientific-computing/</guid>
		<description><![CDATA[I read an interesting article (via The Third Bit) a couple of weeks ago that addresses some of the shortcomings of &#8216;Scientific Computing&#8217;. The major shortcomings mentioned are Simple text editors vs IDE Lack of version control Not enough testing The major item (to me) is version control. If you aren&#8217;t using some type of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=numericalruby.wordpress.com&amp;blog=1091501&amp;post=20&amp;subd=numericalruby&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<a class="DiggThisButton DiggMedium" href="http://digg.com/submit?url=http%3A%2F%2Fnumericalruby.wordpress.com%2F2007%2F07%2F16%2Fshortcomings-in-scientific-computing%2F&amp;title=Shortcomings+in+Scientific%26nbsp%3BComputing"></a>
<p>I read an <a href="http://www.americanscientist.org/template/AssetDetail/assetid/48548/page/1" title="Where's the Real Bottleneck in Scientific Computing?">interesting article</a> (via <a href="http://pyre.third-bit.com/blog/" title="The Third Bit">The Third Bit</a>) a couple of weeks ago that addresses some of the shortcomings of &#8216;Scientific Computing&#8217;.  The major shortcomings mentioned are </p>
<ul>
<li>Simple text editors vs IDE</li>
<li>Lack of version control</li>
<li>Not enough testing</li>
</ul>
<p>The major item (to me) is version control. If you aren&#8217;t using some type of version control, start now.  <a href="http://www.pragmaticprogrammer.com/titles/svn/" title="Pragmatic Version Control using Subversion 2nd Ed.">Subversion</a> is an easy enough system to get working (although the repository setup is a little harder than it needs to be.)  If you think of this as just a backup system, you miss the real power of version control. In the project I am working on, I can instantly revert back to the code as it was on any date. I can tag or label a certain version for a paper, or other event.  I trace a certain section of code and figure out how it has changed over the versions.  This came in handy recently as I was trying to figure out a certain constant in our code. I was able to look back and find out that the code I inherited was setup that way.  It would have taken much longer to sift through tarballs looking for this value.</p>
<p>I&#8217;m not convinced that the text editor vs IDE is that significant of an issue, but it depends on the language and environment that you are using. Some languages, like Java and C variants, have a lot of boilerplate text for functions and class definitions.  For these languages, having something that can autogenerate this text is a real timesaver. For languages that are more compact, this is less of an issue. The same issue is there for some frameworks, like Microsoft&#8217;s .NET (which I used in some web development contract work I did a few years ago.) The class and function names in .net are generally long to very long, but the visual studio IDE is really good about popping up a list of things that could be called at whatever point you are (since the MS languages are static typed).  There is some value to this, but I&#8217;m not convinced that the efficiency differences are enough to totally change how you work.  My advice would be to use the editor you are most comfortable with, but try other things from time to time and keep an open mind.</p>
<p>One thing that an IDE does provide that is helpful is a good debugging facility. If you have to spend lots of time inside a debugger figuring things out, then you will benefit from doing more testing, but there are times that having a debugger is much more helpful than adding a whole bunch of print statements.</p>
<p>Finally, the article talks about testing.  Testing and verification are vitally important to scientific computing.  There have been cases where published results have turned out to be incorrect due to bugs in the program.  A particular case came out this year where incorrect results were published in a very high profile journal, which lead to some groups not getting grant funding or published results, because their results didn&#8217;t agree.  The problem with correcting this is that it isn&#8217;t a simple change.  Software testing is a real commitment and a pretty fundamental shift in how you develop your code.  I believe that the gains in productivity and knowing what your code is doing offsets the additional time to do the testing, but even in commercial software development, where testing is much more common, it is still not done by everyone.  Even if you don&#8217;t adopt a full methodology, such as TDD, XP, or something like that, you really owe it to yourself to do as much verification as you possibly can.  Test simple cases by hand. Verify things using simple models.  Make sure your results make sense.  I think it would be valuable to pick up a good Test driven development book, or look around for a unit testing program for your particular language and give it a try.</p>
<p>The real bottom line is that there is a lot of development going on outside of scientific computing. Keeping an open mind and trying new things is the best assurance we have of having better quality code. One resource is the <a href="http://swc.scipy.org/" title="Software Carpentry">Software Carpentry</a> page at <a href="http://scipy.org" title="Scientific Tools for Python">scipy.org</a>.  This page is python related, but most of the concepts should transfer to other languages fairly well.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/numericalruby.wordpress.com/20/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/numericalruby.wordpress.com/20/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/numericalruby.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/numericalruby.wordpress.com/20/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/numericalruby.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/numericalruby.wordpress.com/20/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/numericalruby.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/numericalruby.wordpress.com/20/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/numericalruby.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/numericalruby.wordpress.com/20/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/numericalruby.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/numericalruby.wordpress.com/20/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/numericalruby.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/numericalruby.wordpress.com/20/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/numericalruby.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/numericalruby.wordpress.com/20/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=numericalruby.wordpress.com&amp;blog=1091501&amp;post=20&amp;subd=numericalruby&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://numericalruby.wordpress.com/2007/07/16/shortcomings-in-scientific-computing/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/72ddd6509156a7dd3c301b87a46c5219?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bcbroom</media:title>
		</media:content>
	</item>
		<item>
		<title>Text overlay using RMagick</title>
		<link>http://numericalruby.wordpress.com/2007/07/13/text-overlay-using-rmagick/</link>
		<comments>http://numericalruby.wordpress.com/2007/07/13/text-overlay-using-rmagick/#comments</comments>
		<pubDate>Fri, 13 Jul 2007 19:49:06 +0000</pubDate>
		<dc:creator>Brian Broom</dc:creator>
				<category><![CDATA[RMagick]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://numericalruby.com/2007/07/13/text-overlay-using-rmagick/</guid>
		<description><![CDATA[For what I&#8217;m doing (simple text overlay on an image) this is the setup I&#8217;m using. This is based on an article I saw using something to autogenerate images for rails. require 'RMagick' image = Magick::Image.read("input").first.minify drawable = Magick::Draw.new drawable.pointsize = 18.0 drawable.gravity = Magick::SouthEastGravity drawable.font_weight = Magick::BoldWeight tm = drawable.get_type_metrics(image, "numericalruby.com") drawable.fill = 'red' [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=numericalruby.wordpress.com&amp;blog=1091501&amp;post=18&amp;subd=numericalruby&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<a class="DiggThisButton DiggMedium" href="http://digg.com/submit?url=http%3A%2F%2Fnumericalruby.wordpress.com%2F2007%2F07%2F13%2Ftext-overlay-using-rmagick%2F&amp;title=Text+overlay+using%26nbsp%3BRMagick"></a>
<div style="float:right;margin-left:10px;margin-bottom:10px;">
<a href='http://numericalruby.files.wordpress.com/2007/07/test5.png' title='Text overlay example'><img src='http://numericalruby.files.wordpress.com/2007/07/test5.png?w=450' alt='Text overlay example' /></a></div>
<p>For what I&#8217;m doing (simple text overlay on an image) this is the setup I&#8217;m using. This is based on an <a href="http://nubyonrails.com/articles/dynamic-graphics-with-rails-1-2" title="Dynamic Graphics with Rails">article</a> I saw using something to autogenerate images for rails.</p>
<pre style="clear:right;"><code>require 'RMagick'

image = Magick::Image.read("input").first.minify

drawable = Magick::Draw.new

drawable.pointsize = 18.0
drawable.gravity = Magick::SouthEastGravity
drawable.font_weight = Magick::BoldWeight

tm = drawable.get_type_metrics(image, "numericalruby.com")

drawable.fill = 'red'
drawable.opacity(0.25)
drawable.roundrectangle(image.columns-tm.width-30,image.rows-tm.height-30,image.columns-10,image.rows-10,7,7)
drawable.draw(image)

drawable.annotate(image,0,0,20,20,"numericalruby.com") {self.fill='black'}

image.write("output")
</code></pre>
<p>Some good info also available on the <a href="http://www.imagemagick.org/RMagick/doc/index.html" title="RMagick 1.15.0 User's Guide and Reference">RMagick site</a></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/numericalruby.wordpress.com/18/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/numericalruby.wordpress.com/18/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/numericalruby.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/numericalruby.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/numericalruby.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/numericalruby.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/numericalruby.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/numericalruby.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/numericalruby.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/numericalruby.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/numericalruby.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/numericalruby.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/numericalruby.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/numericalruby.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/numericalruby.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/numericalruby.wordpress.com/18/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=numericalruby.wordpress.com&amp;blog=1091501&amp;post=18&amp;subd=numericalruby&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://numericalruby.wordpress.com/2007/07/13/text-overlay-using-rmagick/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/72ddd6509156a7dd3c301b87a46c5219?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bcbroom</media:title>
		</media:content>

		<media:content url="http://numericalruby.files.wordpress.com/2007/07/test5.png" medium="image">
			<media:title type="html">Text overlay example</media:title>
		</media:content>
	</item>
		<item>
		<title>Ruby used for computation book</title>
		<link>http://numericalruby.wordpress.com/2007/06/23/ruby-used-for-computation-book/</link>
		<comments>http://numericalruby.wordpress.com/2007/06/23/ruby-used-for-computation-book/#comments</comments>
		<pubDate>Sun, 24 Jun 2007 03:39:14 +0000</pubDate>
		<dc:creator>Brian Broom</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[scientific]]></category>

		<guid isPermaLink="false">http://numericalruby.com/2007/06/23/ruby-used-for-computation-book/</guid>
		<description><![CDATA[I ran across a neat book a little while ago that was designed as an introduction to numerical calculation in the context of gravitational science, starting with a 2 body system and going through many body. It has a good introduction to things like different order integrators, energy conservation, and stability. They decided to use [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=numericalruby.wordpress.com&amp;blog=1091501&amp;post=15&amp;subd=numericalruby&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<a class="DiggThisButton DiggMedium" href="http://digg.com/submit?url=http%3A%2F%2Fnumericalruby.wordpress.com%2F2007%2F06%2F23%2Fruby-used-for-computation-book%2F&amp;title=Ruby+used+for+computation%26nbsp%3Bbook"></a>
<p>I ran across a <a href="http://www.artcompsci.org/kali/development.html" title="Maya project">neat book</a> a little while ago that was designed as an introduction to numerical calculation in the context of gravitational science, starting with a 2 body system and going through many body. It has a good introduction to things like different order integrators, energy conservation, and stability.  They decided to use Ruby for the book as a simpler introduction to programming than traditional languages.  I think the intention is to include this in a classroom setting, so it will be interesting to see how that works out.  </p>
<p>From my personal experience with this type of class (numerical programming, not gravitation) I really think it will help, because so much overhead of the class is dedicated to how to get the compiler to work and all of the wrangling with things that aren&#8217;t really important.  There is a point where you need to introduce some C code for performance reasons (which is also discussed in one of the chapters), but the nice thing about Ruby is that you can do so much of the prototyping and &#8220;learning&#8221; in Ruby, then replace a very small amount of code and get tremendous performance gains.  It would be very easy for the C part to be provided to a class once students have proven that they can implement the algorithm in Ruby.  </p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/numericalruby.wordpress.com/15/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/numericalruby.wordpress.com/15/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/numericalruby.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/numericalruby.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/numericalruby.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/numericalruby.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/numericalruby.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/numericalruby.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/numericalruby.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/numericalruby.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/numericalruby.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/numericalruby.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/numericalruby.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/numericalruby.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/numericalruby.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/numericalruby.wordpress.com/15/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=numericalruby.wordpress.com&amp;blog=1091501&amp;post=15&amp;subd=numericalruby&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://numericalruby.wordpress.com/2007/06/23/ruby-used-for-computation-book/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/72ddd6509156a7dd3c301b87a46c5219?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bcbroom</media:title>
		</media:content>
	</item>
		<item>
		<title>Reading a file with Ruby</title>
		<link>http://numericalruby.wordpress.com/2007/06/22/reading-a-file-with-ruby/</link>
		<comments>http://numericalruby.wordpress.com/2007/06/22/reading-a-file-with-ruby/#comments</comments>
		<pubDate>Fri, 22 Jun 2007 12:03:24 +0000</pubDate>
		<dc:creator>Brian Broom</dc:creator>
				<category><![CDATA[I/O]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://numericalruby.com/2007/06/22/reading-a-file-with-ruby/</guid>
		<description><![CDATA[I intended to write this just after the file writing post, but got sidetracked with other ideas. The basic setup is like writing to a file. f = File.new(filname) There are two easy methods, and then one with more control. The easy way If all you want is the text of a small file, then [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=numericalruby.wordpress.com&amp;blog=1091501&amp;post=14&amp;subd=numericalruby&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<a class="DiggThisButton DiggMedium" href="http://digg.com/submit?url=http%3A%2F%2Fnumericalruby.wordpress.com%2F2007%2F06%2F22%2Freading-a-file-with-ruby%2F&amp;title=Reading+a+file+with%26nbsp%3BRuby"></a>
<p>I intended to write this just after the file writing post, but got sidetracked with other ideas. The basic setup is like writing to a file.</p>
<pre><code>f = File.new(filname)
</code></pre>
<p>There are two easy methods, and then one with more control.</p>
<h2>The easy way</h2>
<p>If all you want is the text of a small file, then simply use</p>
<pre><code>text = f.read
</code></pre>
<p>which dumps the entire file into a string, with the \n characters embedded inside. This seems to be most useful for files that you are going to do something to, and then write back out.  This is what I used for my template system.  I didn&#8217;t want to have to worry about tags that happened to be split across lines.</p>
<pre><code>text = f.readlines
</code></pre>
<p>is interesting in that it returns an array, where the elements of the array are the lines in the file.  This is easiest to use when each line of the file is an individual &#8216;element&#8217; (whatever that means for your situation.)</p>
<p>Also, you can read a file multiple times by calling #rewind, which resets you at the beginning of the file.  The downside to both of these methods is that they read the entire file into memory.  If the file is too large, you could exhaust the available memory, which would certainly cause bad things.</p>
<h2>The hard way</h2>
<p>The method with the most control is #gets.  This returns the next line of the file, and also updates the #lineno attribute to hold the current line number. If you need (say) the fourth line of the file, you can do so with</p>
<pre><code>3.times {f.gets}
text = f.gets
</code></pre>
<p>I have used this with some of the larger data files that our simulation programs produce.  Since some of these data files can be very large, I wasn&#8217;t sure I wanted to read the whole thing into memory at once.  The way I got around this was to read the file, and build a table of contents array with a new entry when I found a new timestep (which had distinctive text).  Then I could rewind the file and read in only the timestep I was interested in.  To get another timestep you simply repeat the process.</p>
<h2>What about the \n?</h2>
<p>There is a built in method to deal with the extra \n&#8217;s you will get with readlines or gets.  The #chomp method will remove any number of newline characters (and is smart enough to deal with newline, carriage return, or both with one call.) Unless you need the newlines for some reason, all of you calls will likely be</p>
<pre><code>text = f.gets.chomp
</code></pre>
<p>or</p>
<pre><code>text = f.readlines.map {|line| line.chomp}
</code></pre>
<p>and thats all there is to it.  This is fairly simple compared to some other languages I&#8217;ve used, and is one of the reasons I really like using Ruby.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/numericalruby.wordpress.com/14/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/numericalruby.wordpress.com/14/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/numericalruby.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/numericalruby.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/numericalruby.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/numericalruby.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/numericalruby.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/numericalruby.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/numericalruby.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/numericalruby.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/numericalruby.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/numericalruby.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/numericalruby.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/numericalruby.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/numericalruby.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/numericalruby.wordpress.com/14/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=numericalruby.wordpress.com&amp;blog=1091501&amp;post=14&amp;subd=numericalruby&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://numericalruby.wordpress.com/2007/06/22/reading-a-file-with-ruby/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/72ddd6509156a7dd3c301b87a46c5219?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bcbroom</media:title>
		</media:content>
	</item>
		<item>
		<title>Easy Installation of Ruby Classes</title>
		<link>http://numericalruby.wordpress.com/2007/06/21/easy-installation-of-ruby-classes/</link>
		<comments>http://numericalruby.wordpress.com/2007/06/21/easy-installation-of-ruby-classes/#comments</comments>
		<pubDate>Thu, 21 Jun 2007 19:47:52 +0000</pubDate>
		<dc:creator>Brian Broom</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://numericalruby.com/2007/06/21/easy-installation-of-ruby-classes/</guid>
		<description><![CDATA[Just a quick tidbit, but I&#8217;ve got a copule of classes that I consistently require, and it gets a bit redundant to include particular directories. Its not something that I&#8217;m ready to make a gem out of, but I looked into the standard Ruby inlude path. I beleive the paths are fairly standard, but you [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=numericalruby.wordpress.com&amp;blog=1091501&amp;post=13&amp;subd=numericalruby&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<a class="DiggThisButton DiggMedium" href="http://digg.com/submit?url=http%3A%2F%2Fnumericalruby.wordpress.com%2F2007%2F06%2F21%2Feasy-installation-of-ruby-classes%2F&amp;title=Easy+Installation+of+Ruby%26nbsp%3BClasses"></a>
<p>Just a quick tidbit, but I&#8217;ve got a copule of classes that I consistently require, and it gets a bit redundant to include particular directories.  Its not something that I&#8217;m ready to make a gem out of, but I looked into the standard Ruby inlude path.  I beleive the paths are fairly standard, but you can always check this using irb</p>
<pre><code>irb(main):001:0&gt; $:
</code></pre>
<p>will display the include path.</p>
<p>If you copy .rb files to any of these directories (probably requiring sudo) you can require them from anywhere.  I know there are other ways to do this, but this is a very quick way to do it.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/numericalruby.wordpress.com/13/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/numericalruby.wordpress.com/13/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/numericalruby.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/numericalruby.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/numericalruby.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/numericalruby.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/numericalruby.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/numericalruby.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/numericalruby.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/numericalruby.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/numericalruby.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/numericalruby.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/numericalruby.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/numericalruby.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/numericalruby.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/numericalruby.wordpress.com/13/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=numericalruby.wordpress.com&amp;blog=1091501&amp;post=13&amp;subd=numericalruby&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://numericalruby.wordpress.com/2007/06/21/easy-installation-of-ruby-classes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/72ddd6509156a7dd3c301b87a46c5219?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bcbroom</media:title>
		</media:content>
	</item>
		<item>
		<title>YAML for serialization</title>
		<link>http://numericalruby.wordpress.com/2007/06/20/yaml-for-serialization/</link>
		<comments>http://numericalruby.wordpress.com/2007/06/20/yaml-for-serialization/#comments</comments>
		<pubDate>Wed, 20 Jun 2007 20:44:37 +0000</pubDate>
		<dc:creator>Brian Broom</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[YAML]]></category>

		<guid isPermaLink="false">http://numericalruby.com/2007/06/20/yaml-for-serialization/</guid>
		<description><![CDATA[One of the projects I&#8217;m working on for research has to deal with atomic potentials, or a way to try to describe the forces between atoms when they are in a certain configuration. The more realistic ones are fairly complicated functions. One of the tricks in dealing with potentials is to try to plot some [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=numericalruby.wordpress.com&amp;blog=1091501&amp;post=12&amp;subd=numericalruby&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<a class="DiggThisButton DiggMedium" href="http://digg.com/submit?url=http%3A%2F%2Fnumericalruby.wordpress.com%2F2007%2F06%2F20%2Fyaml-for-serialization%2F&amp;title=YAML+for%26nbsp%3Bserialization"></a>
<p>One of the projects I&#8217;m working on for research has to deal with atomic potentials, or a way to try to describe the forces between atoms when they are in a certain configuration.  The more realistic ones are fairly complicated functions.</p>
<p>One of the tricks in dealing with potentials is to try to plot some specific configurations, and see how the energy varies.  &#8216;Wells&#8217; represent stable points where atoms will tend to sit, while &#8216;humps&#8217; are barriers between stable points that an atom must overcome somehow.  The height of the hump gives you an idea of how stable the low points are.</p>
<p>Plotting one of these potentials is not super difficult, just looping over some positions and (in my case) outputting the positions and energies to a file, which is then plotted with gnuplot.  One of the things I wanted to do, however, is to plot a bunch of variations with one of the parameters swept over a range of values.  One way would be to create the potential as an object and modify the parameters that way.  What I <em>wanted</em> to do, is have parameter file that I read in to setup the potential.</p>
<h2>Enter YAML</h2>
<p>It turns out that this is essentially built into ruby, through the YAML methods that are designed for marshaling objects over various communication methods between computers/processes. All you have to do is</p>
<pre><code>require 'yaml' # you may not need this line
thing.to_yaml
</code></pre>
<p>and you get a string representation of the object.  You can then save it to a file.  After this, restoring the object is as simple as</p>
<pre><code>YAML.load(string)
</code></pre>
<p>or</p>
<pre><code>YAML.load_file(filename)
</code></pre>
<h2>Even complicated things</h2>
<p>One of the neatest things about this is that nested objects translate seamlessly with this.  For example, I have to following files that I work with.</p>
<pre><code>#atom.rb
class Atom
  attr_accessor <img src='http://s2.wp.com/wp-includes/images/smilies/icon_mad.gif' alt=':x' class='wp-smiley' /> , :y, :z

  def initialize
    @x = @y = @z = 0.0
  end

  def r_ij(atom_two)
    r_squared = (@x-atom_two.x)**2 + (@y-atom_two.y)**2 + (@z-atom_two.z)**2
    return Math.sqrt(r_squared)
  end
end
</code></pre>
<p>and</p>
<pre><code>#molecule.rb
class Molecule
  attr_accessor :atoms
  def initialize
    @atoms = []
  end
end
</code></pre>
<p>which lets you do this</p>
<pre><code>imac:~/code/ruby-yaml brian$ irb
irb(main):001:0&gt; <strong>require 'atom'</strong>
=&gt; true
irb(main):002:0&gt; <strong>require 'molecule'</strong>
=&gt; true
irb(main):003:0&gt; <strong>require 'yaml'</strong>
=&gt; true
irb(main):004:0&gt; <strong>a = Atom.new</strong>
=&gt; #&lt;Atom:0x59e304 @x=0.0, @z=0.0, @y=0.0&gt;
irb(main):005:0&gt; <strong>a.x,a.y,a.z = [1.0,2.0,3.0]</strong>
=&gt; [1.0, 2.0, 3.0]
irb(main):006:0&gt; <strong>b = Atom.new</strong>
=&gt; #&lt;Atom:0x598774 @x=0.0, @z=0.0, @y=0.0&gt;
irb(main):007:0&gt; <strong>b.x,b.y,b.z = [4.0,5.0,6.0]</strong>
=&gt; [4.0, 5.0, 6.0]
irb(main):008:0&gt; <strong>m = Molecule.new</strong>
=&gt; #&lt;Molecule:0x592b08 @atoms=[]&gt;
irb(main):009:0&gt; <strong>m.atoms.push a</strong>
=&gt; [#&lt;Atom:0x59e304 @x=1.0, @z=3.0, @y=2.0&gt;]
irb(main):010:0&gt; <strong>m.atoms.push b</strong>
=&gt; [#&lt;Atom:0x59e304 @x=1.0, @z=3.0, @y=2.0&gt;,
    #&lt;Atom:0x598774 @x=4.0, @z=6.0, @y=5.0&gt;]
irb(main):011:0&gt; <strong>print save = m.to_yaml</strong>
--- !ruby/object:Molecule
atoms:
- !ruby/object:Atom
  x: 1.0
  y: 2.0
  z: 3.0
- !ruby/object:Atom
  x: 4.0
  y: 5.0
  z: 6.0
=&gt; nil
irb(main):013:0&gt; <strong>new = YAML.load(save)</strong>
=&gt; #&lt;Molecule:0x586cf4
           @atoms=[#&lt;Atom:0x58726c @x=1.0, @z=3.0, @y=2.0&gt;,
                   #&lt;Atom:0x586f38 @x=4.0, @z=6.0, @y=5.0&gt;]&gt;
irb(main):014:0&gt;
</code></pre>
<h2>YAML is plain text</h2>
<p>From my perspective, the real advantage of doing this is having the output in a plain text file.  I can then edit the text in an editor and tweak things this way.  I can also markup the text and run it through a <a href="http://numericalruby.com/2007/06/18/templator-ruby-template-system/" title="Templator">template engine</a> and generate a whole bunch of files.  This was part of the magic behind my <a href="http://numericalruby.com/2007/06/19/rake-tricks/" title="Rake tricks">rake example</a>, where I generated 800 or so configurations in an hour or so, which was very cool.</p>
<p><strong>Edit:</strong> tweaked the formatting in the irb section to see the code better</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/numericalruby.wordpress.com/12/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/numericalruby.wordpress.com/12/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/numericalruby.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/numericalruby.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/numericalruby.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/numericalruby.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/numericalruby.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/numericalruby.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/numericalruby.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/numericalruby.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/numericalruby.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/numericalruby.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/numericalruby.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/numericalruby.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/numericalruby.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/numericalruby.wordpress.com/12/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=numericalruby.wordpress.com&amp;blog=1091501&amp;post=12&amp;subd=numericalruby&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://numericalruby.wordpress.com/2007/06/20/yaml-for-serialization/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/72ddd6509156a7dd3c301b87a46c5219?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bcbroom</media:title>
		</media:content>
	</item>
		<item>
		<title>Rake tricks</title>
		<link>http://numericalruby.wordpress.com/2007/06/19/rake-tricks/</link>
		<comments>http://numericalruby.wordpress.com/2007/06/19/rake-tricks/#comments</comments>
		<pubDate>Tue, 19 Jun 2007 20:25:04 +0000</pubDate>
		<dc:creator>Brian Broom</dc:creator>
				<category><![CDATA[Rake]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://numericalruby.com/2007/06/19/rake-tricks/</guid>
		<description><![CDATA[Recently, I had some plots I was trying to generate. The neat part was that everything broke down into little peices. I looked at using Rake to put everything together. The nice thing about Rake is being able to set up the rules for generating each piece, and letting the computer sort out what needs [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=numericalruby.wordpress.com&amp;blog=1091501&amp;post=11&amp;subd=numericalruby&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<a class="DiggThisButton DiggMedium" href="http://digg.com/submit?url=http%3A%2F%2Fnumericalruby.wordpress.com%2F2007%2F06%2F19%2Frake-tricks%2F&amp;title=Rake%26nbsp%3Btricks"></a>
<p>Recently, I had some plots I was trying to generate. The neat part was that everything broke down into little peices.  I looked at using Rake to put everything together.  The nice thing about Rake is being able to set up the rules for generating each piece, and letting the computer sort out what needs to be done.</p>
<p>Here are the steps I needed</p>
<ol>
<li>Generate .yaml files (I was doing a parameter sweep of a particular function)</li>
<li>Run a script that takes the .yaml file and outputs x,y,z triples as a .dat file</li>
<li>Generate a script for gnuplot to plot the .dat files as .png</li>
<li>Run gnuplot with the script</li>
</ol>
<p>I scoured a bunch of online resources about Rake. The rule and file list tricks seem to not be covered as much (or I&#8217;m not very bright) and they took me a long time to figure out.  On the off chance that someone else isn&#8217;t clear about them, here is the Rakefile I used (with the calculation parts removed.)</p>
<pre><code>SRC = FileList['*.yaml']
DAT = SRC.ext('dat')
GP = SRC.ext('gp')
PNG = SRC.ext('png')

task :default =&gt; [:png]

task :png =&gt; [:dat, :gp]

task :dat =&gt; DAT
task :gp =&gt; GP
task :png =&gt; PNG

rule '.png' =&gt; ['.gp', '.dat'] do |f|
  puts "png test #{f.name} #{f.source}"
  sh "touch #{f.name}"
end

rule '.dat' =&gt; ['.yaml'] do |f|
  puts "dat test #{f.name} #{f.source}"
  sh "touch #{f.name}"
end

rule '.gp' =&gt; ['.yaml'] do |f|
  puts "gp test #{f.name} #{f.source}"
  sh "touch #{f.name}"
end
</code></pre>
<p>So the SRC variable becomes a list of all the .yaml files.  Changing the extension to png, and making a task of :png =&gt; PNG, makes a set of file tasks for the (non-existant) .png files. The rules then give the details of how to generate the .png from the required files.</p>
<p>The thing that was most interesting about this was the ability to define the rules to generate files, without having to specify <em>all</em> the steps for <em>each</em> file, which would be pages and pages (I ended up with over 800 .yaml files).  All I had to do was generate the .yaml files, and then run rake.</p>
<p><em>Refrences</em></p>
<p><a href="http://rake.rubyforge.org/" title="Rake Docs">Rake Docs</a></p>
<p><a href="http://onestepback.org/index.cgi/Tech/Rake/" title="Tutorial">Rake Tutorial from Jim Weirich (creator of Rake)<strong>Update: Fixed link</strong></a></p>
<p><a href="http://docs.rubyrake.org/read/chapter/1/" title="Rake Documentation">Rake Documentation</a></p>
<p><a href="http://www.ibm.com/developerworks/edu/os-dw-os-rubyrake-i.html/" title="Rake">Automating tasks with Rake</a> (registration required)</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/numericalruby.wordpress.com/11/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/numericalruby.wordpress.com/11/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/numericalruby.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/numericalruby.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/numericalruby.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/numericalruby.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/numericalruby.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/numericalruby.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/numericalruby.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/numericalruby.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/numericalruby.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/numericalruby.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/numericalruby.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/numericalruby.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/numericalruby.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/numericalruby.wordpress.com/11/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=numericalruby.wordpress.com&amp;blog=1091501&amp;post=11&amp;subd=numericalruby&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://numericalruby.wordpress.com/2007/06/19/rake-tricks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/72ddd6509156a7dd3c301b87a46c5219?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bcbroom</media:title>
		</media:content>
	</item>
		<item>
		<title>Templator &#8211; Ruby Template System</title>
		<link>http://numericalruby.wordpress.com/2007/06/18/templator-ruby-template-system/</link>
		<comments>http://numericalruby.wordpress.com/2007/06/18/templator-ruby-template-system/#comments</comments>
		<pubDate>Mon, 18 Jun 2007 12:00:14 +0000</pubDate>
		<dc:creator>Brian Broom</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Template system]]></category>

		<guid isPermaLink="false">http://numericalruby.com/2007/06/18/templator-ruby-template-system/</guid>
		<description><![CDATA[Most simulation software is based on a text input file. Mostly I’m doing molecular dynamics simulation using LAMMPS. One input file from the samples is the following # 3d Lennard-Jones melt units lj atom_style atomic lattice fcc 0.8442 region box block 0 20 0 20 0 20 create_box 1 box create_atoms 1 mass 1 1.0 [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=numericalruby.wordpress.com&amp;blog=1091501&amp;post=10&amp;subd=numericalruby&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<a class="DiggThisButton DiggMedium" href="http://digg.com/submit?url=http%3A%2F%2Fnumericalruby.wordpress.com%2F2007%2F06%2F18%2Ftemplator-ruby-template-system%2F&amp;title=Templator+%26%238211%3B+Ruby+Template%26nbsp%3BSystem"></a>
<p>Most simulation software is based on a text input file. Mostly I’m doing molecular dynamics simulation using LAMMPS. One input file from the samples is the following</p>
<pre><code># 3d Lennard-Jones melt

units       lj
atom_style  atomic

lattice     fcc 0.8442
region      box block 0 20 0 20 0 20
create_box  1 box
create_atoms    1
mass        1 1.0

velocity    all create 3.0 87287

pair_style  lj/cut 2.5
pair_coeff  1 1 1.0 1.0 2.5

neighbor    0.3 bin
neigh_modify    every 20 delay 0 check no

fix     1 all nve

dump        id all atom 10 dump.melt

thermo      50
run     250
</code></pre>
<p>So the input file has the setup of where the atoms go, what the temperature is, what type of simulation you are doing, where the output files record and what the filenames are. If you are doing a sweep of some parameter over a large range it is a bit annoying to have to make a bunch of copies of the file, tweak them, and keep everything in sync. I usually like to have the output files have the major parameter in the filename and it is very easy to get these out of sync with the actual parameter.</p>
<p>I started looking for a simple template system with Ruby, where I could specify a template file, and then somehow loop over a range of numbers and replace specific parts.</p>
<p>So, what I wanted was something to take a template file like this…</p>
<pre><code>regular text with &lt;% title %&gt; embedded
inside that I want to try to replace
and variable p equals &lt;% velocity %&gt;
along with another &lt;%title %&gt; tag, since
thats the point. And another &lt;% title%&gt; for
good measure.
</code></pre>
<p>and pass it to a function, along with an option hash, and have it return this…</p>
<pre><code>regular text with test.0.5 embedded
inside that I want to try to replace
and variable p equals 0.5
along with another test.0.5 tag, since
thats the point. And another test.0.5 for
good measure.
</code></pre>
<p>I wrote up some code to take care of this. The base code I came up with is this…</p>
<pre><code>class Templator
  def Templator.generate(template, options)
    #template is text string of the template file
    #options is a hash of things to replace

    #currently not checking they match up

    tag_regex = /&lt;%s*w+_*w*s*%&gt;/
    hits = template.scan(tag_regex)
    tags = hits.map {|item| item.chomp('%&gt;').reverse.chomp('%&lt;').reverse.strip}
    tags.map! {|a| a.intern}
    tags.uniq!

    tags.inject(template) {|ntext,tag|
        ntext.gsub(Templator.symbol_to_tag_regex(tag),
        options[tag].to_s)}
  end

  def Templator.symbol_to_tag_regex(tag_name)
    Regexp.new('&lt;%s*' + tag_name.to_s + 's*%&gt;')
  end
end
</code></pre>
<p>This design is one that is going to be called from Ruby code, and not from the command line. I designed this to be extremely flexible, and easy to automate for, say, a hundred files or so. All you have to do is read in the template file, build the options hash, and pass the two to the Templator#generate method. Here is the driver code I used.</p>
<pre><code>require 'templator'

template = File.open('template.txt') {|f| f.read}

0.5.step(1.5, 1.0) do |x|
  opt = {:title =&gt; "test." + x.to_s, :velocity =&gt; x}
  newtext = Templator.generate(template, opt)
  filename = "test-file-" + x.to_s
  File.open(filename,'w') {|f| f.print newtext }
end
</code></pre>
<p>There are certainly more options for different purposes, but this one did what I wanted. Feel free to use this code if you’re in a similar situation, or if there is something you think it should do that it doesn’t, let me know and I’ll take a look at it.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/numericalruby.wordpress.com/10/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/numericalruby.wordpress.com/10/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/numericalruby.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/numericalruby.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/numericalruby.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/numericalruby.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/numericalruby.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/numericalruby.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/numericalruby.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/numericalruby.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/numericalruby.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/numericalruby.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/numericalruby.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/numericalruby.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/numericalruby.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/numericalruby.wordpress.com/10/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=numericalruby.wordpress.com&amp;blog=1091501&amp;post=10&amp;subd=numericalruby&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://numericalruby.wordpress.com/2007/06/18/templator-ruby-template-system/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/72ddd6509156a7dd3c301b87a46c5219?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bcbroom</media:title>
		</media:content>
	</item>
	</channel>
</rss>
