# Vectorial division

If $\boldsymbol{D}$ is a matrix. One can easily compute

r1=[(D[j+1]-D[j]) for j in range(D.nrows()-1)]


but is there a simple way to compute :

r1=[(D[j+1]-D[j])/D[j] for j in range(D.nrows()-1)]

edit retag close merge delete

What does it mean to divide a vector by a vector?

( 2020-06-23 02:07:46 +0100 )edit

I think he refers to elementwise division.

( 2020-06-23 03:24:02 +0100 )edit

Sort by ยป oldest newest most voted

Numpy arrays admit elementwise operations. So you could convert the matrix to a Numpy array, compute the new matrix R and then come back to a SageMath matrix:

import numpy
R = D.numpy()
R = numpy.diff(R, axis=0)/R[0:-1,:]
R = matrix(R)


You can also opt for a pure Python approach:

nr, nc = D.nrows()-1, D.ncols()
R = matrix(nr, nc, [(D[i+1,j]-D[i,j])/D[i,j]
for i in range(nr) for j in range(nc)])


The first method is faster.

more

Thanks Juanjo

( 2020-06-23 05:49:18 +0100 )edit

Both

r1=[vector((D[j+1][i]-D[j][i])/D[j][i] for i in range(D[j].degree())) for j in range(D.nrows()-1)]


and

r1=[vector((D[j+1, i]-D[j, i])/D[j, i] for i in range(D[j].degree())) for j in range(D.nrows()-1)]


work for me.

(Elementwise vector division is not a standard mathematical operation, so it makes sense that just dividing by D[j] is not implemented in SageMath, nor should it be, since the software is aimed at mathematical use.)

more

John sorry but I do not understand D[j].degree(). Could you explain ?

( 2020-06-23 05:56:40 +0100 )edit

Try it: D = ... some matrix ..., then v = D[0]. Then do v.degree? to see Return the degree of this vector, which is simply the number of entries. You could replace D[j].degree() by D.ncols().

( 2020-06-23 18:05:21 +0100 )edit