/[escript]/trunk/escript/py_src/pdetools.py
ViewVC logotype

Diff of /trunk/escript/py_src/pdetools.py

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

revision 4068 by jfenwick, Thu Oct 11 04:43:39 2012 UTC revision 4069 by gross, Tue Nov 13 07:47:16 2012 UTC
# Line 1393  class ArithmeticTuple(object): Line 1393  class ArithmeticTuple(object):
1393             l=len(other)             l=len(other)
1394             if l!=len(self):             if l!=len(self):
1395                 raise ValueError("length of arguments don't match.")                 raise ValueError("length of arguments don't match.")
1396             for i in range(l): out.append(self[i]*other[i])             for i in range(l):
1397            if self.__isEmpty(self[i]) or self.__isEmpty(other[i]):
1398                out.append(Data())
1399            else:
1400                out.append(self[i]*other[i])
1401         except TypeError:         except TypeError:
1402             for i in range(len(self)): out.append(self[i]*other)          for i in range(len(self)):  
1403            if self.__isEmpty(self[i]) or self.__isEmpty(other):
1404                out.append(Data())
1405            else:
1406                out.append(self[i]*other)
1407         return ArithmeticTuple(*tuple(out))         return ArithmeticTuple(*tuple(out))
1408    
1409     def __rmul__(self,other):     def __rmul__(self,other):
1410         """        """
1411         Scales by ``other`` from the left.        Scales by ``other`` from the left.
1412    
1413         :param other: scaling factor        :param other: scaling factor
1414         :type other: ``float``        :type other: ``float``
1415         :return: itemwise other*self        :return: itemwise other*self
1416         :rtype: `ArithmeticTuple`        :rtype: `ArithmeticTuple`
1417         """        """
1418         out=[]        out=[]
1419         try:        try:
1420             l=len(other)        l=len(other)
1421             if l!=len(self):        if l!=len(self):
1422                 raise ValueError("length of arguments don't match.")            raise ValueError("length of arguments don't match.")
1423             for i in range(l): out.append(other[i]*self[i])        for i in range(l):
1424         except TypeError:          if self.__isEmpty(self[i]) or self.__isEmpty(other[i]):
1425             for i in range(len(self)): out.append(other*self[i])              out.append(Data())
1426         return ArithmeticTuple(*tuple(out))          else:
1427                out.append(other[i]*self[i])
1428          except TypeError:
1429          for i in range(len(self)):  
1430            if self.__isEmpty(self[i]) or self.__isEmpty(other):
1431                out.append(Data())
1432            else:
1433                out.append(other*self[i])
1434          return ArithmeticTuple(*tuple(out))
1435    
1436     def __div__(self,other):     def __div__(self,other):
1437         """         """
# Line 1429  class ArithmeticTuple(object): Line 1445  class ArithmeticTuple(object):
1445         return self*(1/other)         return self*(1/other)
1446    
1447     def __rdiv__(self,other):     def __rdiv__(self,other):
1448         """        """
1449         Scales by (1/``other``) from the left.        Scales by (1/``other``) from the left.
1450    
1451         :param other: scaling factor        :param other: scaling factor
1452         :type other: ``float``        :type other: ``float``
1453         :return: itemwise other/self        :return: itemwise other/self
1454         :rtype: `ArithmeticTuple`        :rtype: `ArithmeticTuple`
1455         """        """
1456         out=[]        out=[]
1457         try:        try:
1458             l=len(other)        l=len(other)
1459             if l!=len(self):        if l!=len(self):
1460                 raise ValueError("length of arguments don't match.")            raise ValueError("length of arguments don't match.")
1461             for i in range(l): out.append(other[i]/self[i])        
1462         except TypeError:        for i in range(l):
1463             for i in range(len(self)): out.append(other/self[i])          if self.__isEmpty(self[i]):
1464         return ArithmeticTuple(*tuple(out))              raise ZeroDivisionError("in component %s"%i)
1465            else:
1466                if self.__isEmpty(other[i]):
1467                out.append(Data())
1468                else:
1469                out.append(other[i]/self[i])
1470          except TypeError:
1471          for i in range(len(self)):
1472            if self.__isEmpty(self[i]):
1473                raise ZeroDivisionError("in component %s"%i)
1474            else:
1475                if self.__isEmpty(other):
1476                out.append(Data())
1477                else:
1478                out.append(other/self[i])
1479          return ArithmeticTuple(*tuple(out))
1480    
1481     def __iadd__(self,other):     def __iadd__(self,other):
1482         """        """
1483         Inplace addition of ``other`` to self.        Inplace addition of ``other`` to self.
1484    
1485         :param other: increment        :param other: increment
1486         :type other: ``ArithmeticTuple``        :type other: ``ArithmeticTuple``
1487         """        """
1488         if len(self) != len(other):        if len(self) != len(other):
1489             raise ValueError("tuple lengths must match.")        raise ValueError("tuple lengths must match.")
1490         for i in range(len(self)):        for i in range(len(self)):
1491             self.__items[i]+=other[i]        if self.__isEmpty(self.__items[i]):
1492         return self            self.__items[i]=other[i]
1493          else:
1494              self.__items[i]+=other[i]
1495              
1496          return self
1497    
1498     def __add__(self,other):     def __add__(self,other):
1499         """        """
1500         Adds ``other`` to self.        Adds ``other`` to self.
1501    
1502         :param other: increment        :param other: increment
1503         :type other: ``ArithmeticTuple``        :type other: ``ArithmeticTuple``
1504         """        """
1505         out=[]        out=[]
1506         try:        try:
1507             l=len(other)        l=len(other)
1508             if l!=len(self):        if l!=len(self):
1509                 raise ValueError("length of arguments don't match.")            raise ValueError("length of arguments don't match.")
1510             for i in range(l): out.append(self[i]+other[i])        for i in range(l):
1511         except TypeError:          if self.__isEmpty(self[i]):
1512             for i in range(len(self)): out.append(self[i]+other)              out.append(other[i])
1513         return ArithmeticTuple(*tuple(out))          elif self.__isEmpty(other[i]):
1514                out.append(self[i])
1515            else:
1516                out.append(self[i]+other[i])
1517          except TypeError:
1518            for i in range(len(self)):    
1519            if self.__isEmpty(self[i]):
1520                out.append(other)
1521            elif self.__isEmpty(other):
1522                out.append(self[i])
1523            else:
1524                out.append(self[i]+other)
1525          return ArithmeticTuple(*tuple(out))
1526    
1527     def __sub__(self,other):     def __sub__(self,other):
1528         """        """
1529         Subtracts ``other`` from self.        Subtracts ``other`` from self.
1530    
1531         :param other: decrement        :param other: decrement
1532         :type other: ``ArithmeticTuple``        :type other: ``ArithmeticTuple``
1533         """        """
1534         out=[]        out=[]
1535         try:        try:
1536             l=len(other)        l=len(other)
1537             if l!=len(self):        if l!=len(self):
1538                 raise ValueError("length of arguments don't match.")            raise ValueError("length of arguments don't match.")
1539             for i in range(l): out.append(self[i]-other[i])        for i in range(l):
1540         except TypeError:          if self.__isEmpty(other[i]):
1541             for i in range(len(self)): out.append(self[i]-other)              out.append(self[i])
1542         return ArithmeticTuple(*tuple(out))          elif self.__isEmpty(self[i]):
1543                out.append(-other[i])
1544            else:
1545                out.append(self[i]-other[i])
1546          except TypeError:
1547            for i in range(len(self)):    
1548            if  self.__isEmpty(other):
1549                out.append(self[i])
1550            elif self.__isEmpty(self[i]):
1551                out.append(-other)
1552            else:
1553                out.append(self[i]-other)
1554                
1555          return ArithmeticTuple(*tuple(out))
1556    
1557     def __isub__(self,other):     def __isub__(self,other):
1558         """        """
1559         Inplace subtraction of ``other`` from self.        Inplace subtraction of ``other`` from self.
1560    
1561         :param other: decrement        :param other: decrement
1562         :type other: ``ArithmeticTuple``        :type other: ``ArithmeticTuple``
1563         """        """
1564         if len(self) != len(other):        if len(self) != len(other):
1565             raise ValueError("tuple length must match.")        raise ValueError("tuple length must match.")
1566         for i in range(len(self)):        for i in range(len(self)):
1567             self.__items[i]-=other[i]        if not self.__isEmpty(other[i]):
1568         return self            if self.__isEmpty(self.__items[i]):
1569              self.__items[i]=-other[i]
1570              else:
1571              self.__items[i]=other[i]
1572          return self
1573    
1574     def __neg__(self):     def __neg__(self):
1575         """        """
1576         Negates values.        Negates values.
1577         """        """
1578         out=[]        out=[]
1579         for i in range(len(self)):        for i in range(len(self)):
1580             out.append(-self[i])        if self.__isEmpty(self[i]):
1581         return ArithmeticTuple(*tuple(out))            out.append(Data())
1582          else:
1583              out.append(-self[i])
1584          
1585          return ArithmeticTuple(*tuple(out))
1586       def __isEmpty(self, d):
1587        if isinstance(d, Data):
1588        return d.isEmpty()
1589        else:
1590        return False
1591    
1592    
1593  class HomogeneousSaddlePointProblem(object):  class HomogeneousSaddlePointProblem(object):

Legend:
Removed from v.4068  
changed lines
  Added in v.4069

  ViewVC Help
Powered by ViewVC 1.1.26