viernes, 30 de julio de 2010

Genius + Brute Force = Ingenious

These two statements have been branded with fire in my mind:
  • Genius find a solution instantly
  • Brute Force searches for a needle in a haystack
Sometimes we need to generate random numbers. History has kept evidence of Dice and Coin toss as methods of generating random choices.

Donald Knuth invented algorithms for pseudo-random number generation.

Edsger Dijkstra stated that there are two approaches to solve computer problems: Mozart Vs. Beethoven. (As a curious fact, Mozart and Beethoven met each other in 1787) The former is a genius, but the latter works by brute force.

In India, Pachisi and Chaturanga were played with Dice. Orthodox Chess can be also played with dice, but Chess isn't normally played that way. The praise of what is random have been lost.

In Watchmen movie, Dr. Manhattan talks about the "Pleasure of Randomness" If there's an origin for that phrase, I'd like to find it out. Also, some levels of randomness are required to produce Fine Arts.

To try to get in touch with that "Pleasure of Randomness", I decided to create a little program that generated three numbers between zero and one-hundred inclusive. Then, I will use only the first two numbers and some operations to generate a result equal to the third number. For example, if those three numbers where {2, 3, 6}, then (2+3)+(3-2)=6. Of course that 2*3=6, but what would you do if numbers were {53, 92, 59}? This is what I did, using Python IDLE:
## OUR THREE RANDOM NUMBERS

a=53 ; b=92 ; c=59 ;

## EXPLORING NUMBERS
92-53 = 39   (b-a)
53-39 = 14 a-(b-a)
39-14 = 25 (b-a)-(a-(b-a))

25-14 = 11 (b-a)-(a-(b-a)) - (a-(b-a)) //???

92-11-11-11 = 59 //MAGIC!

b \
-((b-a)-(a-(b-a))-(a-(b-a))) \
-((b-a)-(a-(b-a))-(a-(b-a))) \
-((b-a)-(a-(b-a))-(a-(b-a))) \
== c

## SIMPLIFICATION

>>> ((b-a)-(a-(b-a))-(a-(b-a)))
11
>>> ((b-a)-(a-b+a)-(a-(b-a)))
11
>>> (b-a-a+b-a-(a-(b-a)))
11
>>> (b-a-a+b-a-(a-b+a))
11
>>> (b-a-a+b-a+(-a+b-a))
11
>>> (b-a-a+b-a-a+b-a)
11
>>> b-a-a+b-a-a+b-a
11
>>> 3*b-5*a
11
>>> (9*b-15*a)
33
>>> b-(9*b-15*a)==c
True
>>> b+(-9*b+15*a)
59
>>> b-9*b+15*a
59
>>> 15*a-8*b
59
Looking at this simplification, some questions pops out from my mind: Would a genius had gotten an equation like this from the start? Would have a genius known that a solution found using this method could be simplified without even trying to solve one problem in the first place? I don't know, but I'm sure I'm not a genius.

This simple game taught me some lessons:
  • Pseudo-Random exploration is helpful to find a solution.
  • Be ware of the word 'repeat'. It could hide a mathematical operation.

---
20100729T0948:
round a problem many times
look for hindrances, obstacles, inconveniences
re-round each found obstacle and find inner obstacles
until one obstacle is solved
then, that solution will solve another
and so, like a domino effect,
the first and original problem
starts to get solved
- [id, status, created, modified, position, modcount]

20100729T1125:
writing undefined sentences to describe the problem
making calculations with parameters to find a result
opening many files to understand and solve the code

20100729T1143:
TDD to automate tests "seems" the way to go.
GUI needs to be scriptable, and HTML allows this.
Maybe JavaScript could be a problem, but if you could
append a [SCRIPT] tag and run it, it will be no problem

Enterprise, Human Resources, People and Wars:
- If you kill people, you must find and train new ones.
- If you forgive people, they will squeeze you.

martes, 20 de julio de 2010

SQL = Sets Quarantined, Lost

I have two tables in a MySQL database. They both have persons, but BOTH tables have persons that are not in both. So, I wanted to bring all those people that are not repeated. I can do this using inner loops, but I tried to do it by MySQL alone.

I elaborated an example with two sets: t1=[a,b,c]; t2=[a,b,d]. What I want to bring is this set: r=[c,d]; because [a,b] can be retrieved by a simple INNER JOIN.


#TEMPORARY TABLES WE WILL USE
#TABLES WITH ELEMENTS WILL BE: t1 AND t2

DROP TEMPORARY TABLE IF EXISTS t1,t2,plus,minus;

#FIRST DATA SET
CREATE TEMPORARY TABLE t1 AS
SELECT 'a' AS f1 UNION SELECT 'b' UNION SELECT 'c';

#SECOND DATA SET
CREATE TEMPORARY TABLE t2 AS
SELECT 'a' AS f2 UNION SELECT 'b' UNION SELECT 'd';

#CREATING THE "UNION"
CREATE TEMPORARY TABLE plus AS
SELECT * FROM t1
UNION
SELECT * FROM t2;

#CREATING THE "INTERSECTION"
CREATE TEMPORARY TABLE minus AS
SELECT t1.* FROM t1
INNER JOIN t2
ON t1.f1=t2.f2;

#CHECK OUR TEMPORARY TABLES
SELECT * FROM t1; #FIRST TABLE
SELECT * FROM t2; #SECOND TABLE
SELECT * FROM plus; #ALL ELEMENTS
SELECT * FROM minus; #COMMON ELEMENTS

#AND THIS IS THE OPERATOR I NEED IN MYSQL
SELECT plus.* FROM plus
LEFT JOIN minus
ON minus.f1=plus.f1
WHERE minus.f1 IS NULL

