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

revision 3535 by caltinay, Thu Jun 23 02:08:18 2011 UTC revision 3536 by caltinay, Thu Jun 23 04:42:38 2011 UTC
# Line 68  class Symbol(object): Line 68  class Symbol(object):
68              x=Symbol([[a+b,0,0],[0,b-c,0],[0,0,c-a]])              x=Symbol([[a+b,0,0],[0,b-c,0],[0,0,c-a]])
69
70          returns a rank 2 symbol with the shape (3,3) whose elements are          returns a rank 2 symbol with the shape (3,3) whose elements are
71          explicitely specified by numeric values and other symbols/expressions          explicitly specified by numeric values and other symbols/expressions
72          within a list or numpy array.          within a list or numpy array.
73
74          The dimensionality of the symbol can be specified through the `dim`          The dimensionality of the symbol can be specified through the `dim`
# Line 265  class Symbol(object): Line 265  class Symbol(object):
265              return 'combineData(%s,%s)'%(str(temp_arr.tolist()).replace("'",""),str(self.getShape()))              return 'combineData(%s,%s)'%(str(temp_arr.tolist()).replace("'",""),str(self.getShape()))
266
267      def coeff(self, x, expand=True):      def coeff(self, x, expand=True):
268            """
269            Returns the coefficient of the term "x" or 0 if there is no "x".
270
271            If "x" is a scalar symbol then "x" is searched in all components of
272            this symbol. Otherwise the shapes must match and the coefficients are
273            checked component by component.
274
275            Example::
276
277                x=Symbol('x', (2,2))
278                y=3*x
279                print y.coeff(x)
280                print y.coeff(x[1,1])
281
282            will print::
283
284                [[3 3]
285                 [3 3]]
286
287                [[0 0]
288                 [0 3]]
289
290            :param x: the term whose coefficients are to be found
291            :type x: ``Symbol``, ``numpy.ndarray``, `list`
292            :return: the coefficient(s) of the term
293            :rtype: ``Symbol``
294            """
295          self._ensureShapeCompatible(x)          self._ensureShapeCompatible(x)
296          result=Symbol(self._arr, dim=self.dim)          if hasattr(x, '__array__'):
297          if isinstance(x, Symbol):              y=x.__array__()
298              if x.getRank()>0:          else:
299                  a=result._arr.flat              y=numpy.array(x)
300                  b=x._arr.flat
301                  for idx in range(len(a)):          if y.ndim>0:
302                      s=b.next()              result=numpy.zeros(self.getShape(), dtype=object)
303                      if s==0:              for idx in numpy.ndindex(y.shape):
304                          a[idx]=0                  if y[idx]!=0:
305                      else:                      res=self[idx].coeff(y[idx], expand)
306                          a[idx]=a[idx].coeff(s, expand)                      if res is not None:
307              else:                          result[idx]=res
308                  if x._arr.item()==0:          elif y.item()==0:
309                      result=Symbol(numpy.zeros(self.getShape()), dim=self.dim)              result=numpy.zeros(self.getShape(), dtype=object)
310                  else:          else:
311                      coeff_item=lambda item: getattr(item, 'coeff')(x._arr.item(), expand)              coeff_item=lambda item: getattr(item, 'coeff')(y.item(), expand)
312                      result=result.applyfunc(coeff_item)              none_to_zero=lambda item: 0 if item is None else item
313          elif x==0:              result=self.applyfunc(coeff_item)
314              result=Symbol(numpy.zeros(self.getShape()), dim=self.dim)              result=result.applyfunc(none_to_zero)._arr
315          else:          return Symbol(result, dim=self.dim)
coeff_item=lambda item: getattr(item, 'coeff')(x, expand)
result=result.applyfunc(coeff_item)

# replace None by 0
if result is None: return 0
a=result._arr.flat
for idx in range(len(a)):
if a[idx] is None: a[idx]=0
return result
316
317      def diff(self, *symbols, **assumptions):      def diff(self, *symbols, **assumptions):
318          """          """
# Line 541  class Symbol(object): Line 559  class Symbol(object):
559              sh1=other.getShape()              sh1=other.getShape()
560          elif isinstance(other, numpy.ndarray):          elif isinstance(other, numpy.ndarray):
561              sh1=other.shape              sh1=other.shape
562            elif isinstance(other, list):
563                sh1=numpy.array(other).shape
564          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):
565              sh1=()              sh1=()
566          else:          else:
# Line 600  class Symbol(object): Line 620  class Symbol(object):
620              else:              else:
621                  yield s                  yield s
622
623        def __array__(self):
624            return self._arr
625
626      # unary/binary operations follow      # unary/binary operations follow
627
628      def __pos__(self):      def __pos__(self):

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