<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>Magik Tricks</title>
    <link rel="alternate" type="text/html" href="http://www.thetatons.com/magiktricks/" />
    <link rel="self" type="application/atom+xml" href="http://www.thetatons.com/magiktricks/atom.xml" />
   <id>tag:www.thetatons.com,2008:/magiktricks/1</id>
    <link rel="service.post" type="application/atom+xml" href="http://www.thetatons.com/magiktricks-mt/mt-atom.cgi/weblog/blog_id=1" title="Magik Tricks" />
    <updated>2008-03-19T18:36:53Z</updated>
    <subtitle>A Programmer&apos;s Guide To Smallworld Magik</subtitle>
    <generator uri="http://www.sixapart.com/movabletype/">Movable Type 3.2ysb5-20051201</generator>
 
<entry>
    <title>Basic Math Operations</title>
    <link rel="alternate" type="text/html" href="http://thetatons.com/magiktricks/2008/03/basic_math_operations.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://www.thetatons.com/magiktricks-mt/mt-atom.cgi/weblog/blog_id=1/entry_id=5" title="Basic Math Operations" />
    <id>tag:www.thetatons.com,2008:/magiktricks//1.5</id>
    
    <published>2008-03-19T18:30:52Z</published>
    <updated>2008-03-19T18:36:53Z</updated>
    
    <summary><![CDATA[The following math functions are available in Smallworld Magik.&nbsp; These are useful to know because many spatial programming methods require some sort of calculation.Addition2 +3 = 5Subtraction2 &ndash;3 = -1Multiplication2&nbsp;*3=6Division2 / 3= 2/34/2 = 2Exponentiation2**3=8Division of whole numbers21 _div &nbsp;5...]]></summary>
    <author>
        <name>Christian</name>
        <uri>magiktricks.thetatons.com</uri>
    </author>
            <category term="Basic Magik" />
    
    <content type="html" xml:lang="en" xml:base="http://www.thetatons.com/magiktricks/">
        <![CDATA[<span /><p>The following math functions are available in Smallworld Magik.<span>&nbsp; </span>These are useful to know because many spatial programming methods require some sort of calculation.</p><p><span>Addition<br /></span>2 +3 = 5</p><p><span>Subtraction<br /></span>2 &ndash;3 = -1</p><p><span>Multiplication<br /></span>2&nbsp;*3=6</p><p><span>Division<br /></span>2 / 3= 2/3<br />4/2 = 2</p><p><span>Exponentiation<br /></span>2**3=8</p><p><span>Division of whole numbers<br /></span>21 _div <span>&nbsp;</span>5 = 4</p><p><span>Remainder<br /></span>21 _mod 5 = 1<span>&nbsp;</span></p><p><span>Rounding<br /></span>1.234.round(2) = 1.23<br />1.234.round(0) = 1<br />1.234.rounded = 1 (rounded returns nearest whole number)<span>&nbsp;</span></p><p><span>Ceiling (smallest integer greater than _self)<br /></span>1.2.ceiling = 2<span>&nbsp;</span></p><p><span>Floor (largest whole number less than _self)<br /></span>3.4.floor = 3<span>&nbsp;</span></p><p><span>Negative &amp; Positive<br /></span>4.negative? = False<br />0-4.negative? = True<br />4.positive? = True <br />0-4.positive? = False</p><p><span>Absolute Value<br /></span>(0-4).abs = 4<span>&nbsp;</span></p><p><span>Force Float<br /></span>2/3.as_float = 0.6666666667</p><p><span>Radians &amp; Degrees<br /></span>100.degrees_to_radians = 1.745329252<br />3.141892654.radians_to_degrees.rounded = 180</p><p><span>Greatest Common Divisor<br /></span>15.gcd(25) = 5<br />100.gcd(75) = 25</p><p><span>Lowest Common Multiple<br /></span>2.lcm(3) = 6<br />5.lcm(10) = 10</p><p><span>Hypotenuse <span>&nbsp;</span>(returns sqrt(_self*_self + other*other))<br /></span>3.hypot(4) = 5</p><p><span>Inverse<br /></span>5.inverse = 1/5<br />5.inverse.as_float = 0.2</p><p><span>Square Root<br /></span>4.sqrt = 2<br />5.sqrt = 2.236067977</p><p><span>Integer Square Root<br /></span>4.isqrt = 2<br />5.isqrt = 2</p><p><span>Log base 10 and Natural Log<br /></span>5.ln = 1.609437912<br />5.log10 = 0.6989700043</p><p><span>Max &amp; Min<br /></span>3.min(7) = 3<br />3.max(7) = 7</p><p><span>Even &amp; Odd<br /></span>3.even? = False<br />3.odd? = True</p><p><span>Prime<br /></span>3.prime? = True<br />4.prime? = False</p><p>There are some other mathematical functions available, including basic trigonometry functions as well as some basic calculus functions.<span>&nbsp; </span>Happy calculating.</p>]]>
        
    </content>
