Monthly Archives: April 2008

Lorentzian clustering

Gravitational clustering in machine learning, a previous idea of mine, has been done before, but it seems that you can also cluster points based on the Lorentz transform spacetime undergoes in general relativity. I’m wondering whether a data clustering algorithm based on the stress-energy tensor (simulated at relativistic speeds, of course) would be feasible. My dissertation is on using tensors in data mining, so it could be a useful example.

Teaching my gifted cousin

My cousin is showing signs of a gift similar to my own in both scope and magnitude… except that this time, I’m here to train him. It will be interesting to see what happens. Will he grow up stronger for the help, I wonder, or has poor training become such an inextricable aspect of my own character that it can no longer be parsed out from what I’ve accomplished? Will someone who is given more be motivated to do less by sheer necessity?

I hope he’ll be more well-adjusted, at least. Aid of any form should help prevent an adversarial view of the world from forming, but perhaps he’ll end up out-of-step as well when he sees how much apathy and incompetence contrast his own vision and ability, regardless of having someone to fall back on.

If he continues as he is progressing now, he will certainly accomplish great things.

Bug in C#

Apparently Microsoft has no easily accessible place to report bugs in their programming languages, so I’m posting it here and hopefully someone who can fix it will eventually read it (and maybe even put an easy-to-find link up to report bugs on MSDN?)

If it’s not a bug with .NET itself, perhaps someone can tell me what I’m doing wrote here, C# newbie that I still am.

I have a DataGridView bound to an ArrayList of objects of the following class:

public class SeriesPoint : IComparable {
    private double _price;
    public DateTime? Timestamp;

    public double Price {
        get { return _price; }
        set { _price = value; }
    }

    public string Date {
        get { return Timestamp.ToString(); }
        set { Timestamp = DateTime.Parse(value); }
    }

    public SeriesPoint() { _price = 0; Timestamp = null; }
    public SeriesPoint(double p, DateTime? d) { _price = p; Timestamp = d; }

    //These are compared only based on date.
    public static bool operator<(SeriesPoint lhs, SeriesPoint rhs) { return lhs.Timestamp < rhs.Timestamp; }
    public static bool operator>(SeriesPoint lhs, SeriesPoint rhs) { return lhs.Timestamp > rhs.Timestamp; }

    //Equivalent in C++: return rhs.timestamp == timestamp;
    public override bool Equals(object obj) {
        if (obj == null || !(obj is SeriesPoint)) return false;
        if (obj == (Object) this) return true;

        return ((SeriesPoint) obj).Timestamp.Equals(Timestamp);
    }

    //Nothing special about how it's hashed.
    public override int GetHashCode() {
        return base.GetHashCode();
    }

    public int CompareTo(object other) {
        //Nulls to the beginning.
        if (this == null && other != null) return -1;
        if (this != null && other == null) return 1;

        if (this < (SeriesPoint) other) return -1;
        if (this > (SeriesPoint) other) return 1;
        return 0;
    }
}

And I run the following to attempt to find the last null value in a sorted list:

int lastnull = sortedpoints.LastIndexOf(new SeriesPoint(0.00, null));

This actually works, but when the function exits and control presumably returns to the main loop, the program suddenly crashes with the following exception in Application.Run() (not even running on the same thread as my code!):

“An unhandled exception of type ‘System.InvalidOperationException’ occurred in System.Windows.Forms.dll

Additional information: Operation is not valid due to the current state of the object.”

I checked all of my code, put try blocks around things, made sure nothing was throwing when it shouldn’t be, removed functions, rewrote functions, reordered functions, checked boundary conditions… nothing. My code is running fine and returning correct results. .NET is then crashing. I have no idea why, but the data binding is definitely a possibility.

I suppose my best course of action is to remove the Equals() method (this fixes the problem) and rewrite my code without it.

Type safety in newer programming languages

Am I the only one who finds newer programming languages, such as Java and C#, to take strong typing to a very undesireable extreme? It seems like a reaction to most programmers not being able to get type safety right in C++, but it ends up making the code look awful, obscuring its function, and requiring a lot of extra work to explicitly cast things around. Especially when working with arrays, it oftentimes requires creating entirely new loops dedicated solely to the purpose of casting. This is stupid in many different ways. ConvertAll sort of helps, but it’s clunky and doesn’t work for collections. Also, why does the cast operator have lower precedence than the dot operator? I would think the other usage would be more common.

In Perl, which I now consider my favorite language (because it doesn’t presume to impose seemingly arbitrary restrictions on the programmer), you can join an array of doubles into a string in one line of code. You can do it in two in C++, which isn’t bad, since the stream operators have overloads for numeric types. Try it in C# or Java and you’ll need at least four or five lines.

I think I prefer the flexibility of weak typing despite the added risk. It’s a pity that “hot” languages are all following the “programming with mittens” paradigm right now.

Sci-Fi has it wrong: ET would not have the same senses as us.

Everything we can directly perceive with our sight is contained in a band of light about 400nm across. This band of light happens to coincide with the type of light achieving penetration through earth’s atmosphere, which, of course, is not a coincidence at all. Our sense of sight grew up around this band of light and it would be foolish to expect extraterrestrial sources of life, should they exist, to see the same band. This throws another wrench into sci-fi cliches 🙂

The same goes for any sense that depends on properties specific to Earth or the Sun. For that matter, they probably couldn’t breathe our air, either.