# Path: arrayfire/algorithm.py
# def sum(a, dim=None, nan_val=None):
#     """
#     Calculate the sum of all the elements along a specified dimension.
# 
#     Parameters
#     ----------
#     a  : af.Array
#          Multi dimensional arrayfire array.
#     dim: optional: int. default: None
#          Dimension along which the sum is required.
#     nan_val: optional: scalar. default: None
#          The value that replaces NaN in the array
# 
#     Returns
#     -------
#     out: af.Array or scalar number
#          The sum of all elements in `a` along dimension `dim`.
#          If `dim` is `None`, sum of the entire Array is returned.
#     """
#     if (nan_val is not None):
#         if dim is not None:
#             return _nan_parallel_dim(a, dim, backend.get().af_sum_nan, nan_val)
#         else:
#             return _nan_reduce_all(a, backend.get().af_sum_nan_all, nan_val)
#     else:
#         if dim is not None:
#             return _parallel_dim(a, dim, backend.get().af_sum)
#         else:
#             return _reduce_all(a, backend.get().af_sum_all)
# 
# def count(a, dim=None):
#     """
#     Count the number of non zero elements in an array along a specified dimension.
# 
#     Parameters
#     ----------
#     a  : af.Array
#          Multi dimensional arrayfire array.
#     dim: optional: int. default: None
#          Dimension along which the the non zero elements are to be counted.
# 
#     Returns
#     -------
#     out: af.Array or scalar number
#          The count of non zero elements in `a` along `dim`.
#          If `dim` is `None`, the total number of non zero elements in `a`.
#     """
#     if dim is not None:
#         return _parallel_dim(a, dim, backend.get().af_count)
#     else:
#         return _reduce_all(a, backend.get().af_count_all)
# 
# Path: arrayfire/arith.py
# def cast(a, dtype):
#     """
#     Cast an array to a specified type
# 
#     Parameters
#     ----------
#     a    : af.Array
#            Multi dimensional arrayfire array.
#     dtype: af.Dtype
#            Must be one of the following:
#                - Dtype.f32 for float
#                - Dtype.f64 for double
#                - Dtype.b8  for bool
#                - Dtype.u8  for unsigned char
#                - Dtype.s32 for signed 32 bit integer
#                - Dtype.u32 for unsigned 32 bit integer
#                - Dtype.s64 for signed 64 bit integer
#                - Dtype.u64 for unsigned 64 bit integer
#                - Dtype.c32 for 32 bit complex number
#                - Dtype.c64 for 64 bit complex number
#     Returns
#     --------
#     out  : af.Array
#            array containing the values from `a` after converting to `dtype`.
#     """
#     out=Array()
#     safe_call(backend.get().af_cast(c_pointer(out.arr), a.arr, dtype.value))
#     return out


import inspect
import os
        import numpy as np
from .library import *
from .util import *
from .util import _is_number
from .bcast import _bcast_var
from .base import *
from .index import *
from .index import _Index4
from .algorithm import (sum, count)
from .arith import cast
    dims = [1]*4

    for n in range(len(idims)):
        dims[n] = idims[n]

    if _is_number(key):
        dims[0] = 1
        return dims
    elif isinstance(key, slice):
        dims[0] = _slice_to_length(key, idims[0])
        return dims
    elif isinstance(key, ParallelRange):
        dims[0] = _slice_to_length(key.S, idims[0])
        return dims
    elif isinstance(key, BaseArray):
        # If the array is boolean take only the number of nonzeros
        if(key.dtype() is Dtype.b8):
            dims[0] = int(sum(key))
        else:
            dims[0] = key.elements()
        return dims
    elif isinstance(key, tuple):
        n_inds = len(key)

        for n in range(n_inds):
            if (_is_number(key[n])):
                dims[n] = 1
            elif (isinstance(key[n], BaseArray)):
                # If the array is boolean take only the number of nonzeros
                if(key[n].dtype() is Dtype.b8):

