Archive

Archive for March, 2010

Simple mathematical concepts I: Induction.

March 29th, 2010 Serabe No comments

4458595395 2c1bbc10e5 m Simple mathematical concepts I: Induction.
After reading this and this I realized something is really, really wrong about mathematical concepts out there. Proof by induction is a really, really simple yet powerful proof method.  In this post, I will talk about induction for natural numbers.

Induction is based on two steps, usually called basis and inductive step. I will proof that 1+2+…+n=n(n+1)/2  for all natural numbers using induction.

  1. Basis or base case consists on proving the predicate for an initial value, usually a small one. In the example 1=1*2/2=1.
  2. Inductive step consists on supposing the if the predicate holds for n, then it also holds for n+1. In the example:

1+2+…+n+(n+1)=(n+1)+n(n+1)/2 because we supposed that predicate held for n. We need to prove that this expression is equivalent to (n+1)(n+2)/2. Indeed,

(n+1)+n(n+1)/2=(n+1)(1+n/2)=(n+1)((2+n)/2)=(n+1)(2+n)/2

so the predicate has been proofed for all natural numbers.

Why does induction work?

Think of any natural number, as big as you want. Got it? We know that the predicate holds for 1, and that if the predicate holds for a number it holds for its successor. So it holds for 2, and for 3 and so on until it reach your number.

Other types of induction.

There are several types of inductions. Two of these types are structural induction and complete induction.

cc Simple mathematical concepts I: Induction. photo credit: hartboy

article clipper Simple mathematical concepts I: Induction.
 
share save 171 16 Simple mathematical concepts I: Induction.

Writing your own JRuby extension. Part II: Creating your first class.

March 12th, 2010 Serabe 1 comment

3783473433 98d837343a m Writing your own JRuby extension. Part II: Creating your first class.
What’s the point of coding a JRuby extension if you don’t create classes? Well, I cannot think of any case, but if you find one, please, let me know.

Do you remember Java’s classes hierarchy? If so, you’ll realize  that Java objects inherit from java.lang.Object by default but that is not actually what we need. But how can we tell JRuby that our object inherits from Object, the king of Ruby world? The answer is so simple: just extend RubyObject! (There is a RubyBasicObject too) Taken from Nokogiri::XML::Node:

Of course, you can extend any other class, as long as it is a “Ruby object”. For example, Nokogiri::XML::Document extends from Nokogiri::XML::Node, and we do not need to do anything special to reflect it, just extend XmlNode like XmlDocument does:

After talking a bit about hierarchy, let’s talk about Java constructors. At least two parameters are needed: a org.jruby.Ruby object and a org.jruby.RubyClass object. Being the importance of the former quite obvious, the reason for the latter may not be so clear. Let me show you some real world code: Nokogiri::XML::Node’s dup method. Take a look at the following test:

Both new and dup methods in subclass rely on Nokogiri::XML::Node’s. In here, you can see the this snippet of code:

The rb_obj_class method returns the class of an object, in this case, self. This way, the new node will be an instance of the same class as the original node. That’s the reason a RubyClass is needed in the constructor, in order to know which is the actual class being instantiated.

By the way, do not forget to call super with the Ruby and RubyClass objects.

Next step: creating methods.

cc Writing your own JRuby extension. Part II: Creating your first class. photo credit: quinn.anya

article clipper Writing your own JRuby extension. Part II: Creating your first class.
 
share save 171 16 Writing your own JRuby extension. Part II: Creating your first class.