Older blog entries for mchirico (starting at number 22)

Unfair

The sign function, yes the function that returns 1,0, or -1 for values greater than 1, 0 and less than zero has not been given the credit it deserves

      sign(4)=1
      sign(0)=0
      sign(-12)=-1

I know, you think it's a silly useless function. Well, it can define abs(). Yes, the absolute value can be defined as the sign() of a number times itself, or the following.


sign(x) (x) = abs(x)

Do you see what I mean? See how powerful it is? We can now define condition statements, find maximum values, and do max min comparison. Hmm...you still don't believe me? Here, see for yourself.

          
          1 - abs(sign(x-y)) is equal to 0 if  x > y

1 - abs(sign(x-y)) is equal to 1 if x = y

1 - abs(sign(x-y)) is equl to 0 if x < y

So with the above expression, you will get a 1 if x is equal to y. For shorthand Delta[x=y] can be replaced with 1 - abs(sign(x-y)). Now note that all the other Delta's can be defined as follows.


Delta[x=y] = 1 - abs(sign(x-y))

Delta[x!=y] = abs(sign(x-y))

Delta[x<y] = 1-sign(1+sign(x-y))

Delta[x<=y] = sign(1-sign(x-y))

Delta[x>y] = 1-sign(1-sign(x-y))

Delta[x>=y] = sign(1+sign(x-y))

And, it's possible to return the max or min value. And you don't need no stinking temperary variable.

  
         Max(x,y) = x + (y - x)* Delta[x<=y]
                   or
         Max(x,y) = x + (y - x)* ( 1-sign(1+sign(x-y)) )

or with x=5 and y=4

Max(5,4) = 5 + (4 - 5)* ( 1 - sign(1+sign(5-4)) )

Max(5,4) = 5 + (4 - 5)* ( 1 - sign(1 + 1)) Max(5,4) = 5 + (4 - 5)* ( 1 - 1 ) Max(5,4) = 5 + (4 - 5)* ( 0 ) Max(5,4) = 5

Ok, So What

It's not just a cute math problem. This can be used to solve complex SQL queries in a single pass because these Delta conditions can be done before the where expression or in the having expression.

Here's a quick example done in MySQL.

         CREATE TABLE exams (
             pkey int(11) NOT NULL auto_increment,
             name varchar(15),
             exam int,
             score int,
             PRIMARY KEY  (pkey)
           );

insert into exams (name,exam,score) values ('Bob',1,75); insert into exams (name,exam,score) values ('Bob',2,77); insert into exams (name,exam,score) values ('Bob',3,78); insert into exams (name,exam,score) values ('Bob',4,80); insert into exams (name,exam,score) values ('Sue',1,90); insert into exams (name,exam,score) values ('Sue',2,97); insert into exams (name,exam,score) values ('Sue',3,98); insert into exams (name,exam,score) values ('Sue',4,99); mysql> select * from exams; +------+------+------+-------+ | pkey | name | exam | score | +------+------+------+-------+ | 1 | Bob | 1 | 75 | | 2 | Bob | 2 | 77 | | 3 | Bob | 3 | 78 | | 4 | Bob | 4 | 80 | | 5 | Sue | 1 | 90 | | 6 | Sue | 2 | 97 | | 7 | Sue | 3 | 98 | | 8 | Sue | 4 | 99 | +------+------+------+-------+ 8 rows in set (0.00 sec) mysql> select name, sum(score*(1-abs(sign(exam-1)))) as exam1, sum(score*(1-abs(sign(exam-2)))) as exam2, sum(score*(1-abs(sign(exam-3)))) as exam3, sum(score*(1-abs(sign(exam-4)))) as exam4 from exams group by name; +------+-------+-------+-------+-------+ | name | exam1 | exam2 | exam3 | exam4 | +------+-------+-------+-------+-------+ | Bob | 75 | 77 | 78 | 80 | | Sue | 90 | 97 | 98 | 99 | +------+-------+-------+-------+-------+ 2 rows in set (0.00 sec)

Note, the above pivot table was created with one select statement

You may think IF's would work. And that should be true, but it is not the case for MySQL 4.1 There is a bug in that version of MySQL.

SQLite, my favorite database, does not natively support the sign function. But, you can easily create it. Anyway, if you're looking for more examples, then, you might want to check here [See LONGWINDED TIPS]. These examples only scratch the surface of what can be done.

Compile-time polymorphism using templates

I did not know it was possible to do this. Normally you build a base class with virtual functions, and then, derived classes inherit these functions

It is legal C++ and will always work provided p and q have member functions x and y that can be called without parameters.

/* Compile-time polymorphism using templates


"Take a look at Exceptional C++ Style: 40 New Engineering Puzzles, Programming Problems, and Solutions", by Herb Sutter

See Item 5 in the book.

*/ #include <iostream> using namespace std; class A{ public: void x( int a=0) { cout << "A.x" << endl; } void y( int a=0) { cout << "A.y" << endl; } };

