
// n-vector
Vector = function(n, init_values)
{
    var i;
    this.n = n;
    this.vec = new Array();
    
    if (init_values) {
	for (i = 0; i < n; ++i) {
	    this.vec[i] = init_values[i];
	}
    } else {
	for (i = 0; i < n; ++i) {
	    this.vec[i] = 0;
	}
    }
};

Vector.prototype = 
{
    distance: function(fv)
    {
	var dist = 0.0;
	for (var i = 0; i < this.n; ++i) {
	    dist += (this.vec[i] - fv.vec[i]) * (this.vec[i] - fv.vec[i]);
	}
	return Math.sqrt(dist);
    },
    norm: function()
    {
	var norm = 0.0;
	for (var i = 0; i < this.n; ++i) {
	    norm += this.vec[i] * this.vec[i];
	}
	return norm;
    },
    
    lta: function(fv)
    {
	for (var i = 0; i < this.n; ++i) {
	    if (this.vec[i] > fv.vec[i]) {
		return false;
	    }
	}
	return true;
    },
    
    add: function(fv)
    {
	for (var i = 0; i < this.n; ++i) {
	    this.vec[i] += fv.vec[i];
	}
    },
    maxElement: function()
    {
	var max_elm = this.vec[0];
	for (var i = 1; i < this.n; ++i) {
	    if (max_elm < this.vec[i]) {
		max_elm = this.vec[i];
	    }
	}
	return max_elm;
    },
    
    normalize: function(scale)
    {
	var max_elm = this.maxElement();
	for (var i = 0; i < this.n; ++i) {
	    this.vec[i] = (this.vec[i] / max_elm) * scale;
	}
    },
};
