Beispiel #1
0
    def _BinaryOperatorVariable(self, op, other, operatorClass=None, opShape=None, canInline=True, unit=None):
        """
        :Parameters:
          - `op`: the operator function to apply (takes two arguments for `self` and `other`)
          - `other`: the quantity to be operated with
          - `operatorClass`: the `Variable` class that the binary operator should inherit from 
          - `opShape`: the shape that should result from the operation
        """
        if not isinstance(other, Variable):
            from fipy.variables.constant import _Constant
            other = _Constant(value=other)

        opShape, baseClass, other = self._shapeClassAndOther(opShape, operatorClass, other)
        
        if opShape is None or baseClass is None:
            return NotImplemented
    
        for v in [self, other]:
            if not v.getUnit().isDimensionless():
                canInline = False
                
        # obtain a general operator class with the desired base class
        operatorClass = operatorClass or self._OperatorVariableClass(baseClass)
        from fipy.variables import binaryOperatorVariable
        binOp = binaryOperatorVariable._BinaryOperatorVariable(operatorClass)
        
        return binOp(op=op, var=[self, other], opShape=opShape, canInline=canInline, unit=unit, 
                     inlineComment=inline._operatorVariableComment(canInline=canInline))
Beispiel #2
0
    def _UnaryOperatorVariable(self, op, operatorClass=None, opShape=None, canInline=True, unit=None):
        """
        Check that getUnit() works for unOp

            >>> (-Variable(value="1 m")).getUnit()
            <PhysicalUnit m>
            
        """
        operatorClass = operatorClass or self._OperatorVariableClass()
        from fipy.variables import unaryOperatorVariable
        unOp = unaryOperatorVariable._UnaryOperatorVariable(operatorClass)
        
        # If the caller has not specified a shape for the result, determine the 
        # shape from the base class or from the inputs
        if opShape is None:
            opShape = self.shape
        
        if opShape is None:
            return NotImplemented

        if not self.getUnit().isDimensionless():
            canInline = False

        return unOp(op=op, var=[self], opShape=opShape, canInline=canInline, unit=unit, 
                    inlineComment=inline._operatorVariableComment(canInline=canInline))