</entry>
<entry>
    <title>I Object</title>
    <link rel="alternate" type="text/html" href="http://thetatons.com/magiktricks/2008/02/i_object.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://www.thetatons.com/magiktricks-mt/mt-atom.cgi/weblog/blog_id=1/entry_id=4" title="I Object" />
    <id>tag:www.thetatons.com,2008:/magiktricks//1.4</id>
    
    <published>2008-02-25T18:43:27Z</published>
    <updated>2008-03-19T18:36:53Z</updated>
    
    <summary><![CDATA[Smallworld Magik is an object oriented language, so almost everything you deal with is an object.&nbsp; Some examples of objects:Integers (1, 13, 2008)Floating point (1.0, 3.1415, 2e3, 0.57721)Note: the e in 2e6 is 2x10^6, not 2*e^6 where e = Natural...]]></summary>
    <author>
        <name>Christian</name>
        <uri>magiktricks.thetatons.com</uri>
    </author>
            <category term="Basic Magik" />
    
    <content type="html" xml:lang="en" xml:base="http://www.thetatons.com/magiktricks/">
        <![CDATA[<p>Smallworld Magik is an object oriented language, so almost everything you deal with is an object.<span>&nbsp; </span>Some examples of objects:</p><ul><li>Integers (1, 13, 2008)</li><li>Floating point (1.0, 3.1415, 2e3, 0.57721)</li><ul><li>Note: the e in 2e6 is 2x10^6, not 2*e^6 where e = Natural Logarithmic Base </li><li>Note: 0.57721 works while .57721 will fail</li></ul><li>Character( %c, %l, %t)</li><ul><li>Note the % denotes a character object</li></ul></ul><p>There are a few special objects as well.<span>&nbsp; </span>The keywords _<strong>unset</strong>, _<strong>true</strong>, _<strong>false</strong>, and _<strong>maybe</strong> are all objects and can be treated as such.</p><p>The string &ldquo;It&rsquo;s the end of the world as we know it.&rdquo; is actually an array of characters.<span>&nbsp; </span>Knowing this, we can treat it as a collection without having to parse the string.<span>&nbsp; </span>As objects, they can be compared with each other.<span>&nbsp; </span>This code returns the string in alphabetical order, with weight given to punctuation, and capitalization first.<span>&nbsp; </span>This is accomplished through Magik&rsquo;s <strong>_cf</strong> comparison which evaluates characters based on their ASCII values.<span>&nbsp; </span></p><strong>Magik2&gt; &quot;&quot;.new_appending(_scatter<span>&nbsp; </span>sorted_collection.new_from(&quot;It's the end of the world as we know it.&quot;).as_simple_vector())<br />$<br />&quot;<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>'.Iaddeeeefhhiklnnooorssttttwww&quot;<br /></strong><p>Or reversed:</p><strong>Magik2&gt; &quot;&quot;.new_appending(_scatter<span>&nbsp; </span>sorted_collection.new_from(&quot;It's the end of the world as we know it.&quot;).as_simple_vector()).reversed()<br /></strong><strong>$<br /></strong><strong>&quot;wwwttttssrooonnlkihhfeeeeddaI.'<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&quot;<br /></strong><p>We create an empty string &ldquo;&rdquo; and append to it the result of the as_simple_vector method on the sorted collection we created from our string.<span>&nbsp; </span>Simplicity defined.</p><p>In the next example we create a string object and assign it to tmp1.<span>&nbsp; </span>We then assign the tmp1 to tmp2.<span>&nbsp; </span>In this case tmp1 and tmp2 don&rsquo;t just have the same value, they are the same object.</p><strong>Magik2&gt; tmp1 &lt;&lt; &quot;Hello&quot;<br />$<br />&quot;Hello&quot; <br />Magik2&gt; tmp2 &lt;&lt; tmp1<br />$<br />&quot;Hello&quot;<br /></strong><p>This would not be true in the next case.<span>&nbsp; </span>Both tmp1 and tmp2 are assigned the same value, but they are not the same object.<span>&nbsp; </span></p><strong>Magik2&gt; tmp1 &lt;&lt; &quot;Hello&quot;<br />$<br />&quot;Hello&quot; <br />Magik2&gt; tmp2 &lt;&lt; &quot;Hello&quot;<br />$<br />&quot;Hello&quot; <br /></strong><p>Final notes on the subject of objects:</p><ul><li>Variable assignment doesn&rsquo;t copy, it references.<span>&nbsp; </span></li><li>Variable assignment is NOT strongly typed so you don&rsquo;t need to declare it first, just assign a variable to an object, be it a number, a string, an array, a collection, or even a running application.</li></ul>]]>
        
    </content>
</entry>
<entry>
    <title>Variable Assignment</title>
    <link rel="alternate" type="text/html" href="http://thetatons.com/magiktricks/2008/02/variable_assignment.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://www.thetatons.com/magiktricks-mt/mt-atom.cgi/weblog/blog_id=1/entry_id=3" title="Variable Assignment" />
    <id>tag:www.thetatons.com,2008:/magiktricks//1.3</id>
    
    <published>2008-02-08T22:13:58Z</published>
    <updated>2008-03-19T18:36:53Z</updated>
    
    <summary><![CDATA[Assigning variables in Smallworld Magik can be accomplished with a simple construct known as the left chevron (&lt;&lt;).&nbsp; A simple variable assignment will look like this:a &lt;&lt; 7This expression states that the variable &ldquo;a&rdquo; becomes 10.&nbsp; More complicated expressions can...]]></summary>
    <author>
        <name>Christian</name>
        <uri>magiktricks.thetatons.com</uri>
    </author>
            <category term="Basic Magik" />
    
    <content type="html" xml:lang="en" xml:base="http://www.thetatons.com/magiktricks/">
        <![CDATA[<p>Assigning variables in Smallworld Magik can be accomplished with a simple construct known as the left chevron (&lt;&lt;).<span>&nbsp; </span>A simple variable assignment will look like this:</p><strong>a &lt;&lt; 7<br /></strong><p>This expression states that the variable &ldquo;a&rdquo; becomes 10.<span>&nbsp; </span>More complicated expressions can also be expressed in this way.</p><strong>b &lt;&lt; 7 * a + 7<br /></strong><p>or </p><strong>c &lt;&lt; (b-3).squared<br /></strong><p>Variables may be named almost anything you want, with the following constraints:</p><ul><li>Start with a-z, or !</li><li>Contain a-z, 0-9, _, ?, !</li><li>Unlimited length</li><li>Case independent (sorry camelCase junkies)</li></ul><p>Variables may also be assigned in serial or parallel.<span>&nbsp; </span>A series assignment might look like this:</p><strong>a &lt;&lt; b &lt;&lt; c &lt;&lt; 7<br /></strong>This is the same as <strong>a &lt;&lt; (b &lt;&lt; (c &lt;&lt; 1))<br /></strong><strong>three &lt;&lt; (two &lt;&lt; (one &lt;&lt; 1) +1) +1<br /></strong><p>Parallel variable assignment might look like this:</p><strong>(a,b,c) &lt;&lt; (1,2,3)<br /></strong><p><strong>(s,c) &lt;&lt; theta.sincos()</strong><span>&nbsp; </span>&lt;--this works because sincos() returns two values, sin and cos</p><p>There is a somewhat classic programmer problem for swapping two variables without using a temp variable.<span>&nbsp; </span>One of the many solutions being as follows: </p><strong>a &lt;&lt; a+b<br />b &lt;&lt; a-b<br />a &lt;&lt; a-b <br /></strong><p>Smallworld Magik has a more elegant solution however, using a swapping assignment.</p><strong>(a,b) &lt;&lt; (b,a)<br /></strong><p>Variable can be assigned inside other structures as well.<span>&nbsp; </span>A common use for this is to assign a variable during an if-then evaluation.</p><strong>_if (z&lt;&lt; (x.squared + y.squared).sqrt) &lt;= minimum_distance<br />_then<br /><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>write(&ldquo;The value &ldquo;,z,&rdquo; is too small!&rdquo;)<br />_endif<br /></strong><p>There are other variations to the left chevron that have their uses.</p><p><strong>a +&lt;&lt; 1</strong> will increment a by 1.</p><p><strong>a -&lt;&lt; 1</strong> will de-increment a by 1.</p><p><strong>a *&lt;&lt; 2</strong> will multiply a by 2.</p><p><strong>a /&lt;&lt; 2</strong> will divide a by 2.</p><p>A seldom used, but kind of interesting one is known as the boot assignment.</p><strong>a &lt;&lt; b ^&lt;&lt; 10<br /></strong><p>This is the same as saying a &lt;&lt; b followed by b &lt;&lt; 10.<span>&nbsp; </span>Essentially b becomes 10 and returns its original value which is in turn assigned to a.</p><p>One final note on Smallworld Magik variables is that developers tend to use longer names to describe most variables separated by an underscore (_) with the exception being when it is an iterated variable inside of a loop. <span>&nbsp;</span>The variable &lsquo;e&rsquo; is used by many to indicate an element inside of a loop, while more permanent element references would be named an_element.</p>]]>
        
    </content>
</entry>
<entry>
    <title>Beginning at the Beginning</title>
    <link rel="alternate" type="text/html" href="http://thetatons.com/magiktricks/2008/01/beginning_at_the_beginning.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://www.thetatons.com/magiktricks-mt/mt-atom.cgi/weblog/blog_id=1/entry_id=2" title="Beginning at the Beginning" />
    <id>tag:www.thetatons.com,2008:/magiktricks//1.2</id>
    
    <published>2008-01-23T19:37:38Z</published>
    <updated>2008-03-19T18:36:53Z</updated>
    
    <summary><![CDATA[It is customary to declare your intention to learn the language by creating the &ldquo;Hello World&rdquo; program.&nbsp;&nbsp; The simplest way to do this in Magik is to type write(&ldquo;Hello World!&rdquo;) at the command line.Magik2&gt; write(&quot;Hello World&quot;)$Hello WorldMagik2&gt;This works because Magik...]]></summary>
    <author>
        <name>Christian</name>
        <uri>magiktricks.thetatons.com</uri>
    </author>
            <category term="Basic Magik" />
    
    <content type="html" xml:lang="en" xml:base="http://www.thetatons.com/magiktricks/">
        <![CDATA[<p>It is customary to declare your intention to learn the language by creating the<a href="http://en.wikipedia.org/wiki/Hello_world" target="_blank"> &ldquo;Hello World&rdquo;</a> program.<span>&nbsp;&nbsp; </span>The simplest way to do this in Magik is to type <strong>write(&ldquo;Hello World!&rdquo;)</strong> at the command line.</p><strong>Magik2&gt; write(&quot;Hello World&quot;)<br />$<br />Hello World<br />Magik2&gt;<br /></strong><p>This works because Magik compiles code on the fly and executes them instantly from the command line.<span>&nbsp;&nbsp; </span>This is great for a lot of stuff, but it doesn&rsquo;t qualify as having coded our first program, so we need to find a new place to write code that doesn&rsquo;t compile instantly.<span>&nbsp; </span>The best place for this is the scratch buffer.</p><p>To get to the scratch buffer press <strong>&lt;ctrl&gt;-x, b</strong> to bring up the &ldquo;switch to buffer:&rdquo; command on the very bottom of your emacs screen.<span>&nbsp; </span>Type <strong>*scratch*</strong> and hit enter and the scratch buffer will appear.<span>&nbsp; </span>In most cases typing <strong>*s</strong> followed by a <strong>tab</strong> should auto-complete the line for you, unless you have another buffer that starts with *s.<span>&nbsp; </span>Most people don&rsquo;t.</p><p>The following text is populated in the scratch buffer the first time you enter it. </p><p>This buffer is for notes you don't want to save, and for Lisp evaluation.<br />If you want to create a file, visit that file with C-x C-f,<br />then enter the text in that file's own buffer.</p><p>This needs to be deleted before you can utilize the buffer.<span>&nbsp; </span>Once the buffer is empty you can begin writing a program.<span>&nbsp; </span>At the moment it is a plain text buffer, but we can fix that.<span>&nbsp; </span>Pressing <strong>&lt;alt-x&gt; </strong>and typing <strong>magik-mode</strong> will enable Magik mode in the buffer.<span>&nbsp; </span>This allows Magik specific functions like auto complete, keyword recognition, and word coloring to work.</p><p>To write our first program we will use one of the simplest constructs available, a procedure or a proc.</p><strong>_global hi &lt;&lt;<br /><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>_proc()<br /><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>write(&ldquo;Hello World&rdquo;)<br /><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>_endproc<br /></strong><span><p>We declared a global variable named &ldquo;hi&rdquo;, which is not case sensitive so all you java educated <a href="http://en.wikipedia.org/wiki/CamelCase" target="_blank">camelCase </a>programmers take note.&nbsp;<span> </span>The &ldquo;&lt;&lt;&rdquo; is Magik&rsquo;s assignment indicator and is read &ldquo;becomes&rdquo; by most people.<span>&nbsp; </span>_proc and _endproc are keywords and the write statement between them should look familiar.<span>&nbsp; </span>Keywords are identified by the underscore at the beginning.<span>&nbsp; </span>If you enabled magik-mode earlier, pressing <strong>F12 </strong>should color them in a unique (usually blue) color.</p><p>The program needs to be compiled so that the command line will recognize it.<span>&nbsp; </span>To do this, press <strong>F2</strong> then <strong>b</strong> which is short for buffer.&nbsp; There are other options for compiling less than the full buffer such as <strong>F2&nbsp; </strong>then&nbsp;<strong>m</strong> for compiling the method. If your code can be fully parsed without errors the *gis* buffer should now show the following:</p><strong>Magik2&gt; Loading C:\Temp\Tua030992780<br />--- line 0<br />Defining variable hi<br />--- line 7<br />True 0 <br />Magik2&gt;<br /></strong><p>Now type in your program name at the command prompt and hopefully you will see the expected output.</p><p><strong>Magik2&gt; hi()<br />$<br />Hello World<br />Magik2&gt;</strong></p><p>Congratulations on writing your first Magik procedure.<span>&nbsp; </span>If you did it correctly you just declared to the world, or at least the command line, that you are now a member of the rare breed of programmers that can write Smallworld Magik under the Skills heading on their r&eacute;sum&eacute;s.</p></span>]]>
        
    </content>
</entry>
<entry>
    <title>Why am I doing this?</title>
    <link rel="alternate" type="text/html" href="http://thetatons.com/magiktricks/2008/01/why_am_i_doing_this.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://www.thetatons.com/magiktricks-mt/mt-atom.cgi/weblog/blog_id=1/entry_id=1" title="Why am I doing this?" />
    <id>tag:www.thetatons.com,2008:/magiktricks//1.1</id>
    
    <published>2008-01-22T17:23:26Z</published>
    <updated>2008-03-19T18:36:53Z</updated>
    
    <summary><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; When I was hired as a GIS intern I had never even heard of the Magik language.&nbsp; Over that first summer I tried to learn the language, but it was challenging because there were no reliable sources of information.&nbsp;...]]></summary>
    <author>
        <name>Christian</name>
        <uri>magiktricks.thetatons.com</uri>
    </author>
            <category term="General" />
    
    <content type="html" xml:lang="en" xml:base="http://www.thetatons.com/magiktricks/">
        <![CDATA[<p>&nbsp;&nbsp;&nbsp;&nbsp; When I was hired as a GIS intern I had never even heard of the Magik language.<span>&nbsp; </span>Over that first summer I tried to learn the language, but it was challenging because there were no reliable sources of information.<span>&nbsp; </span>I couldn&rsquo;t find any good books or guides or how to manuals.<span>&nbsp; </span>I&rsquo;d have even settled for a few pieces of pseudo-code written on a post it note somewhere.<span>&nbsp; </span>I did manage to learn the language by pouring over the source code in our system and talking to other programmers, but it would have been really nice to have a reference manual or something.</p><p>&nbsp;&nbsp;&nbsp; So, with several years of experience, I thought I&rsquo;d start a web log of some of the tricks I&rsquo;ve picked up.<span>&nbsp; </span>This won&rsquo;t be &ldquo;The Most Ultimate Guide to Magik Programming Ever.&rdquo;<span>&nbsp;&nbsp; </span>I&rsquo;m going to add some entry level stuff at first and gradually get more sophisticated as time goes on.<span>&nbsp;&nbsp; </span>My intention is to share the basics with other new Magik programmers.<span>&nbsp; </span>Many in the industry lament the lack of young new Magik programmers, so hopefully I can make it easier for new people to break into the small cadre of Magik programmers.<span>&nbsp; </span>It&rsquo;s a very specific language for a very specific purpose, but it&rsquo;s a nice niche for any programmer that wants to work in the GIS industry.<span>&nbsp; </span>If any Magik geniuses (Joe, Tim, etc.) read this and want to comment, please do.<span>&nbsp; </span>Especially if I&rsquo;m wrong, please correct me.</p>]]>
        
    </content>
</entry>

</feed> 


