Exemple #1
0
def eval_lfunction_invmult_ops(fs, context):
    if len(fs) == 1:
        return fs[0]
    elif len(fs) > 2:
        mcdp_dev_warning('Maybe this should be smarter?')
        rest = eval_lfunction_invmult_ops(fs[1:], context)
        return eval_lfunction_invmult_ops([fs[0], rest], context)
    else:
        assert len(fs) == 2
        Fs = tuple(map(context.get_ftype, fs))

        if isinstance(Fs[0], Nat) and isinstance(Fs[1], Nat):
            dp = InvMult2Nat(Nat(), Fs)
        else:
            if isinstance(Fs[0], RcompUnits) and \
               isinstance(Fs[1], RcompUnits):
                R = mult_table(Fs[0], Fs[1])
                dp = InvMult2(R, Fs)
            elif isinstance(Fs[0], Rcomp) and isinstance(Fs[1], Rcomp):
                R = Rcomp()
                dp = InvMult2(R, Fs)
            else:
                msg = 'Could not create invmult for types {}.'.format(Fs)
                raise_desc(DPNotImplementedError, msg, Fs0=Fs[0], Fs1=Fs[1])

        return create_operation_lf(context,
                                   dp=dp,
                                   functions=fs,
                                   name_prefix='_invmult',
                                   op_prefix='_ops',
                                   res_prefix='_result')
Exemple #2
0
def vu_rcomp_mult_constants2(a, b):
    """ Multiplies two ValueWithUnits that are also RcompUnits """
    check_isinstance(a.unit, RcompUnits)
    check_isinstance(b.unit, RcompUnits)
    R = mult_table(a.unit, b.unit)
    value = a.value * b.value
    return ValueWithUnits(value=value, unit=R)
Exemple #3
0
def vu_rcomp_mult_constants2(a, b):
    """ Multiplies two ValueWithUnits that are also RcompUnits """
    check_isinstance(a.unit, RcompUnits)
    check_isinstance(b.unit, RcompUnits)
    R = mult_table(a.unit, b.unit)
    value = a.value * b.value
    return ValueWithUnits(value=value, unit=R)
Exemple #4
0
def get_mult_op(context, r, c):
    rtype = context.get_rtype(r)

    # Case 1: rcompunits, rcompunits
    if isinstance(rtype, RcompUnits) and isinstance(c.unit, RcompUnits):
        F = rtype
        R = mult_table(rtype, c.unit)
        unit, value = c.unit, c.value
        dp = MultValueDP(F=F, R=R, unit=unit, value=value) 
    elif isinstance(rtype, Nat) and isinstance(c.unit, Nat):
        dp = MultValueNatDP(c.value)
    elif isinstance(rtype, Nat) and isinstance(c.unit, RcompUnits):
        # will cast Nat to R_dimensionless automatically
        F = R_dimensionless
        R = c.unit
        unit, value = c.unit, c.value
        dp = MultValueDP(F, R, unit=unit, value=value)
    elif isinstance(rtype, RcompUnits) and isinstance(c.unit, Nat):
        F = rtype
        R = F
        unit, value = R_dimensionless, c.cast_to(R_dimensionless)
        dp = MultValueDP(F=F, R=F, unit=unit, value=value)
    else:
        msg = 'Cannot create multiplication operation.'
        raise_desc(DPInternalError, msg, rtype=rtype, c=c)

    r2 = create_operation(context, dp, resources=[r],
                          name_prefix='_mult', op_prefix='_x',
                          res_prefix='_y')
    return r2
