home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Media Share 9
/
MEDIASHARE_09.ISO
/
utility
/
sm22a.zip
/
SYMBMATH.H11
< prev
next >
Wrap
Text File
|
1993-04-21
|
4KB
|
169 lines
7.9 Lists, Arrays, Vectors and Matrices
SymbMath can construct lists of arbitrary length, and the
entries in the lists can be of any type of value whatsoever:
constants, expressions with undefined variables, or even other lists.
Lists are another kind of value in SymbMath, and they can be assigned
to variables just like simple values. (Since variables in SymbMath
language are untyped, you can assign any value to any variable.).
7.9.1 Entering Lists
To define a list, put its elements between square brackets:
a = [1,2,3]
b = [f(2), g(1), h(1)] # assumes f,g,h defined
c = [[1,2],3,[4,5]]
A function can have a list for its value:
f(x) = [1,x,x^2]
You can define lists another way, with the list command:
list(f(x), x from xmin to xmax step dx)
This is similar to the sum command, but the result is a list:
[f(xmin), f(xmin+dx), ..., f(xmin+x*dx), ...]
which continues until the last value of xmin + x*dx <= xmax.
Try
a = list(j^2, j from 0 to 10 step 1)
f(x) = list(x^j, j from 0 to 6 step 1)
b = f(-2)
The third way to construct a list is to transform the sum to
the list.
Input:
y1=[a,b,c]
sum(y1)+d
list(last)
end
Output:
y1 = [a, b, c]
a + b + c + d
[a, b, c, d]
This is how you extend an existing list to include a new
element.
7.9.2 Accessing Lists
To find the value of the j-th element in a list x, use the
formula x[j]. The first element of x is always x[1]. If the x[j]
is itself a list, then its k-th element is accessed by repeating the
similar step. Try:
Input:
x = [[1,2],3,[4,5]]
x[1]
x[2]
end
Output:
x = [[1, 2], 3, [4, 5]]
[1, 2]
3
An entire sub-list of a list x can be accessed with the
command x[j], which is the list:
[x[j], x[j+1], ... ]
7.9.3 Modifying Lists
The function subs() substitutes the value of the element in
the list, as in the variables. e.g.
Input:
l=[a,b,c]
subs(l, a=a0)
end
Output:
l = [a, b, c]
[a0, b, c]
But you can't use this form unless the element of the list is
already defined.
7.9.4 List Operations
Lists can be added, subtracted, multiplied, and divided by
other lists or by constants. When two lists are combined, they are
combined term-by-term, and the combination stops when the shortest
list is exhausted. When a scalar is combined with a list, it is
combined with each element of the list. Try:
a = [1,2,3]
b = [4,5,6]
a + b
a / b
3 * a
b - 4
Example 7.9.1. Two lists are added.
Input:
list1=[a1,a2,a3]
list2=[b1,b2,b3]
list1+list2
last[1]
end
Output:
list1 = [a1,a2,a3]
list2 = [b1,b2,b3]
[a1 + b1, a2 + b2, a3 + b3]
a1 + b1
If L is a list, then f(L) results in a list of the values,
even though f() is the differentiation or integration function d() or
inte(). Try list with:
Input:
sqrt([a, b, c])
d([x, x^2, x^3], x)
end
You can find the number of elements in a list with:
length(a)
If you use a list as the value of a variable in a user-
defined function, SymbMath will try to use the list in the
calculation.
You can sum all the elements in a list x with the user-
defined function:
listsum(x)
in the statistics package 'stat.sm'.
Example:
x=[1,2,3]
listsum(x^2)
This functions takes the sum of the squares of the elements of
the list x.
See the statistics package 'stat.sm' for other statistics
operations (e.g. average, max, min).
See the list plot package 'listplot.sm' for plotting a list.
7.9.5 Vector Operations
SymbMath uses lists to represent vectors, and lists of lists to
represent matrices.
Vectors and matrices can be operated by "+" and "-" with vectors
and matrixes, by "*" and "/" with a scalar, and by subs(), diff() and
inte(). These operations are on each element, as in lists and arrays.
You can use lists as vectors, adding them and multiplying them
by scalars. For example, the dot product of two vectors of a and b is:
dot = listsum(a*b)
You can even make this into a function:
Dot(x_, y_) = listsum(x*y)
a = [2,3,5]
b = [4,3,2]
Dot(a,b)
How about the cross product:
Cross(a,b) = [a[2]*b[3]-b[2]*a[3],a[3]*b[1]-b[3]*a[1],a[1]*b[2]-b[1]*a[2]]