Programa para calcular el ángulo entre dos vectores N-Dimensionales

Dada una array arr[] que consta de magnitudes de dos vectores N-dimensionales A y B , la tarea es encontrar el ángulo entre los dos vectores.

Ejemplos:

Entrada: arr[] = {-0.5, -2, 1}, brr[] = {-1, -1, -0.3}  Salida: 0.845289 Explicación: Colocando los valores en la fórmula  se obtiene el resultado requerido. 


cos\theta=\frac{\vec{a}.\vec{b}}{|\vec{a}||\vec{b}|}

Entrada: arr[] = {1, -2, 3}, brr[] = {2, 3, -1} 
Salida: -0.5

Enfoque: La idea se basa en la fórmula matemática de encontrar el producto escalar de dos vectores y dividirlo por el producto de la magnitud de los vectores A, B.

Fórmula:

Considerando que los dos vectores están separados por el ángulo θ . el producto escalar de los dos vectores viene dado por la ecuación:
\vec{a}.\vec{b} = |\vec{a}||\vec{b}|.cos\theta

Por lo tanto, cos\theta=\frac{\vec{a}.\vec{b}}{|\vec{a}||\vec{b}|}

A continuación se muestra la implementación del enfoque anterior:

C++

// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the magnitude
// of the given vector
double magnitude(double arr[], int N)
{
    // Stores the final magnitude
    double magnitude = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++)
        magnitude += arr[i] * arr[i];
 
    // Return square root of magnitude
    return sqrt(magnitude);
}
 
// Function to find the dot
// product of two vectors
double dotProduct(double arr[],
                  double brr[], int N)
{
    // Stores dot product
    double product = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++)
        product = product + arr[i] * brr[i];
 
    // Return the product
    return product;
}
 
void angleBetweenVectors(double arr[],
                         double brr[], int N)
{
    // Stores dot product of two vectors
    double dotProductOfVectors
        = dotProduct(arr, brr, N);
 
    // Stores magnitude of vector A
    double magnitudeOfA
        = magnitude(arr, N);
 
    // Stores magnitude of vector B
    double magnitudeOfB
        = magnitude(brr, N);
 
    // Stores angle between given vectors
    double angle = dotProductOfVectors
                   / (magnitudeOfA * magnitudeOfB);
 
    // Print the angle
    cout << angle;
}
 
// Driver Code
int main()
{
    // Given magnitude arrays
    double arr[] = { -0.5, -2, 1 };
    double brr[] = { -1, -1, 0.3 };
 
    // Size of the array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function call to find the
    // angle between two vectors
    angleBetweenVectors(arr, brr, N);
 
    return 0;
}

Java

// Java program for the above approach
class GFG{
 
// Function to find the magnitude
// of the given vector
static double magnitude(double arr[], int N)
{
     
    // Stores the final magnitude
    double magnitude = 0;
 
    // Traverse the array
    for(int i = 0; i < N; i++)
        magnitude += arr[i] * arr[i];
 
    // Return square root of magnitude
    return Math.sqrt(magnitude);
}
 
// Function to find the dot
// product of two vectors
static double dotProduct(double[] arr,
                         double[] brr, int N)
{
     
    // Stores dot product
    double product = 0;
 
    // Traverse the array
    for(int i = 0; i < N; i++)
        product = product + arr[i] * brr[i];
 
    // Return the product
    return product;
}
 
static void angleBetweenVectors(double[] arr,
                                double[] brr, int N)
{
     
    // Stores dot product of two vectors
    double dotProductOfVectors = dotProduct(arr, brr, N);
 
    // Stores magnitude of vector A
    double magnitudeOfA = magnitude(arr, N);
 
    // Stores magnitude of vector B
    double magnitudeOfB = magnitude(brr, N);
 
    // Stores angle between given vectors
    double angle = dotProductOfVectors /
                   (magnitudeOfA * magnitudeOfB);
 
    // Print the angle
    System.out.println(angle);
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given magnitude arrays
    double[] arr = { -0.5, -2, 1 };
    double[] brr = { -1, -1, 0.3 };
 
    // Size of the array
    int N = arr.length;
 
    // Function call to find the
    // angle between two vectors
    angleBetweenVectors(arr, brr, N);
}
}
 
// This code is contributed by user_qa7r

Python3

# Python3 program for the above approach
import math
 
# Function to find the magnitude
# of the given vector
def magnitude(arr, N):
 
    # Stores the final magnitude
    magnitude = 0
 
    # Traverse the array
    for i in range(N):
        magnitude += arr[i] * arr[i]
 
    # Return square root of magnitude
    return math.sqrt(magnitude)
 
