function Matrix( w, h )
{
	this.data = new Array( w * h );
	this.width = w;
	this.height = h;
}

Matrix.prototype = new Object;

Matrix.prototype.get = function( x, y )
{
	if (x < 0 || x >= this.width || y < 0 || y >= this.height)
		return undefined;
	return this.data[x+y*this.width];
}

Matrix.prototype.set = function( x, y, v )
{
	if (x < 0 || x >= this.width || y < 0 || y >= this.height)
		return;
	this.data[x+y*this.width] = v;
}

Matrix.prototype.setValues = function()
{
	var i = 0;
	var max = this.width * this.height;
	for (i = 0; i < max; ++i)
	{
		this.data[i] = arguments[i];
	}
}

Matrix.prototype.identity = function()
{
	if (this.width != this.height)
		return;
	var x, y;
	for (x = 0; x < this.width; ++x)
	for (y = 0; y < this.height; ++y)
		this.set( x, y, (x == y) ? 1 : 0 );
}

Matrix.prototype.multiply = function( m2 )
{
	if (this.width != m2.height)
		return undefined;
	var r = new Matrix( m2.width, this.height );
	var x, y, z;
	for (x = 0; x < m2.width; ++x)
		for (y = 0; y < this.height; ++y)
		{
			var t = 0;
			for (z = 0; z < this.width; ++z)
			{
				t += this.get( z, y ) * m2.get( x, z );
			}
			r.set( x, y, t );
		}
	return r;
}

Matrix.prototype.toString = function()
{
	var str = '[ ';
	var x, y;
	for (y = 0; y < this.height; ++y)
	{
		for (x = 0; x < this.width; ++x)
		{
			str += this.get( x, y ) + ' ';
		}
		str += "\n  ";
	}
	str += ']';
	return str;
}
