/[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 3536 by caltinay, Thu Jun 23 04:42:38 2011 UTC revision 3614 by caltinay, Wed Sep 28 05:16:08 2011 UTC
# Line 311  class Symbol(object): Line 311  class Symbol(object):
311              coeff_item=lambda item: getattr(item, 'coeff')(y.item(), expand)              coeff_item=lambda item: getattr(item, 'coeff')(y.item(), expand)
312              none_to_zero=lambda item: 0 if item is None else item              none_to_zero=lambda item: 0 if item is None else item
313              result=self.applyfunc(coeff_item)              result=self.applyfunc(coeff_item)
314              result=result.applyfunc(none_to_zero)._arr              result=result.applyfunc(none_to_zero)
315          return Symbol(result, dim=self.dim)          return Symbol(result, dim=self.dim)
316    
317        def subs(self, old, new):
318            """
319            Substitutes an expression.
320            """
321            if isinstance(old, Symbol) and old.getRank()>0:
322                old._ensureShapeCompatible(new)
323                if hasattr(new, '__array__'):
324                    new=new.__array__()
325                else:
326                    new=numpy.array(new)
327    
328                result=Symbol(self._arr, dim=self.dim)
329                if new.ndim>0:
330                    for idx in numpy.ndindex(self.getShape()):
331                        for symidx in numpy.ndindex(new.shape):
332                            result[idx]=result[idx].subs(old[symidx], new[symidx])
333                else: # substitute scalar for non-scalar
334                    for idx in numpy.ndindex(self.getShape()):
335                        for symidx in numpy.ndindex(old.getShape()):
336                            result[idx]=result[idx].subs(old[symidx], new.item())
337            else: # scalar
338                if isinstance(new, Symbol):
339                    if new.getRank()>0:
340                        raise TypeError("Cannot substitute, incompatible ranks.")
341                    new=new.item()
342                if isinstance(old, Symbol):
343                    old=old.item()
344                subs_item=lambda item: getattr(item, 'subs')(old, new)
345                result=self.applyfunc(subs_item)
346            return result
347    
348      def diff(self, *symbols, **assumptions):      def diff(self, *symbols, **assumptions):
349          """          """
350          """          """
# Line 539  class Symbol(object): Line 570  class Symbol(object):
570              out=Symbol(out, dim=self.dim)              out=Symbol(out, dim=self.dim)
571          return out          return out
572    
573        def expand(self):
574            """
575            """
576            return self.applyfunc(sympy.expand, sympy.Basic)
577    
578      def simplify(self):      def simplify(self):
579          """          """
580          """          """
# Line 564  class Symbol(object): Line 600  class Symbol(object):
600          elif isinstance(other,int) or isinstance(other,float) or isinstance(other,sympy.Basic):          elif isinstance(other,int) or isinstance(other,float) or isinstance(other,sympy.Basic):
601              sh1=()              sh1=()
602          else:          else:
603              raise TypeError("Unsupported argument type '%s' for binary operation"%other.__class__.__name__)              raise TypeError("Unsupported argument type '%s' for operation"%other.__class__.__name__)
604          if not sh0==sh1 and not sh0==() and not sh1==():          if not sh0==sh1 and not sh0==() and not sh1==():
605              raise TypeError("Incompatible shapes for binary operation")              raise TypeError("Incompatible shapes for operation")
606    
607      @staticmethod      @staticmethod
608      def _symComp(sym):      def _symComp(sym):

Legend:
Removed from v.3536  
changed lines
  Added in v.3614

  ViewVC Help
Powered by ViewVC 1.1.26