On 6/6/13 11:00 PM, David Roe wrote:
You can't inherit from both dense and sparse, but you can probably
create a PoolingMatrix class that doesn't inherit from either then a
PoolingMatrix_dense and PoolingMatrix_sparse that inherit from both the
relevant matrix class and your generic PoolingMatrix class.
David


I think David's reply to you is a great way to tackle the problem.

Another way to tackle the problem is to not inherit from matrix, but to wrap a matrix (i.e., have a PoolingMatrix class with just a member being the matrix). You'll implement your own __add__, __mul__, etc., that will just do those operations on the matrices, but that's pretty straightforward.

class PoolingMatrix(object):

    def __init__(self, matrix):
        self.m = matrix
    def __add__(self, other):
        return PoolingMatrix(self.m+other.m)

etc.

Then you just pass in whatever kind of matrix you want.

Thanks,

Jason





On Thu, Jun 6, 2013 at 9:40 PM, Rob <ulamga...@gmail.com
<mailto:ulamga...@gmail.com>> wrote:

    But there's always another problem right?

    I need to prepare to use some pretty big matrices, so I'd like to
    make Pooling_Matrix be a subclass of either

      * sage.matrix.matrix_integer_dense.Matrix_integer_dense,
        if the call is through Pooling_Matrix(parent, entries, copy,
        coerce) where parent is that of a
        sage.matrix.matrix_integer_dense.Matrix_integer_dense object; or

      * sage.matrix.matrix_integer_sparse.Matrix_integer_sparse,
        if the call is through Pooling_Matrix(parent, entries, copy,
        coerce) where parent is that of a
        sage.matrix.matrix_integer_sparse.Matrix_integer_sparse object.

    Currently I am using the call (through a constructor pooling_matrix):
    Pooling_Matrix(parent, entries, copy, coerce)

    with the class definition starting below (a lot of methods I don't
    want to write twice are omitted).  The important line is the first
    line of __init__:

    sage.matrix.matrix_integer_dense.Matrix_integer_dense.__init__(self,
    parent, entries, copy, coerce)
    I'd like for Pooling_Matrix to inherit from the sparse class and use
    the following as first line of __init__ instead when the parent in
    the Pooling_Matrix call is that of a sparse matrix.

    sage.matrix.matrix_integer_sparse.Matrix_integer_sparse.__init__(self,
    parent, entries, copy, coerce)

    The Pooling_Matrix class is adding bookkeeping and diagnostics on
    top of matrices, but I need to keep the underlying matrix dense or
    sparse, respectively and access the matrix methods/data/etc. through
    new methods of Pooling_Matrix that are agnostic to whether the
    underlying matrix is dense or sparse, and takes advantage of
    sparsity to make the operations tractable in the huge sparse matrix
    case.

    Thanks for any answers!

    -Rob


    class
    Pooling_Matrix_Dense(sage.matrix.matrix_integer_dense.Matrix_integer_dense):
         """
         SUMMARY:
             Pooling_Matrix extends
    sage.matrix.constructor.MatrixFactory to provide functionality for
             evaluating the disjunctness of the matrix.

         BASIC CONSTRUCTION:
             Pooling_Matrix(m), where m is a 2D-array or a nonnegative
    matrix over '\mathbf{Z}'.
         EXAMPLE CONSTRUCTION::

             sage: m = Pooling_Matrix(matrix(ZZ,
    [[1,0,0],[0,1,0],[0,0,1],[0,0,1]])
             sage: m
             [1 0 1]
             [0 1 0]
             [0 0 1]

         INPUT:

         - ``mat`` -- a nonnegative integer matrix (over '\mathbb{Z}')
         """

         def __init__(self, parent, entries, copy, coerce):

    sage.matrix.matrix_integer_dense.Matrix_integer_dense.__init__(self,
    parent, entries, copy, coerce)
             self.d_lower_bound = -1
             self.d_upper_bound = self.ncols()
             self.d_max = None
             ...




    On Wednesday, June 5, 2013 12:02:40 PM UTC-7, Rob wrote:

        Thanks David, I am getting the following to work.

        def make_pool(data):
             rows = len(data)
             cols = len(data[0])
             parent_arg = parent(matrix(ZZ, data))
             return Pool(parent_arg, flatten(data), False, False)

        class
        Pool(sage.matrix.matrix___integer_dense.Matrix_integer___dense):
             def __init__(self, parent, entries, coerce, copy):

        sage.matrix.matrix_integer___dense.Matrix_integer_dense.____init__(self,
        parent, entries, coerce, copy)
                 self.d_lower_bound = -1


        m = make_pool([[1,0],[0,1]])
        print m.rows()
        m.d_lower_bound

        On Wednesday, June 5, 2013 11:45:39 AM UTC-7, David Roe wrote:

            The issue is that Matrix_integer_dense has a __cinit__
            method, which means that all subclasses must conform to the
            same inputs for their __init__ methods.  So you need to do

                 def __init__(self, parent, entries, coerce, copy):

            
