Friday, February 6, 2015

Vector.java in pgm



Vector.java
public class Vector
{
   private final int N;        
   private double[] data;    
   public Vector(int N)
   {
        this.N = N;
        this.data = new double[N];
    }
                     public Vector(double[] data)
{
        N = data.length;
        this.data = new double[N];
        for (int i = 0; i < N; i++)
        this.data[i] = data[i];
    }
     Vector x = new Vector(1.0, 2.0, 3.0, 4.0);
     Vector y = new Vector(5.0, 2.0, 4.0, 1.0);
      public Vector(double... data) {
        N = data.length;

        this.data = new double[N];
        for (int i = 0; i < N; i++)
            this.data[i] = data[i];
    }
    public int length()
    {
        return N;
    }

    public double dot(Vector that)
    {
        if (this.N != that.N) throw new RuntimeException("Dimensions don't agree");
        double sum = 0.0;
        for (int i = 0; i < N; i++)
            sum = sum + (this.data[i] * that.data[i]);
        return sum;
    }
    public double magnitude()
   {
        return Math.sqrt(this.dot(this));
    }

    public double distanceTo(Vector that)
   {
        if (this.N != that.N) throw new RuntimeException("Dimensions don't agree");
        return this.minus(that).magnitude();
    }
    public Vector plus(Vector that)
   {
        if (this.N != that.N) throw new RuntimeException("Dimensions don't agree");
        Vector c = new Vector(N);
        for (int i = 0; i < N; i++)
            c.data[i] = this.data[i] + that.data[i];
        return c;
    }

    public Vector minus(Vector that)
   {
        if (this.N != that.N) throw new RuntimeException("Dimensions don't agree");
        Vector c = new Vector(N);
        for (int i = 0; i < N; i++)
            c.data[i] = this.data[i] - that.data[i];
        return c;
    }

    public double cartesian(int i)
   {
        return data[i];
    }
    public Vector times(double factor)
   {
        Vector c = new Vector(N);
        for (int i = 0; i < N; i++)
            c.data[i] = factor * data[i];
        return c;
    }
                  public Vector direction()
                  {
        if (this.magnitude() == 0.0) throw new RuntimeException("Zero-vector has no direction");
        return this.times(1.0 / this.magnitude());
                    }
                  public String toString()
                  {
        String s = "(";
        for (int i = 0; i < N; i++)
                        {
            s += data[i];
            if (i < N-1) s+= ", ";
        }
        return s + ")";
 }
 public static void main(String[] args)
{
        double[] xdata = { 1.0, 2.0, 3.0, 4.0 };
        double[] ydata = { 5.0, 2.0, 4.0, 1.0 };

        Vector x = new Vector(xdata);
        Vector y = new Vector(ydata);

        System.out.println("x        =  " + x);
        System.out.println("y        =  " + y);
        System.out.println("x + y    =  " + x.plus(y));
        System.out.println("10x      =  " + x.times(10.0));
        System.out.println("|x|      =  " + x.magnitude());
        System.out.println("   =  " + x.dot(y));
        System.out.println("|x - y|  =  " + x.minus(y).magnitude());
    }
}

No comments:

Post a Comment