# Path: arrayfire/algorithm.py
# def sum(a, dim=None, nan_val=None):
#     """
#     Calculate the sum of all the elements along a specified dimension.
# 
#     Parameters
#     ----------
#     a  : af.Array
#          Multi dimensional arrayfire array.
#     dim: optional: int. default: None
#          Dimension along which the sum is required.
#     nan_val: optional: scalar. default: None
#          The value that replaces NaN in the array
# 
#     Returns
#     -------
#     out: af.Array or scalar number
#          The sum of all elements in `a` along dimension `dim`.
#          If `dim` is `None`, sum of the entire Array is returned.
#     """
#     if (nan_val is not None):
#         if dim is not None:
#             return _nan_parallel_dim(a, dim, backend.get().af_sum_nan, nan_val)
#         else:
#             return _nan_reduce_all(a, backend.get().af_sum_nan_all, nan_val)
#     else:
#         if dim is not None:
#             return _parallel_dim(a, dim, backend.get().af_sum)
#         else:
#             return _reduce_all(a, backend.get().af_sum_all)
# 
# def count(a, dim=None):
#     """
#     Count the number of non zero elements in an array along a specified dimension.
# 
#     Parameters
#     ----------
#     a  : af.Array
#          Multi dimensional arrayfire array.
#     dim: optional: int. default: None
#          Dimension along which the the non zero elements are to be counted.
# 
#     Returns
#     -------
#     out: af.Array or scalar number
#          The count of non zero elements in `a` along `dim`.
#          If `dim` is `None`, the total number of non zero elements in `a`.
#     """
#     if dim is not None:
#         return _parallel_dim(a, dim, backend.get().af_count)
#     else:
#         return _reduce_all(a, backend.get().af_count_all)
# 
# Path: arrayfire/arith.py
# def cast(a, dtype):
#     """
#     Cast an array to a specified type
# 
#     Parameters
#     ----------
#     a    : af.Array
#            Multi dimensional arrayfire array.
#     dtype: af.Dtype
#            Must be one of the following:
#                - Dtype.f32 for float
#                - Dtype.f64 for double
#                - Dtype.b8  for bool
#                - Dtype.u8  for unsigned char
#                - Dtype.s32 for signed 32 bit integer
#                - Dtype.u32 for unsigned 32 bit integer
#                - Dtype.s64 for signed 64 bit integer
#                - Dtype.u64 for unsigned 64 bit integer
#                - Dtype.c32 for 32 bit complex number
#                - Dtype.c64 for 64 bit complex number
#     Returns
#     --------
#     out  : af.Array
#            array containing the values from `a` after converting to `dtype`.
#     """
#     out=Array()
#     safe_call(backend.get().af_cast(c_pointer(out.arr), a.arr, dtype.value))
#     return out


# Path: arrayfire/array.py
import inspect
import os
        import numpy as np
from .library import *
from .util import *
from .util import _is_number
from .bcast import _bcast_var
from .base import *
from .index import *
from .index import _Index4
from .algorithm import (sum, count)
from .arith import cast

    dims = [1]*4

    for n in range(len(idims)):
        dims[n] = idims[n]

    if _is_number(key):
        dims[0] = 1
        return dims
    elif isinstance(key, slice):
        dims[0] = _slice_to_length(key, idims[0])
        return dims
    elif isinstance(key, ParallelRange):
        dims[0] = _slice_to_length(key.S, idims[0])
        return dims
    elif isinstance(key, BaseArray):
        # If the array is boolean take only the number of nonzeros
        if(key.dtype() is Dtype.b8):
            dims[0] = int(sum(key))
        else:
            dims[0] = key.elements()
        return dims
    elif isinstance(key, tuple):
        n_inds = len(key)

        for n in range(n_inds):
            if (_is_number(key[n])):
                dims[n] = 1
            elif (isinstance(key[n], BaseArray)):
                # If the array is boolean take only the number of nonzeros
                if(key[n].dtype() is Dtype.b8):

                    dims[n] = int(sum(key[n]))