Changeset 719
- Timestamp:
- 08/09/10 12:16:27 (1 year ago)
- Files:
-
- PythonPackages/FuncDesigner/FuncDesigner/ooFun.py (modified) (14 diffs)
- PythonPackages/FuncDesigner/FuncDesigner/ooVar.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
PythonPackages/FuncDesigner/FuncDesigner/ooFun.py
r718 r719 9 9 10 10 Copy = lambda arg: asscalar(arg) if type(arg)==ndarray and arg.size == 1 else arg.copy() if hasattr(arg, 'copy') else copy(arg) 11 Len = lambda x: 1 if isscalar(x) else x.size if type(x)==ndarray else len(x) 11 12 12 13 try: … … 77 78 _point_id = 0 78 79 _point_id1 = 0 80 _f_key_prev = None 81 _f_val_prev = None 79 82 #_c = 0.0 80 83 … … 158 161 159 162 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) 162 164 if Xsize == 1: 163 165 return ones(Ysize) … … 197 199 # overload "-a" 198 200 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) 200 202 r._getFuncCalcEngine = lambda *args, **kwargs: -self._getFuncCalcEngine(*args, **kwargs) 201 203 r._D = lambda *args, **kwargs: dict([(key, -value) for key, value in self._D(*args, **kwargs).items()]) … … 213 215 def aux_dx(x, y): 214 216 #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) 217 218 if Xsize != 1: 218 219 assert Xsize == Ysize or Ysize == 1, 'incorrect size for oofun devision' … … 223 224 return r 224 225 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) 227 227 r = -x / y**2 228 228 if Ysize != 1: … … 257 257 def __mul__(self, other): 258 258 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) 261 260 if Xsize == 1: 262 261 return Copy(y) … … 336 335 f = lambda x: x[ind] 337 336 def d(x): 338 Xsize = 1 if isscalar(x) else x.size337 Xsize = Len(x) 339 338 condBigMatrix = Xsize > 100 340 339 if condBigMatrix and scipyInstalled: … … 374 373 f = lambda x: x[ind1:ind2] 375 374 def d(x): 376 condBigMatrix = (1 if isscalar(x) else x.size) > 100 #and (ind2-ind1) > 0.25*x.size375 condBigMatrix = Len(x) > 100 #and (ind2-ind1) > 0.25*x.size 377 376 if condBigMatrix and not scipyInstalled: 378 377 self.pWarn(scipyAbsentMsg) … … 553 552 554 553 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])))) 556 555 557 556 if cond_same_point: 558 557 self.same += 1 559 return self. f_val_prev.copy() # self.f_val_prev is ndarray always558 return self._f_val_prev.copy() # self._f_val_prev is ndarray always 560 559 561 560 self.evals += 1 … … 579 578 580 579 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) 584 583 else: 585 584 return tmp … … 796 795 def _getDerivativeSelf(self, x, fixedVarsScheduleID, Vars, fixedVars): 797 796 Input = self._getInput(x, fixedVarsScheduleID=fixedVarsScheduleID, Vars=Vars, fixedVars=fixedVars) 797 expectedTotalInputLength = sum([Len(elem) for elem in Input]) 798 798 799 # if hasattr(self, 'size') and isscalar(self.size): nOutput = self.size 799 800 # else: nOutput = self(x).size 801 800 802 hasUserSuppliedDerivative = self.d is not None 801 803 if hasUserSuppliedDerivative: … … 847 849 if not isscalar(tmp) and type(tmp) in (ndarray, tuple, list): # i.e. not a scipy.sparse matrix 848 850 tmp = atleast_2d(tmp) 849 expectedTotalInputLength = sum([elem.size for elem in Input])851 850 852 if tmp.shape[1] != expectedTotalInputLength: 851 853 # TODO: add debug msg … … 863 865 continue 864 866 if isinstance(tmp, ndarray): 865 TMP = tmp[:, ac:ac+ inp.size]867 TMP = tmp[:, ac:ac+Len(inp)] 866 868 elif isscalar(tmp): 867 869 TMP = tmp 868 870 else: # scipy.sparse matrix 869 871 TMP = csc_tmp[:, ac:ac+inp.size] 870 ac += 1 if isscalar(inp) else inp.size872 ac += Len(inp) 871 873 derivativeSelf.append(TMP) 872 874 PythonPackages/FuncDesigner/FuncDesigner/ooVar.py
r714 r719 3 3 from numpy import nan, asarray, isfinite, empty, zeros, inf, any, array, prod, atleast_1d, asfarray, isscalar, ndarray 4 4 from misc import FuncDesignerException, checkSizes 5 from ooFun import oofun 5 from ooFun import oofun, Len 6 6 7 7 class oovar(oofun): … … 35 35 elif hasattr(x, 'xf'): 36 36 # TODO: possibility of squeezing 37 r = atleast_1d(asfarray(x.xf[self]))37 r = x.xf[self] 38 38 else: 39 39 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 42 43 s = 'incorrect size for oovar %s: %d is required, %d is obtained' % (self.name, self.size, Size) 43 44 raise FuncDesignerException(s)
