Loading [MathJax]/jax/output/HTML-CSS/jax.js
Ask Your Question
0

Creating a vector of indexed variables

asked 5 years ago

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=[x1,,x8]. And is it possible to create such a list (vector) without loop ?

Preview: (hide)

3 Answers

Sort by » oldest newest most voted
0

answered 5 years ago

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

(x0,x1,x2,x3,x4,x5,x6,x7)

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

(¯x0,¯x1,¯x2,¯x3,¯x4,¯x5,¯x6,¯x7)

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 x0 into 1, but you have missed the LaTeX representation. The output here is:

(123,¯x1,¯x2,¯x3,¯x4,¯x5,¯x6,¯x7)

Preview: (hide)
link

Comments

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

Cyrille gravatar imageCyrille ( 5 years ago )

Happy to help!

Yes, internally, you have to work x[2] instead of x2. 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 xi, for i0,,7. (The show command just displays the LaTeX output 10x25x4.)

dsejas gravatar imagedsejas ( 5 years ago )
0

answered 5 years ago

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.

(X1X2X3X4X5X6X7X8)

Preview: (hide)
link
0

answered 5 years ago

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]]

Preview: (hide)
link

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: 5 years ago

Seen: 2,848 times

Last updated: Oct 08 '19