ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 22 Dec 2019 10:21:50 -0600Matrices to Tableshttps://ask.sagemath.org/question/49116/matrices-to-tables/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=A1*x(t-1)+A2*x(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_1*xt+A_2*xt1
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.Sat, 21 Dec 2019 12:57:46 -0600https://ask.sagemath.org/question/49116/matrices-to-tables/Comment by rburing for <p>I'm new to Sage, so this is probably a basic question.</p>
<p>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=A1<em>x(t-1)+A2</em>x(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.</p>
<p>Here's my code:</p>
<p>xt=x1
xt1=x0
for k in range(100):
xtp1=A_1<em>xt+A_2</em>xt1
xk=xt
xt=xtp1
xt1=xt
xt.transpose()</p>
<p>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.</p>
<p>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.</p>
https://ask.sagemath.org/question/49116/matrices-to-tables/?comment=49122#post-id-49122Please 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.Sun, 22 Dec 2019 10:09:35 -0600https://ask.sagemath.org/question/49116/matrices-to-tables/?comment=49122#post-id-49122Answer by rburing for <p>I'm new to Sage, so this is probably a basic question.</p>
<p>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=A1<em>x(t-1)+A2</em>x(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.</p>
<p>Here's my code:</p>
<p>xt=x1
xt1=x0
for k in range(100):
xtp1=A_1<em>xt+A_2</em>xt1
xk=xt
xt=xtp1
xt1=xt
xt.transpose()</p>
<p>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.</p>
<p>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.</p>
https://ask.sagemath.org/question/49116/matrices-to-tables/?answer=49123#post-id-49123Here 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)Sun, 22 Dec 2019 10:21:50 -0600https://ask.sagemath.org/question/49116/matrices-to-tables/?answer=49123#post-id-49123