www.delorie.com/gnu/docs/calc/calc_274.html | search |
Buy the book! | |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The a + (calc-summation
) [sum
] command computes
the sum of a formula over a certain range of index values. The formula
is taken from the top of the stack; the command prompts for the
name of the summation index variable, the lower limit of the
sum (any formula), and the upper limit of the sum. If you
enter a blank line at any of these prompts, that prompt and
any later ones are answered by reading additional elements from
the stack. Thus, ' k^2 RET ' k RET 1 RET 5 RET a + RET
produces the result 55.
The choice of index variable is arbitrary, but it's best not to
use a variable with a stored value. In particular, while
i
is often a favorite index variable, it should be avoided
in Calc because i
has the imaginary constant (0, 1)
as a value. If you pressed = on a sum over i
, it would
be changed to a nonsensical sum over the "variable" (0, 1)!
If you really want to use i
as an index variable, use
s u i RET first to "unstore" this variable.
(See section 13.1 Storing Variables.)
A numeric prefix argument steps the index by that amount rather than by one. Thus ' a_k RET C-u -2 a + k RET 10 RET 0 RET yields `a_10 + a_8 + a_6 + a_4 + a_2 + a_0'. A prefix argument of plain C-u causes a + to prompt for the step value, in which case you can enter any formula or enter a blank line to take the step value from the stack. With the C-u prefix, a + can take up to five arguments from the stack: The formula, the variable, the lower limit, the upper limit, and (at the top of the stack), the step value.
Calc knows how to do certain sums in closed form. For example, `sum(6 k^2, k, 1, n) = 2 n^3 + 3 n^2 + n'. In particular, this is possible if the formula being summed is polynomial or exponential in the index variable. Sums of logarithms are transformed into logarithms of products. Sums of trigonometric and hyperbolic functions are transformed to sums of exponentials and then done in closed form. Also, of course, sums in which the lower and upper limits are both numbers can always be evaluated just by grinding them out, although Calc will use closed forms whenever it can for the sake of efficiency.
The notation for sums in algebraic formulas is `sum(expr, var, low, high, step)'. If step is omitted, it defaults to one. If high is omitted, low is actually the upper limit and the lower limit is one. If low is also omitted, the limits are `-inf' and `inf', respectively.
Infinite sums can sometimes be evaluated: `sum(.5^k, k, 1, inf)'
returns 1. This is done by evaluating the sum in closed
form (to `1. - 0.5^n' in this case), then evaluating this
formula with n
set to inf
. Calc's usual rules
for "infinite" arithmetic can find the answer from there. If
infinite arithmetic yields a `nan', or if the sum cannot be
solved in closed form, Calc leaves the sum
function in
symbolic form. See section 5.5 Infinities.
As a special feature, if the limits are infinite (or omitted, as described above) but the formula includes vectors subscripted by expressions that involve the iteration variable, Calc narrows the limits to include only the range of integers which result in legal subscripts for the vector. For example, the sum `sum(k [a,b,c,d,e,f,g]_(2k),k)' evaluates to `b + 2 d + 3 f'.
The limits of a sum do not need to be integers. For example, `sum(a_k, k, 0, 2 n, n)' produces `a_0 + a_n + a_(2 n)'. Calc computes the number of iterations using the formula `1 + (high - low) / step', which must, after simplification as if by a s, evaluate to an integer.
If the number of iterations according to the above formula does not come out to an integer, the sum is illegal and will be left in symbolic form. However, closed forms are still supplied, and you are on your honor not to misuse the resulting formulas by substituting mismatched bounds into them. For example, `sum(k, k, 1, 10, 2)' is invalid, but Calc will go ahead and evaluate the closed form solution for the limits 1 and 10 to get the rather dubious answer, 29.25.
If the lower limit is greater than the upper limit (assuming a positive step size), the result is generally zero. However, Calc only guarantees a zero result when the upper limit is exactly one step less than the lower limit, i.e., if the number of iterations is -1. Thus `sum(f(k), k, n, n-1)' is zero but the sum from `n' to `n-2' may report a nonzero value if Calc used a closed form solution.
Calc's logical predicates like a < b return 1 for "true"
and 0 for "false." See section 11.10 Logical Operations. This can be
used to advantage for building conditional sums. For example,
`sum(prime(k)*k^2, k, 1, 20)' is the sum of the squares of all
prime numbers from 1 to 20; the prime
predicate returns 1 if
its argument is prime and 0 otherwise. You can read this expression
as "the sum of k^2, where k is prime." Indeed,
`sum(prime(k)*k^2, k)' would represent the sum of all primes
squared, since the limits default to plus and minus infinity, but
there are no such sums that Calc's built-in rules can do in
closed form.
As another example, `sum((k != k_0) * f(k), k, 1, n)' is the sum of f(k) for all k from 1 to n, excluding one value k_0. Slightly more tricky is the summand `(k != k_0) / (k - k_0)', which is an attempt to describe the sum of all 1/(k-k_0) except at k = k_0, where this would be a division by zero. But at k = k_0, this formula works out to the indeterminate form 0 / 0, which Calc will not assume is zero. Better would be to use `(k != k_0) ? 1/(k-k_0) : 0'; the `? :' operator does an "if-then-else" test: This expression says, "if k != k_0, then 1/(k-k_0), else zero." Now the formula 1/(k-k_0) will not even be evaluated by Calc when k = k_0.
The a - (calc-alt-summation
) [asum
] command
computes an alternating sum. Successive terms of the sequence
are given alternating signs, with the first term (corresponding
to the lower index value) being positive. Alternating sums
are converted to normal sums with an extra term of the form
`(-1)^(k-low)'. This formula is adjusted appropriately
if the step value is other than one. For example, the Taylor
series for the sine function is `asum(x^k / k!, k, 1, inf, 2)'.
(Calc cannot evaluate this infinite series, but it can approximate
it if you replace inf
with any particular odd number.)
Calc converts this series to a regular sum with a step of one,
namely `sum((-1)^k x^(2k+1) / (2k+1)!, k, 0, inf)'.
The a * (calc-product
) [prod
] command is
the analogous way to take a product of many terms. Calc also knows
some closed forms for products, such as `prod(k, k, 1, n) = n!'.
Conditional products can be written `prod(k^prime(k), k, 1, n)'
or `prod(prime(k) ? k : 1, k, 1, n)'.
The a T (calc-tabulate
) [table
] command
evaluates a formula at a series of iterated index values, just
like sum
and prod
, but its result is simply a
vector of the results. For example, `table(a_i, i, 1, 7, 2)'
produces `[a_1, a_3, a_5, a_7]'.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
webmaster | delorie software privacy |
Copyright © 2003 by The Free Software Foundation | Updated Jun 2003 |