Computer >> 컴퓨터 >  >> 프로그램 작성 >> C++

두 벡터의 내적 및 외적을 위한 C++ 프로그램

<시간/>

x, y 및 방향을 포함하는 벡터 A와 벡터 B가 있다고 가정해 보겠습니다. 두 개의 벡터가 주어졌을 때 주어진 두 벡터 배열의 외적과 내적을 찾는 것이 작업입니다.

벡터란 무엇입니까?

수학에서 크기와 방향이 있는 양을 벡터라고 하고 크기가 하나만 있는 양을 스칼라라고 합니다. 벡터가 시작되는 지점을 초기점이라고 하고 벡터가 끝나는 지점을 끝점이라고 합니다. 벡터의 시작점과 끝점 사이의 거리를 벡터의 크기라고 합니다.

다음과 같은 여러 유형의 벡터가 있습니다.

  • 단위 벡터 - 크기가 1인 벡터를 단위 벡터라고 합니다.
  • 제로 벡터 − 이 유형의 벡터에서는 초기점과 끝점이 동일하기 때문에 NULL 벡터라고도 합니다.
  • 초기 벡터 - 두 개 이상의 벡터가 동일한 시작점 또는 시작점을 갖는 경우 이를 공동 초기 벡터라고 합니다.
  • 공선 벡터 - 두 개 이상의 벡터가 같은 선에 평행하면 공선 벡터라고 합니다.
  • 등 벡터 - 두 벡터의 크기와 방향이 같으면 동일한 벡터라고 합니다.

내적이란 무엇입니까?

내적은 −

로 정의되는 스칼라 곱으로도 알려져 있습니다.

두 벡터 A =a1 * i + a2 * j + a3 * k 및 B =b1 * i + b2 * j + b3 * k 여기서 i, j 및 k는 값이 1임을 의미하는 단위 벡터입니다. x, y 및 z는 벡터의 방향이고 내적 또는 스칼라 곱은 a1 * b1 + a2 * b2 + a3 * b3과 같습니다.

Input-: A = 2 * i + 7 * j + 2 * k
B = 3 * i + 1 * j + 5 * k
Output-: 2 * 3 + 7 * 1 + 2 * 5 = 23

교차곱이란 무엇입니까?

외적은 −

로 정의되는 벡터 곱으로도 알려져 있습니다.

두 벡터 A =a1 * i + a2 * j + a3 * k 와 B =b1 * i + b2 * j + b3 * k 가 있다고 가정해 보겠습니다. 그런 다음 외적은 (a2 * b3 – a3 * b2) * i - (a1 * b3 – a3 * b1) * j + (a1 * b2 – a2 * b1) * k와 같습니다. 여기서 a2 * b3 – a3 * b2, a1 * b3 – a3 * b1 및 a1 * b1 – a2 * b1은 단위 벡터의 계수이고 i, j, k는 벡터의 방향입니다.

Input-: A = 2 * i + 7 * j + 2 * k
B = 3 * i + 1 * j + 5 * k
Output-: (7 * 5 - 2 * 1)i + (2 * 5 - 2 * 3)j - (2 * 1 - 7 * 3)k

알고리즘

Start
Step 1 -> declare a function to calculate the dot product of two vectors
   int dot_product(int vector_a[], int vector_b[])
   Declare int product = 0
   Loop For i = 0 and i < size and i++
      Set product = product + vector_a[i] * vector_b[i]
   End
   return product
Step 2 -> Declare a function to calculate the cross product of two vectors
   void cross_product(int vector_a[], int vector_b[], int temp[])
   Set temp[0] = vector_a[1] * vector_b[2] - vector_a[2] * vector_b[1]
   Set temp[1] = -(vector_a[0] * vector_b[2] - vector_a[2] * vector_b[0])
   Set temp[2] = vector_a[0] * vector_b[1] - vector_a[1] * vector_b[0]
Step 3-> In main()
   Declare vector int vector_a[] = { 4, 2, -1 }
   Declare vector int vector_b[] = { 5, 7, 1 }
   Declare variable int temp[size]
   Call function for dot product as dot_product(vector_a, vector_b)
   Call function for dot product as cross_product(vector_a, vector_b)
   Loop For i = 0 and i < size and i++
   Print temp[i]
End
Stop

예시

#include <bits/stdc++.h>
#define size 3
using namespace std;
//function to calculate dot product of two vectors
int dot_product(int vector_a[], int vector_b[]) {
   int product = 0;
   for (int i = 0; i < size; i++)
   product = product + vector_a[i] * vector_b[i];
   return product;
}
//function to calculate cross product of two vectors
void cross_product(int vector_a[], int vector_b[], int temp[]) {
   temp[0] = vector_a[1] * vector_b[2] - vector_a[2] * vector_b[1];
   temp[1] = -(vector_a[0] * vector_b[2] - vector_a[2] * vector_b[0]);
   temp[2] = vector_a[0] * vector_b[1] - vector_a[1] * vector_b[0];
}
int main() {
   int vector_a[] = { 4, 2, -1 };
   int vector_b[] = { 5, 7, 1 };
   int temp[size];
   cout << "Dot product:";
   cout << dot_product(vector_a, vector_b) << endl;
   cout << "Cross product:";
   cross_product(vector_a, vector_b, temp);
   for (int i = 0; i < size; i++)
   cout << temp[i] << " ";
   return 0;
}

출력

Dot product:33
Cross product:9 -9 18