Changeset 2826 for trunk

Show
Ignore:
Timestamp:
11/19/2008 07:52:18 AM (7 weeks ago)
Author:
guyer
Message:

getTypecode -> obj2sctype to unify with NumPy

Location:
trunk/fipy
Files:
6 modified

Legend:

Unmodified
Added
Removed
  • trunk/fipy/tools/dimensions/physicalField.py

    r2781 r2826  
    760760            return tuple(result) 
    761761 
     762    def getsctype(self, default=None): 
     763        """ 
     764 
     765        Returns the Numpy sctype of the underlying array. 
     766 
     767            >>> PhysicalField(1, 'm').getsctype() 
     768            <type 'numpy.int32'> 
     769            >>> PhysicalField(1., 'm').getsctype() 
     770            <type 'numpy.float64'> 
     771            >>> PhysicalField((1,1.), 'm').getsctype() 
     772            <type 'numpy.float64'> 
     773             
     774        """ 
     775         
     776        if not hasattr(self, 'typecode'): 
     777            self.typecode = numerix.obj2sctype(rep=numerix.array(self.getNumericValue()),  
     778                                               default=default) 
     779         
     780        return self.typecode 
     781 
    762782    def getUnit(self): 
    763783        """ 
  • trunk/fipy/tools/numerix.py

    r2822 r2826  
    11701170    return lst 
    11711171 
     1172def obj2sctype(rep, default=None): 
     1173    if _isPhysical(rep): 
     1174        sctype = rep.getsctype(default=default) 
     1175    else: 
     1176        if type(rep) in (type(()), type([])): 
     1177            rep = array(rep) 
     1178        sctype = NUMERIX.obj2sctype(rep=rep, default=default) 
     1179         
     1180    if sctype is None: 
     1181        return obj2sctype(type(rep), default=default) 
     1182    else: 
     1183        return sctype 
     1184         
     1185 
    11721186def getTypecode(arr): 
    11731187    """ 
  • trunk/fipy/variables/cellVariable.py

    r2825 r2826  
    8585            >>> print b 
    8686            [2] 
    87             >>> b.getTypecode() 
    88             'l' 
     87            >>> b.getsctype() 
     88            <type 'numpy.int32'> 
    8989             
    9090        replacing with the same thing is no problem 
  • trunk/fipy/variables/meshVariable.py

    r2781 r2826  
    7373            array = numerix.zeros(self.elementshape  
    7474                                  + self._getShapeFromMesh(mesh), 
    75                                   numerix.obj2sctype(numerix.array(value))) 
     75                                  numerix.obj2sctype(value)) 
    7676            if numerix._broadcastShape(array.shape, numerix.shape(value)) is None: 
    7777                if not isinstance(value, Variable): 
  • trunk/fipy/variables/operatorVariable.py

    r2825 r2826  
    246246 
    247247        >>> a = Variable(1.) * Variable(1) 
    248         >>> a.getTypecode() 
    249         'd' 
     248        >>> a.getsctype() 
     249        <type 'numpy.float64'> 
    250250 
    251251    The following test is to correct an `--inline` bug that was 
  • trunk/fipy/variables/variable.py

    r2825 r2826  
    316316             
    317317    def __repr__(self): 
    318         if len(self.name) > 0: 
     318        if hasattr(self, 'name') and len(self.name) > 0: 
    319319            return self.name 
    320320        else: 
     
    549549 
    550550        if where is not None: 
    551             tmp = numerix.zeros(numerix.getShape(where), numerix.getTypecode(value)) 
     551            tmp = numerix.zeros(numerix.getShape(where), numerix.obj2sctype(value)) 
    552552            tmp[:] = value 
    553553            tmp = numerix.where(where, tmp, self.getValue()) 
     
    603603    shape = property(fget=lambda self: self.getShape(), doc="Tuple of array dimensions.") 
    604604 
     605    def getsctype(self, default=None): 
     606        """ 
     607 
     608        Returns the Numpy sctype of the underlying array. 
     609 
     610            >>> Variable(1).getsctype() 
     611            <type 'numpy.int32'> 
     612            >>> Variable(1.).getsctype() 
     613            <type 'numpy.float64'> 
     614            >>> Variable((1,1.)).getsctype() 
     615            <type 'numpy.float64'> 
     616             
     617        """ 
     618         
     619        if not hasattr(self, 'typecode'): 
     620            self.typecode = numerix.obj2sctype(rep=self.getNumericValue(), default=default) 
     621         
     622        return self.typecode 
     623     
    605624    def getTypecode(self): 
    606625        """ 
     
    618637         
    619638        if not hasattr(self, 'typecode'): 
    620             self.typecode = numerix.getTypecode(self.getValue()) 
     639            self.typecode = numerix.getTypecode(self.getNumericValue()) 
    621640         
    622641        return self.typecode 
     
    737756            argDict['result'] = self.getValue() 
    738757            self.canInline = True 
    739             self.typecode = numerix.getTypecode(argDict['result']) 
     758            self.typecode = numerix.obj2sctype(argDict['result']) 
    740759        else: 
    741760            if self.value is None: 
    742                 if self.getTypecode() == '?': 
    743                     argDict['result'] = numerix.empty(dim, 'b') 
     761                if self.getsctype() == numpy.bool_: 
     762                    argDict['result'] = numerix.empty(dim, numpy.int8) 
    744763                else: 
    745                     argDict['result'] = numerix.empty(dim, self.getTypecode()) 
     764                    argDict['result'] = numerix.empty(dim, self.getsctype()) 
    746765            else: 
    747766                argDict['result'] = self.value