6.18.2010

Microsoft Security Essentials

If you're looking for an anti-virus along with malware/spyware protection and you require all these things to be free on the Windows platform - take a look at Microsoft's Security Essentials. From the reviews I have read, it certainly seems like it will loosen the grips of the stranglehold that the two top AV software companies currently have on the market. I mean, how can you compete with free?

5.04.2010

A SQL Server Cursor example

Every now and then the need to write a SQL Server cursor comes up.  Since I find myself going back through a labyrinth of code when this happens, I thought it would be a good idea to post it here.  That's if I remember to look here the next time I need to create one - which just might be several seasons later.

*The humorous thing about this is that as I went to SQL Server Management Studio to copy the cursor example, it was gone.  Apparently, I closed the script window immediately after running it thinking I wouldn't need to use this in a long time.  No worries though, I wrote this up again in a short time.


declare @SOMEID int
declare appcursor cursor FAST_FORWARD
FOR SELECT Table1ID FROM tblTable1

open appcursor
fetch next from appcursor into @SOMEID
while @@fetch_status=0
begin

delete 
from tblWhatever
where WhateverID = (
select top 1 WhateverIDFK
from tblSomewhere
where SomeID = @SOMEID
order by DateOfEntry
)

fetch next from appcursor into @SOMEID

end
close appcursor
deallocate appcursor

One thing to note is the FAST_FORWARD hint.  You should always declare this when you need a forward-only, read-only result set.  FAST_FORWARD allows you to traverse the dataset with the least amount of overhead.  Cursors are generally slow because of the nature of their use.  Most times, you are forced to use a cursor because you have seemingly no other way to perform certain database actions, which frequently require small units of work like in the example above.  In the case where you need to update a result set, consider using FORWARD_ONLY (only use fetch next).  Please see this MSDN article for more information on cursor types.

3.10.2010

1024-bit RSA encryption cracked

A simple glance of this article's title (below) made my heart beat jump, literally. A quick google news search led to the legitimacy of this claim. Another title from gadgetsteria.com starts with "Good-bye earth: RSA encryption cracked when CPU put on digital diet…."

http://www.engadget.com/2010/03/09/1024-bit-rsa-encryption-cracked-by-carefully-starving-cpu-of-ele/

"By fluctuating the voltage to the CPU such that it generated a single hardware error per clock cycle, they found that they could cause the server to flip single bits of the private key at a time, allowing them to slowly piece together the password." 
It took 81 P4 chips and 104 hours of cpu time to achieve this feat.  This is a scary thought knowing that it will only be a matter of time when a single cpu with that computing power will be able to fit inside the confines of a netbook.  I'm about halfway done with this blog post and my heart is still beating at an accelerated rate.

RSA is an algorithm that Rivest, Shamir and Adleman first described in 1978. It is a public-key encryption method that is widely used for protecting all sorts of information, especially in the e-commerce space. It relies on the extreme difficulty of calculating the factors of very large numbers that only have two prime factors. The public key is exactly that, a product of two large primes. A 128-bit key (the product) can be a number from:

1 to 2^128
or
1 to 3.4028236692093846346337460743177e+38
or
1 to 340,282,366,920,938,000,000,000,000,000,000,000,000 

Now, the 1024-bit encryption that was just cracked has a public key that is MUCH larger.  It can be a number from:

1 to 2^1024
or 
1 to 1.797693134862315907729305190789e+308
or
(figure it out yourself)

Conventional wisdom told us that as long as we have keys long enough to withstand any brute force attack with the most powerful supercomputers of the time, cracking an encryption algorithm would simply be impossible.  But, this broke all the rules. They bypassed everything and went straight down to the cpu level. How did these guys at the University of Michigan have the prescience to do this? I don't know, but I am impressed and still very scared. 

3.01.2010

Subversion - an open source version control system

Subversion, you are simply awesome! You are as elegant and graceful in version control as Kim Yu-Na (김연아) is on the ice. Need I say more? I didn't think so.

Here is my version control setup on the Windows platform:

  • Subversion (creating a repository)
  • TortoiseSVN (windows shell program)
  • AnkhSVN (visual studio integration)
This is a typical setup for the Windows/Visual Studio user. Instructions (with pictures) can be found here. Thanks Matteo.

I've been recently tasked with moving an svn repository to another machine and I had no idea where to start.  So I did a quick search and found the "Relocate" option in the TortoiseSVN menu. Ok I'm done. That was easy. Now that I have all this free time, I'll go watch some more Queen Yu-Na videos...


Here's an interesting question/answer about SVN Relocate or Switch on Stackoverflow.

2.25.2010

The Tuple Type in the Microsoft .NET Framework 4.0

My first impression of the new System.Tuple type in the .NET 4.0 Framework was not a good one. Probably because at first look it seems like an implementation of the already existing System.Collections.Generic KeyValuePair. As you would expect, Microsoft had a few compelling reasons for this change.

Many of the MS language teams each created their own private version of the tuple. After some time, the teams within MS learned of this and wanted to use it in their code as well. After collaboration between the Base Class Libraries (BCL) and the Managed Extensibility Framwork (MEF) teams, they decided to include it in the upcoming Framework release.

