예제 #1
0
    def subtract(self, other, out=None):
        '''
        Overloads - for data containers.

        Returns the difference of the container data with another container 
        data viewed as vectors.
        other: DataContainer
        '''
        if not isinstance(other, (DataContainer, Number)):
            return NotImplemented
        if isinstance(other, Number):
            tmp = other + numpy.zeros(self.shape, self.dtype)
            other = self.copy()
            other.fill(tmp)
        assert_validities(self, other)
        pl_one = numpy.asarray([1.0, 0.0], dtype=numpy.float32)
        mn_one = numpy.asarray([-1.0, 0.0], dtype=numpy.float32)
        if out is None:
            z = self.same_object()
            z.handle = pysirf.cSIRF_axpby \
                (pl_one.ctypes.data, self.handle, mn_one.ctypes.data, other.handle)
            check_status(z.handle)
        else:
            assert_validities(self, out)
            z = out
            try_calling(pysirf.cSIRF_axpbyAlt \
                (pl_one.ctypes.data, self.handle, mn_one.ctypes.data, other.handle, z.handle))
        return z
예제 #2
0
    def axpby(self, a, b, y, out=None, **kwargs):
        '''
        Addition for data containers.

        Returns the sum of the container data with another container 
        data viewed as vectors.
        y: DataContainer
        out:   DataContainer to store the result to.
        '''
        # if isinstance(other , ( Number, int, float, numpy.float32 )):
        #     tmp = other + numpy.zeros(self.as_array().shape)
        #     other = self.copy()
        #     other.fill(tmp)

        assert_validities(self, y)
        alpha = numpy.asarray([a.real, a.imag], dtype=numpy.float32)
        beta = numpy.asarray([b.real, b.imag], dtype=numpy.float32)

        if out is None:
            z = self.same_object()
            z.handle = pysirf.cSIRF_axpby \
                (alpha.ctypes.data, self.handle, beta.ctypes.data, y.handle)
        else:
            assert_validities(self, out)
            z = out
            try_calling(pysirf.cSIRF_axpbyAlt \
                (alpha.ctypes.data, self.handle, beta.ctypes.data, y.handle, z.handle))
        check_status(z.handle)
        return z
예제 #3
0
파일: SIRF.py 프로젝트: devhliu/SIRF
 def divide(self, other):
     '''
     Returns the elementwise ratio of this and another container 
     data viewed as vectors.
     other: DataContainer
     '''
     assert_validities(self, other)
     z = self.same_object()
     z.handle = pysirf.cSIRF_divide(self.handle, other.handle)
     check_status(z.handle)
     return z
예제 #4
0
파일: SIRF.py 프로젝트: devhliu/SIRF
 def dot(self, other):
     '''
     Returns the dot product of the container data with another container 
     data viewed as vectors.
     other: DataContainer
     '''
     assert_validities(self, other)
     handle = pysirf.cSIRF_dot(self.handle, other.handle)
     check_status(handle)
     r = pyiutil.floatDataFromHandle(handle)
     pyiutil.deleteDataHandle(handle)
     return r
예제 #5
0
    def log(self, out=None):
        '''Returns the element-wise log of the DataContainer data

           uses NumPy
        '''
        if out is None:
            z = self.clone()
        else:
            assert_validities(self, out)
            z = out
        z.fill(numpy.log(self.as_array()))
        return z
예제 #6
0
파일: SIRF.py 프로젝트: devhliu/SIRF
    def __add__(self, other):
        '''
        Overloads + for data containers.

        Returns the sum of the container data with another container 
        data viewed as vectors.
        other: DataContainer
        '''
        assert_validities(self, other)
        one = numpy.asarray([1.0, 0.0], dtype = numpy.float32)
        z = self.same_object()
        z.handle = pysirf.cSIRF_axpby \
            (one.ctypes.data, self.handle, one.ctypes.data, other.handle)
        check_status(z.handle)
        return z;
예제 #7
0
    def maximum(self, other, out=None):
        '''Element-wise maximum of DataContainer elements.

        Compare two DataContainers and returns a new array containing the element-wise maxima. Output can be pre-allocated in variable out.

        uses NumPy
        SIRF/CIL compatibility
        '''
        if out is None:
            z = self.clone()
        else:
            assert_validities(self, out)
            z = out
        if isinstance(other, Number):
            z.fill(numpy.maximum(self.as_array(), other))
        else:
            assert_validities(self, other)
            z.fill(numpy.maximum(self.as_array(), other.as_array()))
        return z
