Comment

Comment by anon4

Ok this is seriously amazing (coming from a C++ guy). However, one thing I don't like with putting matrix dimensions in templates is that then you can't construct them at runtime. I do understand the obvious - that you can't have both static type checks on all operations and runtime-determined matrix sizes. Though I would kill for a language which would specialise my code at runtime and throw an exception for compilation errors. So you could write, e.g.

    template <int m, int n, int l>
    Matrix<m, l>
    mul(Matrix<m, n> lhs, Matrix <n, l> rhs)
    {...impl...}
And then be able to call it like

    int m, n, k, l = ... read from file or whatever
    Matrix <m, n> m1 = ...;
    Matrix <k, l> m2 = ...;
    try {
        Matrix <int o, int p> m3 = m1 * m2;
        // ^ code compiled dynamically
        // or loaded from cache, based on
        // runtime types of m1 and m2.
        // o and p set to the result
        // of type inference.
        // I could imagine even having
        // specialised versions with inline
        // assembly for specific dimensions.
    } catch (DynamicCompilationException e) {
        print("dimensions not compatible");
    }
Java could be it, if it had reified generics. You'd create an implementation of Num, or load one from cache, then instantiate the template and attempt to call the mul function.

Or you could abuse the invoke dynamic feature - create specialised functions matrixMultiply$m$n$l and classes Matrix$m$n from some other templating language as needed, then do an invoke dynamic based on type. But this would be very cumbersome to use, I think.

parent anon4 11 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton