29 Numerics library [numerics]

29.5 Complex numbers [complex.numbers]

Заголовок определяет шаблон класса и многочисленные функции для представления и управления комплексными числами.<complex>

Эффект обработки шаблона complex для любого другого типа , чем float, doubleили long double не определен. Специальности complex<float>, complex<double>и complex<long double> являются literal types.

Если результат функции не определен математически или не входит в диапазон представимых значений для ее типа, поведение не определено.

Если z это lvalue-выражение типа, cv complex<T> то:

  • выражение reinterpret_­cast<cv T(&)[2]>(z) должно быть правильным,

  • reinterpret_­cast<cv T(&)[2]>(z)[0] обозначает действительную часть z, а

  • reinterpret_­cast<cv T(&)[2]>(z)[1] обозначают мнимую часть z.

Более того, если a это выражение типа, cv complex<T>* а выражение a[i] четко определено для целочисленного выражения i, то:

  • reinterpret_­cast<cv T*>(a)[2*i] обозначает действительную часть a[i], а

  • reinterpret_­cast<cv T*>(a)[2*i + 1] обозначают мнимую часть a[i].

29.5.1 Header <complex> synopsis [complex.syn]

namespace std {
  template<class T> class complex;
  template<> class complex<float>;
  template<> class complex<double>;
  template<> class complex<long double>;

  // [complex.ops], operators
  template<class T>
    complex<T> operator+(const complex<T>&, const complex<T>&);
  template<class T> complex<T> operator+(const complex<T>&, const T&);
  template<class T> complex<T> operator+(const T&, const complex<T>&);

  template<class T> complex<T> operator-(
    const complex<T>&, const complex<T>&);
  template<class T> complex<T> operator-(const complex<T>&, const T&);
  template<class T> complex<T> operator-(const T&, const complex<T>&);

  template<class T> complex<T> operator*(
    const complex<T>&, const complex<T>&);
  template<class T> complex<T> operator*(const complex<T>&, const T&);
  template<class T> complex<T> operator*(const T&, const complex<T>&);

  template<class T> complex<T> operator/(
    const complex<T>&, const complex<T>&);
  template<class T> complex<T> operator/(const complex<T>&, const T&);
  template<class T> complex<T> operator/(const T&, const complex<T>&);

  template<class T> complex<T> operator+(const complex<T>&);
  template<class T> complex<T> operator-(const complex<T>&);

  template<class T> constexpr bool operator==(
    const complex<T>&, const complex<T>&);
  template<class T> constexpr bool operator==(const complex<T>&, const T&);
  template<class T> constexpr bool operator==(const T&, const complex<T>&);

  template<class T> constexpr bool operator!=(const complex<T>&, const complex<T>&);
  template<class T> constexpr bool operator!=(const complex<T>&, const T&);
  template<class T> constexpr bool operator!=(const T&, const complex<T>&);

  template<class T, class charT, class traits>
  basic_istream<charT, traits>&
  operator>>(basic_istream<charT, traits>&, complex<T>&);

  template<class T, class charT, class traits>
  basic_ostream<charT, traits>&
  operator<<(basic_ostream<charT, traits>&, const complex<T>&);

  // [complex.value.ops], values
  template<class T> constexpr T real(const complex<T>&);
  template<class T> constexpr T imag(const complex<T>&);

  template<class T> T abs(const complex<T>&);
  template<class T> T arg(const complex<T>&);
  template<class T> T norm(const complex<T>&);

  template<class T> complex<T> conj(const complex<T>&);
  template<class T> complex<T> proj(const complex<T>&);
  template<class T> complex<T> polar(const T&, const T& = 0);

  // [complex.transcendentals], transcendentals
  template<class T> complex<T> acos(const complex<T>&);
  template<class T> complex<T> asin(const complex<T>&);
  template<class T> complex<T> atan(const complex<T>&);

  template<class T> complex<T> acosh(const complex<T>&);
  template<class T> complex<T> asinh(const complex<T>&);
  template<class T> complex<T> atanh(const complex<T>&);

  template<class T> complex<T> cos  (const complex<T>&);
  template<class T> complex<T> cosh (const complex<T>&);
  template<class T> complex<T> exp  (const complex<T>&);
  template<class T> complex<T> log  (const complex<T>&);
  template<class T> complex<T> log10(const complex<T>&);

