Ejemplo n.º 1
0
    def transpose_multiply(self, b):
        """
        Multiply the transpose of this matrix from the right by another matrix
        or vector.

        Arguments:

            b(:code:`invlib.matrix` or :code:`invlib.vector`): The matrix
                or vector to multiply the transpose of this matrix with.

        Returns:

            The matrix or vector that results from multiplying the transpose
            of this matrix from the right with another matrix or vector,
            respectively.
        """
        dtype = Matrix._get_dtype(self.matrix)

        if isinstance(b, Matrix):
            f   = resolve_precision("matrix_matrix_transpose_multiply", dtype)
            ptr = f(self.invlib_pointer, b.invlib_pointer)
            return Matrix(ptr, dtype)

        if isinstance(b, Vector):
            f   = resolve_precision("matrix_vector_transpose_multiply", dtype)
            ptr = f(self.invlib_pointer, b.invlib_pointer)
            return Vector(ptr, dtype)

        raise ValueError("Argument b must be of type invlib.matrix.Matrix or "
                         "invlib.vector.Vector.")
Ejemplo n.º 2
0
    def from_invlib_pointer(ptr, dtype):
        f_rows = resolve_precision("vector_rows", dtype)
        n = f_rows(ptr)

        shape   = (n, 1)
        stride  = get_stride(dtype)
        strides = (stride,) * len(shape)

        b = resolve_precision("vector_element_pointer", dtype)(ptr)
        ctype = get_ctypes_scalar_type(dtype)
        b   = c.cast(b, c.POINTER(ctype))
        arr = np.ctypeslib.as_array(b, shape = shape)
        return arr
Ejemplo n.º 3
0
    def multiply(self, b):

        if isinstance(b, Matrix):
            f   = resolve_precision("matrix_matrix_multiply", self.dtype)
            ptr = f(self.invlib_ptr, b.invlib_ptr)
            return Matrix(ptr, self.dtype)

        elif isinstance(b, Vector):
            f   = resolve_precision("matrix_vector_multiply", self.dtype)
            ptr = f(self.invlib_ptr, b.invlib_ptr)
            return Vector(ptr, self.dtype)

        raise ValueError("Argument b must be of type invlib.matrix.Matrix or "
                         "invlib.vector.Vector.")
Ejemplo n.º 4
0
    def subtract(self, v):
        if isinstance(v, Vector):
            f = resolve_precision("vector_subtract", self.dtype)
            ptr = f(self.invlib_ptr, v.invlib_ptr)
            return Vector(ptr, self.dtype)

        raise Vector.wrong_argument_error
Ejemplo n.º 5
0
    def __new__(self, invlib_ptr, dtype):
        f_rows = resolve_precision("vector_rows", dtype)
        n = f_rows(invlib_ptr)

        shape = (n, 1)
        stride = get_stride(dtype)
        strides = (stride, ) * len(shape)

        b = resolve_precision("vector_get_data_pointer", dtype)(invlib_ptr)
        ctype = get_c_type(dtype)
        b = c.cast(b, c.POINTER(ctype))
        arr = np.ctypeslib.as_array(b, shape=shape)
        obj = super(Vector, self).__new__(Vector, shape, dtype, arr.data, 0,
                                          strides, 'C')

        self.invlib_ptr = invlib_ptr
        return obj
Ejemplo n.º 6
0
    def multiply_transpose(self, b):

        if isinstance(b, Vector):
            f   = resolve_precision("matrix_vector_multiply_transpose", self.dtype)
            ptr = f(self.invlib_ptr, b.invlib_ptr)
            return Vector(ptr, self.dtype)

        raise ValueError("Argument b must be of type invlib.vector.Vector.")
Ejemplo n.º 7
0
    def __array_finalize__(self, obj):

        if obj is None:
            return None

        Vector.check_precision(obj)
        Vector.check_memory_layout(obj)
        f = resolve_precision("create_vector", obj.dtype)
        self.invlib_ptr = f(obj.ctypes.data, obj.size, False)
Ejemplo n.º 8
0
    def __array_finalize__(self, obj):

        if obj is None:
            return None

        Matrix.check_precision(obj)
        Matrix.check_memory_layout(obj)
        f = resolve_precision("create_matrix", obj.dtype)
        m, n = obj.shape
        self.invlib_ptr = f(obj.ctypes.data, m, n, False)