예제 #8
0
    def power(self, other, out=None):
        '''Power function for DataContainers

        uses NumPy
        SIRF/CIL compatibility
        '''
        if out is None:
            z = self.clone()
        else:
            assert_validities(self, out)
            z = out

        if isinstance(other, Number):
            tmp = numpy.power(self.as_array(), other)
            z.fill(tmp)
        else:
            assert_validities(self, other)
            z.fill(numpy.power(self.as_array(), other.as_array()))
        return z
예제 #9
0
 def dot(self, other):
     '''
     Returns the dot product of the container data with another container 
     data viewed as vectors.
     other: DataContainer
     '''
     assert_validities(self,other)
     # Check if input are the same size
     if numpy.prod(self.dimensions()) != numpy.prod(other.dimensions()):
         raise ValueError("Input sizes are expected to be equal, got " + numpy.prod(self.dimensions()) + " and " + numpy.prod(other.dimensions()) + " instead.")
     handle = pysirf.cSIRF_dot(self.handle, other.handle)
     check_status(handle)
     re = pyiutil.floatReDataFromHandle(handle)
     im = pyiutil.floatImDataFromHandle(handle)
     pyiutil.deleteDataHandle(handle)
     if im == 0:
         return re
     else:
         return re + 1j*im
예제 #10
0
 def divide(self, other, out=None):
     '''
     Returns the elementwise ratio of this and another container 
     data viewed as vectors.
     other: DataContainer
     out:   DataContainer to store the result to.
     '''
     if isinstance(other, (Number, int, float, numpy.float32)):
         tmp = other + numpy.zeros_like(self.as_array().shape)
         other = self.copy()
         other.fill(tmp)
     assert_validities(self, other)
     if out is None:
         z = self.same_object()
     else:
         assert_validities(self, out)
         z = out
     z.handle = pysirf.cSIRF_divide(self.handle, other.handle)
     check_status(z.handle)
     return z
예제 #11
0
 def divide(self, other, out=None):
     '''
     Returns the elementwise ratio of this and another container 
     data viewed as vectors.
     other: DataContainer
     out:   DataContainer to store the result to.
     '''
     if not isinstance (other, ( DataContainer , Number )):
         return NotImplemented
     if isinstance(other , Number ):
         tmp = other + numpy.zeros(self.shape, self.dtype)
         other = self.copy()
         other.fill(tmp)
     assert_validities(self, other)
     if out is None:
         out = self.same_object()
         out.handle = pysirf.cSIRF_ratio(self.handle, other.handle)
         check_status(out.handle)
         #out = self.copy()
     else:
         assert_validities(self, out)
         try_calling(pysirf.cSIRF_divide(self.handle, other.handle, out.handle))
     return out
예제 #12
0
    def axpby(self, a, b, y, out=None, **kwargs):
        '''
        Addition for data containers.

        Returns the sum of the container data with another container 
        data viewed as vectors.
        a: multiplier to self, can be a number or a DataContainer
        b: multiplier to y, can be a number or a DataContainer 
        y: DataContainer
        out:   DataContainer to store the result to.
        '''
        # splits axpby in 3 steps if a and b are not numbers as
        # pysirf.cSIRF_axpby requires them as numbers
        if not (isinstance(a, Number) and isinstance(b, Number)):
            if out is None:
                out = y.multiply(b)
            else:
                y.multiply(b, out=out)
            tmp = self.multiply(a)
            out.add(tmp, out=out)
            return out

        assert_validities(self, y)
        alpha = numpy.asarray([a.real, a.imag], dtype=numpy.float32)
        beta = numpy.asarray([b.real, b.imag], dtype=numpy.float32)

        if out is None:
            z = self.same_object()
            z.handle = pysirf.cSIRF_axpby \
                (alpha.ctypes.data, self.handle, beta.ctypes.data, y.handle)
        else:
            assert_validities(self, out)
            z = out
            try_calling(pysirf.cSIRF_axpbyAlt \
                (alpha.ctypes.data, self.handle, beta.ctypes.data, y.handle, z.handle))
        check_status(z.handle)
        return z
