Ticket #8939: trac_8939-matrix_template-4.4.2-part2.patch

File trac_8939-matrix_template-4.4.2-part2.patch, 15.0 KB (added by burcin, 12 years ago)

address referee comments

  • module_list.py

    # HG changeset patch
    # User Burcin Erocal <burcin@erocal.org>
    # Date 1285444299 -7200
    # Node ID d33d41553177538fa977a38300a8c75acaa21e12
    # Parent  82e56baf5711ab1a312bfc9c202f1b1d9b80c725
    trac 8939: add tests for fmpz_poly_linkage.pxi and a short description of how to use the new matrix templates.
    
    diff --git a/module_list.py b/module_list.py
    a b  
    481481              extra_compile_args=["-std=c99", "-D_XPG6"],
    482482              depends = [SAGE_ROOT + "/local/include/FLINT/flint.h"]),
    483483
     484    Extension('sage.libs.flint.fmpz_poly_linkage_tests',
     485              sources = ["sage/libs/flint/fmpz_poly_linkage_tests.pyx"],
     486              libraries = ["flint", "gmp", "gmpxx", "m"],
     487              include_dirs = [SAGE_ROOT+'/local/include/FLINT/'],
     488              language="c++",
     489              depends = [SAGE_ROOT + "/local/include/FLINT/flint.h"]),
     490   
    484491    Extension('sage.libs.flint.fmpz_poly',
    485492              sources = ["sage/libs/flint/fmpz_poly.pyx"],
    486493              libraries = ["csage", "flint", "gmp", "gmpxx", "m", "stdc++"],
  • sage/libs/flint/fmpz_poly_linkage.pxi

    diff --git a/sage/libs/flint/fmpz_poly_linkage.pxi b/sage/libs/flint/fmpz_poly_linkage.pxi
    a b  
     1###############################################################################
     2#   Sage: Open Source Mathematical Software
     3#       Copyright (C) 2010 Burcin Erocal <burcin@erocal.org>
     4#  Distributed under the terms of the GNU General Public License (GPL),
     5#  version 2 or any later version.  The full text of the GPL is available at:
     6#                  http://www.gnu.org/licenses/
     7###############################################################################
     8r"""
     9Defines base functions for arithmetic with FLINT's fmpz_poly_t elements which
     10can be used by templates.
     11
     12This file is used by the templated dense matrix implementation in
     13``sage.matrix.matrix_ZZx_dense``.
     14
     15"""
    116
    217cdef inline int celement_construct(fmpz_poly_t e, unsigned long parent) \
    318        except -2:
     
    924
    1025cdef inline int celement_set(fmpz_poly_t a, fmpz_poly_t b,
    1126        unsigned long parent) except -2:
     27    """
     28    TESTS::
     29
     30        sage: P.<x> = ZZ[]
     31        sage: type(x)
     32        <type 'sage.rings.polynomial.polynomial_integer_dense_flint.Polynomial_integer_dense_flint'>
     33        sage: p = P.random_element(); p
     34        2*x - 8
     35        sage: from sage.libs.flint.fmpz_poly_linkage_tests import celement_set_for_doctests as celement_set
     36        sage: celement_set(p, x+1)
     37        sage: p
     38        x + 1
     39    """
    1240    fmpz_poly_set(a, b)
    1341
    1442cdef inline int celement_swap(fmpz_poly_t a, fmpz_poly_t b) except -2:
     43    """
     44    TESTS::
     45
     46        sage: P.<x> = ZZ[]
     47        sage: type(x)
     48        <type 'sage.rings.polynomial.polynomial_integer_dense_flint.Polynomial_integer_dense_flint'>
     49        sage: p = P.random_element(); p
     50        2*x - 8
     51        sage: r = P.random_element(); r
     52        -x^2 + x
     53        sage: from sage.libs.flint.fmpz_poly_linkage_tests import celement_swap_for_doctests as celement_swap
     54        sage: celement_swap(p, r)
     55        sage: p
     56        -x^2 + x
     57        sage: r
     58        2*x - 8
     59    """
    1560    fmpz_poly_swap(a, b)
    1661
    1762cdef inline int celement_add(fmpz_poly_t res, fmpz_poly_t a, fmpz_poly_t b,
    1863        unsigned long parent) except -2:
     64    """
     65    TESTS::
     66
     67        sage: P.<x> = ZZ[]
     68        sage: type(x)
     69        <type 'sage.rings.polynomial.polynomial_integer_dense_flint.Polynomial_integer_dense_flint'>
     70        sage: p = P.random_element(); p
     71        2*x - 8
     72        sage: r = P.random_element(); r
     73        -x^2 + x
     74        sage: res = P(0); res
     75        0
     76        sage: from sage.libs.flint.fmpz_poly_linkage_tests import celement_add_for_doctests as celement_add
     77        sage: celement_add(res, p, r)
     78        sage: p
     79        2*x - 8
     80        sage: r
     81        -x^2 + x
     82        sage: p+r
     83        -x^2 + 3*x - 8
     84        sage: res
     85        -x^2 + 3*x - 8
     86        sage: celement_add(res, p, P.one_element())
     87        sage: res
     88        2*x - 7
     89        sage: celement_add(res, p, P.zero_element())
     90        sage: res
     91        2*x - 8
     92    """
    1993    fmpz_poly_add(res, a, b)
    2094
    2195cdef inline int celement_mul(fmpz_poly_t res, fmpz_poly_t a, fmpz_poly_t b,
    2296        unsigned long parent) except -2:
     97    """
     98    TESTS::
     99
     100        sage: P.<x> = ZZ[]
     101        sage: type(x)
     102        <type 'sage.rings.polynomial.polynomial_integer_dense_flint.Polynomial_integer_dense_flint'>
     103        sage: p = P.random_element(); p
     104        2*x - 8
     105        sage: r = P.random_element(); r
     106        -x^2 + x
     107        sage: res = P(0); res
     108        0
     109        sage: from sage.libs.flint.fmpz_poly_linkage_tests import celement_mul_for_doctests as celement_mul
     110        sage: celement_mul(res, p, r)
     111        sage: p
     112        2*x - 8
     113        sage: r
     114        -x^2 + x
     115        sage: p*r
     116        -2*x^3 + 10*x^2 - 8*x
     117        sage: res
     118        -2*x^3 + 10*x^2 - 8*x
     119        sage: celement_mul(res, p, P.one_element())
     120        sage: res
     121        2*x - 8
     122        sage: celement_mul(res, p, P.zero_element())
     123        sage: res
     124        0
     125    """
    23126    fmpz_poly_mul(res, a, b)
    24127
    25128cdef inline int celement_neg(fmpz_poly_t a, fmpz_poly_t b,
    26129        unsigned long parent) except -2:
     130    """
     131    TESTS::
     132
     133        sage: P.<x> = ZZ[]
     134        sage: type(x)
     135        <type 'sage.rings.polynomial.polynomial_integer_dense_flint.Polynomial_integer_dense_flint'>
     136        sage: p = P.random_element(); p
     137        2*x - 8
     138        sage: res = P(0); res
     139        0
     140        sage: from sage.libs.flint.fmpz_poly_linkage_tests import celement_neg_for_doctests as celement_neg
     141        sage: celement_neg(res, p)
     142        sage: p
     143        2*x - 8
     144        sage: res
     145        -2*x + 8
     146        sage: celement_neg(res, P.one_element())
     147        sage: res
     148        -1
     149        sage: celement_neg(res, P.zero_element())
     150        sage: res
     151        0
     152    """
    27153    fmpz_poly_neg(a, b)
    28154
    29 cdef inline int celement_invert(fmpz_poly_t a, fmpz_poly_t b,
    30         unsigned long parent) except -2:
    31     raise NotImplementedError, "this function shouldn't be called!!!"
     155#cdef inline int celement_invert(fmpz_poly_t a, fmpz_poly_t b,
     156#        unsigned long parent) except -2:
     157#    raise NotImplementedError, "this function shouldn't be called!!!"
    32158
    33159cdef inline bint celement_is_zero(fmpz_poly_t e, unsigned long parent):
     160    """
     161    TESTS::
     162
     163        sage: P.<x> = ZZ[]
     164        sage: type(x)
     165        <type 'sage.rings.polynomial.polynomial_integer_dense_flint.Polynomial_integer_dense_flint'>
     166        sage: from sage.libs.flint.fmpz_poly_linkage_tests import celement_is_zero_for_doctests as celement_is_zero
     167        sage: p = P.random_element(); p
     168        2*x - 8
     169        sage: celement_is_zero(p)
     170        False
     171        sage: p
     172        2*x - 8
     173        sage: res = P(0); res
     174        0
     175        sage: celement_is_zero(res)
     176        True
     177        sage: res
     178        0
     179        sage: celement_is_zero(P.one_element())
     180        False
     181    """
    34182    return fmpz_poly_length(e) == 0
    35183
    36184cdef inline long celement_len(fmpz_poly_t e, unsigned long parent):
     185    """
     186    TESTS::
     187
     188        sage: P.<x> = ZZ[]
     189        sage: type(x)
     190        <type 'sage.rings.polynomial.polynomial_integer_dense_flint.Polynomial_integer_dense_flint'>
     191        sage: from sage.libs.flint.fmpz_poly_linkage_tests import celement_len_for_doctests as celement_len
     192        sage: p = P.random_element(); p
     193        2*x - 8
     194        sage: celement_len(p)
     195        2
     196        sage: p
     197        2*x - 8
     198        sage: celement_len(P.zero_element())
     199        0
     200        sage: celement_len(P.one_element())
     201        1
     202        sage: celement_len(P.gen())
     203        2
     204    """
    37205    return fmpz_poly_length(e)
  • new file sage/libs/flint/fmpz_poly_linkage_tests.pyx

    diff --git a/sage/libs/flint/fmpz_poly_linkage_tests.pyx b/sage/libs/flint/fmpz_poly_linkage_tests.pyx
    new file mode 100644
    - +  
     1###############################################################################
     2#   Sage: Open Source Mathematical Software
     3#       Copyright (C) 2010 Burcin Erocal <burcin@erocal.org>
     4#  Distributed under the terms of the GNU General Public License (GPL),
     5#  version 2 or any later version.  The full text of the GPL is available at:
     6#                  http://www.gnu.org/licenses/
     7###############################################################################
     8"""
     9These functions are used to test the ``cdef``ed functions in
     10``sage/libs/flint/fmpz_poly_linkage.pxi``.
     11"""
     12from sage.libs.flint.fmpz_poly cimport *
     13include "fmpz_poly_linkage.pxi"
     14
     15from sage.rings.polynomial.polynomial_integer_dense_flint cimport \
     16        Polynomial_integer_dense_flint
     17
     18def celement_set_for_doctests(Polynomial_integer_dense_flint a,
     19        Polynomial_integer_dense_flint b):
     20    """
     21    TESTS::
     22
     23        sage: P.<x> = ZZ[]
     24        sage: type(x)
     25        <type 'sage.rings.polynomial.polynomial_integer_dense_flint.Polynomial_integer_dense_flint'>
     26        sage: p = P.random_element(); p
     27        2*x - 8
     28        sage: from sage.libs.flint.fmpz_poly_linkage_tests import celement_set_for_doctests
     29        sage: celement_set_for_doctests(p, x+1)
     30        sage: p
     31        x + 1
     32    """
     33    celement_set(a.__poly, b.__poly, 0)
     34
     35def celement_swap_for_doctests(Polynomial_integer_dense_flint a,
     36        Polynomial_integer_dense_flint b):
     37    """
     38    TESTS::
     39
     40        sage: P.<x> = ZZ[]
     41        sage: type(x)
     42        <type 'sage.rings.polynomial.polynomial_integer_dense_flint.Polynomial_integer_dense_flint'>
     43        sage: p = P.random_element(); p
     44        2*x - 8
     45        sage: r = P.random_element(); r
     46        -x^2 + x
     47        sage: from sage.libs.flint.fmpz_poly_linkage_tests import celement_swap_for_doctests
     48        sage: celement_swap_for_doctests(p, r)
     49        sage: p
     50        -x^2 + x
     51        sage: r
     52        2*x - 8
     53    """
     54    celement_swap(a.__poly, b.__poly)
     55
     56def celement_add_for_doctests(Polynomial_integer_dense_flint res,
     57        Polynomial_integer_dense_flint a, Polynomial_integer_dense_flint b):
     58    """
     59    TESTS::
     60
     61        sage: P.<x> = ZZ[]
     62        sage: type(x)
     63        <type 'sage.rings.polynomial.polynomial_integer_dense_flint.Polynomial_integer_dense_flint'>
     64        sage: p = P.random_element(); p
     65        2*x - 8
     66        sage: r = P.random_element(); r
     67        -x^2 + x
     68        sage: res = P(0); res
     69        0
     70        sage: from sage.libs.flint.fmpz_poly_linkage_tests import celement_add_for_doctests
     71        sage: celement_add_for_doctests(res, p, r)
     72        sage: p
     73        2*x - 8
     74        sage: r
     75        -x^2 + x
     76        sage: p+r
     77        -x^2 + 3*x - 8
     78        sage: res
     79        -x^2 + 3*x - 8
     80    """
     81    celement_add(res.__poly, a.__poly, b.__poly, 0)
     82
     83def celement_mul_for_doctests(Polynomial_integer_dense_flint res,
     84        Polynomial_integer_dense_flint a, Polynomial_integer_dense_flint b):
     85    """
     86    TESTS::
     87
     88        sage: P.<x> = ZZ[]
     89        sage: type(x)
     90        <type 'sage.rings.polynomial.polynomial_integer_dense_flint.Polynomial_integer_dense_flint'>
     91        sage: p = P.random_element(); p
     92        2*x - 8
     93        sage: r = P.random_element(); r
     94        -x^2 + x
     95        sage: res = P(0); res
     96        0
     97        sage: from sage.libs.flint.fmpz_poly_linkage_tests import celement_mul_for_doctests
     98        sage: celement_mul_for_doctests(res, p, r)
     99        sage: p
     100        2*x - 8
     101        sage: r
     102        -x^2 + x
     103        sage: p*r
     104        -2*x^3 + 10*x^2 - 8*x
     105        sage: res
     106        -2*x^3 + 10*x^2 - 8*x
     107    """
     108    celement_mul(res.__poly, a.__poly, b.__poly, 0)
     109
     110def celement_neg_for_doctests(Polynomial_integer_dense_flint a,
     111        Polynomial_integer_dense_flint b):
     112    """
     113    TESTS::
     114
     115        sage: P.<x> = ZZ[]
     116        sage: type(x)
     117        <type 'sage.rings.polynomial.polynomial_integer_dense_flint.Polynomial_integer_dense_flint'>
     118        sage: p = P.random_element(); p
     119        2*x - 8
     120        sage: res = P(0); res
     121        0
     122        sage: from sage.libs.flint.fmpz_poly_linkage_tests import celement_neg_for_doctests
     123        sage: celement_neg_for_doctests(res, p)
     124        sage: p
     125        2*x - 8
     126        sage: res
     127        -2*x + 8
     128    """
     129    celement_neg(a.__poly, b.__poly, 0)
     130
     131#def celement_invert_for_doctests(Polynomial_integer_dense_flint a, Polynomial_integer_dense_flint b):
     132#   celement_invert(a.__poly, b.__poly, 0)
     133
     134def celement_is_zero_for_doctests(Polynomial_integer_dense_flint e):
     135    """
     136    TESTS::
     137
     138        sage: P.<x> = ZZ[]
     139        sage: type(x)
     140        <type 'sage.rings.polynomial.polynomial_integer_dense_flint.Polynomial_integer_dense_flint'>
     141        sage: from sage.libs.flint.fmpz_poly_linkage_tests import celement_is_zero_for_doctests
     142        sage: p = P.random_element(); p
     143        2*x - 8
     144        sage: celement_is_zero_for_doctests(p)
     145        False
     146        sage: p
     147        2*x - 8
     148        sage: celement_is_zero_for_doctests(P(0))
     149        True
     150    """
     151    return celement_is_zero(e.__poly, 0)
     152
     153def celement_len_for_doctests(Polynomial_integer_dense_flint e):
     154    """
     155    TESTS::
     156
     157        sage: P.<x> = ZZ[]
     158        sage: type(x)
     159        <type 'sage.rings.polynomial.polynomial_integer_dense_flint.Polynomial_integer_dense_flint'>
     160        sage: from sage.libs.flint.fmpz_poly_linkage_tests import celement_len_for_doctests
     161        sage: p = P.random_element(); p
     162        2*x - 8
     163        sage: celement_len_for_doctests(p)
     164        2
     165        sage: p
     166        2*x - 8
     167    """
     168    return celement_len(e.__poly, 0)
  • sage/matrix/matrix_dense_template.pxi

    diff --git a/sage/matrix/matrix_dense_template.pxi b/sage/matrix/matrix_dense_template.pxi
    a b  
    55#  version 2 or any later version.  The full text of the GPL is available at:
    66#                  http://www.gnu.org/licenses/
    77###############################################################################
     8"""
     9This is a template for dense matrices where the entries are represented by
     10C structs, for example FLINT polynomials (elements of ZZ[x] and ZZ_p[x]).
     11
     12The templating system works in a similar fashion to the template for polynomial
     13classes provided in ``sage/rings/polynomial/polynomial_template.pxi``. In
     14order to use this template, write a wrapper file similar to
     15``sage/libs/flint/fmpz_poly_linkage.pxi``, defining the basic functions
     16used to manipulate the underlying C structs. In your `pyx` file defining the
     17element class for your object, first include the linkage file, then this
     18template file. In the corresponding pxd file, include the header template
     19``sage/matrix/matrix_dense_template_header.pxi``.
     20
     21See ``sage/matrix/matrix_ZZx_dense.pyx`` and
     22``sage/matrix/matrix_ZZx_dense.pxd`` for an example.
     23"""
    824
    925from sage.structure.element cimport Element
    1026
  • sage/matrix/matrix_misc.py

    diff --git a/sage/matrix/matrix_misc.py b/sage/matrix/matrix_misc.py
    a b  
    190190                (N[i], N[j]) = (N[j], N[i])
    191191                           
    192192    # return reduced matrix and operations matrix
    193     return (matrix(r)/den, matrix(N), d)
     193    return (matrix(r).apply_map(lambda x: x/den), matrix(N), d)