Ejemplo n.º 9
0
    def evaluate_api(self, x):

        if not isinstance(x, Vector):
            raise ValueError("Argument x must be of type invlib.Vector.")

        dtype = x.dtype
        f = resolve_precision("forward_model_evaluate", dtype)
        fs = to_forward_model_struct(self, dtype)
        ptr = f(fs, x.invlib_pointer)
        return Vector(ptr, dtype)
Ejemplo n.º 10
0
    def scale(self, c):
        """
        Scale vector by scalar factor.

        Arguments:

            v(:code:`float`): The scaling factor.

        """
        f = resolve_precision("vector_scale", self.dtype)
        f(self.invlib_pointer, c)
Ejemplo n.º 11
0
    def __new__(self, invlib_ptr, dtype):

        f_rows = resolve_precision("matrix_rows", dtype)
        m = f_rows(invlib_ptr)
        f_cols = resolve_precision("matrix_cols", dtype)
        n = f_cols(invlib_ptr)

        shape   = (m, n)
        stride = get_stride(dtype)
        strides = (stride * n, stride)


        b = resolve_precision("matrix_get_data_pointer", dtype)(invlib_ptr)
        ctype = get_c_type(dtype)
        b   = c.cast(b, c.POINTER(ctype))
        arr = np.ctypeslib.as_array(b, shape = (m, n))
        obj = super(Matrix, self).__new__(Matrix, shape, dtype, arr.data, 0, strides, 'C')

        self.invlib_ptr = invlib_ptr

        return obj
Ejemplo n.º 12
0
    def jacobian_api(self, x):

        if not isinstance(x, Vector):
            raise ValueError("Argument x must be of type invlib.Vector.")

        dtype = x.dtype

        y = Vector(np.zeros(self.m, dtype = dtype))
        f = resolve_precision("forward_model_jacobian", dtype)
        jac = f(to_forward_model_struct(self, dtype),
                x.invlib_pointer,
                y.invlib_pointer)

        return Matrix(jac, dtype), y
Ejemplo n.º 13
0
    def dot(self, v):
        """
        Compute the dot product of this vector with :code:`v`.

        Arguments:

            v(:code:`invlib.Vector`): The vector to compute the
                dot product with.

        Returns:

            The scalar results of the dot product.
        """
        f = resolve_precision("vector_dot", self.dtype)
        return f(self.invlib_pointer, v.invlib_pointer)
Ejemplo n.º 14
0
    def matrix_info(ptr, dtype):

        f = resolve_precision("matrix_info", dtype)
        ms = f(ptr)

        m   = ms.m
        n   = ms.n
        nnz = ms.nnz
        fmt = ms.format

        if fmt == 0:
            b = (get_ctypes_scalar_type(dtype) * (m * n)).from_address(ms.data_pointers[0])
            elements = [np.ctypeslib.as_array(b).reshape(m, n)]
            indices  = None
            starts   = None

        if fmt > 0:

            b = (get_ctypes_scalar_type(dtype) * nnz).from_address(ms.data_pointers[0])
            elements  = [np.ctypeslib.as_array(b)]

            indices = []
            b = (get_ctypes_index_type() * nnz).from_address(ms.index_pointers[0])
            indices += [np.ctypeslib.as_array(b)]

            starts = []

            if fmt == 1:

                b = (get_ctypes_index_type() * (n + 1)).from_address(ms.start_pointers[0])
                starts += [np.ctypeslib.as_array(b, shape = (n + 1,))]

            if fmt == 2:

                b = (get_ctypes_index_type() * (m + 1)).from_address(ms.start_pointers[0])
                starts += [np.ctypeslib.as_array(b, shape = (m + 1,))]

            if fmt == 3:
                b = (get_ctypes_scalar_type(dtype) * nnz).from_address(ms.data_pointers[1])
                elements += [np.ctypeslib.as_array(b)]
                b = (get_ctypes_index_type() * nnz).from_address(ms.index_pointers[1])
                indices += [np.ctypeslib.as_array(b)]
                b = (get_ctypes_index_type() * (n + 1)).from_address(ms.start_pointers[0])
                starts += [np.ctypeslib.as_array(b, shape = (n + 1,))]
                b = (get_ctypes_index_type() * (m + 1)).from_address(ms.start_pointers[1])
                starts += [np.ctypeslib.as_array(b, shape = (m + 1,))]

        return m, n, nnz, fmt, elements, indices, starts