예제 #13
0
    def add(self, other, out=None):
        '''
        Addition for data containers.

        Returns the sum of the container data with another container 
        data viewed as vectors.
        other: DataContainer
        out:   DataContainer to store the result to.
        '''
        if isinstance(other, (Number, int, float, numpy.float32)):
            tmp = other + numpy.zeros(self.as_array().shape)
            other = self.copy()
            other.fill(tmp)
        assert_validities(self, other)
        one = numpy.asarray([1.0, 0.0], dtype=numpy.float32)
        if out is None:
            z = self.same_object()
        else:
            assert_validities(self, out)
            z = out
        z.handle = pysirf.cSIRF_axpby \
            (one.ctypes.data, self.handle, one.ctypes.data, other.handle)
        check_status(z.handle)
        return z
예제 #14
0
    def sapyb(self, a, y, b, out=None, **kwargs):
        '''
        Addition for data containers. Can be in place.

        Returns the sum of the container data with another container 
        data viewed as vectors.
        a: multiplier to self, can be a number or a DataContainer
        b: multiplier to y, can be a number or a DataContainer 
        y: DataContainer
        out:   DataContainer to store the result to, can be self or y.
        '''

        assert_validities(self, y)

        if out is not None:
            assert_validities(self, out)
            z = out
        else:
            z = self.same_object()

        if isinstance(a, Number):
            alpha = numpy.asarray([a.real, a.imag], dtype=numpy.float32)
            if isinstance(b, Number):
                #a is scalar, b is scalar
                beta = numpy.asarray([b.real, b.imag], dtype=numpy.float32)
                if out is None:
                    z.handle = pysirf.cSIRF_axpby(alpha.ctypes.data,
                                                  self.handle,
                                                  beta.ctypes.data, y.handle)
                else:
                    try_calling(
                        pysirf.cSIRF_axpbyAlt(alpha.ctypes.data, self.handle,
                                              beta.ctypes.data, y.handle,
                                              z.handle))

            else:
                #a is scalar, b is array
                one = numpy.asarray([1.0, 0.0], dtype=numpy.float32)
                tmp = y.multiply(b)

                if out is None:
                    z.handle = pysirf.cSIRF_axpby(alpha.ctypes.data,
                                                  self.handle, one.ctypes.data,
                                                  tmp.handle)
                else:
                    try_calling(
                        pysirf.cSIRF_axpbyAlt(alpha.ctypes.data, self.handle,
                                              one.ctypes.data, tmp.handle,
                                              z.handle))
        else:
            assert_validities(self, a)
            if isinstance(b, Number):
                #a is array, b is scalar
                one = numpy.asarray([1.0, 0.0], dtype=numpy.float32)
                beta = numpy.asarray([b.real, b.imag], dtype=numpy.float32)
                tmp = self.multiply(a)
                if out is None:
                    z.handle = pysirf.cSIRF_axpby(one.ctypes.data, tmp.handle,
                                                  beta.ctypes.data, y.handle)
                else:
                    try_calling(
                        pysirf.cSIRF_axpbyAlt(one.ctypes.data, tmp.handle,
                                              beta.ctypes.data, y.handle,
                                              z.handle))
            else:
                #a is array, b is array
                assert_validities(self, b)
                if out is None:
                    try:
                        z.handle = pysirf.cSIRF_xapyb(self.handle, a.handle,
                                                      y.handle, b.handle)
                        check_status(z.handle)
                    except error as e:
                        if 'NotImplemented' in str(e):
                            tmp = self.multiply(a)
                            z = y.multiply(b)
                            z.add(tmp, out=z)
                        else:
                            raise RuntimeError(str(e))
                else:
                    try:
                        try_calling(
                            pysirf.cSIRF_xapybAlt(self.handle, a.handle,
                                                  y.handle, b.handle,
                                                  z.handle))
                    except error as e:
                        if 'NotImplemented' in str(e):
                            tmp = self.multiply(a)
                            y.multiply(b, out=z)
                            z.add(tmp, out=z)
                        else:
                            raise RuntimeError(str(e))

        check_status(z.handle)
        return z