Vector Inner product
A matrix product of two vectors X and Y are called inner product that results a scalar output. The two vectors X and Y must have the same number of elements in order to measure inner product these two vectors.
A vector X has n elements
A vector Y has n elements
Inner product of Two vectors X and Y
or
or
Output
A vector X has n elements
![X vector](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiyWI7vJjxxKGp_tU5QOLh2QKKiUznlBVj8GWBUoXCZmFKWqpkzZgIRtRdgS-uyAwRhOYHjW1WWdXFWhJLSb8853q9yg-qYLJD6MVGHOhnFGxnM8pawyFOhcWtEUXJq3wlWIMnFnFsxcrc/s1600/X+vector.png)
A vector Y has n elements
![Y vector](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg3Mv-kBoyjZfYzJSQjTdxwKSaM9slVAgTBNQAC7PYQC9UQOFQabu9UkczuKO5pAb0py7CQokRN9ylE5Lz4q9779fIkeIQ0RVs-uFbz92AGIri7bP8MXrWE2OK8RFHT8icUyiCh2QcNOPc/s1600/Y+vector.png)
Inner product of Two vectors X and Y
![inner product of X and Y](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjwf62GbACFTHbGGwopASEhQRNyxG248Eb9erksclWzZrl2fyDbu5Mo9DdT46Qwf3YjfdCfo6fiaIJB8IntNRNCas-uwqTC7Mg8zypP4dAlHBtoAgW4SBM-mG35npJCG8Gtk0AwXfyI1u0/s1600/XY.png)
or
![vectors inner product](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhTH07Sed8OqibNKauhSxi9OsRyD7luRX-NNKEneV72hFIhT8weLnZE-RiKW0UBy619mZ6df8HATysuJT3AKcTp1L37QIHJ6Fe8pFZHv7reuA4Eb8cPqpMdYeuvZ4uw1fWywbdMdEPIkFo/s1600/innerproduct.png)
or
![vectors inner product](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0mq9Fj_bqsfoGABzfZH7mjTVAcDd-13wQcjE8CaC8la3yEVhTBbQieW307hASY0F5SUYt0B2Qkqi58qJnajLeIAaIRjYL_lL2-4xb1hKD0h32ZPbn6ZzRJ1oI5hEXw09P5Rm4lI3b-Mk/s1600/innerproduct2.png)
Java code - vector inner product
The following Java code inner product of two vectors having same number of elements. The vectors can be defined in Java program by a integer array and a double array.
import java.util.Arrays;
public class Vector
{
public static double innerproduct(double vec[],double vec2[])
{
double inp=0.0;
for(int n=0;n<vec.length;n++)
inp = inp + vec[n] * vec2[n];
return inp;
}
public static int innerproduct(int vec[],int vec2[])
{
int inp=0;
for(int n=0;n<vec.length;n++)
inp = inp + vec[n] * vec2[n];
return inp;
}
public static void main(String[] args)
{
System.out.println("\n Find inner product of
two vectors X and Y\n");
double X[]={5.0, 1.2, -2.1};
double Y[]={3.0, -2.6, 1.4};
System.out.println( "\n vector X :" +Arrays.toString(X));
System.out.println( " vector Y :" + Arrays.toString(Y));
double inp = Vector.innerproduct(X, Y);
System.out.println( "Inner product of X and Y :" + inp );
int iX[]={5, 1, 2};
int iY[]={1, 4, -3};
System.out.println( "\n vector iX :" +Arrays.toString(iX));
System.out.println( " vector iY :" + Arrays.toString(iY));
int inp2 = Vector.innerproduct(iX, iY);
System.out.println( "Inner product of iX and iY :" + inp2 );
}
}
Output
Find inner product of two vectors X and Y vector X :[5.0, 1.2, -2.1] vector Y :[3.0, -2.6, 1.4] Inner product of X and Y :8.94 vector X :[5, 1, 2] vector iY :[1, 4, -3] Inner product of iX and iY :3
Comments
Post a Comment