MathComplexArray
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RicardoSantos

//@version=5

// @description Array methods to handle complex number arrays.
library(title='MathComplexArray')


// Complex numbers prototype: {
//     description:
//         A set of utility functions to handle complex numbers.
//     references:
//         https://en.wikipedia.org/wiki/Complex_number
//         js: https://rosettacode.org/wiki/Fast_Fourier_transform
//         https://github.com/trekhleb/javascript-algorithms/blob/477f30b0bdac6024874d2976de1eca7afe0176dd/src/algorithms/math/complex-number/ComplexNumber.js#L3
//         https://github.com/infusion/Complex.js/

// required imports:
import RicardoSantos/MathComplexCore/2 as complex
import RicardoSantos/CommonTypesMath/1 as TMath

// @function Prototype to initialize a array of complex numbers.
// @param size size of the array.
// @param initial_complex Complex number to be used as default value, in the form of array [real, imaginary].
// @returns float array, pseudo complex Array in the form of a array [0:real, 1:imaginary, 2:real, 3:imaginary,...]
export new (int size=0, TMath.complex initial_complex) => //{
    if size > 0
    	array.new<TMath.complex>(size, initial_complex)
    else
        array.new<TMath.complex>(0)
//}

// @function Get the complex number in a array, in the form of a array [real, imaginary]
// @param id float array, ID of the array.
// @param index int, Index of the complex number.
// @returns float array, pseudo complex number in the form of a array [real, imaginary]
export get (array<TMath.complex> id, int index) => //{
    array.get(id, index)
//}

// @function Sets the values complex number in a array.
// @param id float array, ID of the array.
// @param index int, Index of the complex number.
// @param complex_number float array, Complex number, in the form: [real, imaginary].
// @returns Void, updates array id.
export set (array<TMath.complex> id, int index, TMath.complex complex_number) => //{
	array.set(id, index, complex_number)
//}

// @function Push the values into a complex number array.
// @param id float array, ID of the array.
// @param complex_number float array, Complex number, in the form: [real, imaginary].
// @returns Void, updates array id.
export push (array<TMath.complex> id, TMath.complex complex_number) => //{
    array.push(id, complex_number)
//}

// @function Pop the values from a complex number array.
// @param id float array, ID of the array.
// @param complex_number float array, Complex number, in the form: [real, imaginary].
// @returns Void, updates array id.
export pop (array<TMath.complex> id) => //{
    array.pop(id)
//}

// @function Reads a array of complex numbers into a string, of the form: "[ [a+bi], ... ]""
// @param id float array, ID of the array.
// @param format string, format of the number conversion, default='#.##########'.
// @returns string, translated complex array into string.
export to_string (array<TMath.complex> id, string format='#.##########') => //{
    string _str = '[ '
    for _e in id
        _str += str.format('[ {0} ]', complex.to_string(_e, format))
    _str
//}


if barstate.ishistory[1] and (barstate.isrealtime or barstate.islast)
    a = new(1, complex.new(0.0, 0.0))
    string _text = to_string(a, '#.##')
    b = new(2, complex.new(1.0, 2.0))
    _text += '\n' + to_string(b, '#.##')
    label.new(x=bar_index, y=0.0, text=_text, style=label.style_label_left, textalign=text.align_left)


