Orthogonal Vectors: Two vectors are orthogonal to each other when their dot product is 0. How do we define the dot product? Dot product(scalar product) of two n-dimensional vectors A and B, is given by this expression.
Thus the vectors A and B are orthogonal to each other if and only if
Note: In a compact form the above expression can be written as (A^T)B. Example: Consider the vectors v1 and v2 in 3D space.
Taking the dot product of the vectors.
Hence the vectors are orthogonal to each other. Code: Python program to illustrate orthogonal vectors.
C++14
#include <iostream>
#include <vector>
using namespace std;
vector<vector< int > > transpose(vector<vector< int > > matrix)
{
vector<vector< int > > transpose(matrix[0].size(), vector< int >(matrix.size()));
for ( int i = 0; i < matrix.size(); i++) {
for ( int j = 0; j < matrix[0].size(); j++) {
transpose[j][i] = matrix[i][j];
}
}
return transpose;
}
int dot(vector<vector< int > > vect_A, vector<vector< int > > vect_B)
{
int product = 0;
int n = vect_A[0].size();
for ( int i = 0; i < n; i++) {
product = product + vect_A[0][i] * vect_B[i][0];
}
return product;
}
int main()
{
vector<vector< int > > v1 = {{1, -2, 4}};
vector<vector< int > > v2 = {{2, 5, 2}};
vector<vector< int > > transposeOfV1 = transpose(v1);
int result = dot(v2, transposeOfV1);
cout << "Result = " << result << endl;
return 0;
}
|
Java
import java.util.Arrays;
public class OrthogonalVector {
public static void main(String[] args) {
int [][] v1 = {{ 1 , - 2 , 4 }};
int [][] v2 = {{ 2 , 5 , 2 }};
int [][] transposeOfV1 = transpose(v1);
int result = dot(v2, transposeOfV1);
System.out.println( "Result = " + result);
}
public static int [][] transpose( int [][] matrix) {
int [][] transpose = new int [(matrix[ 0 ].length)][(matrix.length)];
for ( int i = 0 ; i < matrix.length; i++) {
for ( int j = 0 ; j < matrix[ 0 ].length; j++) {
transpose[j][i] = matrix[i][j];
}
}
return transpose;
}
static int dot( int vect_A[][], int vect_B[][])
{
int product = 0 ;
int n = vect_A[ 0 ].length;
for ( int i = 0 ; i < n; i++)
product = product + vect_A[ 0 ][i] * vect_B[i][ 0 ];
return product;
}
}
|
Python3
import numpy
v1 = [[ 1 , - 2 , 4 ]]
v2 = [[ 2 , 5 , 2 ]]
transposeOfV1 = numpy.transpose(v1)
result = numpy.dot(v2, transposeOfV1)
print ("Result = ", result)
|
C#
using System;
public class OrthogonalVector
{
public static void Main()
{
int [][] v1 = { new [] { 1, -2, 4 } };
int [][] v2 = { new [] { 2, 5, 2 } };
int [][] transposeOfV1 = Transpose(v1);
int result = Dot(v2, transposeOfV1);
Console.WriteLine( "Result = " + result);
}
public static int [][] Transpose( int [][] matrix)
{
int [][] transpose = new int [(matrix[0].Length)][];
for ( int i = 0; i < matrix[0].Length; i++)
{
transpose[i] = new int [(matrix.Length)];
for ( int j = 0; j < matrix.Length; j++)
{
transpose[i][j] = matrix[j][i];
}
}
return transpose;
}
static int Dot( int [][] vect_A, int [][] vect_B)
{
int product = 0;
int n = vect_A[0].Length;
for ( int i = 0; i < n; i++)
{
product = product + vect_A[0][i] * vect_B[i][0];
}
return product;
}
}
|
Javascript
function transpose(matrix) {
const transpose = new Array(matrix[0].length);
for (let i = 0; i < matrix[0].length; i++) {
transpose[i] = new Array(matrix.length);
for (let j = 0; j < matrix.length; j++) {
transpose[i][j] = matrix[j][i];
}
}
return transpose;
}
function dotProduct(matrixA, matrixB) {
const n = matrixA[0].length;
let product = 0;
for (let i = 0; i < n; i++) {
product += matrixA[0][i] * matrixB[i][0];
}
return product;
}
const v1 = [[1, -2, 4]];
const v2 = [[2, 5, 2]];
const transposeOfV1 = transpose(v1);
const result = dotProduct(v2, transposeOfV1);
console.log( "Result = " + result);
|
Unit Vector: Let’s consider a vector A. The unit vector of the vector A may be defined as
Let’s understand this by taking an example. Consider a vector A in 2D space.
The magnitude of A is given by
So the unit vector of A can be calculated as
Properties of unit vector:
- Unit vectors are used to define directions in a coordinate system.
- Any vectors can be written as a product of a unit vector and a scalar magnitude.
Orthonormal vectors: These are the vectors with unit magnitude. Now, take the same 2 vectors which are orthogonal to each other and you know that when I take a dot product between these 2 vectors it is going to 0. So If we also impose the condition that we want each of these vectors to have unit magnitude then what we could possibly do is by taking this vector and then divide this vector by the magnitude of this vector as we see in the unit vector. Now we can write v1 and v2 as
So what we do is we have taken the vectors from the previous example and converted them into unit vectors by dividing them by their magnitudes. So, these vectors will still be orthogonal to each other and now individually they also have unit magnitude. Such vectors are known as orthonormal vectors. Note: All orthonormal vectors are orthogonal by the definition itself.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
11 Oct, 2023
Like Article
Save Article