<commit_before>//
//  Complex.swift
//  NumberKit
//
//  Created by <NAME> on 15/08/2015.
//  Copyright © 2015 <NAME>. All rights reserved.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

import Darwin


/// The `ComplexType` protocol defines an interface for complex numbers. A complex
/// number consists of two floating point components: a real part `re` and an imaginary
/// part `im`. It is typically expressed as: re + im * i where i is the imaginary unit.
/// i satisfies the equation i * i = -1.
///
/// - Note: The `ComplexType` protocol abstracts over the floating point type on which
///         the complex type implementation is based on.
public protocol ComplexType: Equatable {
  
  /// The floating point number type on which this complex number is based.
  typealias Float: FloatingPointType
  
  /// Creates a complex number without imaginary part from the given real part.
  init(_ re: Float)
  
  /// The real part of the complex number.
  var re: Float { get }
  
  /// The imaginary part of the complex number.
  var im: Float { get }
  
  /// Returns the real part of the complex number if the number has no complex
  /// part, nil otherwise
  var realValue: Float? { get }
  
  /// Returns true if this complex number has no imaginary part.
  var isReal: Bool { get }
  
  /// Returns true if this complex number is zero.
  var isZero: Bool { get }
  
  /// Returns true if either real or imaginary parts are not a number.
  var isNaN: Bool { get }
  
  /// Returns true if either real or imaginary parts are infinite.
  var isInfinite: Bool { get }
  
  /// Returns the absolute value of this complex number.
  var abs: Float { get }
  
  /// Returns the argument/phase of this complex number.
  var arg: Float { get }
  
  /// Returns the negated complex number.
  var negate: Self { get }
  
  /// Multiplies `self` by i.
  var i: Self { get }
  
  /// Returns the conjugate of this complex number.
  var conjugate: Self { get }
  
  /// Returns the reciprocal of this complex number.
  var reciprocal: Self { get }
  
  /// Returns the norm of this complex number.
  var norm: Float { get }
  
  /// Returns the square root of this complex number
  var sqrt: Self { get }
  
  /// Returns the natural exponential of this complex number.
  var exp: Self { get }
  
  /// Returns the natural logarithm of this complex number.
  var log: Self { get }
  
  /// Returns the sum of `self` and `rhs` as a complex number.
  func plus(rhs: Self) -> Self
  
  /// Returns the difference between `self` and `rhs` as a complex number.
  func minus(rhs: Self) -> Self
  
  /// Returns the result of multiplying `self` with `rhs` as a complex number.
  func times(rhs: Self) -> Self
  
  /// Returns the result of multiplying `self` with scalar `rhs` as a complex number.
  func times(rhs: Float) -> Self
  
  /// Returns the result of dividing `self` by `rhs` as a complex number.
  func dividedBy(rhs: Self) -> Self
  
  /// Returns the result of dividing `self` by scalar `rhs` as a complex number.
  func dividedBy(rhs: Float) -> Self
  
  /// Returns this complex number taken to the power of `ex`.
  func toPowerOf(ex: Self) -> Self
}


/// Protocol `FloatNumberType` is supposed to be used in combination with struct
/// `Complex<T>`. It defines the functionality needed for a floating point
/// implementation to build complex numbers on top. The `FloatingPointType`
/// protocol from the Swift standard library is not sufficient currently.
///
/// - Note: For some reason, `_BuiltinFloatLiteralConvertible` is needed here to
///         allow `Complex<T>` to implement protocol `FloatLiteralConvertible`.
///         Replacing it with `FloatLiteralConvertible` does not work either.
public protocol FloatNumberType: Hashable,
                                 Comparable,
                                 FloatingPointType,
                                 AbsoluteValuable,
                                 _BuiltinFloatLiteralConvertible {
  var i: Complex<Self> { get }
  var abs: Self { get }
  var sqrt: Self { get }
  var sin: Self { get }
  var cos: Self { get }
  var exp: Self { get }
  var log: Self { get }
  func pow(ex: Self) -> Self
  func hypot(y: Self) -> Self
  func atan2(y: Self) -> Self
  func +(lhs: Self, rhs: Self) -> Self
  func -(lhs: Self, rhs: Self) -> Self
  func *(lhs: Self, rhs: Self) -> Self
  func /(lhs: Self, rhs: Self) -> Self
}


