VCTR
Loading...
Searching...
No Matches
SplitComplexMath.h
1/*
2 ==============================================================================
3 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
5 Copyright 2022- by sonible GmbH.
6
7 This file is part of VCTR - Versatile Container Templates Reconceptualized.
8
9 VCTR is free software: you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License version 3
11 only, as published by the Free Software Foundation.
12
13 VCTR is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License version 3 for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 version 3 along with VCTR. If not, see <https://www.gnu.org/licenses/>.
20 ==============================================================================
21*/
22
23namespace vctr
24{
31template <is::expression RealExpression, is::expression ImagExpression>
32constexpr auto makeSplitComplexExpression (RealExpression&& realExpression, ImagExpression&& imagExpression)
33{
34 VCTR_ASSERT (realExpression.size() == imagExpression.size());
35 return SplitComplexExpression<RealExpression, ImagExpression> { std::forward<RealExpression> (realExpression), std::forward<ImagExpression> (imagExpression) };
36}
37
42template <is::anySplitComplexVectorOrExpression SrcAType, is::anySplitComplexVectorOrExpression SrcBType>
43constexpr auto operator+ (SrcAType&& srcA, SrcBType&& srcB)
44{
45 return makeSplitComplexExpression (srcA.real() + srcB.real(), srcA.imag() + srcB.imag());
46}
47
52template <is::anySplitComplexVectorOrExpression SrcAType, is::anySplitComplexVectorOrExpression SrcBType>
53constexpr auto operator- (SrcAType&& srcA, SrcBType&& srcB)
54{
55 return makeSplitComplexExpression (srcA.real() - srcB.real(), srcA.imag() - srcB.imag());
56}
57
62template <is::anySplitComplexVectorOrExpression SrcAType, is::anySplitComplexVectorOrExpression SrcBType>
63constexpr auto operator* (SrcAType&& srcA, SrcBType&& srcB)
64{
65 // (a + bi) * (c + di) = (ac - bd) + (ad + bc)i
66 // The sub-expressions are built inline rather than stored in local variables first: expressions hold lvalue
67 // sources by reference, so an expression built from locals would dangle after returning.
68 return makeSplitComplexExpression (srcA.real() * srcB.real() - srcA.imag() * srcB.imag(),
69 srcA.real() * srcB.imag() + srcA.imag() * srcB.real());
70}
71
76template <is::anySplitComplexVectorOrExpression SrcType>
77constexpr auto operator* (SrcType&& src, typename std::remove_cvref_t<SrcType>::value_type single)
78{
79 return makeSplitComplexExpression (src.real() * single, src.imag() * single);
80}
81
86template <is::anySplitComplexVectorOrExpression SrcType>
87constexpr auto operator* (SrcType&& src, std::complex<typename std::remove_cvref_t<SrcType>::value_type> single)
88{
89 return makeSplitComplexExpression (src.real() * single.real() - src.imag() * single.imag(),
90 src.real() * single.imag() + src.imag() * single.real());
91}
92
97template <is::anySplitComplexVectorOrExpression SrcType>
98constexpr auto operator/ (SrcType&& src, typename std::remove_cvref_t<SrcType>::value_type single)
99{
100 return makeSplitComplexExpression (src.real() / single, src.imag() / single);
101}
102
109template <is::anySplitComplexVectorOrExpression SrcType>
110constexpr auto operator/ (SrcType&& src, std::complex<typename std::remove_cvref_t<SrcType>::value_type> single)
111{
112 return std::forward<SrcType> (src) * (std::conj (single) / std::norm (single));
113}
114
119template <is::anySplitComplexVectorOrExpression SrcType>
120constexpr auto operator+ (SrcType&& src, std::complex<typename std::remove_cvref_t<SrcType>::value_type> single)
121{
122 return makeSplitComplexExpression (src.real() + single.real(), src.imag() + single.imag());
123}
124
129template <is::anySplitComplexVectorOrExpression SrcType>
130constexpr auto operator- (SrcType&& src, std::complex<typename std::remove_cvref_t<SrcType>::value_type> single)
131{
132 return makeSplitComplexExpression (src.real() - single.real(), src.imag() - single.imag());
133}
134
139template <is::anySplitComplexVectorOrExpression SrcAType, is::anySplitComplexVectorOrExpression SrcBType>
140constexpr auto operator/ (SrcAType&& srcA, SrcBType&& srcB)
141{
142 // (a + bi) / (c + di) = ((ac + bd) + (bc - ad)i) / (c² + d²)
143 // The denominator is built inline for both parts on purpose, see operator*.
144 return makeSplitComplexExpression ((srcA.real() * srcB.real() + srcA.imag() * srcB.imag()) / (srcB.real() * srcB.real() + srcB.imag() * srcB.imag()),
145 (srcA.imag() * srcB.real() - srcA.real() * srcB.imag()) / (srcB.real() * srcB.real() + srcB.imag() * srcB.imag()));
146}
147
152template <is::anySplitComplexVectorOrExpression SrcAType, is::anySplitComplexVectorOrExpression SrcBType>
153constexpr auto multiplyConjugate (SrcAType&& srcA, SrcBType&& srcB)
154{
155 return makeSplitComplexExpression (srcA.real() * srcB.real() + srcA.imag() * srcB.imag(),
156 srcA.imag() * srcB.real() - srcA.real() * srcB.imag());
157}
158
163template <is::anySplitComplexVectorOrExpression SrcType>
164constexpr auto splitComplexConjugate (SrcType&& src) // TODO: RENAME?
165{
166 return makeSplitComplexExpression (multiplyByConstant<+1> << src.real(), // TODO: REMOVE multiply!
167 multiplyByConstant<-1> << src.imag());
168}
169
174template <is::anySplitComplexVectorOrExpression SrcType>
175constexpr auto magnitudeSquared (SrcType&& src)
176{
177 return src.real() * src.real() + src.imag() * src.imag();
178}
179
184template <is::anySplitComplexVectorOrExpression SrcType>
185constexpr auto magnitude (SrcType&& src)
186{
187 return sqrt << magnitudeSquared (src);
188}
189
205template <is::anySplitComplexVectorOrExpression SrcType>
206constexpr auto realImagToComplex (SrcType&& src)
207{
208 // An rvalue split-complex expression hands over its component expressions, so that the returned expression doesn't
209 // refer to the temporary.
211 return realImagToComplex (std::forward<SrcType> (src).realExpression, std::forward<SrcType> (src).imagExpression);
212 else
213 return realImagToComplex (src.real(), src.imag());
214}
215} // namespace vctr
Evaluates to true if T derives from a specialization of SplitComplexExpression.
Definition: SplitComplex.h:97
constexpr auto makeSplitComplexExpression(RealExpression &&realExpression, ImagExpression &&imagExpression)
Combines real and imaginary expressions into a split-complex expression.
Definition: SplitComplexMath.h:32
constexpr auto magnitude(SrcType &&src)
Returns an expression containing the magnitude of each value in a split-complex source.
Definition: SplitComplexMath.h:185
constexpr auto operator*(SrcAType &&srcA, SrcBType &&srcB)
Returns an expression that multiplies split-complex vector or expression a with split-complex vector ...
Definition: SplitComplexMath.h:63
constexpr auto realImagToComplex(SrcType &&src)
Returns an expression that converts a split-complex source into interleaved std::complex values.
Definition: SplitComplexMath.h:206
constexpr auto magnitudeSquared(SrcType &&src)
Returns an expression containing the squared magnitude of each value in a split-complex source.
Definition: SplitComplexMath.h:175
constexpr ExpressionChainBuilder< expressions::Sqrt > sqrt
Computes the square root of the source values.
Definition: Sqrt.h:115
constexpr auto operator/(SrcType &&src, typename std::remove_cvref_t< SrcType >::value_type single)
Returns an expression that divides each value in a split-complex source by a real scalar.
Definition: SplitComplexMath.h:98
constexpr auto multiplyConjugate(SrcAType &&srcA, SrcBType &&srcB)
Returns an expression that multiplies split-complex source a by the complex conjugate of source b.
Definition: SplitComplexMath.h:153
constexpr auto splitComplexConjugate(SrcType &&src)
Returns an expression containing the complex conjugate of each value in a split-complex source.
Definition: SplitComplexMath.h:164
constexpr auto operator-(SrcAType &&srcA, SrcBType &&srcB)
Returns an expression that subtracts split-complex vector or expression b from split-complex vector o...
Definition: SplitComplexMath.h:53
constexpr auto operator+(SrcAType &&srcA, SrcBType &&srcB)
Returns an expression that adds two split-complex vector or expression sources.
Definition: SplitComplexMath.h:43
The main namespace of the VCTR project.
Definition: Array.h:24
An expression combining separate real and imaginary component expressions.
Definition: SplitComplex.h:70