Hi Friedrich,

I was thinking maybe something like this:

from sage.matrix.matrix_mod2_dense import Matrix_mod2_dense, 
from sage.matrix.matrix_gf2e_dense import Matrix_gf2e_dense

class LinearLayer:
    def foo(self):
        return self[0, 1]

def LinearLayerFactory(K):
    if K.characteristic() == 2 and K.degree() == 1:
        return type("LinearLayerGF2", (Matrix_mod2_dense, LinearLayer), {})
    if K.characteristic() == 2 and K.degree() == 1:
        return type("LinearLayerGF2E", (Matrix_gf2e_dense, LinearLayer), {})
    else:
        raise NotImplementedError

T = LinearLayerFactory(GF(2))

T(MatrixSpace(GF(2), 2, 2), [0, 1, 2, 3], False, False).foo()

Cheers,
Martin


Friedrich Wiemer <friedrichwie...@gmail.com> writes:
> Hi,
>
> I worked on an implementation of linear layers (basically a matrix over
> GF(2) or GF(2^n) with some special methods) in the crypto module during
> the sage days 94 and came up with this: #25735.
>
> Martin commented that it might make sense to just inherit from an
> appropriate matrix class, to avoid another layer of indirection. Seems a
> totally valid point for me, so I'm now trying to find out, what would be
> the appropriate inheritance. As we only need matrices over GF(2) or
> GF(2^n), I assume the correct super class would be their common super
> class Matrix_dense. But when I implement it, e.g. like this:
>
> %%cython
> cimport sage.matrix.matrix_dense as matrix_dense
> from sage.matrix.constructor import matrix
> cdef class LinearLayer(matrix_dense.Matrix_dense):
>   cdef public object _m
>  
>   def __init__(self, parent, entries=None):
>     m = matrix(parent, entries)
>     self._m = m
>
> the matrix self._m "forgets" that it was a Matrix_mod2_dense or
> Matrix_gf2e_dense before:
>
> test_m = random_matrix(GF(2**4), 4, 4)
> test_m.__class__.__mro__
> (<type 'sage.matrix.matrix_gf2e_dense.Matrix_gf2e_dense'>,
> <type 'sage.matrix.matrix_dense.Matrix_dense'>,
> <type 'sage.matrix.matrix2.Matrix'>,
> <type 'sage.matrix.matrix1.Matrix'>,
> <type 'sage.matrix.matrix0.Matrix'>,
> <type 'sage.structure.element.Matrix'>,
> <type 'sage.structure.element.ModuleElement'>,
> <type 'sage.structure.element.Element'>,
> <type 'sage.structure.sage_object.SageObject'>,
> <type 'object'>)
> LinearLayer(test_m.parent(), test_m)
> test1._m.__class__.__mro__
> (<type 'sage.matrix.matrix_generic_dense.Matrix_generic_dense'>,
> <type 'sage.matrix.matrix_dense.Matrix_dense'>,
> <type 'sage.matrix.matrix2.Matrix'>,
> <type 'sage.matrix.matrix1.Matrix'>,
> <type 'sage.matrix.matrix0.Matrix'>,
> <type 'sage.structure.element.Matrix'>,
> <type 'sage.structure.element.ModuleElement'>,
> <type 'sage.structure.element.Element'>,
> <type 'sage.structure.sage_object.SageObject'>,
> <type 'object'>)
>
>     Thus operations which are specialised in Matrix_mod2_dense or 
> Matrix_gf2e_dense are not available anymore.
>     Am I misunderstanding some concept here?
>
>     Thanks in advance for your help,
>     Friedrich


-- 

_pgp: https://keybase.io/martinralbrecht
_www: https://martinralbrecht.wordpress.com
_jab: martinralbre...@jabber.ccc.de
_otr: 47F43D1A 5D68C36F 468BAEBA 640E8856 D7951CCF

-- 
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 https://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.

Reply via email to