  template<class T> complex<T> pow  (const complex<T>&, const T&);
  template<class T> complex<T> pow  (const complex<T>&, const complex<T>&);
  template<class T> complex<T> pow  (const T&, const complex<T>&);

  template<class T> complex<T> sin  (const complex<T>&);
  template<class T> complex<T> sinh (const complex<T>&);
  template<class T> complex<T> sqrt (const complex<T>&);
  template<class T> complex<T> tan  (const complex<T>&);
  template<class T> complex<T> tanh (const complex<T>&);

  // [complex.literals], complex literals
  inline namespace literals {
    inline namespace complex_literals {
      constexpr complex<long double> operator""il(long double);
      constexpr complex<long double> operator""il(unsigned long long);
      constexpr complex<double> operator""i(long double);
      constexpr complex<double> operator""i(unsigned long long);
      constexpr complex<float> operator""if(long double);
      constexpr complex<float> operator""if(unsigned long long);
    }
  }
}

29.5.2 Class template complex [complex]

namespace std {
  template<class T>
  class complex {
  public:
    using value_type = T;

    constexpr complex(const T& re = T(), const T& im = T());
    constexpr complex(const complex&);
    template<class X> constexpr complex(const complex<X>&);

    constexpr T real() const;
    void real(T);
    constexpr T imag() const;
    void imag(T);

    complex<T>& operator= (const T&);
    complex<T>& operator+=(const T&);
    complex<T>& operator-=(const T&);
    complex<T>& operator*=(const T&);
    complex<T>& operator/=(const T&);

    complex& operator=(const complex&);
    template<class X> complex<T>& operator= (const complex<X>&);
    template<class X> complex<T>& operator+=(const complex<X>&);
    template<class X> complex<T>& operator-=(const complex<X>&);
    template<class X> complex<T>& operator*=(const complex<X>&);
    template<class X> complex<T>& operator/=(const complex<X>&);
  };
}

Класс complex описывает объект, который может хранить декартовы компоненты real() и imag()комплексного числа.

29.5.3 complex specializations [complex.special]

namespace std {
  template<> class complex<float> {
  public:
    using value_type = float;

    constexpr complex(float re = 0.0f, float im = 0.0f);
    constexpr explicit complex(const complex<double>&);
    constexpr explicit complex(const complex<long double>&);

    constexpr float real() const;
    void real(float);
    constexpr float imag() const;
    void imag(float);

    complex<float>& operator= (float);
    complex<float>& operator+=(float);
    complex<float>& operator-=(float);
    complex<float>& operator*=(float);
    complex<float>& operator/=(float);

    complex<float>& operator=(const complex<float>&);
    template<class X> complex<float>& operator= (const complex<X>&);
    template<class X> complex<float>& operator+=(const complex<X>&);
    template<class X> complex<float>& operator-=(const complex<X>&);
    template<class X> complex<float>& operator*=(const complex<X>&);
    template<class X> complex<float>& operator/=(const complex<X>&);
  };

  template<> class complex<double> {
  public:
    using value_type = double;

    constexpr complex(double re = 0.0, double im = 0.0);
    constexpr complex(const complex<float>&);
    constexpr explicit complex(const complex<long double>&);

    constexpr double real() const;
    void real(double);
    constexpr double imag() const;
    void imag(double);

    complex<double>& operator= (double);
    complex<double>& operator+=(double);
    complex<double>& operator-=(double);
    complex<double>& operator*=(double);
    complex<double>& operator/=(double);

    complex<double>& operator=(const complex<double>&);
    template<class X> complex<double>& operator= (const complex<X>&);
    template<class X> complex<double>& operator+=(const complex<X>&);
    template<class X> complex<double>& operator-=(const complex<X>&);
    template<class X> complex<double>& operator*=(const complex<X>&);
    template<class X> complex<double>& operator/=(const complex<X>&);
  };

