Lists and transformations of lists

Example 1. Vectors and other similar mathematical objects are presented in Mathematica through a list of arranged elements, unified in curly brackets { }. The list can be stored in an ordinary variable. Extraction of a specific element is done with the operation [[ ]] - double square brackets. Here are two five dimensional vectors.

{1,2,3,4,5}
a={-2.2,3.2,0,2,0}
a[[1]]
a[[3]]

{1, 2, 3, 4, 5}

RowBox[{{, RowBox[{RowBox[{-, 2.2}], ,, 3.2, ,, 0, ,, 2, ,, 0}], }}]

RowBox[{-, 2.2}]

0

Example 2. Squaring, increasing power or any other action is done for each separate element.

b={0,1,2,3,4,5}

{0, 1, 2, 3, 4, 5}

c1 = x^b c2 = % + y^b

{1, x, x^2, x^3, x^4, x^5}

{2, x + y, x^2 + y^2, x^3 + y^3, x^4 + y^4, x^5 + y^5}

Example 3. Specific replacements with variable values and extraction of 2nd and 3rd element from the obtained results.

c1 /.x->2
c2 /. {x->1, y->2z}
%[[2]]      (* Extraction of the second element from the prevoius result *)
%%[[3]]     (* Third element from the before last result *)

{1, 2, 4, 8, 16, 32}

{2, 1 + 2 z, 1 + 4 z^2, 1 + 8 z^3, 1 + 16 z^4, 1 + 32 z^5}

1 + 2 z

1 + 4 z^2

Example 4. Differentiation - first and second derivative with respect to x. Mathematical symbols from the palettes may also be used, such as ∂_x, ∂_ (x, x), ∂_ (x, x, x), ∂_ (x, y) , etc.

D[c1, x] ∂_x c1 D[c1, x, x] ∂_ (x, x) c1

{0, 1, 2 x, 3 x^2, 4 x^3, 5 x^4}

{0, 1, 2 x, 3 x^2, 4 x^3, 5 x^4}

{0, 0, 2, 6 x, 12 x^2, 20 x^3}

{0, 0, 2, 6 x, 12 x^2, 20 x^3}

Example 5.  The table is also a list, but its elements are calculated with formulas.

Table[k^3, {k, 1, 10}] % //TableForm

{1, 8, 27, 64, 125, 216, 343, 512, 729, 1000}

1
8
27
64
125
216
343
512
729
1000

Example 6.  For example the table of the function x^(1/2) with x changing in the interval [0, 1] with a 0.2 step is done like this:

RowBox[{t1, =, RowBox[{Table, [, RowBox[{x^(1/2), ,,  , RowBox[{{, RowBox[{x, ,, 0, ,, 1, ,,  , 0.2}], }}]}], ]}]}]

RowBox[{{, RowBox[{0, ,, 0.447214, ,, 0.632456, ,, 0.774597, ,, 0.894427, ,, 1.}], }}]

Example 7.  Here are  generating of 5 random numbers in the interval [0,1]. Then - 6 random numbers in the interval [1, 49] which are sorted in ascending way with the fucntion Sort.

Table[Random[],{5}]
Table[Random[Integer,{1,49}],{6}]  
s1=Sort[%]

RowBox[{{, RowBox[{0.224989, ,, 0.759434, ,, 0.809121, ,, 0.262081, ,, 0.0345155}], }}]

{24, 33, 23, 44, 17, 43}

{17, 23, 24, 33, 43, 44}

Example 8. Showing the list vertically.

TableForm[s1]

17
23
24
33
43
44

Example 9.  The same with the function ColumnForm.

ColumnForm[s1]

17
23
24
33
43
44

Example 10. Forming a two dimensional list, which is a list of lists and it its presentation in different ways.

a=.
a=Table[i+j,{i,1,3},{j,1,3}]
TableForm[a]
ColumnForm[a]

{{2, 3, 4}, {3, 4, 5}, {4, 5, 6}}

2 3 4
3 4 5
4 5 6
{2, 3, 4}
{3, 4, 5}
{4, 5, 6}

Example 11. Table of two rows and four columns.

b=Table[i+j,{i,1,2},{j,1,4}]
TableForm[b]
MatrixForm[b]

{{2, 3, 4, 5}, {3, 4, 5, 6}}

2 3 4 5
3 4 5 6

( 2   3   4   5 )            3   4   5   6

Example 12.  Extraction of matrix elements.

b[[1]]
b[[2,3]]
Part[b,1]

{2, 3, 4, 5}

5

{2, 3, 4, 5}

Example 13.  Product of vectors and matrices.

Clear[a,b,c,p,q,r,u,v]
u={a,b,c}
v={p,q,r}
u.v

{a, b, c}

{p, q, r}

a p + b q + c r


Created by Mathematica  (December 21, 2007)