Bug? in n() function for vectors?

28 views
Skip to first unread message

GaryMak

unread,
Aug 16, 2013, 5:40:25 PM8/16/13
to sage-s...@googlegroups.com
Hi all

I hesitate to declare a <<bug>>, since it's probably something I cannot find in the help, but the following is very strange ....

The following code always seems to output many vectors "quad" of length 4 each of whose numerical approx "quad.n()" is a vector which only has length 3 ...

###########  START CODE ###########

vec = vector([1.0,1.0,1.0,1.0])
ZZ = diagonal_matrix([exp(2*pi*I/4)^ii for ii in range(0,4)])

for ss in range(0,100):
    quad = vector([0,0,0,0])
    for jj in range(0,4):
        indy = round(random()*4.0)
        if len(quad.n())==3:
            print quad.n(), quad
        quad = quad + vec*ZZ^indy

###########  END CODE ###########

I believe I have eliminated every non-essential: for example, even though the loop in the vble "jj" is not explicitly used, it nevertheless seems to be necessary to produce this weird behaviour, as does some complex number being in the calcs, and most bizarrely, so is the fact that "indy" be random! (ie it does not happen if I just fix "indy" to be 0,1,2,3 or 4).

Needless to say, this is rather tricky to work around!

Many thanks in advance for any help.

Best regards

Gary.


GaryMak

unread,
Aug 16, 2013, 5:42:01 PM8/16/13
to sage-s...@googlegroups.com
PS sorry I forgot to mention this happens in 5.7 and 5.11 on a Mac OSX 5.6.8

Jason Grout

unread,
Aug 16, 2013, 6:34:56 PM8/16/13
to sage-s...@googlegroups.com
On 8/16/13 4:40 PM, GaryMak wrote:
> Hi all
>
> I hesitate to declare a <<bug>>, since it's probably something I cannot
> find in the help, but the following is very strange ....
>
> The following code always seems to output many vectors "quad" of length
> 4 each of whose numerical approx "quad.n()" is a vector which only has
> length 3 ...

Indeed, it is a bug. The problem is that quad is a sparse vector with
zero as its last entry. You see, diagonal_matrix creates a sparse
matrix, so ZZ^indy creates a sparse vector. That means that
quad+ZZ^indy is a sparse vector. Then the bug bites us when the last
element is zero. The .n() function for a sparse vector calls this
one-liner:

vector(dict([(e[0],e[1].n(prec, digits)) for e in
self._entries.iteritems()]), sparse=True)

Notice that the size isn't specified anywhere, so it is guessed from the
biggest element of the dictionary. Since the last element was zero,
it's not in the dictionary, so the vector is shortened.

Instead, that sparse _numerical_approx function should be:

vector(R, len(self), dict([(e[0],e[1].n(prec, digits)) for e in
self._entries.iteritems()]), sparse=True)

where the ring R is somehow determined. Maybe the dictionary can be
constructed first, then the ring in which the entries live can be
determined, and then the vector call can be made.

Alternatively, a final zero element can be added to the dictionary, so
the one-liner above becomes:

entries=dict([(e[0],e[1].n(prec, digits)) for e in
self._entries.iteritems()])
if len(self)-1 not in entries:
entries[len(self)-1]=0
vector(entries, sparse=True)

since this apparently works:

v=vector({1:32,4:2,10:0},sparse=True)


Gary: are you comfortable making a trac ticket and patch for this? It
looks like the function that needs to be fixed is the very last function
in SAGE_ROOT/devel/sage/sage/modules/free_module_element.pyx:

https://github.com/sagemath/sage/blob/master/src/sage/modules/free_module_element.pyx#L4503


Thanks,

Jason


Jason Grout

unread,
Aug 16, 2013, 9:06:59 PM8/16/13
to sage-s...@googlegroups.com
On 8/16/13 5:34 PM, Jason Grout wrote:
> entries=dict([(e[0],e[1].n(prec, digits)) for e in
> self._entries.iteritems()])
> if len(self)-1 not in entries:
> entries[len(self)-1]=0
> vector(entries, sparse=True)


It could be shortened by using the setdefault method:

entries=dict([(e[0],e[1].n(prec, digits)) for e in
self._entries.iteritems()])
entries.setdefault(len(self)-1, 0)
vector(entries, sparse=True)

Thanks,

Jason


GaryMak

unread,
Aug 17, 2013, 4:58:40 AM8/17/13
to sage-s...@googlegroups.com
Dear Jason,

thank you very much.

Re the patch: sadly, I tried (thanks to Basu's infinitely patient hints) to get involved with amending the docs a few months ago, but failed miserably and even put up an ad for someone to help me with SAGE/computing here at Imperial (!) but got nowhere with that ... I would love to be able to do this but there are certain basics of how you guys have set this up which I simply do not understand. And, frankly, my one year of formal IT tuition in 1987 just doesn't cut it! So if you guys know of anyone in London who can do some hand-holding (for which I'm happy to pay) through this stuff then please put us in contact and I'll have another go ...

Best regards

Gary

GaryMak

unread,
Aug 17, 2013, 8:29:21 AM8/17/13
to sage-s...@googlegroups.com
By the way, following Jason's reasoning above, by re-defining ZZ (which I have just realised was a bad choice of variable name sorry!) as 

ZZ = diagonal_matrix([exp(2*pi*I/4)^ii for ii in range(0,4)]).dense_matrix()

the problem does indeed go away. So there is a temporary workaround ...
Reply all
Reply to author
Forward
0 new messages