/// Struct `Complex<T>` implements the `ComplexType` interface on top of the
/// floating point type `T`; i.e. both the rational and the imaginary part of the
/// complex number are represented as values of type `T`.
///
/// - Note: `T` needs to implement the `FloatNumberType` protocol. The `FloatingPointType`
///         protocol that is defined in the Swift standard library is not sufficient to
///         implement a complex number as it does not, at all, define interfaces for
///         basic arithmetic operations.
public struct Complex<T: FloatNumberType>: ComplexType,
                                           Hashable,
                                           IntegerLiteralConvertible,
                                           FloatLiteralConvertible,
                                           CustomStringConvertible {
  
  /// The real part of thix complex number.
  public let re: T
  
  /// The imaginary part of this complex number.
  public let im: T
  
  /// Creates a complex number with the given real part and no imaginary part.
  public init(_ re: T) {
    self.re = re
    self.im = T(0)
  }
  
  /// Creates a complex number with the given real and integer imaginary parts.
  public init(_ re: T, _ im: IntMax) {
    self.init(re, T(im))
  }

  /// Creates a complex number with the given real and imaginary parts.
  public init(_ re: T, _ im: T) {
    self.re = re
    self.im = im
  }
  
  /// Creates a real number initialized to integer `value`.
  public init(integerLiteral value: IntMax) {
    self.init(T(value))
  }
  
  /// Creates a real number initialized to floating point number `value`.
  public init(floatLiteral value: T) {
    self.init(value)
  }
  
  /// Returns a textual representation of this complex number
  public var description: String {
    if im.isZero {
      return String(re)
    } else if re.isZero {
      return String(im) + "i"
    } else {
      return im.isSignMinus ? "\(re)\(im)i" : "\(re)+\(im)i"
    }
  }
  
  /// Returns a hash value for this complex number
  public var hashValue: Int {<commit_msg>Initialize complex numbers from polar coordinates.<commit_after>//
//  Complex.swift
//  NumberKit
//
//  Created by <NAME> on 15/08/2015.
//  Copyright © 2015 <NAME>. All rights reserved.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

import Darwin


/// The `ComplexType` protocol defines an interface for complex numbers. A complex
/// number consists of two floating point components: a real part `re` and an imaginary
/// part `im`. It is typically expressed as: re + im * i where i is the imaginary unit.
/// i satisfies the equation i * i = -1.
///
/// - Note: The `ComplexType` protocol abstracts over the floating point type on which
///         the complex type implementation is based on.
public protocol ComplexType: Equatable {
  
  /// The floating point number type on which this complex number is based.
  typealias Float: FloatingPointType
  
  /// Creates a complex number without imaginary part from the given real part.
  init(_ re: Float)
  
  /// The real part of the complex number.
  var re: Float { get }
  
  /// The imaginary part of the complex number.
  var im: Float { get }
  
  /// Returns the real part of the complex number if the number has no complex
  /// part, nil otherwise
  var realValue: Float? { get }
  
  /// Returns true if this complex number has no imaginary part.
  var isReal: Bool { get }
  
  /// Returns true if this complex number is zero.
  var isZero: Bool { get }
  
  /// Returns true if either real or imaginary parts are not a number.
  var isNaN: Bool { get }
  
  /// Returns true if either real or imaginary parts are infinite.
  var isInfinite: Bool { get }
  
  /// Returns the absolute value of this complex number.
  var abs: Float { get }
  
  /// Returns the argument/phase of this complex number.
  var arg: Float { get }
  
  /// Returns the negated complex number.
  var negate: Self { get }
  
  /// Multiplies `self` by i.
  var i: Self { get }
  
  /// Returns the conjugate of this complex number.
  var conjugate: Self { get }
  
  /// Returns the reciprocal of this complex number.
  var reciprocal: Self { get }
  
  /// Returns the norm of this complex number.
  var norm: Float { get }
  
  /// Returns the square root of this complex number
  var sqrt: Self { get }
  
  /// Returns the natural exponential of this complex number.
  var exp: Self { get }
  
  /// Returns the natural logarithm of this complex number.
  var log: Self { get }
  
  /// Returns the sum of `self` and `rhs` as a complex number.
  func plus(rhs: Self) -> Self
  
  /// Returns the difference between `self` and `rhs` as a complex number.
  func minus(rhs: Self) -> Self
  
  /// Returns the result of multiplying `self` with `rhs` as a complex number.
  func times(rhs: Self) -> Self
  
  /// Returns the result of multiplying `self` with scalar `rhs` as a complex number.
  func times(rhs: Float) -> Self
  
  /// Returns the result of dividing `self` by `rhs` as a complex number.
  func dividedBy(rhs: Self) -> Self
  
  /// Returns the result of dividing `self` by scalar `rhs` as a complex number.
  func dividedBy(rhs: Float) -> Self
  
  /// Returns this complex number taken to the power of `ex`.
  func toPowerOf(ex: Self) -> Self
}


/// Protocol `FloatNumberType` is supposed to be used in combination with struct
/// `Complex<T>`. It defines the functionality needed for a floating point
/// implementation to build complex numbers on top. The `FloatingPointType`
/// protocol from the Swift standard library is not sufficient currently.
///
/// - Note: For some reason, `_BuiltinFloatLiteralConvertible` is needed here to
///         allow `Complex<T>` to implement protocol `FloatLiteralConvertible`.
///         Replacing it with `FloatLiteralConvertible` does not work either.
public protocol FloatNumberType: Hashable,
                                 Comparable,
                                 FloatingPointType,
                                 AbsoluteValuable,
                                 _BuiltinFloatLiteralConvertible {
  var i: Complex<Self> { get }
  var abs: Self { get }
  var sqrt: Self { get }
  var sin: Self { get }
  var cos: Self { get }
  var exp: Self { get }
  var log: Self { get }
  func pow(ex: Self) -> Self
  func hypot(y: Self) -> Self
  func atan2(y: Self) -> Self
  func +(lhs: Self, rhs: Self) -> Self
  func -(lhs: Self, rhs: Self) -> Self
  func *(lhs: Self, rhs: Self) -> Self
  func /(lhs: Self, rhs: Self) -> Self
}


/// Struct `Complex<T>` implements the `ComplexType` interface on top of the
/// floating point type `T`; i.e. both the rational and the imaginary part of the
/// complex number are represented as values of type `T`.
///
/// - Note: `T` needs to implement the `FloatNumberType` protocol. The `FloatingPointType`
///         protocol that is defined in the Swift standard library is not sufficient to
///         implement a complex number as it does not, at all, define interfaces for
///         basic arithmetic operations.
public struct Complex<T: FloatNumberType>: ComplexType,
                                           Hashable,
                                           IntegerLiteralConvertible,
                                           FloatLiteralConvertible,
                                           CustomStringConvertible {
  
  /// The real part of thix complex number.
  public let re: T
  
  /// The imaginary part of this complex number.
  public let im: T
  
  /// Creates a complex number with the given real part and no imaginary part.
  public init(_ re: T) {
    self.re = re
    self.im = T(0)
  }
  
  /// Creates a complex number with the given real and integer imaginary parts.
  public init(_ re: T, _ im: IntMax) {
    self.init(re, T(im))
  }

  /// Creates a complex number with the given real and imaginary parts.
  public init(_ re: T, _ im: T) {
    self.re = re
    self.im = im
  }
  
  /// Creates a complex number from polar coordinates
  public init(abs: T, arg: T) {
    self.re = abs * arg.cos
    self.im = abs * arg.sin
  }
  
  /// Creates a real number initialized to integer `value`.
  public init(integerLiteral value: IntMax) {
    self.init(T(value))
  }
  
  /// Creates a real number initialized to floating point number `value`.
  public init(floatLiteral value: T) {
    self.init(value)
  }
  
  /// Returns a textual representation of this complex number
  public var description: String {
    if im.isZero {
      return String(re)
    } else if re.isZero {
      return String(im) + "i"
    } else {
      return im.isSignMinus ? "\(re)\(im)i" : "\(re)+\(im)i"
    }
  }
  
  /// Returns a hash value for this complex number
  public var hashValue: Int {
{'fraction_non_alphanumeric': 0.09551037695891572, 'fraction_numerical': 0.0026824791754906114, 'mean_word_length': 2.3349811676082863, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7623225', 'n_tokens_mistral': 3933, 'n_tokens_neox': 3869, 'n_words': 1785}