mathajax

Vector Dot Product

The Java code finds the dot product of two vectors v1 and v2.  it is also called as scalar product or inner product and  defined as sum of  product of  respective axis element of  the two vectors. Their vector's element can be integer or double of java datatype.



2-D vectors dot product
Dot product of 2-D vectors

N Dimensional vector dot product
Dot product of N-D vectors

Dot product of 2D vectors

A class Vector defined two member functions named "dot-product" with parameter type difference, in order to support  parameters  int or double type 2D vectors. 
.
Both member function can be invoked with two array parameters, which represents the vectors to be dot-product. These vectors must have 2 elements.

for example  below shows how to calculate dot product two vectors A and B using above equation
     Vector A = [1 5]
     Vector B = [2 3]
     
     A.B =  1x2 + 5x3
         =  2  + 15
     A.B  =  17     
  

   


import java.util.Arrays;

public class Vector {

 public static int dotproduct(int vec1[],int vec2[]) {  
    return (vec1[0] * vec2[0] +  vec1[1] * vec2[1]);
 }
 
 public static double dotproduct(double vec1[],double vec2[]) {  
       return (vec1[0] * vec2[0] +  vec1[1] * vec2[1]);   
 }
 
 public static void main(String[] args)
 {
  
       int vector1 [] = {1,2};
       int vector2 [] = {4,5};
       double csim1= Vector.dotproduct(vector1, vector2);
             
       System.out.println("nDotproduct of two 2D vectors");
       System.out.println("ninteger 2D vectors");
       System.out.println("Vector 1 :" + Arrays.toString(vector1));
       System.out.println("Vector 2 :" + Arrays.toString(vector2));
       System.out.println("DP value :" + csim1);
       
       
       double vector3 [] = {5.2, 2.6};
       double vector4 [] = {9.8,7.6};
       System.out.println("ndouble 2D vectors");
       double csim2=Vector.dotproduct(vector3, vector4);
       System.out.println("Vector 1 :" + Arrays.toString(vector3));
       System.out.println("Vector 2 :" + Arrays.toString(vector4));
       System.out.println("DP value :" + csim2);
  
 }

}


Output
Dotproduct of two 2D vectors

integer 2D vectors
Vector 1 :[1, 2]
Vector 2 :[4, 5]
DP value :14.0

double 2D vectors
Vector 1 :[5.2, 2.6]
Vector 2 :[9.8, 7.6]
DP value :70.72



Dot product of N-Dimensional vectors

A class Vector defined two member functions named "dot product" with parameter type difference, in order to support  parameters  int or double type N - Dimensional vectors. 

Both member function can be invoked with two array parameters, which represents the vectors to be dot-product.  These vectors can have more than 2 elements, but same in numbers of elements.  


package metric;

import java.util.Arrays;

public class Vectornd {

 
public static int dotproduct(int vec1[],int vec2[]) 
    {
     int dop=0;
     for(int n=0;n<vec1.length;n++)
      dop +=  vec1[0] * vec2[0];     
  return dop;
 }
 
  public static double dotproduct(double vec1[],double vec2[]) 
    {  
     double dop=0;
     for(int n=0;n<vec1.length;n++)
      dop +=  vec1[0] * vec2[0];     
  return dop;   
 }
  
  
 public static void main(String[] args)
 {
    
     int vector1 [] = {1,4,3,6};
     int vector2 [] = {4,2,6,1};
     double dop1= Vectornd.dotproduct(vector1, vector2);
             
 System.out.println("n Dot Product of two ND vectors");
 System.out.println("ninteger ND vectors");
 System.out.println("Vector 1 :" + Arrays.toString(vector1));
 System.out.println("Vector 2 :" + Arrays.toString(vector2));
 System.out.println("DP value :" + dop1);
       
       
 double vector3 [] = {5.2,1.3,5.7,2.6};
 double vector4 [] = {9.8,7.6,12.5,4.1};
        System.out.println("ndouble ND vectors");
 double dop2=Vectornd.dotproduct(vector3, vector4);
 System.out.println("Vector 1 :" + Arrays.toString(vector3));
 System.out.println("Vector 2 :" + Arrays.toString(vector4));
 System.out.println("DP value :" + dop2);

    }

}


Output
 Dot Product of two N-D vectors

integer N-D vectors
Vector 1 :[1, 4, 3, 6]
Vector 2 :[4, 2, 6, 1]
DP value :16.0

double N-D vectors
Vector 1 :[5.2, 1.3, 5.7, 2.6]
Vector 2 :[9.8, 7.6, 12.5, 4.1]
DP value :203.84000000000003




Comments

Popular posts from this blog

Solving System of Linear Equations by Gauss Jordan Elimination

Matrix Forward and Back Substitution

Solve System of Linear Equations by LU Decompose

Matrix Determinant by Upper Triangular Matrix

Chebyshev distance between two points