# Function to find the dot
# product of two vectors
 
 
def dotProduct(arr, brr, N):
 
    # Stores dot product
    product = 0
 
    # Traverse the array
    for i in range(N):
        product = product + arr[i] * brr[i]
 
    # Return the product
    return product
 
 
def angleBetweenVectors(arr, brr, N):
 
    # Stores dot product of two vectors
    dotProductOfVectors = dotProduct(arr, brr, N)
 
    # Stores magnitude of vector A
    magnitudeOfA = magnitude(arr, N)
 
    # Stores magnitude of vector B
    magnitudeOfB = magnitude(brr, N)
 
    # Stores angle between given vectors
    angle = (dotProductOfVectors
             / (magnitudeOfA * magnitudeOfB))
 
    # Print the angle
    print('%.5f'%angle)
 
# Driver Code
if __name__ == "__main__":
 
    # Given magnitude arrays
    arr = [-0.5, -2, 1]
    brr = [-1, -1, 0.3]
 
    # Size of the array
    N = len(arr)
 
    # Function call to find the
    # angle between two vectors
    angleBetweenVectors(arr, brr, N)
 
    # This code is contributed by ukasp.

C#

// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
  
// Function to find the magnitude
// of the given vector
static double magnitude(double []arr, int N)
{
     
    // Stores the final magnitude
    double magnitude = 0;
 
    // Traverse the array
    for(int i = 0; i < N; i++)
        magnitude += arr[i] * arr[i];
 
    // Return square root of magnitude
    return Math.Sqrt(magnitude);
}
 
// Function to find the dot
// product of two vectors
static double dotProduct(double []arr,
                         double []brr, int N)
{
     
    // Stores dot product
    double product = 0;
 
    // Traverse the array
    for(int i = 0; i < N; i++)
        product = product + arr[i] * brr[i];
 
    // Return the product
    return product;
}
 
static void angleBetweenVectors(double []arr,
                                double []brr, int N)
{
     
    // Stores dot product of two vectors
    double dotProductOfVectors = dotProduct(arr, brr, N);
 
    // Stores magnitude of vector A
    double magnitudeOfA = magnitude(arr, N);
 
    // Stores magnitude of vector B
    double magnitudeOfB = magnitude(brr, N);
 
    // Stores angle between given vectors
    double angle = dotProductOfVectors /
     (magnitudeOfA * magnitudeOfB);
 
    // Print the angle
    Console.Write(angle);
}
 
// Driver Code
public static void Main()
{
     
    // Given magnitude arrays
    double []arr = { -0.5, -2, 1 };
    double []brr = { -1, -1, 0.3 };
 
    // Size of the array
    int N = arr.Length;
 
    // Function call to find the
    // angle between two vectors
    angleBetweenVectors(arr, brr, N);
}
}
 
// This code is contributed by bgangwar59

Javascript

<script>
 
// Javascript program for the above approach
 
// Function to find the magnitude
// of the given vector
function magnitude(arr, N)
{
    // Stores the final magnitude
    var magnitude = 0;
 
    // Traverse the array
    for (var i = 0; i < N; i++)
        magnitude += arr[i] * arr[i];
 
    // Return square root of magnitude
    return Math.sqrt(magnitude);
}
 
// Function to find the dot
// product of two vectors
function dotProduct(arr, brr,N)
{
    // Stores dot product
    var product = 0;
 
    // Traverse the array
    for (var i = 0; i < N; i++)
        product = product + arr[i] * brr[i];
 
    // Return the product
    return product;
}
 
function angleBetweenVectors(arr, brr, N)
{
    // Stores dot product of two vectors
    var dotProductOfVectors
        = dotProduct(arr, brr, N);
 
    // Stores magnitude of vector A
    var magnitudeOfA
        = magnitude(arr, N);
 
    // Stores magnitude of vector B
    var magnitudeOfB
        = magnitude(brr, N);
 
    // Stores angle between given vectors
    var angle = dotProductOfVectors
                   / (magnitudeOfA * magnitudeOfB);
 
    // Print the angle
    document.write( angle.toFixed(6));
}
 
// Driver Code
// Given magnitude arrays
var arr = [ -0.5, -2, 1 ];
var brr = [ -1, -1, 0.3 ];
// Size of the array
var N = arr.length;
// Function call to find the
// angle between two vectors
angleBetweenVectors(arr, brr, N);
 
</script>
Producción: 

0.845289

 

Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por sam_2200 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *