Ask Your Question
0

Unflatten a vector

asked 2019-04-09 15:22:18 +0100

abel gravatar image

updated 2019-04-11 02:34:16 +0100

slelievre gravatar image

I have a vector say a $9 \times 1$ vector which looks like $$\begin{bmatrix} 2x +1 \newline x \newline 1 \newline x \newline x^2 + 2x \newline 2x \newline x \newline 2x^2 \newline 0 \newline \end{bmatrix}$$ with entries in $\mathbb{F}_3[x]$.

There are 9 rows in this matrix and i want to write a function which takes an $n^2 \times 1$ matrix and turns it into a $n \times n$ matrix. So, in this case, we want the function would turn the above vector into

$$ \begin{bmatrix} 2x+1 & x & 1 \newline x & x^2+2x & 2x \newline x & 2x^2 & 0 \newline \end{bmatrix} $$

In this Sage, I tried this:

sage: v = Matrix(GF(3)[x], [[2*x+1],[x],[1],[x],[x^2+2*x],[2*x],[x],[2*x^2],[0]])

Then, write the following:

sage: Matrix(v.base_ring(), 3, 3, v)

This gave an error:

inconsistent number of rows: should be 3 but got 1
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2019-04-10 23:26:02 +0100

Another option: if v is a matrix of any shape with $3^2$ entries, then v.list() will list those entries. So then you can do matrix(3, 3, v.list()) to turn v into a 3 by 3 matrix.

edit flag offensive delete link more

Comments

and take the transpose then. that works.

abel gravatar imageabel ( 2019-04-12 11:54:01 +0100 )edit
1

answered 2019-04-09 16:17:23 +0100

rburing gravatar image

updated 2019-04-09 16:19:08 +0100

You can do the following if v is a $3^2 \times 1$ matrix (as in your example):

Matrix([vector(v[3*k:3*k+3,0].transpose()) for k in range(3)])

Or more easily, if v is a vector with $3^2$ components:

v = vector(GF(3)[x], [2*x+1,x,1,x,x^2+2*x,2*x,x,2*x^2,0])
Matrix([v[3*k:3*k+3] for k in range(3)])
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-04-09 15:22:18 +0100

Seen: 623 times

Last updated: Apr 11 '19