  template<> class complex<long double> {
  public:
    using value_type = long double;

    constexpr complex(long double re = 0.0L, long double im = 0.0L);
    constexpr complex(const complex<float>&);
    constexpr complex(const complex<double>&);

    constexpr long double real() const;
    void real(long double);
    constexpr long double imag() const;
    void imag(long double);

    complex<long double>& operator=(const complex<long double>&);
    complex<long double>& operator= (long double);
    complex<long double>& operator+=(long double);
    complex<long double>& operator-=(long double);
    complex<long double>& operator*=(long double);
    complex<long double>& operator/=(long double);

    template<class X> complex<long double>& operator= (const complex<X>&);
    template<class X> complex<long double>& operator+=(const complex<X>&);
    template<class X> complex<long double>& operator-=(const complex<X>&);
    template<class X> complex<long double>& operator*=(const complex<X>&);
    template<class X> complex<long double>& operator/=(const complex<X>&);
  };
}

29.5.4 complex member functions [complex.members]

template<class T> constexpr complex(const T& re = T(), const T& im = T());

Effects: Создает объект класса complex.

Postconditions: real() == re && imag() == im.

constexpr T real() const;

Returns: Стоимость реальной составляющей.

void real(T val);

Effects: Присваивается val к реальному компоненту.

constexpr T imag() const;

Returns: Значение мнимой составляющей.

void imag(T val);

Effects: Присваивается val мнимой составляющей.

29.5.5 complex member operators [complex.member.ops]

complex<T>& operator+=(const T& rhs);

Effects: Добавляет скалярное значение rhs к действительной части комплексного значения *this и сохраняет результат в действительной части *this, оставляя мнимую часть неизменной.

Returns: *this.

complex<T>& operator-=(const T& rhs);

Effects: Вычитает скалярное значение rhs из действительной части комплексного значения *this и сохраняет результат в действительной части *this, оставляя мнимую часть неизменной.

Returns: *this.

complex<T>& operator*=(const T& rhs);

Effects: Умножает скалярное значение rhs на комплексное значение *this и сохраняет результат в формате *this.

Returns: *this.

complex<T>& operator/=(const T& rhs);

Effects: Делит скалярное значение rhs на комплексное *this и сохраняет результат в формате *this.

Returns: *this.

template<class X> complex<T>& operator+=(const complex<X>& rhs);

Effects: Добавляет комплексное значение rhs к комплексному значению *this и сохраняет сумму в *this.

Returns: *this.

template<class X> complex<T>& operator-=(const complex<X>& rhs);

Effects: Вычитает комплексное значение rhs из комплексного значения *this и сохраняет разницу в *this.

Returns: *this.

template<class X> complex<T>& operator*=(const complex<X>& rhs);

Effects: Умножает комплексное значение rhs на комплексное значение *this и сохраняет продукт в *this.

Returns: *this.

template<class X> complex<T>& operator/=(const complex<X>& rhs);

Effects: Делит комплексное значение rhs на комплексное *this и сохраняет частное в *this.

Returns: *this.

29.5.6 complex non-member operations [complex.ops]

template<class T> complex<T> operator+(const complex<T>& lhs);

Returns: complex<T>(lhs).

Remarks: унарный оператор.

template<class T> complex<T> operator+(const complex<T>& lhs, const complex<T>& rhs); template<class T> complex<T> operator+(const complex<T>& lhs, const T& rhs); template<class T> complex<T> operator+(const T& lhs, const complex<T>& rhs);

Returns: complex<T>(lhs) += rhs.

template<class T> complex<T> operator-(const complex<T>& lhs);

Returns: complex<T>(-lhs.real(),-lhs.imag()).

Remarks: унарный оператор.

template<class T> complex<T> operator-(const complex<T>& lhs, const complex<T>& rhs); template<class T> complex<T> operator-(const complex<T>& lhs, const T& rhs); template<class T> complex<T> operator-(const T& lhs, const complex<T>& rhs);

Returns: complex<T>(lhs) -= rhs.

template<class T> complex<T> operator*(const complex<T>& lhs, const complex<T>& rhs); template<class T> complex<T> operator*(const complex<T>& lhs, const T& rhs); template<class T> complex<T> operator*(const T& lhs, const complex<T>& rhs);

Returns: complex<T>(lhs) *= rhs.

template<class T> complex<T> operator/(const complex<T>& lhs, const complex<T>& rhs); template<class T> complex<T> operator/(const complex<T>& lhs, const T& rhs); template<class T> complex<T> operator/(const T& lhs, const complex<T>& rhs);

Returns: complex<T>(lhs) /= rhs.