class B{ public: void x(int a=0, double b=5.0) { cout << "B.x a,b:" << a << " " << b << endl; }

void y(int a=0, double b=5.0) { cout << "B.y a,b:" << a << " " << b << endl; } };

template<typename T> void h(T& t) { t.x(); t.y(); }

int main(void) { A p; B q;

h(p); h(q); }

SQLite 3

SQLite version 3 went stable last night. I love that database. Cannot argue with the licensing model - public domain. You can do anything you want with it.

17 Sep 2004 (updated 17 Sep 2004 at 04:35 UTC) »
Google Math Puzzle

http://7427466391.com


      Congratulations. You've made it to level 2. Go to
      www.Linux.org and enter Bobsyouruncle as the login and the answer to this
      equation as the password.


f(1)= 7182818284 f(2)= 8182845904 f(3)= 8747135266 f(4)= 7427466391 f(5)= __________

This was the answer to this question

The second question above is interesting. All the numbers come from E. Note the last number in f(1) is a 4. The 4th position in E, after the decimal, and is the start of f(2). Now, the first number in f(2) is an 8. And 8 places forward from f(2) gives you f(3). OK, the last 2 digits of f(3) is 66. Moving 66 places forward from the last digit in f(3) gives you f(4). So, I'm guessing that moving 74 digits after the last digit in f(4) will give you f(5).

For f(5), I get 9880753195

But get this! Someone has locked the account. on www.Linux.org. If you do not get it in 3 tries it locks

Here is E for reference. Maybe I made a mistake counting. In truth, I was doing it on my fingers -- wife is cooking needs the computer for a recipe.

2.7182818284590452353602874713526624977572470936999595749669676277240766303535\
475945713821785251664274274663919320030599218174135966290435729003342952605956\
307381323286279434907632338298807531952510190115738341879307021540891499348841\
675092447614606680822648001684774118537423454424371075390777449920695517027618\
386062613313845830007520449338265602976067371132007093287091274437470472306969\
772093101416928368190255151086574637721112523897844250569536967707854499699679\
468644549059879316368892300987931


13 Sep 2004 (updated 13 Sep 2004 at 05:40 UTC) »

This GraphViz is fun to play around with. You can create complex undirected graphs, at the command line, with neato, or directed graphs with dot. It will output png, jpeg, and a few other formats

The Linux Journal article on GraphViz has a good introduction.

To download the software, visit GraphViz Site

10 Sep 2004 (updated 10 Sep 2004 at 02:35 UTC) »
Serving The Community -- Well Not Yet.

I wanted to write an article exploring filesystems (rough draft). But show users how to do this without reformatting a drive; instead, create a file and mount it just like a physical device. In essence, create a virtual filesystem. Along the way I wanted to include encryption and ACL (Access Control Lists).

OK, the article appears technically correct, but it does not sit well with me. If go into more detail, it becomes a marathon to read. Fedora core 2 has a lot of nice features, so I took the easy way out and started from that base. But not everyone runs Fedora core 2. I would love to explain it from compiling the 2.6 kernel source. But, that adds in a major topic.

I have the same problem with the Working With Time Article . It doesn't flow.

Maybe I should do the following: write the articles for myself and make it a complete reference, then, carve away all the excess fat. The bloat then diet method.

27 Aug 2004 (updated 27 Aug 2004 at 03:51 UTC) »
The 61 Second

For me the fascination started with a second. The 61 second. I tried to ignore it; but, when I put on my running shoes and stared my morning run, I kept thinking about it. See Time Servers, with their impressive 64 bit NTP timestamp start time in 1900 - exactly 2208988800 seconds before Unix time began, which we know to be Jan 1970 UTC.

This makes for a simple conversion NTP - 2208988800 = Unix time in seconds. But what happens during a leap second? All clocks on modern computers support 61 seconds in a minute to make up for this extra second. We either loose this simple formula for conversion, by tacking on the extra leap second to the NTP master clock, or we go back in time; the same second is repeated. As it turns out. We go back in time. Yep, during a leap second, the NTP master clock repeats itself. The same second rolls over twice. Anyway you look at it that 64 bit timestamp repeats itself. So, by the clock we go back in time. Seems like I said that before.

This gnawed at me. It's not our fault. Here we are so advanced with our cesium master clocks and hydrogen master clocks, which loose less than a second every 1.4 million years, that we have out done any natural event. Nothing in nature produces such precise signal intervals. It's nature. We add in a leap second to excuse nature, because the rotation of the earth has its imperfections, caused in part by the salinity of the ocean water.

But wait. If nothing in nature produces such precision, haven't we identified what signal alien life should send us, and what signal we should be sending them? A simple signal with precise intervals. It would certainly draw attention on both sides. It is almost as if nature has cleared the bandwidth for us to communicate.

