From: Michael Orlitzky Date: Mon, 12 Nov 2018 02:59:29 +0000 (-0500) Subject: cone/rearrangement.py: add a few more tests. X-Git-Url: http://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=d6b360ff88d5c1b0e7d29a06bd85d0dcc1e6eac9;p=sage.d.git cone/rearrangement.py: add a few more tests. Add a few more tests to the rearrangement_cone() function. In particular, delete the has_rearrangement_property() function since it only served to test the rearrangement cone. The property tests were moved into the cone doctests. The cone constructor now also checks that "p" is within bounds, and throws a ValueError if not. There are tests for this too. --- diff --git a/mjo/cone/rearrangement.py b/mjo/cone/rearrangement.py index b868dd8..76b0dad 100644 --- a/mjo/cone/rearrangement.py +++ b/mjo/cone/rearrangement.py @@ -95,68 +95,54 @@ def rearrangement_cone(p,n): sage: all( K.contains(P*r) for r in K ) True - """ - - def d(j): - v = [1]*n # Create the list of all ones... - v[j] = 1 - p # Now "fix" the ``j``th entry. - return v - - V = VectorSpace(QQ, n) - G = V.basis() + [ d(j) for j in xrange(n) ] - return Cone(G) + The smallest ``p`` components of every element of the rearrangement + cone should sum to a nonnegative number (this tests that the + generators really are what we think they are):: + sage: set_random_seed() + sage: def _has_rearrangement_property(v,p): + ....: return sum( sorted(v)[0:p] ) >= 0 + sage: all( _has_rearrangement_property( + ....: rearrangement_cone(p,n).random_element(), + ....: p + ....: ) + ....: for n in xrange(2, 10) + ....: for p in xrange(1, n-1) + ....: ) + True -def has_rearrangement_property(v, p): - r""" - Test if the vector ``v`` has the "rearrangement property." - - The rearrangement cone of order ``p`` in `n` dimensions has its - members vectors of length `n`. The "rearrangement property," - satisfied by its elements, is to have its smallest ``p`` components - sum to a nonnegative number. - - We believe that we have a description of the extreme vectors of the - rearrangement cone: see ``rearrangement_cone()``. This function is - used to test that conic combinations of those extreme vectors are in - fact elements of the rearrangement cone. We can't test all conic - combinations, obviously, but we can test a random one. - - To become more sure of the result, generate a bunch of vectors with - ``random_element()`` and test them with this function. - - INPUT: - - - ``v`` -- An element of a cone suspected of being the rearrangement - cone of order ``p``. - - - ``p`` -- The suspected order of the rearrangement cone. - - OUTPUT: - - If ``v`` has the rearrangement property (that is, if its smallest ``p`` - components sum to a nonnegative number), ``True`` is returned. Otherwise - ``False`` is returned. + The rearrangenent cone of order ``p`` is contained in the + rearrangement cone of order ``p + 1``:: - SETUP:: + sage: set_random_seed() + sage: n = ZZ.random_element(2,10) + sage: p = ZZ.random_element(1,n) + sage: K1 = rearrangement_cone(p,n) + sage: K2 = rearrangement_cone(p+1,n) + sage: all( x in K2 for x in K1 ) + True - sage: from mjo.cone.rearrangement import (has_rearrangement_property, - ....: rearrangement_cone) + The order ``p`` should be between ``1`` and ``n``, inclusive:: - EXAMPLES: + sage: rearrangement_cone(0,3) + Traceback (most recent call last): + ... + ValueError: order p=0 should be between 1 and n=3, inclusive + sage: rearrangement_cone(5,3) + Traceback (most recent call last): + ... + ValueError: order p=5 should be between 1 and n=3, inclusive - Every element of a rearrangement cone should have the property:: + """ + if p < 1 or p > n: + raise ValueError('order p=%d should be between 1 and n=%d, inclusive' + % + (p,n)) - sage: set_random_seed() - sage: all( has_rearrangement_property( - ....: rearrangement_cone(p,n).random_element(), - ....: p - ....: ) - ....: for n in xrange(2, 10) - ....: for p in xrange(1, n-1) - ....: ) - True + def d(j): + v = [1]*n # Create the list of all ones... + v[j] = 1 - p # Now "fix" the ``j``th entry. + return v - """ - components = sorted(v)[0:p] - return sum(components) >= 0 + G = identity_matrix(ZZ,n).rows() + [ d(j) for j in xrange(n) ] + return Cone(G)