Ejemplo n.º 15
0
    def compute(self, y):

        if not isinstance(y, Vector):
            raise Exception("y must be of type invlib.Vector")

        if not self.dtype == y.dtype:
            raise Exception("x_a and y must use the same dtype.")

        f = resolve_precision("oem", self.dtype)
        ptr = f(to_forward_model_struct(self.forward_model, self.dtype),
                self.sa_inv.invlib_pointer,
                self.se_inv.invlib_pointer,
                self.x_a.invlib_pointer,
                None,
                y.invlib_pointer,
                to_optimizer_struct(self.optimizer, self.dtype),
                self.optimizer.solver.to_invlib_pointer(self.dtype))
        return Vector(ptr, self.dtype)
Ejemplo n.º 16
0
    def subtract(self, v):
        """
        Compute the difference of this vector and another vector.

        Arguments:

            v(:code:`invlib.Vector`): The vector to add to this vector.

        Returns:

            :code:`invlib.Vector` containing the difference of the two vectors.
        """
        if isinstance(v, Vector):
            f = resolve_precision("vector_subtract", self.dtype)
            ptr = f(self.invlib_pointer, v.invlib_pointer)
            return Vector(ptr, self.dtype)

        raise Vector.wrong_argument_error
Ejemplo n.º 17
0
 def create_invlib_vector(obj):
     Vector._check_precision(obj)
     Vector._check_memory_layout(obj)
     f   = resolve_precision("create_vector", obj.dtype)
     ptr = f(obj.size, obj.ctypes.data, False)
     return ptr
Ejemplo n.º 18
0
 def rows(self):
     """
     Number of rows of the column vector.
     """
     f = resolve_precision("vector_rows", self.dtype)
     return f(self.invlib_pointer)
Ejemplo n.º 19
0
 def rows(self):
     f = resolve_precision("vector_rows", self.dtype)
     return f(self.invlib_ptr)
Ejemplo n.º 20
0
    def __init__(self, *args, **kwargs):

        if len(args) == 2:
            ptr, dtype = args
            if not ((type(ptr) == int) and (dtype in [np.float32, np.float64])):
                raise ValueError("Expected the two positional arguments "
                                 " provided to matrix constructor to be a "
                                 "ctypes pointer and numpy.dtype but found "
                                 "something else.")
            matrix = Matrix.from_invlib_pointer(ptr, dtype)
            format = None

        elif len(args) == 1:
            matrix, = args

            if isinstance(matrix, Matrix):
                matrix = matrix.matrix

            if "format" in kwargs:
                format = kwargs["format"]
            else:
                format = None

        fi = -1
        # Try to deduce format from type of matrix.
        if format is None:
            if type(matrix) == np.ndarray:
                fi = 0
            elif type(matrix) == sp.sparse.csc_matrix:
                fi = 1
            elif type(matrix) == sp.sparse.csr_matrix:
                fi = 2
            elif type(matrix) == tuple:
                fi = 3
            else:
                raise Exception("numpy.ndarray or scipy sprase matrix required"\
                                "to create matrix.")
        else:
            if format == "dense":
                matrix = np.asarray(matrix)
                fi = 0
            elif format == "sparse_csc":
                try:
                    matrix = matrix.to_csc()
                    fi = 1
                except:
                    raise ValueError("To create a matrix in sparse CSC format "\
                                     "the provided matrix must be convertible "\
                                     "to a scipy.sparse.csc_matrix matrix.")
            elif format == "sparse_csr":
                try:
                    matrix = matrix.to_csr()
                    fi = 2
                except:
                    raise ValueError("To create a matrix in sparse CSR format "\
                                     "the provided matrix must be convertible "\
                                     "to a scipy.sparse.csr_matrix matrix.")
            elif format == "sparse_hyb":
                try:
                    matrix = (matrix.to_csc(),
                              matrix.to_csr())
                    fi = 3
                except:
                    raise ValueError("To create a matrix in sparse Hybrid format "\
                                     "the provided matrix must be convertible "\
                                     "to a scipy.sparse.csc_matrix and scipy.sparse"
                                     ".csr_matrix.")

        Matrix._check_precision(matrix)
        if fi == 0:
            Matrix._check_memory_layout(matrix)

        dtype = Matrix._get_dtype(matrix)
        f = resolve_precision("create_matrix", dtype)
        self._invlib_pointer = f(Matrix._to_matrix_struct(matrix), False)
        self.matrix = matrix
Ejemplo n.º 21
0
 def scale(self, c):
     f = resolve_precision("vector_scale", self.dtype)
     print(self.invlib_ptr)
     f(self.invlib_ptr, c)
Ejemplo n.º 22
0
 def dot(self, v):
     f = resolve_precision("vector_dot", self.dtype)
     return f(self.invlib_ptr, v.invlib_ptr)