VCTR
Loading...
Searching...
No Matches
SplitComplex.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
23#include <complex>
24
25namespace vctr
26{
27namespace detail
28{
35template <class T>
36struct SplitComplexContainerType
37{
38 using Type = T;
39};
40
42template <class T, template <class> class A>
43struct SplitComplexContainerType<Vector<std::complex<T>, A>>
44{
45 using Type = Vector<T, A>;
46};
47
52template <class T, size_t e, size_t s>
53struct SplitComplexContainerType<Array<std::complex<T>, e, s>>
54{
55 using Type = Array<T, e>;
56};
57} // namespace detail
58
59
67template <is::expression RealExpression, is::expression ImagExpression>
68requires (std::same_as<typename RealExpression::value_type, typename ImagExpression::value_type>)
70{
72 using value_type = typename RealExpression::value_type;
73
75 RealExpression realExpression;
76
78 ImagExpression imagExpression;
79
81 constexpr RealExpression& real() { return realExpression; }
82
84 constexpr const RealExpression& real() const { return realExpression; }
85
87 constexpr ImagExpression& imag() { return imagExpression; }
88
90 constexpr const ImagExpression& imag() const { return imagExpression; }
91};
92
93namespace is
94{
96template <class T>
97concept derivedFromSplitComplexExpression = requires (T& d) { [] <expression... Ts> (const SplitComplexExpression<Ts...>&) {} (d); };
98
100template <class T>
102} // namespace is
103
125template <is::anyVctrWithRealFloatValueType ContainerTypeParam>
127{
128public:
129 //==============================================================================
133 using ContainerType = std::remove_const_t<ContainerTypeParam>;
134
136 using value_type = std::remove_const_t<typename ContainerType::value_type>;
137
139 using ComplexType = std::complex<value_type>;
140
145 using element_type = std::remove_reference_t<decltype (*std::declval<ContainerType&>().data())>;
146
148 static constexpr bool isMutable = ! std::is_const_v<element_type>;
149
150 //==============================================================================
151 // Constructors
152 //==============================================================================
153
159 constexpr SplitComplexBase() requires std::default_initializable<ContainerType> = default;
160
162 explicit SplitComplexBase (size_t size)
163 requires is::vctrVector<ContainerType>
164 : realParts (size, value_type (0)),
165 imagParts (size, value_type (0))
166 { }
167
172 template <class Real, class Imag>
173 requires std::constructible_from<ContainerType, Real&&> && std::constructible_from<ContainerType, Imag&&>
174 constexpr SplitComplexBase (Real&& realPart, Imag&& imagPart)
175 : realParts (std::forward<Real> (realPart)),
176 imagParts (std::forward<Imag> (imagPart))
177 {
178 VCTR_ASSERT (realParts.size() == imagParts.size());
179 }
180
184 template <class Other>
185 requires (! std::same_as<Other, ContainerTypeParam>) && std::constructible_from<ContainerType, Span<value_type>>
187 : realParts (other.real()),
188 imagParts (other.imag())
189 { }
190
192 template <class Other>
193 requires (! std::same_as<Other, ContainerTypeParam>) && std::constructible_from<ContainerType, Span<const value_type>>
195 : realParts (other.real()),
196 imagParts (other.imag())
197 { }
198
204 template <is::anyVctrWithComplexFloatValueType Interleaved>
205 requires (! is::view<ContainerType>)
206 SplitComplexBase (const Interleaved& interleaved)
207 {
208 if constexpr (is::vctrVector<ContainerType>)
209 resize (interleaved.size());
210
211 *this = interleaved;
212 }
213
218 template <is::expression RealExpression, is::expression ImagExpression>
220 {
221 *this = expressions;
222 }
223
224 //==============================================================================
225 // Assignment
226 //==============================================================================
227 // Declaring the copy assignment operators below suppresses the implicit copy and move constructors, so they are
228 // explicitly defaulted here.
229
231 constexpr SplitComplexBase (const SplitComplexBase&) = default;
232
234 constexpr SplitComplexBase (SplitComplexBase&&) = default;
235
238 requires (! is::view<ContainerType>) = default;
239
242 requires (! is::view<ContainerType>) = default;
243
252 {
253 VCTR_ASSERT (size() == other.size());
254
255 realParts = other.real();
256 imagParts = other.imag();
257 return *this;
258 }
259
263 {
264 return *this = std::as_const (other);
265 }
266
271 template <is::expression RealExpression, is::expression ImagExpression>
272 requires isMutable
274 {
275 if constexpr (is::vctrVector<ContainerType>)
276 resize (expressions.realExpression.size());
277
278 assignSplitComplexExpression (expressions.realExpression, expressions.imagExpression);
279 }
280
282 template <class Other>
283 requires isMutable && (std::convertible_to<typename SplitComplexBase<Other>::value_type, value_type>)
284 constexpr void operator= (const SplitComplexBase<Other>& other)
285 {
286 VCTR_ASSERT (size() == other.size());
287
288 realParts = other.real();
289 imagParts = other.imag();
290 }
291
293 template <is::anyVctrWithComplexFloatValueType Interleaved>
294 requires isMutable
295 constexpr void operator= (const Interleaved& interleaved)
296 {
297 VCTR_ASSERT (size() == interleaved.size());
298
299 const auto* src = interleaved.data();
300 const auto n = size();
301
302 for (size_t i = 0; i < n; ++i)
303 {
304 realParts[i] = src[i].real();
305 imagParts[i] = src[i].imag();
306 }
307 }
308
309 //==============================================================================
310 // Element access
311 //==============================================================================
312
318 constexpr auto real() const { return Span (realParts); }
319
321 constexpr auto imag() const { return Span (imagParts); }
322
324 constexpr auto real() { return Span (realParts); }
325
327 constexpr auto imag() { return Span (imagParts); }
328
331 constexpr ComplexType get (size_t i) const { return { realParts[i], imagParts[i] }; }
332
334 constexpr void set (size_t i, value_type realValue, value_type imagValue)
335 requires isMutable
336 {
337 realParts[i] = realValue;
338 imagParts[i] = imagValue;
339 }
340
342 constexpr void set (size_t i, ComplexType value)
343 requires isMutable
344 {
345 set (i, value.real(), value.imag());
346 }
347
348 //==============================================================================
349 // Accessing sub spans
350 //==============================================================================
351 // These forward to the subSpan functions of the underlying containers, so they work for every container type and
352 // the returned SplitComplexSpan has the same extent and constness as a subSpan of the underlying container: It is
353 // read-only for a const owning container or a SplitComplexSpan<const T>, see real() const.
354
360 template <size_t startIdx>
361 constexpr auto subSpan()
362 {
363 return makeSplitComplexSpan (realParts.template subSpan<startIdx>(), imagParts.template subSpan<startIdx>());
364 }
365
371 template <size_t startIdx>
372 constexpr auto subSpan() const
373 {
374 return makeSplitComplexSpan (realParts.template subSpan<startIdx>(), imagParts.template subSpan<startIdx>());
375 }
376
381 constexpr auto subSpan (size_t startIdx)
382 {
383 return makeSplitComplexSpan (realParts.subSpan (startIdx), imagParts.subSpan (startIdx));
384 }
385
390 constexpr auto subSpan (size_t startIdx) const
391 {
392 return makeSplitComplexSpan (realParts.subSpan (startIdx), imagParts.subSpan (startIdx));
393 }
394
399 template <size_t startIdx, size_t numElements>
400 constexpr auto subSpan()
401 {
402 return makeSplitComplexSpan (realParts.template subSpan<startIdx, numElements>(), imagParts.template subSpan<startIdx, numElements>());
403 }
404
409 template <size_t startIdx, size_t numElements>
410 constexpr auto subSpan() const
411 {
412 return makeSplitComplexSpan (realParts.template subSpan<startIdx, numElements>(), imagParts.template subSpan<startIdx, numElements>());
413 }
414
419 constexpr auto subSpan (size_t startIdx, size_t numElements)
420 {
421 return makeSplitComplexSpan (realParts.subSpan (startIdx, numElements), imagParts.subSpan (startIdx, numElements));
422 }
423
428 constexpr auto subSpan (size_t startIdx, size_t numElements) const
429 {
430 return makeSplitComplexSpan (realParts.subSpan (startIdx, numElements), imagParts.subSpan (startIdx, numElements));
431 }
432
433 //==============================================================================
434 // Size and content manipulation
435 //==============================================================================
436
438 constexpr size_t size() const { return realParts.size(); }
439
441 constexpr bool empty() const { return realParts.empty(); }
442
444 constexpr void resize (size_t newSize)
446 {
447 realParts.resize (newSize);
448 imagParts.resize (newSize);
449 }
450
452 constexpr void fill (ComplexType value)
453 requires isMutable
454 {
455 realParts.fill (value.real());
456 imagParts.fill (value.imag());
457 }
458
463 constexpr void clear()
464 requires is::vctrVector<ContainerType>
465 {
466 realParts.clear();
467 imagParts.clear();
468 }
469
470 //==============================================================================
471 // Math operators.
472 //==============================================================================
473 // The compound assignment operators build the split-complex expressions defined in SplitComplexMath.h and
474 // assign them to this container. The assignment evaluates the real and the imaginary parts in a single pass,
475 // so it is safe that this container is also a source of the expression.
476
478 template <is::anySplitComplexVectorOrExpression V>
479 constexpr void operator*= (const V& v)
480 requires isMutable
481 {
482 const auto& self = *this;
483 *this = self * v;
484 }
485
487 constexpr void operator*= (value_type c)
488 requires isMutable
489 {
490 const auto& self = *this;
491 *this = self * c;
492 }
493
495 constexpr void operator*= (ComplexType c)
496 requires isMutable
497 {
498 const auto& self = *this;
499 *this = self * c;
500 }
501
503 template <is::anySplitComplexVectorOrExpression V>
504 constexpr void operator/= (const V& v)
505 requires isMutable
506 {
507 const auto& self = *this;
508 *this = self / v;
509 }
510
512 constexpr void operator/= (value_type c)
513 requires isMutable
514 {
515 const auto& self = *this;
516 *this = self / c;
517 }
518
520 constexpr void operator/= (ComplexType c)
521 requires isMutable
522 {
523 const auto& self = *this;
524 *this = self / c;
525 }
526
528 template <is::anySplitComplexVectorOrExpression V>
529 constexpr void operator+= (const V& v)
530 requires isMutable
531 {
532 const auto& self = *this;
533 *this = self + v;
534 }
535
537 constexpr void operator+= (value_type c)
538 requires isMutable
539 {
540 const auto& self = *this;
541 *this = self + ComplexType (c);
542 }
543
545 constexpr void operator+= (ComplexType c)
546 requires isMutable
547 {
548 const auto& self = *this;
549 *this = self + c;
550 }
551
553 template <is::anySplitComplexVectorOrExpression V>
554 constexpr void operator-= (const V& v)
555 requires isMutable
556 {
557 const auto& self = *this;
558 *this = self - v;
559 }
560
562 constexpr void operator-= (value_type c)
563 requires isMutable
564 {
565 const auto& self = *this;
566 *this = self - ComplexType (c);
567 }
568
570 constexpr void operator-= (ComplexType c)
571 requires isMutable
572 {
573 const auto& self = *this;
574 *this = self - c;
575 }
576
577private:
578 //==============================================================================
582 template <class SpanType>
583 static constexpr auto makeSplitComplexSpan (SpanType realSpan, SpanType imagSpan)
584 {
585 return SplitComplexBase<SpanType> (realSpan, imagSpan);
586 }
587
600 template <class RealExpression, class ImagExpression>
601 constexpr void assignSplitComplexExpression (const RealExpression& re, const ImagExpression& im)
602 {
603 VCTR_ASSERT (re.size() == size());
604 VCTR_ASSERT (im.size() == size());
605
606 if (! std::is_constant_evaluated())
607 {
608 if constexpr (has::getNeon<RealExpression> && has::getNeon<ImagExpression>)
609 {
610 assignSplitComplexExpressionNeon (re, im);
611 return;
612 }
613
614 if constexpr (has::getAVX<RealExpression> && has::getAVX<ImagExpression>)
615 {
616 if (Config::supportedCPUInstructionSets.fma)
617 {
618 assignSplitComplexExpressionFMA (re, im);
619 return;
620 }
621 }
622
623 if constexpr (has::getSSE<RealExpression> && has::getSSE<ImagExpression>)
624 {
625 if (Config::supportedCPUInstructionSets.sse4_1)
626 {
627 assignSplitComplexExpressionSSE4_1 (re, im);
628 return;
629 }
630 }
631 }
632
633 assignSplitComplexExpressionScalar (re, im, 0);
634 }
635
637 template <class RealExpression, class ImagExpression>
638 constexpr void assignSplitComplexExpressionScalar (const RealExpression& re, const ImagExpression& im, size_t startIdx)
639 {
640 auto* r = realParts.data();
641 auto* i = imagParts.data();
642 const auto n = size();
643
644 for (auto idx = startIdx; idx < n; ++idx)
645 {
646 const value_type realValue = re[idx];
647 const value_type imagValue = im[idx];
648 r[idx] = realValue;
649 i[idx] = imagValue;
650 }
651 }
652
654 template <class RealExpression, class ImagExpression>
655 void assignSplitComplexExpressionNeon (const RealExpression& re, const ImagExpression& im)
656 requires Config::archARM
657 {
658 constexpr auto inc = NeonRegister<value_type>::numElements;
659 const auto nSIMD = detail::previousMultipleOf<inc> (size());
660
661 re.prepareNeonEvaluation();
662 im.prepareNeonEvaluation();
663 auto* r = realParts.data();
664 auto* i = imagParts.data();
665
666 size_t idx = 0;
667 for (; idx < nSIMD; idx += inc)
668 {
669 const auto realValues = re.getNeon (idx);
670 const auto imagValues = im.getNeon (idx);
671 realValues.store (r + idx);
672 imagValues.store (i + idx);
673 }
674
675 assignSplitComplexExpressionScalar (re, im, idx);
676 }
677
681 template <class RealExpression, class ImagExpression>
682 VCTR_TARGET ("fma")
683 void assignSplitComplexExpressionFMA (const RealExpression& re, const ImagExpression& im)
684 requires Config::archX64
685 {
686 constexpr auto inc = AVXRegister<value_type>::numElements;
687 const auto nSIMD = detail::previousMultipleOf<inc> (size());
688
689 re.prepareAVXEvaluation();
690 im.prepareAVXEvaluation();
691 auto* r = realParts.data();
692 auto* i = imagParts.data();
693
694 size_t idx = 0;
695 for (; idx < nSIMD; idx += inc)
696 {
697 const auto realValues = re.getAVX (idx);
698 const auto imagValues = im.getAVX (idx);
699 realValues.storeUnaligned (r + idx);
700 imagValues.storeUnaligned (i + idx);
701 }
702
703 assignSplitComplexExpressionScalar (re, im, idx);
704 }
705
709 template <class RealExpression, class ImagExpression>
710 VCTR_TARGET ("sse4.1")
711 void assignSplitComplexExpressionSSE4_1 (const RealExpression& re, const ImagExpression& im)
712 requires Config::archX64
713 {
714 constexpr auto inc = SSERegister<value_type>::numElements;
715 const auto nSIMD = detail::previousMultipleOf<inc> (size());
716
717 re.prepareSSEEvaluation();
718 im.prepareSSEEvaluation();
719 auto* r = realParts.data();
720 auto* i = imagParts.data();
721
722 size_t idx = 0;
723 for (; idx < nSIMD; idx += inc)
724 {
725 const auto realValues = re.getSSE (idx);
726 const auto imagValues = im.getSSE (idx);
727 realValues.storeUnaligned (r + idx);
728 imagValues.storeUnaligned (i + idx);
729 }
730
731 assignSplitComplexExpressionScalar (re, im, idx);
732 }
733
734 //==============================================================================
736 ContainerType realParts;
737
739 ContainerType imagParts;
740};
741
743template <class Interleaved>
745
749template <is::expression RealExpression, is::expression ImagExpression>
751
758template <class LhsContainerType, class RhsContainerType>
759requires std::same_as<typename SplitComplexBase<LhsContainerType>::value_type, typename SplitComplexBase<RhsContainerType>::value_type>
761{
762 return lhs.real() == rhs.real() && lhs.imag() == rhs.imag();
763}
764
765//==============================================================================
767template <is::realFloatNumber T>
769
771template <is::realFloatNumber T, size_t extent>
773
775template <is::realFloatNumber T, size_t extent = std::dynamic_extent>
777} // namespace vctr
The view type.
Definition: Span.h:66
A container for complex values that stores the real and imaginary parts in two separate,...
Definition: SplitComplex.h:127
constexpr SplitComplexBase(SplitComplexExpression< RealExpression, ImagExpression > &&expressions)
Creates a split-complex container from a split-complex expression.
Definition: SplitComplex.h:219
constexpr SplitComplexBase(const SplitComplexBase &)=default
Copy constructor.
std::complex< value_type > ComplexType
The std::complex type matching value_type, returned by the element access functions.
Definition: SplitComplex.h:139
constexpr SplitComplexBase(SplitComplexBase &&)=default
Move constructor.
constexpr SplitComplexBase(Real &&realPart, Imag &&imagPart)
Creates a SplitComplexBase from a real and an imaginary part container of the same size.
Definition: SplitComplex.h:174
constexpr void set(size_t i, ComplexType value)
Sets the complex value at index i.
Definition: SplitComplex.h:342
constexpr auto real()
Returns a view of the real parts.
Definition: SplitComplex.h:324
constexpr void set(size_t i, value_type realValue, value_type imagValue)
Sets the real and imaginary part at index i.
Definition: SplitComplex.h:334
constexpr auto subSpan()
Returns a SplitComplexSpan that views numElements elements, starting at startIdx.
Definition: SplitComplex.h:400
constexpr SplitComplexBase()=default
Creates a default constructed SplitComplexBase.
std::remove_reference_t< decltype(*std::declval< ContainerType & >().data())> element_type
The element type of the underlying containers, which is const qualified for a SplitComplexSpan<const ...
Definition: SplitComplex.h:145
constexpr auto imag() const
Returns a view of the imaginary parts.
Definition: SplitComplex.h:321
constexpr void operator*=(const V &v)
Multiplies this by a split-complex vector or expression in place.
Definition: SplitComplex.h:479
constexpr void operator/=(const V &v)
Divides this by a split-complex vector or expression in place.
Definition: SplitComplex.h:504
constexpr void operator+=(const V &v)
Adds a split-complex vector or expression to this in place.
Definition: SplitComplex.h:529
constexpr void clear()
Removes all elements, like std::vector::clear.
Definition: SplitComplex.h:463
constexpr auto real() const
Returns a view of the real parts.
Definition: SplitComplex.h:318
constexpr auto subSpan() const
Returns a SplitComplexSpan that views the elements from startIdx to the end.
Definition: SplitComplex.h:372
constexpr auto subSpan(size_t startIdx) const
Returns a SplitComplexSpan that views the elements from startIdx to the end.
Definition: SplitComplex.h:390
constexpr auto subSpan() const
Returns a SplitComplexSpan that views numElements elements, starting at startIdx.
Definition: SplitComplex.h:410
static constexpr bool isMutable
True if the elements can be modified through this container, false for a SplitComplexSpan<const T>.
Definition: SplitComplex.h:148
constexpr SplitComplexBase & operator=(const SplitComplexBase &)=default
Copy and move assignment of owning containers, e.g.
std::remove_const_t< typename ContainerType::value_type > value_type
The real floating point type of the real and imaginary parts, e.g.
Definition: SplitComplex.h:136
constexpr auto subSpan(size_t startIdx)
Returns a SplitComplexSpan that views the elements from startIdx to the end.
Definition: SplitComplex.h:381
constexpr ComplexType get(size_t i) const
Returns a copy of the complex value at index i.
Definition: SplitComplex.h:331
constexpr bool empty() const
Returns true if the container holds no values.
Definition: SplitComplex.h:441
constexpr auto subSpan(size_t startIdx, size_t numElements)
Returns a SplitComplexSpan that views numElements elements, starting at startIdx.
Definition: SplitComplex.h:419
std::remove_const_t< ContainerTypeParam > ContainerType
The type of the containers storing the real and imaginary parts.
Definition: SplitComplex.h:133
constexpr auto subSpan(size_t startIdx, size_t numElements) const
Returns a SplitComplexSpan that views numElements elements, starting at startIdx.
Definition: SplitComplex.h:428
constexpr size_t size() const
Returns the number of complex values.
Definition: SplitComplex.h:438
constexpr auto imag()
Returns a view of the imaginary parts.
Definition: SplitComplex.h:327
constexpr void fill(ComplexType value)
Sets all elements to the given complex value.
Definition: SplitComplex.h:452
constexpr void operator-=(const V &v)
Subtracts a split-complex vector or expression from this in place.
Definition: SplitComplex.h:554
constexpr auto subSpan()
Returns a SplitComplexSpan that views the elements from startIdx to the end.
Definition: SplitComplex.h:361
constexpr void resize(size_t newSize)
Resizes both parts.
Definition: SplitComplex.h:444
Evaluates to true if T is a split-complex container or split-complex expression.
Definition: SplitComplex.h:101
Evaluates to true if T derives from a specialization of SplitComplexBase.
Definition: ContainerAndExpressionConcepts.h:472
Evaluates to true if T derives from a specialization of SplitComplexExpression.
Definition: SplitComplex.h:97
Constrains a type to be an expression template.
Definition: ContainerAndExpressionConcepts.h:263
Constrains a type to be any instance of vctr::Vector (not std::vector).
Definition: ContainerAndExpressionConcepts.h:291
Constrains a type to be a view rather than an owning container.
Definition: ContainerAndExpressionConcepts.h:287
constexpr ExpressionChainBuilder< expressions::Forward > forward
An expression that forwards the source unaltered.
Definition: Forward.h:89
The main namespace of the VCTR project.
Definition: Array.h:24
constexpr bool operator==(const SplitComplexBase< LhsContainerType > &lhs, const SplitComplexBase< RhsContainerType > &rhs)
Compares lhs and rhs for equality.
Definition: SplitComplex.h:760
An expression combining separate real and imaginary component expressions.
Definition: SplitComplex.h:70
ImagExpression imagExpression
The expression producing the imaginary parts.
Definition: SplitComplex.h:78
constexpr ImagExpression & imag()
Returns the expression producing the imaginary parts.
Definition: SplitComplex.h:87
RealExpression realExpression
The expression producing the real parts.
Definition: SplitComplex.h:75
constexpr RealExpression & real()
Returns the expression producing the real parts.
Definition: SplitComplex.h:81
typename RealExpression::value_type value_type
The real valued type both component expressions produce.
Definition: SplitComplex.h:72
constexpr const RealExpression & real() const
Returns the expression producing the real parts.
Definition: SplitComplex.h:84
constexpr const ImagExpression & imag() const
Returns the expression producing the imaginary parts.
Definition: SplitComplex.h:90