Scientific Programming with Ruby

Scientific Programming with Ruby

Linear algebra is at the heart of most large-scale scientific computing. LAPACK is the gold standard for linear algebra libraries, first written in FORTRAN.

There's a port to Ruby here. Once you have that, the rest is incidental, but there are also plotting routines in Ruby.

Integrating scientific python into an existing Rails webapp

In lieu of substantially hacking apart either codebase to fit the other, I would first propose you evaluate an SOA solution.

e.g. create an API interface to your Python system, and have
the Rails app call that API for what the HR and public-site systems
require.

Ruby's yield feature in relation to computer science

Ruby's yield is not an iterator like in C# and Python. yield itself is actually a really simple concept once you understand how blocks work in Ruby.

Yes, blocks are a functional programming feature, even though Ruby is not properly a functional language. In fact, Ruby uses the method lambda to create block objects, which is borrowed from Lisp's syntax for creating anonymous functions — which is what blocks are. From a computer science standpoint, Ruby's blocks (and Lisp's lambda functions) are closures. In Ruby, methods usually take only one block. (You can pass more, but it's awkward.)

The yield keyword in Ruby is just a way of calling a block that's been given to a method. These two examples are equivalent:

def with_log
output = yield # We're calling our block here with yield
puts "Returned value is #{output}"
end

def with_log(&stuff_to_do) # the & tells Ruby to convert into
# an object without calling lambda
output = stuff_to_do.call # We're explicitly calling the block here
puts "Returned value is #{output}"
end

In the first case, we're just assuming there's a block and say to call it. In the other, Ruby wraps the block in an object and passes it as an argument. The first is more efficient and readable, but they're effectively the same. You'd call either one like this:

with_log do
a = 5
other_num = gets.to_i
@my_var = a + other_num
end

And it would print the value that wound up getting assigned to @my_var. (OK, so that's a completely stupid function, but I think you get the idea.)

Blocks are used for a lot of things in Ruby. Almost every place you'd use a loop in a language like Java, it's replaced in Ruby with methods that take blocks. For example,

[1,2,3].each {|value| print value} # prints "123"
[1,2,3].map {|value| 2**value} # returns [2, 4, 8]
[1,2,3].reject {|value| value % 2 == 0} # returns [1, 3]

As Andrew noted, it's also commonly used for opening files and many other places. Basically anytime you have a standard function that could use some custom logic (like sorting an array or processing a file), you'll use a block. There are other uses too, but this answer is already so long I'm afraid it will cause heart attacks in readers with weaker constitutions. Hopefully this clears up the confusion on this topic.

What will a Python programmer gain by learning Ruby?

For the most part, nothing. Most of Ruby's strengths/weaknesses are the same as Python's, except that Ruby is slightly more "functional". However if you have Haskell as an option, much more can be learned about functional programming from Haskell than from Ruby.

Second, if you're looking at things from a theoretical computer science perspective, then Ruby is far from a language of choice. Ruby and a lot of its libraries break a lot of standard OOP dogma which I believe many academics would find repulsive (this is based mainly on my chats about languages with various professors).

From an academic perspective I think Haskell would have the most appeal to you. If you're interested in AI or logic, then Prolog is also an excellent choice.

Scientific Programming Stack for Clojure

Start with Incanter: http://incanter.org/

Then look at Java scientific libraries.



Related Topics



Leave a reply



Submit