template<class T> constexpr bool operator==(const complex<T>& lhs, const complex<T>& rhs); template<class T> constexpr bool operator==(const complex<T>& lhs, const T& rhs); template<class T> constexpr bool operator==(const T& lhs, const complex<T>& rhs);

Returns: lhs.real() == rhs.real() && lhs.imag() == rhs.imag().

Remarks: Предполагается, что мнимая часть аргументов равна T()0,0 T.

template<class T> constexpr bool operator!=(const complex<T>& lhs, const complex<T>& rhs); template<class T> constexpr bool operator!=(const complex<T>& lhs, const T& rhs); template<class T> constexpr bool operator!=(const T& lhs, const complex<T>& rhs);

Returns: rhs.real() != lhs.real() || rhs.imag() != lhs.imag().

template<class T, class charT, class traits> basic_istream<charT, traits>& operator>>(basic_istream<charT, traits>& is, complex<T>& x);

Requires: Входные значения должны быть преобразованы в T.

Effects: Экстракты комплексного числа x в виде: u, (u)или (u,v), где u это действительная часть и v мнимая часть ([istream.formatted]).

Если обнаруживается неправильный ввод, вызывается is.setstate(ios_­base​::​failbit) (который может throw ios​::​failure ([iostate.flags])).

Returns: is.

Remarks: Эта экстракция выполняется как серия более простых экстракций. Таким образом, пропуск пробелов должен быть одинаковым для всех более простых извлечений.

template<class T, class charT, class traits> basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>& o, const complex<T>& x);

Effects: Вставляет комплексное число x в поток, o как если бы это было реализовано следующим образом:

basic_ostringstream<charT, traits> s;
s.flags(o.flags());
s.imbue(o.getloc());
s.precision(o.precision());
s << '(' << x.real() << "," << x.imag() << ')';
return o << s.str();

[ Note: В языковом стандарте, в котором запятая используется в качестве символа десятичной точки, использование запятой в качестве разделителя полей может быть неоднозначным. Вставка showpoint в выходной поток заставляет все выходные данные показывать явный десятичный знак; в результате все вставленные последовательности комплексных чисел могут быть однозначно извлечены. ] end note

29.5.7 complex value operations [complex.value.ops]

template<class T> constexpr T real(const complex<T>& x);

Returns: x.real().

template<class T> constexpr T imag(const complex<T>& x);

Returns: x.imag().

template<class T> T abs(const complex<T>& x);

Returns: Величина x.

template<class T> T arg(const complex<T>& x);

Returns: Фазовый угол x, или atan2(imag(x), real(x)).

template<class T> T norm(const complex<T>& x);

Returns: Квадрат величины x.

template<class T> complex<T> conj(const complex<T>& x);

Returns: Комплексное сопряжение x.

template<class T> complex<T> proj(const complex<T>& x);

Returns: Проекция x на сферу Римана.

Remarks: Ведет себя так же, как функция C cproj, определенная в 7.3.9.4.

template<class T> complex<T> polar(const T& rho, const T& theta = 0);

Requires: rho должно быть неотрицательным и отличным от NaN. theta будет конечным.

Returns: complex Значение , соответствующее комплексное число, величина которого rho и чье фазовый угол theta.

29.5.8 complex transcendentals [complex.transcendentals]

template<class T> complex<T> acos(const complex<T>& x);

Returns: Комплексный арккосинус x.

Remarks: Действует так же, как функция C cacos, определенная в 7.3.5.1.

template<class T> complex<T> asin(const complex<T>& x);

Returns: Сложный арксинус x.

Remarks: Ведет себя так же, как функция C casin, определенная в 7.3.5.2.

template<class T> complex<T> atan(const complex<T>& x);

Returns: Комплексная арктангенс x.

Remarks: Действует так же, как функция C catan, определенная в 7.3.5.3.

template<class T> complex<T> acosh(const complex<T>& x);

Returns: Комплексный арочный гиперболический косинус x.

Remarks: Действует так же, как функция C cacosh, определенная в 7.3.6.1.

template<class T> complex<T> asinh(const complex<T>& x);

Returns: Сложный дуговый гиперболический синус x.

Remarks: Ведет себя так же, как функция C casinh, определенная в 7.3.6.2.

template<class T> complex<T> atanh(const complex<T>& x);

