TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
vector_cross_product.cpp
Go to the documentation of this file.
1
52
53#include <array>
54#include <cassert>
55#include <cmath>
56
61namespace math {
66namespace vector_cross {
74std::array<double, 3> cross(const std::array<double, 3> &A,
75 const std::array<double, 3> &B) {
76 std::array<double, 3> product;
78 product[0] = (A[1] * B[2]) - (A[2] * B[1]);
79 product[1] = -((A[0] * B[2]) - (A[2] * B[0]));
80 product[2] = (A[0] * B[1]) - (A[1] * B[0]);
81 return product;
82}
83
91double mag(const std::array<double, 3> &vec) {
92 double magnitude =
93 sqrt((vec[0] * vec[0]) + (vec[1] * vec[1]) + (vec[2] * vec[2]));
94 return magnitude;
95}
96} // namespace vector_cross
97} // namespace math
98
103static void test() {
105 std::array<double, 3> t_vec =
106 math::vector_cross::cross({1, 2, 3}, {4, 5, 6});
107 assert(t_vec[0] == -3 && t_vec[1] == 6 && t_vec[2] == -3);
108
110 double t_mag = math::vector_cross::mag({6, 8, 0});
111 assert(t_mag == 10);
112
114 std::array<double, 3> t_vec2 =
115 math::vector_cross::cross({1, 2, 3}, {1, 2, 3});
116 assert(t_vec2[0] == 0 && t_vec2[1] == 0 &&
117 t_vec2[2] == 0); // checking each element
118 assert(math::vector_cross::mag(t_vec2) ==
119 0); // checking the magnitude is also zero
120}
121
128int main() {
130 test();
131 return 0;
132}
for assert
Functions for Vector Cross Product algorithms.
std::array< double, 3 > cross(const std::array< double, 3 > &A, const std::array< double, 3 > &B)
Function to calculate the cross product of the passed arrays containing the direction ratios of the t...
double mag(const std::array< double, 3 > &vec)
Calculates the magnitude of the mathematical vector from it's direction ratios.
static void test()
test function.
int main()
Main Function.