Archive for the ‘English’ Category.

Building OpenCV 2.0 on Mac OS X 10.6

I first tried it this way:


$ cd OpenCV-2.0.0
$ mkdir build
$ cd build
$ cmake ..
$ make

But it failed. I got lots of errors when trying to link highgui to the Carbon libraries. The problem is that Carbon is 32-bit only, and by default OpenCV was being built for 64-bit.

Here the (very simple) solution to that:


$ CFLAGS="-arch i386" CXXFLAGS="-arch i386" cmake ..
$ make

Worried

I’ve lost about 8 kilograms in the past 5~6 months. While at first that’s a good thing (I was getting a little fat), it makes me worried because I made no effort at all to lose that much weight. However, I feel fine, except for an occasional dizziness that usually strikes me in the morning.

I’ll go to a doctor, but they always tell me I’m “too anxious” and that’s all, no matter what symptoms I have. That’s actually quite good, but it’s also a waste of time and money :(

Why Lua’s 0 (zero) as a true value makes sense

When I started to learn about Lua, I found it a little odd that the numeric 0 (zero) value is treated as a true boolean value by the interpreter, which was different from all other programming languages I had worked with.

However, it turns out that it makes a lot of sense to have 0 as a true value, and that’s when you take short circuit expressions into account.

For those who don’t know, short-circuit expressions are used to conditionally select values without having to code that selection into an if-elseif-else structure. Short-circuit expressions are built using logical (and, or, not) operators. The and operator returns the value of the first false expression, or the last expression, in case all others were true. The or operator returns the value of the first true expression, or the last expression, in case all others were false. Here are a few examples in Lua:

Code:

word = 1 < 2 and "hello" or "world"
print(word)

Output:

hello

Code:

level = 10 > 9 and 2 or 4
print(level)

Output:

2

Now, consider the following Lua short-circuit expression:

bit = math.random() < 0.5 and 0 or 1

Running it many times will psuedo-randomly assign both 0 and 1 values to the bit variable:

Code:

for i=1,8 do
    bit = math.random() < 0.5 and 0 or 1
    print(bit)
end

Output:

0
1
1
1
0
0
1
0

Now, consider what happens when we try to do the same thing in Python 2.6:

Code:

import random
for i in range(8):
    bit = random.random() < 0.5 and 0 or 1
    print(bit)

Output:

1
1
1
1
1
1
1
1

The first operand of the and expression is always false, because 0 is considered to be a false boolean value by the Python interpreter.

Even though one could argue that it’s just a matter of knowing how to rewrite such logical expressions, I believe it’s good not having to worry about undesired behavior caused by 0’s in short-circuit expressions. It allows me to write expressions just as they come to my mind, which makes me more productive than if I had to re-arrange them.

Creationism vs. Evolutionism

As a Christian, I’m just growing sick of that absolutely unfruitful discussion. I can’t stand listening to what theologians have to say, nor can I stand what atheists have to say. It’s all a huge load of garbage.

Here are my takes on the issue:

  1. People should only talk about what really concerns them. For me, a theologian who tries to force his faith into scientific reasoning is just as mistaken as the atheist who attempts to prove his unbelief using materialist arguments.
  2. Following the same line of reasoning from the item above, scientific books aren’t religious texts, nor are religious texts scientific books. As Francis Collins points out in The Language of God, scientific books describe the natural world, while religious books (for me, not bookS, only the Christian Bible) talk about the spiritual world.
  3. The whole thing doesn’t make a difference at all for me. The Book of Genesis presents an entirely spiritual issue (the fall of mankind). Whether things happened literally as described there, or if God used evolution as the mechanism for creation, is something that I don’t care much about. I find both alternatives equally admirable in my Creator.
  4. If people want to talk about science, they should talk about science. And if the want to talk about faith, they should talk about faith. Don’t try to mix them. Personally, I believe there is perfect agreement between them, but that agreement doesn’t seem to have been found yet. And we should probably not try to find it: up until now, it has proven to be quite an unhealthy thing.

Maybe I have some more points in the back of my mind, but I think the three above are the main ones. Those are personal opinions. I am NOT trying to convince anyone here. The only thing I would like to ask people for is:

Please stop talking about what you don’t know (or understand)!

And that goes to people in both sides of the issue.

A word for my fellow brothers in Christ: concentrate on what really matters, which is the Gospel of our Lord Jesus. It’s about love, forgiveness, wisdom, tolerance, respect, dignity, honor, and a bunch of other really good things. If you ever happen to discover if the universe was created in literal 7 days or if God decided to take billions of years to create will very likely not add a single bit to the kind and loving character you were taught to pursue.

“Doppler effect” kitchen tiles

The tiles in my kitchen remind me of some Doppler effect illustrations I have seen:

"Doppler Effect" kitchen tiles

Understanding Bayes’ Theorem

Disclaimer: I am writing this because I finally understood how Bayes’ Theorem works. Therefore, this is much more a reference note to myself than anything else, but I am publishing the information here because I think it might be useful for other people. I am no math expert (as can be deduced from the fact that I only understood the theorem now, after having seen it many times), so please let me know if you find any mistakes in my explanation.

Bayes’ Theorem states that

Bayes' Theorem

where P(B|A) is the probability of event B occurring, given event A occurred; P(A|B) is the probability of event A occurring, given event B occurred; P(B) is the probability of only event B occurring; and P(A) is the probability of only event A occurring.

I have seen, and even used, this formula a number of times in my life, but I had never really understood what it was saying. But before I explain it, I will introduce a few basic concepts.

Relative frequency

Given an experiment with two, non-mutually exclusive possible outcomes, A and B, and n repetitions of that experiment, and let n1 be the number of occurrences of event A alone, n2 the number of occurrences of event B alone and n3 the number of occurrences of events A and B simultaneosly, the relative frequency of the occurrence of event A, or probability P(A) of event A, is given by

Probability of an event A

where nA is the number of times event A occurred. Likewise, the relative frequency of the occurrence of event B, or probability P(B) of event B, is given by

Probability of an event B

where nB is the number of times event B occurred. Finally, the relative frequency of the occurrence of both events, or probability P(AB) of events A and B, is given by

Probability of two events A and B

Conditional probability

The relative frequency of event A occurring, given event B occurred, is given by

Number of occurrences of  event A, given B

Notice that in the denominator, we account for the occurrences of event B alone and of event B alongside with event A. The lower the value of nB (recall nB is the relative frequency of event B occurring alone), the higher the ratio of the equation above will be (yelding 1 when nB is zero, i.e., event B occurs only when event A occurs). If B occurs more often alongside with A than alone, then the probability of A occurring when B occurs will be higher.

Likewise,

Number of occurrences of  event B, given A

nA|B and nB|A may also be denoted P(A|B) and P(B|A), respectively. P(A|B) is read as “the probability of event A, given B”.

Given the above equations, we observe that

Number of occurrences of events A and B

or

Probability of events A and B

Now we’re ready to understand Bayes’ Theorem

Bayes’ Theorem

As mentioned in the beggining of this post, Bayes’ Theorem states that:

Bayes' Theorem

Now, here’s how you should interpret it. A better way to visualize it is to write it as

Another way to visualize Bayes' Theorem

given the last equation from the previous section. The explanation is similar to the one given for the equation for nA|B in the previous section. If P(A) is close to P(AB) (recall P(A) is the probability of event A alone or alongside with event B), that means most occurrences of event A happen when event B also occurs. From that, we can intuitively conclude that event B will very likely occur given event A occured, since the occurrence of event A is strongly related to the occurrence of both A and B.

The explanation above can be expressed in terms of very informal (but I believe reasonable) logical statements:

1. A and B may occur
2. A tends to occur only when B also occurs
3. A occurred
4. It’s likely B will also occur

Note: this explanation is strongly based on the online tutorials for Digital Image Processing, by Gonzales & Woods. I used the same notation and terms. However, my objective here was to add the clarifying (at least for me!) explanations.

Ternary operator in Python

Here’s how you translate the following C code

int max(int a, int b)
{
    return (a > b ? a : b);
}

to Python:

def max(a, b):
    return a if a > b else b

The general syntax is:

TRUEVAL if CONDEXPR else FALSEVAL

Note: I’m not 100% sure, but I think that only works starting from Python 2.5.

Using feof() and fread()

When reading single bytes from a file in C, one must pay attention to the correct usage of feof() and fread(). At first, the following piece of code seems to work correctly:

const char *filename = "hello";
unsigned char byte;
FILE *fp;
 
fp = fopen(filename, "rb");
 
if (!fp) {
    printf("could not open file\n");
    return 1;
}
 
while(!feof(fp)) {
    fread(&byte, 1, 1, fp);
    printf("%02x\n",byte);
}
 
fclose(fp);

Suppose the file “hello” has the following contents:

0000000: 68 65 6c 6c 6f 0a                                hello.

(which is the string “hello” followed by an LF)

When the code above is run, the following output is produced:

68
65
6c
6c
6f
0a
0a

Notice the last character seems to be read twice. The problem is that feof() only returns true after attempting to read past the end of the file. In order to fix this “read-twice” behavior, the return value of fread() must be checked:

if(!fread(&byte, 1, 1, fp)) {
    break;
}

Note: Using feof() as the while condition is kind of redundant here. In this situation, one could simply use while(1) and the behavior would be the same.

Update: A much better solution was given by my friend Bryan:

const char *filename = "hello";
unsigned char byte;
FILE *fp;
 
fp = fopen(filename, "rb");
 
if (!fp) {
    printf("could not open file\n");
    return 1;
}
 
fread(&byte, 1, 1, fp);
while(!feof(fp)) {
    printf("%02x\n",byte);
    fread(&byte, 1, 1, fp);
}
 
fclose(fp);

Don’t forget that virtual

I am not really a C++ programmer. I usually code in C, and I think all C++ I’ve ever written involved a couple of vectors and maybe one or two classes. So what I’m writing here is certainly old news for C++ programmers.

The other day, while I was reading this essay about the Liskov Substitution Principle, I was intrigued by a situation presented in page 4, which deals with class inheritance and method overriding.

Methods that can be overriden must be declared virtual in C++. I really didn’t know about that. I am used to Java’s behaviour, which I illustrate below:

/*
 * A.java
 */
 
public class A {
 
	public void bar() {
		System.out.println("bar");
	}
}
 
/*
 * B.java
 */
 
public class B extends A {
 
	public void bar() {
		System.out.println("Bar!");
	}
}
 
/*
 * C.java
 */
 
public class C {
 
	public void call(A o) {
		o.bar();
	}
}
 
/*
 * Main.java
 */
 
public class Main {
 
	public static void main(String [] args) {
		A a = new A();
		B b = new B();
		C c = new C();
 
		c.call(a);
		c.call(b);
	}
}

The code above will produce the following output:

bar
Bar!

If I wanted to do the same thing in C++, I would have to write it this way:

#include <iostream>
 
class A {
public:
	A() {}
	virtual ~A() {}
	virtual void bar() { std::cout << "bar" << std::endl; }
};
 
class B : public A {
public:
	B() {}
	virtual ~B() {}
	void bar() { std::cout << "Bar!" << std::endl; }
};
 
class C {
public:
	C() {};
	virtual ~C() {};
	void call(A &o) { o.bar(); }
};
 
int main(int argc, char *argv[]) {
	A a;
	B b;
	C c;
 
	c.call(a);
	c.call(b);
 
	return 0;
}

The output will be the same as the Java program, but pay attention to the virtual modifier placed before the declaration of the bar method in A. If I remove it, the output will be:

bar
bar

Even though B declares a method called bar, it is completely shadowed by the implementation inherited from A. In my opinion, Java’s behaviour is a lot more intuitive. I wonder how many people might have spend hours, maybe days, looking for a bug in a C++ program when the problem was that a method was not declared virtual and method calls weren’t occuring as expected.

I talked about this with Otávio, and he found out C# has a similar characteristic. In C#, a method must be declared as virtual for its subclasses to override it AND the subclasses must use override when overriding the method. Here’s the code he provided me:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication1
{
   class Alice
   {
       public virtual void sayHello()
       {
           Console.WriteLine("Hello, world");
       }
   }
 
   class Bob : Alice
   {
       public override void sayHello()
       {
           Console.WriteLine("Hello, world!");
       }
   }
 
   class Echo
   {
       public void say(Alice toto)
       {
           toto.sayHello();
       }
   }
 
   class Program
   {
       static void Main(string[] args)
       {
           Alice a = new Alice();
           Bob b = new Bob();
           Echo e = new Echo();
 
           e.say(a);
           e.say(b);
       }
   }
}

Some people argue that this makes code clearer, but I’m not convinced. Why not make overriding implicit?

SWI-Prolog Easter Egg

swi-prolog-easteregg

Found it by accident :)