xcal
基于 C++23 的现代图形渲染引擎
载入中...
搜索中...
未找到
complex.hpp
浏览该文件的文档.
1// complex.hpp
2#pragma once
3#ifndef XCMATH_COMPLEX_H
4#define XCMATH_COMPLEX_H
5#include <concepts>
6#include <cstdint>
7#include <type_traits>
8
9#include "../utils/compiling.hpp"
10#include "../utils/concepts.h"
11
12namespace xcmath {
13
14template <typename T>
15class complex {
16 public:
19 static constexpr auto Type = TypeName<T>;
20
21 complex(const T& real, const T& imag) : real(real), imag(imag) {}
22 complex(const T& real)
23 requires std::is_default_constructible_v<T>
24 : complex(real, T{}) {}
26 requires std::is_default_constructible_v<T>
27 : complex(T{}, T{}) {}
28
29 template <typename Vtp2>
30 requires std::convertible_to<Vtp2, T>
31 complex(const T& real, const Vtp2& imag) : complex(real, T(imag)) {}
32
33 template <typename Vtp1>
34 requires std::convertible_to<Vtp1, T>
35 complex(const Vtp1& real, const T& imag) : complex(T(real), imag) {}
36
37 complex operator+(const complex& other) const
38 requires concepts::Add<T>
39 {
40 return complex(real + other.real, imag + other.imag);
41 }
42 complex operator-(const complex& other) const
44 {
45 return complex(real - other.real, imag - other.imag);
46 }
48 requires concepts::Minus<T>
49 {
50 return complex(-real, -imag);
51 }
52 complex operator*(const complex& other) const
54 {
55 return complex(real * other.real - imag * other.imag,
56 real * other.imag + imag * other.real);
57 }
58 complex operator/(const complex& other) const
59 requires concepts::Divide<T>
60 {
61 return *this * other.inv();
62 }
63 template <typename Res = double>
64 Res mod()
65 requires concepts::Sqrt<T>
66 {
67 return sqrt(real * real + imag * imag);
68 }
70 requires concepts::Divide<T>
71 {
72 auto mod_2 = real * real + imag * imag;
73 return complex(real / mod_2, -imag / mod_2);
74 }
75 template <class V>
76 requires std::convertible_to<T, V>
77 operator complex<V>() {
78 return complex<V>((V)real, (V)imag);
79 }
80}; // class complex
81
82inline xcmath::complex<long long> operator""_i(unsigned long long x) {
83 return xcmath::complex<long long>((long long)0, (long long)x);
84};
85inline xcmath::complex<long double> operator""_i(long double x) {
86 return xcmath::complex<long double>((long double)0, (long double)x);
87}
88
96
97} // namespace xcmath
98
99#endif // XCMATH_COMPLEX_H
Complex number class template
Definition complex.hpp:15
complex(const T &real, const Vtp2 &imag)
Definition complex.hpp:31
static constexpr auto Type
Definition complex.hpp:19
complex(const Vtp1 &real, const T &imag)
Definition complex.hpp:35
complex operator/(const complex &other) const
Definition complex.hpp:58
complex(const T &real, const T &imag)
Definition complex.hpp:21
complex operator-(const complex &other) const
Definition complex.hpp:42
complex< T > inv()
Definition complex.hpp:69
complex(const T &real)
Definition complex.hpp:22
complex operator+(const complex &other) const
Definition complex.hpp:37
complex operator*(const complex &other) const
Definition complex.hpp:52
complex operator-() const
Definition complex.hpp:47
Compiler-specific type information handling for MSVC
Definition complex.hpp:12