Opened 2 years ago

# Wrong results in vector matrix multiplication

Reported by: Owned by: sbrandhorst major sage-8.1 linear algebra sd91 N/A u/roed/vmat_action (Commits) 6021daa7081396ae0c418b8f34ad69dc96a682c7

### Description

```sage: M = FreeModule(ZZ,1).span([vector([1/3])])
sage: x = M([1/3])
sage: x
(1/3)
sage: A = Matrix(QQ,)
sage: x*A , A*x
((1/3), (1/3))
```

This is good.

```sage: B = Matrix(ZZ,)
sage: x*B , B*x
((1), (1/3))
```

The first entry is clearly wrong. Somehow the first product is carried out in a wrong basis representation for x.

### comment:2 Changed 2 years ago by roed

The problem appears in line 1122 of `sage.matrix.matrix_integer_dense.pyx`, inside the function `cdef _vector_times_matrix_(self, Vector v)`:

```w = <Vector_integer_dense> v
```

If you change that to

```w = <Vector_integer_dense?> v
```

you'll get an error, because `v` is a `Vector_rational_dense`, and if you change it to

```try:
w = <Vector_integer_dense?> v
except TypeError:
return Matrix_dense._vector_times_matrix_(self, v)
```

you get `(1/3)`.

This function is replacing a more general implementation in `sage.matrix.matrix0`, but it only replaces `_vector_times_matrix_`, not `_matrix_times_vector_`, thus the disparity based on whether `B` is acting on the left or right.

I don't know if the try/except block is the right answer. I'll think about it....

### comment:3 Changed 2 years ago by roed

There's maybe a separate issue in `sage.matrix.action`, since it detects the base ring of the result by checking the base ring of the inputs. But here `x` and `B` both have base ring `ZZ`, and indeed

```sage: from sage.structure.element import coercion_model
sage: from operator import mul
sage: coercion_model.explain(x.parent(), B.parent(), mul)
Action discovered.
Right action by Full MatrixSpace of 1 by 1 dense matrices over Integer Ring on Free module of degree 1 and rank 1 over Integer Ring
Echelon basis matrix:
[1/3]
Result lives in Ambient free module of rank 1 over the principal ideal domain Integer Ring
Ambient free module of rank 1 over the principal ideal domain Integer Ring
```

The result should probably have rational entries....

Last edited 2 years ago by roed (previous) (diff)

### comment:4 Changed 2 years ago by roed

• Branch set to u/roed/vmat_action

### comment:5 Changed 2 years ago by roed

 ​6021daa `Correct base ring for matrices acting on submodules, implement multiplication for Vector_rational_dense times an integral matrix.`