Ask Your Question
1

Matrices to Tables

asked 2019-12-21 20:00:14 +0200

Colin Clout gravatar image

I'm new to Sage, so this is probably a basic question.

I'm trying to use Sage to construct a time series, and turn the output into some form of table or matrix. (I'm trying to do this to learn Sage). The way I want to construct the time series is to use two matrices A1 and A2, and two seed vectors x0 and x1 to calculate xt=A1x(t-1)+A2x(t-2). It's easy to calculate, say x3, x4, x5 by hard coding each vector. And I can write a program that outputs the first k entries in the time series as a series of 1x2 matrices or row vectors. And, of course, I could highlight that output, copy and paste it into wordpad, and then use find and replace to turn it into a csv file.

Here's my code:

xt=x1 xt1=x0 for k in range(100): xtp1=A_1xt+A_2xt1 xk=xt xt=xtp1 xt1=xt xt.transpose()

Where x1 and x0 are two 1x2 matrices (really vectors) representing the first two states of the system, and A_1 and A_2 are 2x2 matrices that represent the linear equations that produce the next time step.

But that last step (copy and pasting into wordpad) is super inelegant. I'd like to be able create a table with the outputs. But the problem is that the outputs are 1xk matrices (or I can easily turn them into 1xk vectors), but the table command takes lists as inputs, and I can't figure out how to turn these 1xk matrices into 1xk lists, or to get the table to take a 1xk matrix as an input. I've tried googling ways to coerce matrices or vectors into lists, but I think my Sage vocabulary isn't developed enough to get the results I need.

edit retag flag offensive close merge delete

Comments

Please provide a self-contained example: give values for x0,x1,A_1,A_2, to make it easier for people to help you. Also, format your code as such by selecting it and pressing the 101010 button.

rburing gravatar imagerburing ( 2019-12-22 17:09:35 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2019-12-22 17:21:50 +0200

rburing gravatar image

Here is a way, based on your code:

x0 = matrix(QQ, [[0],[1]])
x1 = matrix(QQ, [[2],[0]])
A_1 = matrix(QQ, [[0,1],[1,0],])
A_2 = matrix(QQ, [[1,0],[0,1]])

xt=x1
xt1=x0
time_series = []
for k in range(10):
    xtp1=A_1*xt+A_2*xt1
    xk=xt
    xt=xtp1
    xt1=xt
    time_series.append(xt.transpose().list())
table(time_series)
edit flag offensive delete link more

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: 2019-12-21 19:57:46 +0200

Seen: 356 times

Last updated: Dec 22 '19