mathajax

Orthonormal vector

A two vectors can be orthonormal to one another in other words, These vectors are perpendicular to each other and also their vector length equal to 1. To detect whether these two vectors are orthonormal or not, the vectors inner-product and vector length are used. If the result the inner-product is 0 (zero) and norm is 1 (one), the two vectors are orthonormal, otherwise not.
Vector X

Vector Y

Vector Inner Product

Vector norm or length


Java code - Orthonormal

The following Java code finds two vectors X and Y are orthonormal or not to each other by inner-product and norm of these two vector. A vector is defined in Java program by a integer array or a double array.
 
Vector X = [1, 0]
Vector Y = [0, 1]
find inner product of vector X and Y X.Y = 0
find norm of vector X |X| = 1
find norm of vector Y |Y| = 1
if |X| = 1,|Y| = 1 and X .Y = 0 vector X and Y are orthonormal



import java.util.Arrays;

public class Vector 
{
 
  public static boolean isorthonormal(double vec[],double vec2[]) 
   {
     boolean status =false;
  
     double inp=0.0;   
     for(int n=0;n<vec.length;n++)
        inp = inp +  vec[n] * vec2[n];
     
     double nrm=0,nrm2=0;
     for(int n=0;n<vec.length;n++) 
      {
        nrm += vec[n]*vec[n];
        nrm2 += vec2[n]*vec2[n]; 
      }
      
      if ( inp==0 && nrm==1 && nrm2==1)
        status =true;
        
      return status;
   }
 
  public static boolean isorthonormal(int vec[],int vec2[]) 
   {
      boolean status =false;
    
      double inp=0.0;   
      for(int n=0;n<vec.length;n++)
    inp = inp +  vec[n]*vec2[n];
    
      double nrm=0,nrm2=0;
      for(int n=0;n<vec.length;n++) 
      {
      nrm += vec[n]*vec[n];
      nrm2 += vec2[n]*vec2[n]; 
      }
     
     if ( inp==0 && nrm==1 && nrm2==1)
        status =true;
    
      return status;
  }
 
public static void main(String[] args) 
  { 
     System.out.println("\nVector V and V2 is Orthonormal!\n"); 
     double V[]={5.0 ,1.2 , 2.0};  
     double V2[]={5.0 ,1.2 , 2.0};
  
     System.out.println("\nV Vector :" + Arrays.toString(V));  
     System.out.println("V2 Vector :" + Arrays.toString(V2));
     boolean status = Vector.isorthonormal(V,V2);
     System.out.println( "A and A2 Vectors  are orthonormal ");
     System.out.println( status  );

  
    System.out.println("\nVector U and U2 is Orthonormal!\n");   
    int U[] = {2, 5, 4};
    int U2[] = {2, 5, 4};    
    System.out.println("\nU Vector :" + Arrays.toString(U));  
    System.out.println("U2 Vector :" + Arrays.toString(U2));    
    boolean status2 = Vector.isorthonormal(U,U2);
    System.out.println("U and U2 Vectors  are orthonormal " );
    System.out.println(status2 );

 }

}


Output
V Vector :[1.0, 0.0]
V2 Vector :[0.0, 1.0]
A and A2 Vectors  are orthonormal 
true

U Vector :[2, 2]
U2 Vector :[3, 5]
U and U2 Vectors  are orthonormal 
false

Comments

Popular posts from this blog

Matrix Forward and Back Substitution

Chebyshev distance between two points

Solve System of Linear Equations by LU Decompose

Complex number Multiplication and Division

Matrix Determinant, Matrix Adjoint and Matrix Inverse