So we know that MS uses tuples in their code. I can not even remember the last time I used KeyValuePair outside the use of a Dictionary collection. As I am writing this, I can think of a few ways I could have used KeyValuePair more in my code.

With the upcoming release of F# language, interoperability was paramount. Having different representations of a tuple in C# and F# would have led to coding woes. As it says in Microsoft's documentation, "any time you wanted to call a method from an F# assembly that took a tuple as an argument, you would be unable to use the normal C# syntax for a tuple or pass an existing C# tuple. Instead, you would be forced to convert your C# tuple to an F# tuple, and then call the method." Tuples are a core concept in functional languages, so it doesn't hurt to be exposed to it.

Some more interesting things about Tuples:
There is a maximum of 8 elements in a tuple. The eighth tuple must be of type tuple, making it limitless.
Tuples are reference types.

12.18.2009

The transition from C# to VB

For not having written any visual basic code in about a year, the transition from C# back to VB has been a slightly rough one.  I noticed an endless barrage of squiggly lines indicating syntax errors have plagued my code.  VB's compile-time syntax checking in Visual Studio didn't help either.  It let me be even more aware of my failure to comply with VB's rules.

The biggest offender was in my variable declaration.  Yes!  In the variable declaration!  I shall declare all my variables with a Dim.

I shall declare all my variables with a Dim.
I shall declare all my variables with a Dim.
I shall declare all my variables with a Dim.
I shall declare all my variables with a Dim.
I shall declare all my variables with a Dim.
All your base are belong to us.

The point here is, like that of spoken languages, computer languages too need to be learned and perpetually practiced. Just like that violin you quit playing the moment you got accepted to a good university.  Don't neglect it or you may never get it back.

Ok, that's being overly dramatic.  Fine.  But, honestly, going from language to language is more of a nuisance than anything else unless you program in all of them everyday.  I quickly got over the Dim thing. I got used to the absence of the symbols in the beloved C-Style language: { } [ ] ; . I didn't like the fact that I had to google everything from "VB ternary operators" to "VB linq to sql examples."  I'm pretty good with Linq in C#, but in VB it seems so foreign to me. Man, I wish I saved my search history. But, in the end everything got done... with a 30% drop in productivity. Just kidding.

As for the long standing C# vs VB debate, here's my view.  Who cares?  For those of you who share the same passion as I do, we can have a language preference but that doesn't mean should refuse to work on projects written in a different languages.  Besides the fact that we might get fired, it's just not right.  We programmers are taught to learn to how learn.  Our programming languages course showed us how to adapt to other languages.  Where's the fun in exploring new ways to the do same thing?  So what if C# developers get paid more than their VB counterparts on average? Wait, I do.

12.16.2009

Backpropagation in Neural Networks

Backpropagation is a form of Supervised Training which teaches a Neural Network how to work and operate.  The training is done prior to using the network and works only for feed-forward networks.  There are many other ways to train a Neural Network, even in unsupervised ways, but Backpropagation is a widely popular training method because of its "learn by example" applicability in many real world cases.  This kind of network operates on the premise that given an input, it will produce the known and "correct" output.  This is analogous to training a cell phone to recognize your voice and how you pronounce certain words.  So you can train a network with inputs and what their corresponding outputs should be.  You can't train a network, however, to decipher what your cat's mood is at any given point.  Maybe someday?

Let's look into the well known XOR example.  We all know what this is right?  The bitwise Exclusive Or produces a known and correct output given two inputs as shown below.  Download the source code / demo for the Exclusive OR (XOR) problem.
Input data  Output data
(1, 1)          (0)
(1, 0)          (1)
(0, 1)          (1)
(0, 0)          (0)


In Neural Networks, neurons have weighted inputs, activation function and an output.  The input layer in this example has two elements (one for each bit).  The hidden layer calculates values based on the forumula: f(Sum(inputs * weight)).  The weight here is initialized to small random values, let's say between -1 to 1, with a mean of 0.  This produces an output value and since we know what the expected output value is, we calculate this difference and call it the error.  Then, this error is backpropagated to the hidden layer and the input layer where by the weights are adjusted so that each time the same input pattern is presented to the network, the output will be a little closer to the expected output.  The goal of training is to minimize this error a little bit during each iteration, aka epoch.  Here's a snippet from my powerpoint presentation that sums this process up.  

     -For each input-output pattern
–Evaluate output
–Calculate the error between output and expected output
–Adjust weights in the output layer
*Do the same for the hidden layer(s)

You're probably asking the question - why do we need a Neural Network to give us the answer of an XOR operation.  We don't.  It is for theoretical and teaching purposes.  Now, the real uses of this is technique is widely seen in the AI of video games.  Here is a statement that caught my eye when researching this topic.
An agent was trained in Quake III by to collect items, engage in combat, and navigate a map. The controller was a neural network that learned by backpropagation on pre-recorded demos of human players, using the player’s weapon information and location as inputs.
*From Backpropagation without Human Supervision for Visual Control in Quake II by Matt Parker and Bobby D. Bryant