Exemple #5
0
def get_mult_op(context, r, c):
    rtype = context.get_rtype(r)

    # Case 1: rcompunits, rcompunits
    if isinstance(rtype, RcompUnits) and isinstance(c.unit, RcompUnits):
        F = rtype
        R = mult_table(rtype, c.unit)
        unit, value = c.unit, c.value
        dp = MultValueDP(F=F, R=R, unit=unit, value=value)
    elif isinstance(rtype, Nat) and isinstance(c.unit, Nat):
        dp = MultValueNatDP(c.value)
    elif isinstance(rtype, Nat) and isinstance(c.unit, RcompUnits):
        # will cast Nat to R_dimensionless automatically
        F = R_dimensionless
        R = c.unit
        unit, value = c.unit, c.value
        dp = MultValueDP(F, R, unit=unit, value=value)
    elif isinstance(rtype, RcompUnits) and isinstance(c.unit, (Nat, Rcomp)):
        F = rtype
        R = F
        unit, value = R_dimensionless, c.cast_value(R_dimensionless)
        dp = MultValueDP(F=F, R=F, unit=unit, value=value)
    elif isinstance(rtype, Rcomp) and isinstance(c.unit, Rcomp):
        msg = 'Multiplication not implemented with Rcomp().'
        raise_desc(DPNotImplementedError, msg, rtype=rtype, c=c)
    else:
        msg = 'Cannot create multiplication operation.'
        raise_desc(DPSemanticError, msg, rtype=rtype, c=c)

    r2 = create_operation(context, dp, resources=[r], name_prefix='_mult')
    return r2
def eval_lfunction_invmult_ops(fs, context):
    if len(fs) == 1:
        return fs[0]
    elif len(fs) > 2: 
        mcdp_dev_warning('Maybe this should be smarter?')
        rest = eval_lfunction_invmult_ops(fs[1:], context)
        return eval_lfunction_invmult_ops([fs[0], rest], context) 
    else:   
        assert len(fs) == 2
        Fs = tuple(map(context.get_ftype, fs))
    
        if isinstance(Fs[0], Nat) and isinstance(Fs[1], Nat):
            dp = InvMult2Nat(Nat(), Fs)
        else:
            if isinstance(Fs[0], RcompUnits) and \
               isinstance(Fs[1], RcompUnits):
                R = mult_table(Fs[0], Fs[1])
                dp = InvMult2(R, Fs)
            elif isinstance(Fs[0], Rcomp) and isinstance(Fs[1], Rcomp):
                R = Rcomp()
                dp = InvMult2(R, Fs)
            else:
                msg = 'Could not create invplus for types {}.'.format(Fs)
                raise_desc(DPNotImplementedError, msg, Fs0=Fs[0], Fs1=Fs[1])
                
        return create_operation_lf(context, dp=dp, functions=fs,
                        name_prefix='_invmult', op_prefix='_ops',
                        res_prefix='_result') 
def eval_lfunction_create_invmultvalue(lf, constant, context):
    assert isinstance(lf, CFunction), lf
    assert isinstance(constant, ValueWithUnits), constant
    
    F1 = context.get_ftype(lf)
    F2 = constant.unit
    
    if isinstance(F1, Nat) and isinstance(F2, Nat):
        dp = InvMultValueNatDP(constant.value)    
    elif isinstance(F1, RcompUnits) and isinstance(F2, RcompUnits):
        R = mult_table(F1, F2)
        dp = InvMultValueDP(R, F1, constant.unit, constant.value)
    else:
        msg = 'Cannot get InvMultValue for spaces %s and %s' % (F1, F2)
        raise_desc(DPNotImplementedError, msg, F1=F1, F2=F2)

    return create_operation_lf(context, dp=dp, functions=[lf],
                        name_prefix='_invmultvalue', op_prefix='_ops',
                        res_prefix='_result')
Exemple #8
0
def eval_lfunction_create_invmultvalue(lf, constant, context):
    assert isinstance(lf, CFunction), lf
    assert isinstance(constant, ValueWithUnits), constant

    F1 = context.get_ftype(lf)
    F2 = constant.unit

    if isinstance(F1, Nat) and isinstance(F2, Nat):
        dp = InvMultValueNatDP(constant.value)
    elif isinstance(F1, RcompUnits) and isinstance(F2, RcompUnits):
        R = mult_table(F1, F2)
        dp = InvMultValueDP(R, F1, constant.unit, constant.value)
    else:
        msg = 'Cannot get InvMultValue for spaces %s and %s' % (F1, F2)
        raise_desc(DPNotImplementedError, msg, F1=F1, F2=F2)

    return create_operation_lf(context,
                               dp=dp,
                               functions=[lf],
                               name_prefix='_invmultvalue',
                               op_prefix='_ops',
                               res_prefix='_result')