public void Run() { double[] xdata = { 1.0, 2.0, 3.0, 4.0 }; double[] ydata = { 5.0, 2.0, 4.0, 1.0 }; var x = new Vector(xdata); var y = new Vector(ydata); Console.WriteLine(" x = " + x); Console.WriteLine(" y = " + y); var z = x.Plus(y); Console.WriteLine(" z = " + z); z = z.Times(10.0); Console.WriteLine(" 10z = " + z); Console.WriteLine(" |x| = " + x.Magnitude()); Console.WriteLine(" <x, y> = " + x.Dot(y)); Console.WriteLine("dist(x, y) = " + x.DistanceTo(y)); Console.WriteLine("dir(x) = " + x.Direction()); Console.ReadLine(); }
/// <summary> /// Returns the inner product of this vector with that vector. /// </summary> /// <param name="that">that the other vector</param> /// <returns>the dot product between this vector and that vector</returns> /// throws IllegalArgumentException if the lengths of the two vectors are not equal. public double Dot(Vector that) { if (_n != that._n) throw new ArgumentException("Dimensions don't agree"); var sum = 0.0; for (var i = 0; i < _n; i++) sum = sum + (_data[i] * that._data[i]); return sum; }
/// <summary> /// Returns the Euclidean distance between this vector and that vector. /// </summary> /// <param name="that">that the other vector</param> /// <returns>the Euclidean distance between this vector and that vector</returns> /// throws ArgumentException if the lengths of the two vectors are not equal. public double DistanceTo(Vector that) { if (_n != that._n) throw new ArgumentException("Dimensions don't agree"); return Minus(that).Magnitude(); }
/// <summary> /// Returns the product of this factor multiplied by the scalar factor: this * factor. /// </summary> /// <param name="factor">factor the multiplier</param> /// <returns>the scalar product of this vector and factor</returns> public Vector Times(double factor) { var c = new Vector(_n); for (var i = 0; i < _n; i++) c._data[i] = factor * _data[i]; return c; }
/// <summary> /// Returns the sum of this vector and that vector: this + that. /// </summary> /// <param name="that">that the vector to add to this vector</param> /// <returns>the sum of this vector and that vector</returns> /// throws ArgumentException if the lengths of the two vectors are not equal. public Vector Plus(Vector that) { if (_n != that._n) throw new ArgumentException("Dimensions don't agree"); var c = new Vector(_n); for (var i = 0; i < _n; i++) c._data[i] = _data[i] + that._data[i]; return c; }