Ask Your Question
0

Vectorial division

asked 2020-06-22 18:09:31 -0600

Cyrille gravatar image

updated 2020-06-22 18:10:23 -0600

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 flag offensive close merge delete

Comments

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

John Palmieri gravatar imageJohn Palmieri ( 2020-06-22 19:07:46 -0600 )edit

I think he refers to elementwise division.

Juanjo gravatar imageJuanjo ( 2020-06-22 20:24:02 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
0

answered 2020-06-22 20:32:53 -0600

Juanjo gravatar image

updated 2020-06-22 21:14:53 -0600

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.

edit flag offensive delete link more

Comments

Thanks Juanjo

Cyrille gravatar imageCyrille ( 2020-06-22 22:49:18 -0600 )edit
0

answered 2020-06-22 20:55:10 -0600

updated 2020-06-22 20:56:24 -0600

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.)

edit flag offensive delete link more

Comments

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

Cyrille gravatar imageCyrille ( 2020-06-22 22:56:40 -0600 )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().

John Palmieri gravatar imageJohn Palmieri ( 2020-06-23 11:05:21 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-06-22 18:09:31 -0600

Seen: 113 times

Last updated: Jun 22