Vectors in a plane

  • In Mathematics, a point in a plane is represented in a coordinate system using
    2 (floating point) numbers:

  • A vector in a plane is the arrow for (0,0) to some point and it can be represented by
    2 numbers:

Vectors in space

  • In Mathematics, a point in space is represented in a coordinate system using
    3 (floating point) numbers:

  • A vector in space is the arrow for (0,0) to some point and it can be represented by
    3 numbers:

Representing vectors in Java

  • In Mathematics, a vector is represented by 2 or 3 numbers

  • In a Java program, a vector is represented using a double[] array:

        double[] vector; 

  • Example: this vector in a plane:

    is represented in a Java program as follows:

        double[] vector1 = {2, 1}; 

Representing vectors in Java

  • In a Java program, a vector is represented using a double[] array:

        double[] vector; 

  • Example: this vector in space:

    is represented in a Java program as follows:

        double[] vector1 = {3, 1, 2}; 

Vector addition in Mathematics and in Java

  • 2 vectors are added in Mathematics by contenating the arrows:

  • We can perform vector addition in Java as follows:

        double[] vector1 = {2, 1};         // Vector 1 = (2,1)
        double[] vector2 = {1, 2};         // Vector 2 = (1,2)
        double[] vector3 = new double[2];  // Vector 3 is computed
    
        vector3[0] = vector1[0] + vector2[0]; // Compute vector3
        vector3[1] = vector1[1] + vector2[1];
    

    (To add vectors in space, we add: vector3[2] = vector1[2] + vector2[2])

A vector addition method

Vector operations are commonly provided as methods (because they are useful operations):

    public static double[] vectorAdd( double[] vector1, double[] vector2)
    {
        double[] vector3 = new double[vector1.length];

	for ( int i = 0; i < vector1.length; i++ )
	    vector3[i] = vector1[i] + vector2[i];

	return vector3;
    }
  

Let's write a vectorAdd( ) method that adds the input vectors and return the result

A vector addition method

(1) create an output vector to store the result:

    public static double[] vectorAdd( double[] vector1, double[] vector2)
    {
        double[] vector3 = new double[vector1.length];

	for ( int i = 0; i < vector1.length; i++ )
	    vector3[i] = vector1[i] + vector2[i];

	return vector3;
    }
  

 

A vector addition method

(2) add the input vectors and store the result in vector3:

    public static double[] vectorAdd( double[] vector1, double[] vector2)
    {
        double[] vector3 = new double[vector1.length];

	for ( int i = 0; i < vector1.length; i++ )
	    vector3[i] = vector1[i] + vector2[i];

	return vector3;
    }
  

 

A vector addition method

(3) return the result vector vector3:

    public static double[] vectorAdd( double[] vector1, double[] vector2)
    {
        double[] vector3 = new double[vector1.length];

	for ( int i = 0; i < vector1.length; i++ )
	    vector3[i] = vector1[i] + vector2[i];

	return vector3;
    }
  

Demo program on next slide

Vector addition in Java   demo program

    public static void main(String[] args)
    {
        double[] v1 = {1,2,3};
        double[] v2 = {1,1,1};
        double[] v3;
       
        v3 = vectorAdd(v1,v2);
        System.out.println( Arrays.toString(v1) + " + " +
                            Arrays.toString(v2) + " = " +
                            Arrays.toString(v3) );
    }

    public static double[] vectorAdd( double[] vector1, double[] vector2)
    {
        double[] vector3 = new double[vector1.length];

	for ( int i = 0; i < vector1.length; i++ )
	    vector3[i] = vector1[i] + vector2[i];

	return vector3;
    }
  

DEMO: demo/08-array/16-vectors/VectorAdd.java