Returns: Комплексный арочный гиперболический тангенс x.

Remarks: Действует так же, как функция C catanh, определенная в 7.3.6.3.

template<class T> complex<T> cos(const complex<T>& x);

Returns: Комплексный косинус x.

template<class T> complex<T> cosh(const complex<T>& x);

Returns: Комплексный гиперболический косинус x.

template<class T> complex<T> exp(const complex<T>& x);

Returns: Комплексное основание -e экспонента x.

template<class T> complex<T> log(const complex<T>& x);

Returns: Комплексный натуральныйeлогарифм по основанию x. Для всех x, imag(log(x)) лежит в интервале [π, π], а когда x отрицательное вещественное число, imag(log(x)) является π.

Remarks: Срезы ответвлений проходят по отрицательной действительной оси.

template<class T> complex<T> log10(const complex<T>& x);

Returns: Комплексный общий (основанный10) логарифм числа x, определяемый как log(x) / log(10).

Remarks: Срезы ответвлений проходят по отрицательной действительной оси.

template<class T> complex<T> pow(const complex<T>& x, const complex<T>& y); template<class T> complex<T> pow(const complex<T>& x, const T& y); template<class T> complex<T> pow(const T& x, const complex<T>& y);

Returns: Комплексная мощность базы x возведена в yth степень, определяемую как exp(y * log(x)). Возвращаемое значение pow(0, 0) определяется реализацией.

Remarks: Срезы ответвлений проходят по отрицательной действительной оси.

template<class T> complex<T> sin(const complex<T>& x);

Returns: Сложный синус x.

template<class T> complex<T> sinh(const complex<T>& x);

Returns: Сложный гиперболический синус x.

template<class T> complex<T> sqrt(const complex<T>& x);

Returns: Комплексный квадратный корень из xдиапазона в правой полуплоскости. Если аргумент - отрицательное действительное число, возвращаемое значение лежит на положительной мнимой оси.

Remarks: Срезы ответвлений проходят по отрицательной действительной оси.

template<class T> complex<T> tan(const complex<T>& x);

Returns: Комплексный тангенс x.

template<class T> complex<T> tanh(const complex<T>& x);

Returns: Комплексный гиперболический тангенс x.

29.5.9 Additional overloads [cmplx.over]

Следующие шаблоны функций должны иметь дополнительные перегрузки:

arg                   norm
conj                  proj
imag                  real

Дополнительных перегрузок должно быть достаточно для обеспечения:

  1. 1.Если аргумент имеет тип long double, он фактически приводится к типу complex<long double>.

  2. 2.В противном случае, если аргумент имеет тип double или целочисленный тип, он эффективно приводится к complex<​double>.

  3. 3.В противном случае, если аргумент имеет тип float, он фактически приводится к типу complex<float>.

Шаблон функции pow должен иметь дополнительные перегрузки, достаточные для обеспечения вызова хотя бы с одним аргументом типа complex<T>:

  1. 1.Если какой-либо из аргументов имеет тип complex<long double> или тип long double, то оба аргумента эффективно приводятся к complex<long double>.

  2. 2.В противном случае, если любой из аргументов имеет тип complex<double>, doubleили целочисленный тип, то оба аргумента эффективно приводятся к complex<double>.

  3. 3.В противном случае, если у любого из аргументов есть тип complex<float> или float, то оба аргумента эффективно приводятся к complex<float>.

29.5.10 Suffixes for complex number literals [complex.literals]

В этом разделе описаны буквальные суффиксы для построения литералов комплексных чисел. Суффиксы i, ilи if создают комплексные числа типов complex<double>, complex<long double>и, complex<float> соответственно, при этом их мнимая часть обозначается заданным буквальным числом, а действительная часть равна нулю.

constexpr complex<long double> operator""il(long double d); constexpr complex<long double> operator""il(unsigned long long d);

Returns: complex<long double>{0.0L, static_­cast<long double>(d)}.

constexpr complex<double> operator""i(long double d); constexpr complex<double> operator""i(unsigned long long d);

Returns: complex<double>{0.0, static_­cast<double>(d)}.

constexpr complex<float> operator""if(long double d); constexpr complex<float> operator""if(unsigned long long d);

Returns: complex<float>{0.0f, static_­cast<float>(d)}.