/*
AN UNION MINUS AN INTERSECTION BRINGS ALL RECORDS
THAT ARE NOT PRESENT IN ANY SET

YOU SEE, VENN DIAGRAMS AND SET THEORY ARE TOO OLD SCHOOL
THAT THEY SEEM NOT NEEDED NOWADAYS. BUT NOW,
OBJECT-ORIENTED ANALYSIS AND DESIGN IS, AGAIN,
BRINGING ALL THOSE OLD-SCHOOLERS BACK!!
*/

domingo, 11 de julio de 2010

Common Sense = Prejudices

"Common sense is the collection of prejudices acquired by age eighteen."
Albert Einstein

There are faults that we allow because they are logical and common sense. This is an example of how a bad action can be seen as good, and that there's no absolute true.
  1. Mixing milk with water
  2. Mixing flour and yeast to make bread
  3. Accepting or giving "payment" to correct a legal "fault"
  4. Drinking "juice powder" instead of natural fruits
  5. Forbidding abortion on EVERY possible situation.
  6. Cutting big trees because they interfere with power lines
  7. Keeping the extra-change mistake because they won't give it back if the mistake was ours.
  8. Not asking questions; but instead, either requesting or begging for answers.
Sadly, I see some of these topics as 'Necessary Prejudices' in some aspects too.

jueves, 1 de julio de 2010

Frameworks = Mathematical Placebos

Thanks to Online LaTeX Equation Editor. Very helpful for "The Placebo Formula"
---

Programming Frameworks are like something I have called as Mathematical Placebos, extra data that we add to equations to help us reach its results.

This extra data bloats the language ahead of the future. We tend to find many gotchas before and while we are working on a code, and they will delay code delivery.

What started easy, now is not.

To give a mathematical example, a very interesting equation, Euler's Identity, goes like this:



LaTeX: e^{i\pi}+1=0
PHP: it doesn't have built-in calculations for imaginary numbers.
Python: print (math.e**(math.pi*1j))+1

The result is this Python expression is not exactly 'zero', but very near to it. This is a common catch in floating point calculations. ;-P

Do you imagine what amount of tests Leonhard Euler did, in order to find out that relation of constants returns -1?

What I call "The Placebo Formula" (numbers I recall when I solve fractional, algebraic, or exponential equations), goes like this:



LaTeX: \frac{\sqrt[1]{\left(\textbf{1}\right)^{1}}}{1}+0=1
PHP: echo pow(1.0,1.0/1.0)*1.0/1.0+0.0;
Python: print 1.0*1.0**(1.0/1.0)/1.0+0.0

Do you get the idea of all those extra artifacts in both placebo formula and frameworks? You have to remind yourself that if you see a simple "1" in a formula, you can add any of those pseudo-numbers to ease your calculations.

In programming, this effect is also achieved when you create a common Object class for every object, including those objects not yet discovered or planned. When you have to add a behavior to all objects, you add it to that base Object class.

---
PS: I annotated what I need to do in this post instead of actually doing it! (e.g. I typed "use an image of euler's identity and wikipedia link") It is always easier said than done, although this action can prompt you to do a better organization of the way in which you express your thoughts. Write for yourself!

Evolution of Coding

It all started so simple:
  • George Boole, and his Boolean Algebra.
  • Claude Elwood Shannon, and his Information Theory.
  • Alan Mathison Turing, and his Turing Machine.
  • Charles Babbage, and his Programmable Computer.
  • John von Neumann, and his Computer Architecture.
I tried to put this in the normal order of understanding for people, but actually I will use the order on which I normally encounter the problem.
  • Every computer problem is composed by 'only' three main parts: input, process, output.
  • The input part needs both a layout and an interface to look nice.
  • The main process must be structured, and it should support alternate logic to handle possible exceptions.
  • The output also needs a layout specifically designed for screens, papers and files.
  • Not all users will be allowed to use this program. So an authentication system is required. Also, it should allow many users, each one with possible different roles and levels.
  • In the input part, users commit many mistakes, so validation is needed for each data that program receives.
  • To ease distribution of the program, and to centralize the data store, a client/server structure is defined.
  • Many users in different remote location have different languages. So, programs now need to support different types of languages, like english, spanish, etc.
  • Good database software and modern platforms have been developed. So, it should be useful to make programs able to migrate to them. (db, os, platform, web, mobile, etc.)
  • It's hard to create a migrable program, so we should separate our development into three layers or tiers: back,middle,front (A.K.A M.V.C.)
  • There are several programming languages on which we can express many of these principles easier (C++, Java, etc.), so we should migrate our programs to them.
  • We are losing performance as our user base grows up. So, we should test concepts like replication.
  • Some foreign programs and systems also want to have access to our program. So, we should create Web Service or open ports for Socket programming.
  • It's probable that, with all these changes, we have left some doors open because of lack of sanitation. We must protect our programs against Session Hijacking, Cross-Site Scripting (XSS), and several types of Injections (SQL, HTML, CMD)
  • Many users access the same resource simultaneously, and that generates either deadlocks or data inconsistency. So, we should program taking into account concurrency and multiple threads of execution.
  • Our application is growing too much. So, we should apply concepts like distributed/cloud computing, to make our programs and database expandable by simply buying extra hardware.
  • OOP, EXCEPTIONS, CHANGE (files,interrupt)
Now, there's a question: What's next? How could we magically and instantly create or adapt a program to any change we still don't know? A possible simple change might touch EVERY AREA of our program! And a touch of that kind needs TIME to be checked in every area we have created. That's the problem known as Essential Complexity.

---
PS: IPO (Input-Proccess-Output) ... Hajime no 'IPO' ;-P