TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
cross_product.hpp
Go to the documentation of this file.
1// Copyright (c) 2020 Simons Foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0.txt
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// Authors: Olivier Parcollet, Nils Wentzell
16
17/**
18 * @file
19 * @brief Provides a cross product for 3-dimensional vectors or other arrays/views of rank 1.
20 */
21
22#pragma once
23
24#include "../declarations.hpp"
25#include "../macros.hpp"
26#include "../traits.hpp"
27
28namespace nda::linalg {
29
30 /**
31 * @ingroup linalg_tools
32 * @brief Compute the cross product of two 3-dimensional vectors.
33 *
34 * @tparam V Vector type.
35 * @param x Left hand side vector.
36 * @param y Right hand side vector.
37 * @return nda::array of rank 1 containing the cross product of the two vectors.
38 */
39 template <typename V>
40 auto cross_product(V const &x, V const &y) {
41 EXPECTS_WITH_MESSAGE(x.shape()[0] == 3, "nda::linalg::cross_product: Only defined for 3-dimensional vectors");
42 EXPECTS_WITH_MESSAGE(y.shape()[0] == 3, "nda::linalg::cross_product: Only defined for 3-dimensional vectors");
43 array<get_value_t<V>, 1> r(3);
44 r(0) = x(1) * y(2) - y(1) * x(2);
45 r(1) = -x(0) * y(2) + y(0) * x(2);
46 r(2) = x(0) * y(1) - y(0) * x(1);
47 return r;
48 }
49
50} // namespace nda::linalg
auto cross_product(V const &x, V const &y)
Compute the cross product of two 3-dimensional vectors.
#define EXPECTS_WITH_MESSAGE(X,...)
Definition macros.hpp:75