Ejemplo n.º 1
0
def test_one_parent():
    x = Trace('x', 3, {'x': 1.0}, [])
    a = one_parent(x, 'cos')
    assert a.val == np.cos(3)

    with pytest.raises(TypeError):
        c = one_parent(x, 'cos', param='test')

    with pytest.raises(TypeError):
        x = 'test'
        d = one_parent(x, 'cos')
Ejemplo n.º 2
0
def arccos(t):
    '''
    This allows to creat arccos(). ValueError is caught if the input Trace
    instance has value not in the domain of (-1, 1)
    
    Parameters:
        t (Trace instance, scalar, or vector/list of Traces/scalars)

    Return Trace that constitues arccos() elementary function
    '''
    try:   
        t_val = t.val
        if math.in_domain(t_val, math.Ops.arccos):
            return one_parent(t, math.Ops.arccos)
        else:
            raise ValueError('Input out of domain')
    except AttributeError:
        if isinstance(t, numbers.Number):
            if math.in_domain(t, math.Ops.arccos):
                return np.arccos(t)
            else:
                raise ValueError('Input out of domain')
        elif isinstance(t, Iterable) and not isinstance(t, str):
            return np.array([arccos(t_) for t_ in t])
        else:
            raise TypeError('Input(s) must be Trace or scalar')
Ejemplo n.º 3
0
def log(t, base=np.e):
    '''
    This allows to create log().
    Parameters:
        t (Trace instance, scalar, or vector/list of Traces/scalars)
        base (int, or float)
    Return Trace that constitues log() elementary function with input base (default=e)
    '''
    try:  
        t_val = t.val
        formula = None
        if base != np.e:
            formula = f'log_{base}({t._trace_name})'
        if math.in_domain(t_val, math.Ops.log, base):
            return one_parent(t, math.Ops.log, base, formula)
        else:
            raise ValueError('Input out of domain')
    except AttributeError:
        if isinstance(t, numbers.Number):
            if math.in_domain(t, math.Ops.log, base):
                return np.log(t)/np.log(base)
            else:
                raise ValueError('Input out of domain')
        elif isinstance(t, Iterable) and not isinstance(t, str):
            return np.array([log(t_, base) for t_ in t])
        else:
            raise TypeError('Input(s) must be Trace or scalar')
Ejemplo n.º 4
0
def cos(t):
    '''
    This allows to create cos().
    Parameters:
        t (Trace instance, scalar, or vector/list of Traces/scalars)
    Return Trace that constitues cos() elementary function
    '''
    try:  
        t_val = t.val
        return one_parent(t, math.Ops.cos)
    except AttributeError:
        if isinstance(t, numbers.Number):
            return np.cos(t)
        elif isinstance(t, Iterable) and not isinstance(t, str):
            return np.array([cos(t_) for t_ in t])
        else:
            raise TypeError('Input(s) must be Trace or scalar')
Ejemplo n.º 5
0
def exp(t, base=np.e):
    '''
    This allows to create exp().
    Parameters:
        t (Trace instance, scalar, or vector/list of Traces/scalars)
        base (int, or float)
    Return Trace that constitues exp() elementary function with input base (default=e)
    '''
    try:   
        t_val = t.val
        formula = None
        if base != np.e:
            formula = f'{base}^{t._trace_name}'
        return one_parent(t, math.Ops.exp, base, formula)
    except AttributeError:
        if isinstance(t, numbers.Number):
            return np.power(base, t)
        elif isinstance(t, Iterable) and not isinstance(t, str):
            return np.array([exp(t_, base) for t_ in t])
        else:
            raise TypeError('Input(s) must be Trace or scalar')
Ejemplo n.º 6
0
def sqrt(t):
    '''
    This allows to create sqrt().
    Parameters:
        t (Trace instance, scalar, or vector/list of Traces/scalars)
    Return Trace that constitues sqrt() elementary function
    '''
    try:   
        t_val = t.val
        if math.in_domain(t_val, math.Ops.sqrt):
            return one_parent(t, math.Ops.sqrt)
        else:
            raise ValueError('Input out of domain')
    except AttributeError:
        if isinstance(t, numbers.Number):
            if math.in_domain(t, math.Ops.sqrt):
                return t**0.5
            else:
                raise ValueError('Input out of domain')
        elif isinstance(t, Iterable) and not isinstance(t, str):
            return np.array([sqrt(t_) for t_ in t])
        else:
            raise TypeError('Input(s) must be Trace or scalar')