॥ स्वक्ष ॥

Ir al contenido | Ir al menú | Ir a Buscar

Tag - Algorithms

Registro de entradas

2012 Marzo 11 [domingo]

Programming with Julia

Using (or talking) about R makes me want to rant sometimes and if you've ever written a line of Fortran code, you would have certainly wanted to experiment with a new language stack for scientific programming. Yup, I am aware of scipy, numpy, sympy, sage, et al.. and despite their existence, when I came across this language for scientific programming in January this year, sheer curiosity** made me give it a spin.

Starting the year with a scientific language that has clean syntax and some nicer documentation made me feel warm and fuzzy, until, it refused to build due to a BLAS dependency. That problem didnt last for long though, as I was able to pull a fresh commit which had fixed this issue. Its nice to see an active team having interesting (read, sane) development discussions. A Matlab coder has opined thus about Julia while an R programmer has done a comparison between Julia and R.

After some trial-and-error, I managed to grok its syntax, enough to rewrite an old fortran code in JuliaLang [Julia is still pre-release and I ran Version 0.0.0+1331430882.r69af from Commit 69afb7032d (2012-03-11 07:39:42)]:

#!/usr/bin/env julia
# -*- coding: utf-8 -*-
# COPYRIGHT (C) 2012 VidAyer <svaksha@gmail.com>
# LICENSE: GNU AGPLv3, http://www.gnu.org/licenses/agpl-3.0.html
# http://en.wikipedia.org/wiki/Pearson_correlation#Geometric_interpretation
# Example, suppose 5 countries have gross national products
# of 1, 2, 3, 5, and 8 billion dollars, resp. Suppose these 5 countries
# (in the same order) are found to have 11%, 12%, 13%, 15%, and
# 18% poverty. Then let x and y be ordered 5-element vectors containing
# the above data: x = (1, 2, 3, 5, 8) and y = (0.11, 0.12, 0.13, 0.15, 0.18).

gnp = [1, 2, 3, 5, 8]
poverty = [0.11, 0.12, 0.13, 0.15, 0.18]
vectorgnpgnp = 0.0
vectorpovertypoverty = 0.0
vectorgnppoverty = 0.0
costheta = 0.0
n = 1.0

vectorgnpgnp = vectorgnpgnp + gnp[n]*gnp[n]
vectorpovertypoverty = vectorpovertypoverty + poverty[n]*poverty[n]
vectorgnppoverty = vectorgnppoverty + gnp[n]*poverty[n]
costheta = vectorgnppoverty / (sqrt(vectorgnpgnp)*sqrt(vectorpovertypoverty))

# Terminal Out
println("The Vector product value of Cos Theta is: ",costheta)   # correct value should be 0.9208   
println("The Vector product of GNP and Poverty coordinates:  ",vectorgnppoverty) # correct value should be 2.930   
println("The Vector product of GNP is:  ",vectorgnpgnp) # correct value should be 103.0   
println("The Vector product of Poverty coordinates is:  ",vectorpovertypoverty) # correct value should be 0.9830E-01

I was avoiding blogging this code snippet here - code belongs in a DVCS, not in a blog. But I've intentionally not uploaded this program to github because its not fully functional yet -- see my comments within the code. When I cross-check the results from the fortran code (or as mentioned in the WP page --Geometric_interpretation.) I find "sqrt" isnt working as it should -- I need to figure out the syntax a wee bit more, so I'll push this when it works perfectly.

** PS: That it was named after a woman (or atleast I like to think so) isnt what prompted my interest! No kidding!

2008 Enero 10 [jueves]


Being ill and drowsy with medication, this week I stuck to surfing, and saw this math problem, to be precise its graph theory :) Among the various ideas, i suggested Warshall's algorithm for shortest path. If its planer there is Kuratowski's theorem. Which will work better now?