Ask Your Question
0

Creating a vector of indexed variables

asked 2019-10-07 15:22:49 +0100

Cyrille gravatar image

How can I create a vector like the following one

X=matrix(1, 8,(x[0],x[1],x[2],x[3],x[4],x[5],x[6],x[7]))

with a loop like

X = list(var('x_%i' % i) for i in (0..7))
show(X)

but with an output $X = [x[1], ...x[8]]$ not $X = [x_1, \ldots, x_8]$. And is it possible to create such a list (vector) without loop ?

edit retag flag offensive close merge delete

3 Answers

Sort by » oldest newest most voted
0

answered 2019-10-07 16:33:13 +0100

dsejas gravatar image

Hello, @Cyrille! I don't know if this is what you want, but you can create variables with the var command. These variable will be symbolic variables, so they cannot carry any value. Let me explain:

Suppose you want to create 8 of these variables. You have to do

x = var('x', n=8, latex_name='x')

Then you can write show(x), which will produce

$(x_0,x_1,x_2,x_3,x_4,x_5,x_6,x_7)$

Note that the latex_name argument doesn't make any difference in this particular case. However, you can use it at your own discretion to create more complicated LaTeX representations. For example,

x = var("x", n=8, latex_name='\overline{x}')
show(x)

will produce

$(\overline{x}_0,\overline{x}_1,\overline{x}_2,\overline{x}_3,\overline{x}_4,\overline{x}_5,\overline{x}_6,\overline{x}_7)$

The disadvantage of this method is that you cannot assign values to the variables x[0], x[1], etc. In case you want to do that, you have to first convert x to a list:

x = list(var("x", n=8, latex_name='\overline{x}'))
x[0]=123
show(x)

This code has changed the value of $x_0$ into $1$, but you have missed the LaTeX representation. The output here is:

$(123,\overline{x}_1,\overline{x}_2,\overline{x}_3,\overline{x}_4,\overline{x}_5,\overline{x}_6,\overline{x}_7)$

edit flag offensive delete link more

Comments

Once more thanks djesas. I wonder if internaly $x_2$ is known as $x[2]$ ?

Cyrille gravatar imageCyrille ( 2019-10-07 20:26:53 +0100 )edit

Happy to help!

Yes, internally, you have to work x[2] instead of $x_2$. For example, if you want to write a smalllinear combination, you should do something like

x = var("x", n=8, latex_name='x')
lin_comb = 3*x[2] - 5*x[4] + 7*x[2]
show(lin_comb)

Notice how I used x[i] to work with $x_i$, for $i\in{0,\ldots,7}$. (The show command just displays the LaTeX output $10x_2−5x_4$.)

dsejas gravatar imagedsejas ( 2019-10-07 21:26:56 +0100 )edit
0

answered 2019-10-07 21:10:04 +0100

Emmanuel Charpentier gravatar image

Another solution:

sage: X=matrix(1,8,[var("X_{}".format(u)) for u in (1..8)])
sage: X
[X_1 X_2 X_3 X_4 X_5 X_6 X_7 X_8]
sage: latex(X)
\left(\begin{array}{rrrrrrrr}
X_{1} & X_{2} & X_{3} & X_{4} & X_{5} & X_{6} & X_{7} & X_{8}
\end{array}\right)

i. e.

$$ \left(\begin{array}{rrrrrrrr} X_{1} & X_{2} & X_{3} & X_{4} & X_{5} & X_{6} & X_{7} & X_{8} \end{array}\right)$$

edit flag offensive delete link more
0

answered 2019-10-08 14:37:14 +0100

slelievre gravatar image

The Python identifier can't have brackets so this fails:

sage: var('x[0]')
Traceback (most recent call last)
...
ValueError: The name "x[0]" is not a valid Python identifier.

but the LaTeX name can, so you can do:

sage: X = [SR.var('x_{}'.format(i), latex_name='x[{}]'.format(i)) for i in (0 .. 7)]

and then you would have in the Sage REPL:

sage: X
[x_0, x_1, x_2, x_3, x_4, x_5, x_6, x_7]
sage: show(X)
\newcommand{\Bold}[1]{\mathbf{#1}}\left[{x[0]}, {x[1]}, {x[2]}, {x[3]}, {x[4]}, {x[5]}, {x[6]}, {x[7]}\right]

but in a Jupyter notebook, show(X) would give a LaTeX-rendered $$[x[0],x[1],x[2],x[3],x[4],x[5],x[6],x[7]]$$

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-10-07 15:22:49 +0100

Seen: 2,714 times

Last updated: Oct 08 '19