ArrayOperationsInt
// 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 Basic Operations for Integers
library(title='ArrayOperationsInt')

// @function Adds sample_b to sample_a and returns a new array.
// @param sample_a values to be added to.
// @param sample_b values to add.
// @returns int array with added results.
export add(int[] sample_a, int[] sample_b) => //{
    int _size_a = array.size(id=sample_a)
    if _size_a > 0
        int _size_b = array.size(id=sample_b)
        if _size_b > 0
            int[] _output = array.new_int(size=_size_a)
            for _i = 0 to _size_a - 1
                int _ai = array.get(id=sample_a, index=_i)
                int _bi = array.get(id=sample_b, index=_i % _size_b)
                array.set(id=_output, index=_i, value=_bi + _ai)
            _output
//{ usage:
// if barstate.islastconfirmedhistory
//     int[] a = array.from(1,2,3), int[] b = array.from(5,6,1)
//     int[] c = add(sample_a=a, sample_b=b) // [6, 8, 4]
//     label.new(bar_index, 0.0, str.format('{0}\n+\n{1}\n=\n{2}', str.tostring(a, '#'), str.tostring(b, '#'), str.tostring(c, '#')))
//}}

// @function subtracts sample_b from sample_a and returns a new array.
// @param sample_a values to be subtracted from.
// @param sample_b values to subtract.
// @returns int array with subtracted results.
export subtract(int[] sample_a, int[] sample_b) => //{
    int _size_a = array.size(id=sample_a)
    if _size_a > 0
        int _size_b = array.size(id=sample_b)
        if _size_b > 0
            int[] _output = array.new_int(size=_size_a)
            for _i = 0 to _size_a - 1
                int _ai = array.get(id=sample_a, index=_i)
                int _bi = array.get(id=sample_b, index=_i % _size_b)
                array.set(id=_output, index=_i, value=_ai - _bi)
            _output
//{ usage:
// if barstate.islastconfirmedhistory
//     int[] a = array.from(1,2,3), int[] b = array.from(5,6,1)
//     int[] c = subtract(sample_a=a, sample_b=b) // [-4, -4, 2]
//     label.new(bar_index, 0.0, str.format('{0}\n-\n{1}\n=\n{2}', str.tostring(a, '#'), str.tostring(b, '#'), str.tostring(c, '#')))
//}}

// @function multiply sample_a with sample_b and returns a new array.
// @param sample_a values to multiply.
// @param sample_b values to multiply with.
// @returns int array with multiplied results.
export multiply(int[] sample_a, int[] sample_b) => //{
    int _size_a = array.size(id=sample_a)
    if _size_a > 0
        int _size_b = array.size(id=sample_b)
        if _size_b > 0
            int[] _output = array.new_int(size=_size_a)
            for _i = 0 to _size_a - 1
                int _ai = array.get(id=sample_a, index=_i)
                int _bi = array.get(id=sample_b, index=_i % _size_b)
                array.set(id=_output, index=_i, value=int(_ai * _bi))
            _output
//{ usage:
// if barstate.islastconfirmedhistory
//     int[] a = array.from(1,2,3), int[] b = array.from(5)
//     int[] c = multiply(sample_a=a, sample_b=b) // [5, 10, 15]
//     label.new(bar_index, 0.0, str.format('{0}\n*\n{1}\n=\n{2}', str.tostring(a, '#'), str.tostring(b, '#'), str.tostring(c, '#')))
//}}

// @function divide sample_a with sample_b and returns a new array.
// @param sample_a values to divide.
// @param sample_b values to divide with.
// @returns int array with divided results.
export divide(int[] sample_a, int[] sample_b) => //{
    int _size_a = array.size(id=sample_a)
    if _size_a > 0
        int _size_b = array.size(id=sample_b)
        if _size_b > 0
            int[] _output = array.new_int(size=_size_a)
            for _i = 0 to _size_a - 1
                int _ai = array.get(id=sample_a, index=_i)
                int _bi = array.get(id=sample_b, index=_i % _size_b)
                array.set(id=_output, index=_i, value=int(_ai / _bi))
            _output
//{ usage:
// if barstate.islastconfirmedhistory
//     int[] a = array.from(9,5,15), int[] b = array.from(5,2)
//     int[] c = divide(sample_a=a, sample_b=b) // [1, 2, 3]
//     label.new(bar_index, 0.0, str.format('{0}\n/\n{1}\n=\n{2}', str.tostring(a, '#'), str.tostring(b, '#'), str.tostring(c, '#')))
//}}

// @function rise sample_a to  the power of sample_b and returns a new array.
// @param sample_a base values to raise.
// @param sample_b values of exponents.
// @returns int array with raised results.
export power(int[] sample_a, int[] sample_b) => //{
    int _size_a = array.size(id=sample_a)
    if _size_a > 0
        int _size_b = array.size(id=sample_b)
        if _size_b > 0
            int[] _output = array.new_int(size=_size_a)
            for _i = 0 to _size_a - 1
                int _ai = array.get(id=sample_a, index=_i)
                int _bi = array.get(id=sample_b, index=_i % _size_b)
                array.set(id=_output, index=_i, value=int(math.pow(_ai, _bi)))
            _output
//{ usage:
// if barstate.islastconfirmedhistory
//     int[] a = array.from(9,5,15), int[] b = array.from(5,2)
//     int[] c = power(sample_a=a, sample_b=b) // [59049, 25, 759375]
//     label.new(bar_index, 0.0, str.format('{0}\n^\n{1}\n=\n{2}', str.tostring(a, '#'), str.tostring(b, '#'), str.tostring(c, '#')))
//}}

// @function integer remainder of sample_a under the dividend sample_b and returns a new array.
// @param sample_a values of quotients.
// @param sample_b values of dividends.
// @returns int array with remainder results.
export remainder(int[] sample_a, int[] sample_b) => //{
    int _size_a = array.size(id=sample_a)
    if _size_a > 0
        int _size_b = array.size(id=sample_b)
        if _size_b > 0
            int[] _output = array.new_int(size=_size_a)
            for _i = 0 to _size_a - 1
                int _ai = array.get(id=sample_a, index=_i)
                int _bi = array.get(id=sample_b, index=_i % _size_b)
                array.set(id=_output, index=_i, value=int(_ai % _bi))
            _output
//{ usage:
// if barstate.islastconfirmedhistory
//     int[] a = array.from(9,5,15), int[] b = array.from(5,2)
//     int[] c = power(sample_a=a, sample_b=b) // [59049, 25, 759375]
//     label.new(bar_index, 0.0, str.format('{0}\n^\n{1}\n=\n{2}', str.tostring(a, '#'), str.tostring(b, '#'), str.tostring(c, '#')))
//}}