So I downloaded the Equation of Time and a few conversion formulas, calculated sunset for my latitude and longitude. I set my watch to my computer clock, which keeps accurate time to within 1 to 50 ms of a (stratum 2) Time Server. Then I went out and watched the sunset. It was wonderful. The imperfections of nature have allowed for each sunset to be truly unique.

Here's a copy of the sunrise/sunset program -- Look for sunset.c You must put in the date, latitude and longitude in degrees. Convert seconds and minutes to degrees.

$ ./sunrise `date "+%Y %m %d"` 39.95 75.15
Sunrise  08-26-2004  06:23:06   Sunset 08-26-2004  19:40:41

Freshmeat published my article on the Lemon Parser Generator. I'm hoping it will help others explore this tool.

I've been reading Don Knuth's Volume 4 of The Art of Computer Programming. It's a free download; and, if you find a better solution to an exercise, he'll put your name in the book.

MIX. Does that name sound familiar? GNU MIX is my top pick on sourceforge.

Need a Parser? Try this lemon

The lemon parser generator is thread-safe, small (only 2 files of source code), very well tested since it's been well used in SQLite, and it's in the public domain. Since it's in the public domain, it's a perfect fit for your project license.

I've create 4 very simple samples here , since the documentation on lemon is a little light. Although, it is helpful to read the SQLite source code to see how lemon works, you may want to take a quick test drive with these samples first. They are simple, so you can get something to work with immediately.

Now lemon is a parser, or Bison replacement. You'll still need flex or a custom lexical analyzer that can pump tokens into the lemon parser. Hopefully, example 4 will answer some of those questions. When you read it, you'll see I compiled flex under C instead of using flex++ command. It may be faster this way. The main part of the code uses C++, so this is a quick demo using _extern "C"_ statements.

3 Aug 2004 (updated 3 Aug 2004 at 02:41 UTC) »
Lost In Time

There is no way to calculate, down to the second, what time it will be 50 years from now. Nope, cannot do it. You can only guess at the number of leap seconds that must be added.

UTC "Coordinated Universal Time" must be less than 0.9 seconds of UT1. This UT1 is the mean number of seconds in a day, which is decreasing. Yes, days are getting longer because of tidal friction caused by the sun and moon. This is why there are sometimes 61 seconds in a minute. The extra second can only be added at the end of the month, with preferences for June and December; but, at the end of any month to prevent UT1-UTC becoming greater than or equal to 0.9 seconds.

Here's the actual data from a few days ago. As of 7/30/2004 it was off by -0.45501 seconds.

        MJD      x(arcsec)   y(arcsec)   UT1-UTC(sec)
       2004  7 30  53216       0.0688      0.5179     -0.45501
       2004  7 31  53217       0.0714      0.5176     -0.45441
       2004  8  1  53218       0.0739      0.5173     -0.45407
       2004  8  2  53219       0.0765      0.5169     -0.45402
       2004  8  3  53220       0.0791      0.5164     -0.45423
ftp://maia.usno.navy.mil/ser7/ser7.dat

By the way, this has nothing to do with leap years -- that is the rotation of the earth around the sun. The rotation varies a lot less than the mean length of a day.

This slowing down effect is somewhat variable. And must be checked with astronomical observations. Take a look at the ftp site above. You'll see the formula there that offers an approximation.

Interesting note, GPS (time of Global Positioning System satellites), does not adjust for leap seconds. Currently GPS is ahead of UTC by over 13 seconds.

References:

http://www.ucolick.org/~sla/leapsecs/dutc.html#atomic.png
http://tycho.usno.navy.mil/systime.html
http://www.ien.it/luc/cesio/itu/ITU.shtml
http://www.usno.navy.mil/

Interesting article in Sunday's New York Times about cockroaches. In 1979 about a million, swarming, German cockroaches left bites all over a 64-year-old woman and her 24 dogs. Hmm...who lives with 24 dogs in an apartment? Later, 6 years later, according the article, cockroaches had infiltrated the Pentagon. There are roaches in government too -- a fact the article missed.

Anyway, that's not the interesting part of the story. Apparently those were the good old days for cockroaches, who were once though to be indestructible, until American Cyanamid developed hydramethylnon. By the way German roaches are considered the worst...indestructible to DDT and almost any other chemical thrown at them since 1950, that is, until this hydramethylnon came along. What was once thought to be a problem with hydramethylnon; the fact that roaches had to ingest the product, turned out to be a blessing. Cockroaches would ingest the chemical; but, it would remain in their carcasses and feces, which other cockroaches would feed upon. Reports of 90 to 95 percent reductions in cockroach populations were cited across the country. Good read. By the way ants are now more of a problem.

Here's the link...registration is free http://www.nytimes.com/2004/07/25/magazine/25PHENOM.html?pagewanted=1

13 older entries...

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!