/[escript]/trunk/escript/py_src/symbolic/symbol.py
ViewVC logotype

Diff of /trunk/escript/py_src/symbolic/symbol.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3857 by caltinay, Tue Mar 6 07:28:22 2012 UTC revision 3862 by caltinay, Fri Mar 9 06:32:35 2012 UTC
# Line 151  class Symbol(object): Line 151  class Symbol(object):
151          return (self._arr==other._arr).all()          return (self._arr==other._arr).all()
152    
153      def __getitem__(self, key):      def __getitem__(self, key):
154          return self._arr[key]          """
155            Returns an element of this symbol which must have rank >0.
156            Unlike item() this method converts sympy objects and numpy arrays into
157            escript Symbols in order to facilitate expressions that require
158            element access, such as: grad(u)[1]+x
159    
160      def __iter__(self):          :param key: (nd-)index of item to be returned
161          return self._arr.__iter__          :return: the requested element
162            :rtype: ``Symbol``, ``int``, or ``float``
163            """
164            res=self._arr[key]
165            # replace sympy Symbols/expressions by escript Symbols
166            if isinstance(res, sympy.Basic) or isinstance(res, numpy.ndarray):
167                res=Symbol(res)
168            return res
169    
170      def __setitem__(self, key, value):      def __setitem__(self, key, value):
171          if isinstance(value, Symbol):          if isinstance(value, Symbol):
# Line 163  class Symbol(object): Line 174  class Symbol(object):
174              elif hasattr(self._arr[key], "shape"):              elif hasattr(self._arr[key], "shape"):
175                  if self._arr[key].shape==value.getShape():                  if self._arr[key].shape==value.getShape():
176                      for idx in numpy.ndindex(self._arr[key].shape):                      for idx in numpy.ndindex(self._arr[key].shape):
177                          self._arr[key][idx]=value[idx]                          self._arr[key][idx]=value[idx].item()
178                  else:                  else:
179                      raise ValueError("Wrong shape of value")                      raise ValueError("Wrong shape of value")
180              else:              else:
# Line 175  class Symbol(object): Line 186  class Symbol(object):
186          else:          else:
187              self._arr[key]=sympy.sympify(value)              self._arr[key]=sympy.sympify(value)
188    
189        def __iter__(self):
190            return self._arr.__iter__
191    
192      def getDim(self):      def getDim(self):
193          """          """
194          Returns the spatial dimensionality of this symbol.          Returns the spatial dimensionality of this symbol.
# Line 316  class Symbol(object): Line 330  class Symbol(object):
330              result=numpy.zeros(self.getShape(), dtype=object)              result=numpy.zeros(self.getShape(), dtype=object)
331              for idx in numpy.ndindex(y.shape):              for idx in numpy.ndindex(y.shape):
332                  if y[idx]!=0:                  if y[idx]!=0:
333                      res=self[idx].coeff(y[idx], expand)                      res=self._arr[idx].coeff(y[idx], expand)
334                      if res is not None:                      if res is not None:
335                          result[idx]=res                          result[idx]=res
336          elif y.item()==0:          elif y.item()==0:
# Line 375  class Symbol(object): Line 389  class Symbol(object):
389                      for d in range(dim):                      for d in range(dim):
390                          for idx in numpy.ndindex(self.getShape()):                          for idx in numpy.ndindex(self.getShape()):
391                              index=idx+(d,)                              index=idx+(d,)
392                              out[index]=out[index].diff(s[d], **assumptions)                              out[index]=out[index].diff(s[d].item(), **assumptions)
393                      result=Symbol(out, dim=self.dim)                      result=Symbol(out, dim=self.dim)
394                  else:                  else:
395                      raise ValueError("diff: Only rank 0 and 1 supported")                      raise ValueError("diff: Only rank 0 and 1 supported")
# Line 605  class Symbol(object): Line 619  class Symbol(object):
619          Raises TypeError if not compatible.          Raises TypeError if not compatible.
620          """          """
621          sh0=self.getShape()          sh0=self.getShape()
622          if isinstance(other, Symbol):          if isinstance(other, Symbol): # or isinstance(other, Data):
623              sh1=other.getShape()              sh1=other.getShape()
624          elif isinstance(other, numpy.ndarray):          elif isinstance(other, numpy.ndarray):
625              sh1=other.shape              sh1=other.shape

Legend:
Removed from v.3857  
changed lines
  Added in v.3862

  ViewVC Help
Powered by ViewVC 1.1.26