TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
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
61
namespace
math
{
66
namespace
vector_cross
{
74
std::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
91
double
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
103
static
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
128
int
main
() {
130
test
();
131
return
0;
132
}
test
void test()
Definition
caesar_cipher.cpp:100
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
math
for assert
vector_cross
Functions for Vector Cross Product algorithms.
math::vector_cross::cross
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...
Definition
vector_cross_product.cpp:74
math::vector_cross::mag
double mag(const std::array< double, 3 > &vec)
Calculates the magnitude of the mathematical vector from it's direction ratios.
Definition
vector_cross_product.cpp:91
math
vector_cross_product.cpp
Generated by
1.18.0