sage.matrix.matrix_integer___dense.Matrix_integer_dense.____init__(self,
            parent, entries, coerce, copy)
                     ....

            I would suggest having another function (e.g.
            pooling_matrix) which takes arguments in the way you want to
            handle them and creates the appropriate class; this is what
            the function matrix does.  Note that you may need to have a
            separate parent class if you want to do arithmetic with
            these matrices, since by default MatrixSpace will create
            Matrix_integer_dense objects rather than PoolingMatrix
            objects.  See sage.matrix.constructor and
            sage.matrix.matrix_space.
            David


            On Wed, Jun 5, 2013 at 1:53 PM, Rob <ulam...@gmail.com> wrote:

                Here is a kluge that is closer to what I want.  Can be
                copied into and run in a Sage cell.  The deficiency in
                the construction is on lines 6-8 (within commented section).

                class
                
PoolingMatrix(sage.matrix.__matrix_integer_dense.Matrix___integer_dense):
                     # Example construction:
                     #    a = matrix(ZZ, [[1,1],[2,2]])
                     #    m=PoolingMatrix(parent(a), [1,2,3,4], False,
                False)
                     #
                     # But the construction above is silly. You have to
                make a matrix of same dimensions and get its parent before
                     # constructing the matrix you want.
                     # I would prefer these construction capabilities:
                     #    m = PoolingMatrix(ZZ, [[1,0,0],[0,1,0]])
                     #    m = PoolingMatrix(ZZ, 2, [1,0,0,0,1,0])
                     #    m = PoolingMatrix(ZZ, 2, 3, [1,0,0,0,1,0])
                     # And since the ring is always ZZ, maybe the ZZ
                argument should be optional.
                     def __init__(self, parent, data, arg1, arg2):
                         print data

                
sage.matrix.matrix_integer___dense.Matrix_integer_dense.____init__(self,
                parent, data, arg1, arg2)
                         self.d_lower_bound = -1
                         # parent class, maybe dense integer matrices,
                provides the method ncols()
                         self.d_upper_bound = self.ncols()

                a = matrix(ZZ, [[1,1],[2,2]])
                m=PoolingMatrix(parent(a), [1,2,3,4], False, False)
                print m.ncols()


                On Wednesday, June 5, 2013 10:09:55 AM UTC-7, Rob wrote:

                    Thanks for the responses.  Probably the answer is I
                    don't know what __init__ method to call within the
                    inheriting __init__ method.

                    Maybe I'd like to say:

                    class PoolingMatrix(parent_class):
                         def __init__(self, ring_arg, 2D_list_arg):
                             parent_class.__init__(self, ring_arg,
                    2D_list_arg)
                             self.my_variable1 = ...
                             self.my_variable2 = ...

                    I'm getting confused by what's a class, what's a
                    constructor, what's a parent going into a
                    Matrix_integer_dense.__init__ call, etc. (this is
                    for example, not saying Matrix_integer_dense is
                    right).  I do know that I want dense integer
                    matrices of whatever shape the the 2D_list_arg
                    determines.

                    I'd like to know what to put in for parent_class in
                    both places above, and whether the same thing goes
                    in both places.

                    Thanks,
                    Rob



                    On Wednesday, June 5, 2013 9:37:01 AM UTC-7, David
                    Roe wrote:

                        Are you calling some_matrix_thingy.__init__
                        inside your __init__ method?
                        David


                        On Tue, Jun 4, 2013 at 8:10 PM, Rob
                        <ulam...@gmail.com> wrote:

                            I am trying to make a class PoolingMatrix,
                            which needs to be an
                            (binary) integer matrix with extra
                            attributes and functions.  For
                            example, I'd like to say:

                            sage: m = PoolingMatrix(ZZ,
                            [[1,0,0],[0,1,0],[0,0,1]])
                            sage: m.nrows()
                            2
                            sage: m.is_disjunct(2)    # the 3x3 identity
                            matrix is 2-disjunct
                            True

                            But the init specification for PoolingMatrix
                            is tripping me up.  Can
                            anyone provide a suggestion?

                            I'm trying something like:
                            class PoolingMatrix(some_matrix___thin__gy):
                                 def __init__(self, input_ring,
                            input_array):
                            ...

                            Thanks for any assistance
                            -Rob

                            --
                            You received this message because you are
                            subscribed to the Google Groups "sage-devel"
                            group.
                            To unsubscribe from this group and stop
                            receiving emails from it, send an email to
                            sage-devel+...@googlegroups.__co__m.
                            To post to this group, send email to
                            sage-...@googlegroups.com.
                            Visit this group at
                            http://groups.google.com/__group__/sage-devel?hl=en
                            <http://groups.google.com/group/sage-devel?hl=en>.
                            For more options, visit
                            https://groups.google.com/__grou__ps/opt_out
                            <https://groups.google.com/groups/opt_out>.





--
You received this message because you are subscribed to the Google
Groups "sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-devel?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




--
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-devel?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to