<commit_before>//
//  Complex.swift
//  MathEagle
//
//  Created by <NAME> on 05/03/15.
//  Copyright (c) 2015 Jorestha Solutions. All rights reserved.
//

import Foundation

struct Complex: Equatable, Comparable, Addable, Negatable, Substractable, Multiplicable, Dividable, Powerable, SetCompliant, FullMathValue, IntegerLiteralConvertible, Printable {
    
    var real: Double
    var imaginary: Double
    
    
    // MARK: Initialisation
    
    init(_ real: Double, _ imaginary: Double) {
        
        self.real = real
        self.imaginary = imaginary
    }
    
    init(modulus: Double, argument: Double) {
        
        self.real = modulus * cos(argument)
        self.imaginary = modulus * sin(argument)
    }
    
    init(integerLiteral value: IntegerLiteralType) {
        
        self.real = Double(value)
        self.imaginary = 0
    }
    
    static var imaginaryUnit: Complex {
        
        return Complex(0, 1)
    }
    
    
    // MARK: Basic Properties
    
    /**
        Returns the modulus of the complex number.
    */
    var modulus: Double {
        
        return sqrt(self.real**2 + self.imaginary**2)
    }
    
    /**
        Returns the argument of the complex number.
    */
    var argument: Double {
        
        return (self.real == 0.0 && self.imaginary == 0.0) ? 0 : atan(self.imaginary / self.real) + (self.quadrant.rawValue >= 3 ? PI : 0)
    }
    
    /**
        Returns the conjugate of the complex number.
    */
    var conjugate: Complex {
        
        return Complex(self.real, -self.imaginary)
    }
    
    /**
        Returns the quadrant of the complex plane in which the complex number lies.
    */
    var quadrant: Quadrant {
        
        if self.real >= 0 {
            
            return self.imaginary >= 0 ? .First : .Fourth
            
        } else {
            
            return self.imaginary >= 0 ? .Second : .Third
        }
    }
    
    /**
        Returns a description of the complex number of the form "a ± bi"
    */
    var description: String {
        
        return self.imaginary < 0 ? "\(self.real) - \(-self.imaginary)i" : "\(self.real) + \(self.imaginary)i"
    }
    
    
    // MARK: Set Conformance
    
    var isReal: Bool {
        
        return self.imaginary == 0.0;
    }
    
    
    // MARK: Fuzzy Equality
    
    /**
        Returns true if the real and imaginary parts are equal.
    */
    func equals(z: Complex) -> Bool {
        
        return self.real == self.real && self.imaginary == z.imaginary
    }
    
    
    func equals(z: Complex, accuracy: Double) -> Bool {
        
        return self.real.equals(z.real, accuracy: accuracy) && self.imaginary.equals(z.imaginary, accuracy: accuracy)
    }
    
    
    
    // MARK: Set Compliance
    
    var isNatural: Bool {
        
        return self.real.isNatural && self.imaginary == 0.0
    }
    
    var isInteger: Bool {
        
        return self.real.isInteger && self.imaginary == 0.0
    }
    
}



// MARK: Function Extensions

func sqrt(z: Complex) -> Complex {
    
    return Complex(modulus: sqrt(z.modulus), argument: z.argument / 2)
}

func exp(z: Complex) -> Complex {
    
    let c = exp(z.real)
    return Complex(c * cos(z.imaginary), c * sin(z.imaginary))
}

func log(z: Complex) -> Complex {<commit_msg>Cleanup<commit_after>//
//  Complex.swift
//  MathEagle
//
//  Created by <NAME> on 05/03/15.
//  Copyright (c) 2015 Jorestha Solutions. All rights reserved.
//

import Foundation

struct Complex: Equatable, Comparable, Addable, Negatable, Substractable, Multiplicable, Dividable, Powerable, SetCompliant, FullMathValue, IntegerLiteralConvertible, Printable {
    
    var real: Double
    var imaginary: Double
    
    
    // MARK: Initialisation
    
    init(_ real: Double, _ imaginary: Double) {
        
        self.real = real
        self.imaginary = imaginary
    }
    
    init(modulus: Double, argument: Double) {
        
        self.real = modulus * cos(argument)
        self.imaginary = modulus * sin(argument)
    }
    
    init(integerLiteral value: IntegerLiteralType) {
        
        self.real = Double(value)
        self.imaginary = 0
    }
    
    static var imaginaryUnit: Complex {
        
        return Complex(0, 1)
    }
    
    
    // MARK: Basic Properties
    
    /**
        Returns the modulus of the complex number.
    */
    var modulus: Double {
        
        return sqrt(self.real**2 + self.imaginary**2)
    }
    
    /**
        Returns the argument of the complex number.
    */
    var argument: Double {
        
        return (self.real == 0.0 && self.imaginary == 0.0) ? 0 : atan(self.imaginary / self.real) + (self.quadrant.rawValue >= 3 ? PI : 0)
    }
    
    /**
        Returns the conjugate of the complex number.
    */
    var conjugate: Complex {
        
        return Complex(self.real, -self.imaginary)
    }
    
    /**
        Returns the quadrant of the complex plane in which the complex number lies.
    */
    var quadrant: Quadrant {
        
        if self.real >= 0 {
            
            return self.imaginary >= 0 ? .First : .Fourth
            
        } else {
            
            return self.imaginary >= 0 ? .Second : .Third
        }
    }
    
    /**
        Returns a description of the complex number of the form "a ± bi"
    */
    var description: String {
        
        return self.imaginary < 0 ? "\(self.real) - \(-self.imaginary)i" : "\(self.real) + \(self.imaginary)i"
    }
    
    
    // MARK: Set Conformance
    
    var isNatural: Bool {
        
        return self.real.isNatural && self.imaginary == 0.0
    }
    
    var isInteger: Bool {
        
        return self.real.isInteger && self.imaginary == 0.0
    }
    
    var isReal: Bool {
        
        return self.imaginary == 0.0
    }
    
    
    // MARK: Fuzzy Equality
    
    /**
        Returns true if the real and imaginary parts are equal.
    */
    func equals(z: Complex) -> Bool {
        
        return self.real == self.real && self.imaginary == z.imaginary
    }
    
    
    func equals(z: Complex, accuracy: Double) -> Bool {
        
        return self.real.equals(z.real, accuracy: accuracy) && self.imaginary.equals(z.imaginary, accuracy: accuracy)
    }
    
}



// MARK: Function Extensions

func sqrt(z: Complex) -> Complex {
    
    return Complex(modulus: sqrt(z.modulus), argument: z.argument / 2)
}

func exp(z: Complex) -> Complex {
    
    let c = exp(z.real)
    return Complex(c * cos(z.imaginary), c * sin(z.imaginary))
}

func log(z: Complex) -> Complex {
{'fraction_non_alphanumeric': 0.11023859861069163, 'fraction_numerical': 0.009966777408637873, 'mean_word_length': 1.6792071197411003, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6576264', 'n_tokens_mistral': 2099, 'n_tokens_neox': 1978, 'n_words': 531}