Changeset 719

Show
Ignore:
Timestamp:
08/09/10 12:16:27 (1 year ago)
Author:
dmitrey
Message:

some FD changes

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • PythonPackages/FuncDesigner/FuncDesigner/ooFun.py

    r718 r719  
    99 
    1010Copy = lambda arg: asscalar(arg) if type(arg)==ndarray and arg.size == 1 else arg.copy() if hasattr(arg, 'copy') else copy(arg) 
     11Len = lambda x: 1 if isscalar(x) else x.size if type(x)==ndarray else len(x) 
    1112 
    1213try: 
     
    7778    _point_id = 0 
    7879    _point_id1 = 0 
     80    _f_key_prev = None 
     81    _f_val_prev = None 
    7982    #_c = 0.0 
    8083 
     
    158161     
    159162        def aux_d(x, y): 
    160             Xsize = 1 if isscalar(x) else x.size 
    161             Ysize = 1 if isscalar(y) else y.size 
     163            Xsize, Ysize = Len(x), Len(y) 
    162164            if Xsize == 1: 
    163165                return ones(Ysize) 
     
    197199    # overload "-a" 
    198200    def __neg__(self):  
    199         r = oofun(lambda a: -a, self, d = lambda a: -Eye(1 if isscalar(a) else a.size), is_linear = self.is_linear) 
     201        r = oofun(lambda a: -a, self, d = lambda a: -Eye(Len(a)), is_linear = self.is_linear) 
    200202        r._getFuncCalcEngine = lambda *args,  **kwargs: -self._getFuncCalcEngine(*args,  **kwargs) 
    201203        r._D = lambda *args, **kwargs: dict([(key, -value) for key, value in self._D(*args, **kwargs).items()]) 
     
    213215            def aux_dx(x, y): 
    214216                #x, y, = asfarray(x), asfarray(y) # TODO: get rid of it 
    215                 Xsize = 1 if isscalar(x) else x.size 
    216                 Ysize = 1 if isscalar(y) else y.size 
     217                Xsize, Ysize = Len(x), Len(y) 
    217218                if Xsize != 1: 
    218219                    assert Xsize == Ysize or Ysize == 1, 'incorrect size for oofun devision' 
     
    223224                return r                 
    224225            def aux_dy(x, y): 
    225                 Xsize = 1 if isscalar(x) else x.size 
    226                 Ysize = 1 if isscalar(y) else y.size                 
     226                Xsize, Ysize = Len(x), Len(y) 
    227227                r = -x / y**2 
    228228                if Ysize != 1: 
     
    257257    def __mul__(self, other): 
    258258        def aux_d(x, y): 
    259             Xsize = 1 if isscalar(x) else x.size 
    260             Ysize = 1 if isscalar(y) else y.size 
     259            Xsize, Ysize = Len(x), Len(y) 
    261260            if Xsize == 1: 
    262261                return Copy(y) 
     
    336335            f = lambda x: x[ind] 
    337336            def d(x): 
    338                 Xsize = 1 if isscalar(x) else x.size 
     337                Xsize = Len(x) 
    339338                condBigMatrix = Xsize > 100  
    340339                if condBigMatrix and scipyInstalled: 
     
    374373        f = lambda x: x[ind1:ind2] 
    375374        def d(x): 
    376             condBigMatrix = (1 if isscalar(x) else x.size) > 100 #and (ind2-ind1) > 0.25*x.size 
     375            condBigMatrix = Len(x) > 100 #and (ind2-ind1) > 0.25*x.size 
    377376            if condBigMatrix and not scipyInstalled: 
    378377                self.pWarn(scipyAbsentMsg) 
     
    553552             
    554553        cond_same_point = CondSamePointByID or \ 
    555         ('f_key_prev' in self.__dict__  and (self._isFixed or (self.isCostly and  all([array_equal(x[elem], self.f_key_prev[elem]) for elem in dep])))) 
     554        (self._f_val_prev is not None and (self._isFixed or (self.isCostly and  all([array_equal(x[elem], self._f_key_prev[elem]) for elem in dep])))) 
    556555         
    557556        if cond_same_point: 
    558557            self.same += 1 
    559             return self.f_val_prev.copy() # self.f_val_prev is ndarray always  
     558            return self._f_val_prev.copy() # self._f_val_prev is ndarray always  
    560559             
    561560        self.evals += 1 
     
    579578         
    580579        if (type(x) == ooPoint or self.isCostly or self._isFixed): 
    581             self.f_val_prev = copy(tmp)  
    582             self.f_key_prev = dict([(elem, copy(x[elem])) for elem in dep]) 
    583             return copy(self.f_val_prev) 
     580            self._f_val_prev = copy(tmp)  
     581            self._f_key_prev = dict([(elem, copy(x[elem])) for elem in dep]) if self.isCostly else None 
     582            return copy(self._f_val_prev) 
    584583        else: 
    585584            return tmp 
     
    796795    def _getDerivativeSelf(self, x, fixedVarsScheduleID, Vars,  fixedVars): 
    797796        Input = self._getInput(x, fixedVarsScheduleID=fixedVarsScheduleID, Vars=Vars,  fixedVars=fixedVars) 
     797        expectedTotalInputLength = sum([Len(elem) for elem in Input]) 
     798         
    798799#        if hasattr(self, 'size') and isscalar(self.size): nOutput = self.size 
    799800#        else: nOutput = self(x).size  
     801 
    800802        hasUserSuppliedDerivative = self.d is not None 
    801803        if hasUserSuppliedDerivative: 
     
    847849                if not isscalar(tmp) and type(tmp) in (ndarray, tuple, list): # i.e. not a scipy.sparse matrix 
    848850                    tmp = atleast_2d(tmp) 
    849                     expectedTotalInputLength = sum([elem.size for elem in Input]) 
     851                     
    850852                    if tmp.shape[1] != expectedTotalInputLength:  
    851853                        # TODO: add debug msg 
     
    863865                        continue                                     
    864866                    if isinstance(tmp, ndarray): 
    865                         TMP = tmp[:, ac:ac+inp.size
     867                        TMP = tmp[:, ac:ac+Len(inp)
    866868                    elif isscalar(tmp): 
    867869                        TMP = tmp 
    868870                    else: # scipy.sparse matrix 
    869871                        TMP = csc_tmp[:, ac:ac+inp.size] 
    870                     ac += 1 if isscalar(inp) else inp.size 
     872                    ac += Len(inp) 
    871873                    derivativeSelf.append(TMP) 
    872874                     
  • PythonPackages/FuncDesigner/FuncDesigner/ooVar.py

    r714 r719  
    33from numpy import nan, asarray, isfinite, empty, zeros, inf, any, array, prod, atleast_1d, asfarray, isscalar, ndarray 
    44from misc import FuncDesignerException, checkSizes 
    5 from ooFun import oofun 
     5from ooFun import oofun, Len 
    66 
    77class oovar(oofun): 
     
    3535        elif hasattr(x, 'xf'): 
    3636            # TODO: possibility of squeezing 
    37             r = atleast_1d(asfarray(x.xf[self])) 
     37            r = x.xf[self] 
    3838        else: 
    3939            raise FuncDesignerException('Incorrect data type (%s) while obtaining oovar %s value' %(type(x), self.name)) 
    40         Size = r.size 
    41         if isscalar(self.size) and Size != self.size: 
     40         
     41         
     42        if 'size' in self.__dict__ and type(self.size) == int and Len(r)  != self.size: # len(r) for lists/tuples 
    4243            s = 'incorrect size for oovar %s: %d is required, %d is obtained' % (self.name, self.size, Size) 
    4344            raise FuncDesignerException(s)