Templated Matrix class

Andrew Spott andrew.spott at gmail.com
Sat Apr 18 11:07:19 PDT 2009


So, I'm trying to create a matrix class that is templated, but I don't really understand templates, so I don't know why this doesn't work.  Any idea why I can't seem to do 2-D arrays?

-Andrew

module matrix;
private import std.string;

class Matrix(T) {
	private:
	T[][] ma;
	int nn;
	

	public:

	this() {
	}

	this(int n, int m) {
		ma[] = new T[m];
		ma = new T[n][m];
	}

	this(int n, int m, T a) {
		ma = new T[n][m];
		ma[][] = a;
	}

	T opIndex(int n, int m) {
		return ma[n][m];
	}

	void opIndexAssign(T)(T a, int n, int m) {
		ma[n][m] = a;
	}

	int rows() {
		return ma.length;
	}

	int cols() {
		return ma[].length;
	}

	string toString() {
		if (ma.length == 0) return "[]";
		foreach (h; ma[0..$-1]) {
			string s = "[";
			foreach (k; h[0..$-1]) {
				s = std.string.format("%s, %s", s, k);
			}
			s = std.string.format("%s, %s]\n", s, h[$-1]);
    }
		return s;
	}

}




More information about the Digitalmars-d mailing list