You are on page 1of 5

MARCH 19, 2008

Basic Math Operations


The following math functions are available in Smallworld Magik. These are useful to know because many spatial programming methods require some sort of calculation. Addition 2 +3 = 5 Subtraction 2 3 = -1 Multiplication 2 *3=6 Division 2 / 3= 2/3 4/2 = 2 Exponentiation 2**3=8 Division of whole numbers 21 _div 5 = 4 Remainder 21 _mod 5 = 1 Rounding 1.234.round(2) = 1.23 1.234.round(0) = 1 1.234.rounded = 1 (rounded returns nearest whole number) Ceiling (smallest integer greater than _self) 1.2.ceiling = 2 Floor (largest whole number less than _self) 3.4.floor = 3 Negative & Positive 4.negative? = False 0-4.negative? = True 4.positive? = True 0-4.positive? = False Absolute Value (0-4).abs = 4 Force Float 2/3.as_float = 0.6666666667 Radians & Degrees 100.degrees_to_radians = 1.745329252 3.141892654.radians_to_degrees.rounded = 180 Greatest Common Divisor 15.gcd(25) = 5 100.gcd(75) = 25 Lowest Common Multiple 2.lcm(3) = 6 5.lcm(10) = 10 Hypotenuse (returns sqrt(_self*_self + other*other)) 3.hypot(4) = 5 Inverse 5.inverse = 1/5 5.inverse.as_float = 0.2

Square Root 4.sqrt = 2 5.sqrt = 2.236067977 Integer Square Root 4.isqrt = 2 5.isqrt = 2 Log base 10 and Natural Log 5.ln = 1.609437912 5.log10 = 0.6989700043 Max & Min 3.min(7) = 3 3.max(7) = 7 Even & Odd 3.even? = False 3.odd? = True Prime 3.prime? = True 4.prime? = False There are some other mathematical functions available, including basic trigonometry functions as well as some basic calculus functions. Happy calculating.
Posted by Christian at 11:30 AM | Permalink | Comments (0) | TrackBacks (0)

FEBRUARY 25, 2008

I Object
Smallworld Magik is an object oriented language, so almost everything you deal with is an object. Some examples of objects:

Integers (1, 13, 2008) Floating point (1.0, 3.1415, 2e3, 0.57721) o Note: the e in 2e6 is 2x10^6, not 2*e^6 where e = Natural Logarithmic Base o Note: 0.57721 works while .57721 will fail Character( %c, %l, %t) o Note the % denotes a character object

There are a few special objects as well. The keywords _unset, _true, _false, and _maybe are all objects and can be treated as such. The string Its the end of the world as we know it. is actually an array of characters. Knowing this, we can treat it as a collection without having to parse the string. As objects, they can be compared with each other. This code returns the string in alphabetical order, with weight given to punctuation, and capitalization first. This is accomplished through Magiks _cf comparison which evaluates characters based on their ASCII values. Magik2> "".new_appending(_scatter sorted_collection.new_from("It's the end of the world as we know it.").as_simple_vector()) $ " '.Iaddeeeefhhiklnnooorssttttwww" Or reversed: Magik2> "".new_appending(_scatter sorted_collection.new_from("It's the end of the world as we know it.").as_simple_vector()).reversed() $ "wwwttttssrooonnlkihhfeeeeddaI.' " We create an empty string and append to it the result of the as_simple_vector method on the sorted collection we created from our string. Simplicity defined. In the next example we create a string object and assign it to tmp1. We then assign the tmp1 to tmp2. In this case tmp1 and tmp2 dont just have the same value, they are the same object.

Magik2> tmp1 << "Hello" $ "Hello" Magik2> tmp2 << tmp1 $ "Hello" This would not be true in the next case. Both tmp1 and tmp2 are assigned the same value, but they are not the same object. Magik2> tmp1 << "Hello" $ "Hello" Magik2> tmp2 << "Hello" $ "Hello" Final notes on the subject of objects:

Variable assignment doesnt copy, it references. Variable assignment is NOT strongly typed so you dont 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.

Posted by Christian at 11:43 AM | Permalink | Comments (0) | TrackBacks (0)

FEBRUARY 08, 2008

Variable Assignment
Assigning variables in Smallworld Magik can be accomplished with a simple construct known as the left chevron (<<). A simple variable assignment will look like this: a << 7 This expression states that the variable a becomes 10. More complicated expressions can also be expressed in this way. b << 7 * a + 7 or c << (b-3).squared Variables may be named almost anything you want, with the following constraints:

Start with a-z, or ! Contain a-z, 0-9, _, ?, ! Unlimited length Case independent (sorry camelCase junkies)

Variables may also be assigned in serial or parallel. A series assignment might look like this: a << b << c << 7 This is the same as a << (b << (c << 1)) three << (two << (one << 1) +1) +1 Parallel variable assignment might look like this: (a,b,c) << (1,2,3) (s,c) << theta.sincos() <--this works because sincos() returns two values, sin and cos There is a somewhat classic programmer problem for swapping two variables without using a temp variable. One of the many solutions being as follows:

a << a+b b << a-b a << a-b Smallworld Magik has a more elegant solution however, using a swapping assignment. (a,b) << (b,a) Variable can be assigned inside other structures as well. A common use for this is to assign a variable during an if-then evaluation. _if (z<< (x.squared + y.squared).sqrt) <= minimum_distance _then write(The value ,z, is too small!) _endif There are other variations to the left chevron that have their uses. a +<< 1 will increment a by 1. a -<< 1 will de-increment a by 1. a *<< 2 will multiply a by 2. a /<< 2 will divide a by 2. A seldom used, but kind of interesting one is known as the boot assignment. a << b ^<< 10 This is the same as saying a << b followed by b << 10. Essentially b becomes 10 and returns its original value which is in turn assigned to a. 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. The variable e is used by many to indicate an element inside of a loop, while more permanent element references would be named an_element.
Posted by Christian at 03:13 PM | Permalink | Comments (0) | TrackBacks (0)

JANUARY 23, 2008

Beginning at the Beginning


It is customary to declare your intention to learn the language by creating the Hello World program. The simplest way to do this in Magik is to type write(Hello World!)at the command line. Magik2> write("Hello World") $ Hello World Magik2> This works because Magik compiles code on the fly and executes them instantly from the command line. This is great for a lot of stuff, but it doesnt qualify as having coded our first program, so we need to find a new place to write code that doesnt compile instantly. The best place for this is the scratch buffer. To get to the scratch buffer press <ctrl>-x, b to bring up the switch to buffer: command on the very bottom of your emacs screen. Type *scratch* and hit enter and the scratch buffer will appear. In most cases typing *s followed by a tab should auto-complete the line for you, unless you have another buffer that starts with *s. Most people dont. The following text is populated in the scratch buffer the first time you enter it. This buffer is for notes you don't want to save, and for Lisp evaluation. If you want to create a file, visit that file with C-x C-f, then enter the text in that file's own buffer. This needs to be deleted before you can utilize the buffer. Once the buffer is empty you can begin writing a program. At the moment it is a plain text buffer, but we can fix that. Pressing <alt-x> and

typing magik-mode will enable Magik mode in the buffer. This allows Magik specific functions like auto complete, keyword recognition, and word coloring to work. To write our first program we will use one of the simplest constructs available, a procedure or a proc. _global hi << _proc() write(Hello World) _endproc We declared a global variable named hi, which is not case sensitive so all you java educated camelCase programmers take note. The << is Magiks assignment indicator and is read becomes by most people. _proc and _endproc are keywords and the write statement between them should look familiar. Keywords are identified by the underscore at the beginning. If you enabled magikmode earlier, pressing F12 should color them in a unique (usually blue) color. The program needs to be compiled so that the command line will recognize it. To do this, press F2 then b which is short for buffer. There are other options for compiling less than the full buffer such as F2 then m for compiling the method. If your code can be fully parsed without errors the *gis* buffer should now show the following: Magik2> Loading C:\Temp\Tua030992780 --- line 0 Defining variable hi --- line 7 True 0 Magik2> Now type in your program name at the command prompt and hopefully you will see the expected output. Magik2> hi() $ Hello World Magik2> Congratulations on writing your first Magik procedure. 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 rsums.
Posted by Christian at 12:37 PM | Permalink | Comments (1) | TrackBacks (0)

JANUARY 22, 2008

Why am I doing this?


When I was hired as a GIS intern I had never even heard of the Magik language. Over that first summer I tried to learn the language, but it was challenging because there were no reliable sources of information. I couldnt find any good books or guides or how to manuals. Id have even settled for a few pieces of pseudo-code written on a post it note somewhere. 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. So, with several years of experience, I thought Id start a web log of some of the tricks Ive picked up. This wont be The Most Ultimate Guide to Magik Programming Ever. Im going to add some entry level stuff at first and gradually get more sophisticated as time goes on. My intention is to share the basics with other new Magik programmers. 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. Its a very specific language for a very specific purpose, but its a nic e niche for any programmer that wants to work in the GIS industry. If any Magik geniuses (Joe, Tim, etc.) read this and want to comment, please do. Especially if Im wrong, please correct me.
Posted by Christian at 10:23 AM | Permalink | Comments (4) | TrackBacks (0)

You might also like