axiom-developer
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Axiom-developer] [Streams] (new)


From: Bill Page
Subject: [Axiom-developer] [Streams] (new)
Date: Fri, 18 Feb 2005 18:49:37 -0600

Changes http://page.axiom-developer.org/zope/mathaction/Streams/diff
--
Streams display only a few of their initial elements. Otherwise, they
are “lazy”: they only compute elements when you ask for them.

Series and streams make no attempt to compute all their elements!
Rather, they stand ready to deliver elements on demand.

Section 0.5.3 Streams

  Streams are infinite lists which have the ability to calculate
the next element should it be required. For example, a stream of
positive integers and a list of prime numbers can be generated by:
\begin{axiom}
[i for i in 1..]
\end{axiom}

Another method of creating streams is to use the generate(f,a)
function. This applies its first argument repeatedly onto its
second to produce the stream::

   ![a, f (a), f ( f (a)), f ( f ( f (a))) . . .]

Given that the function nextPrime returns the lowest prime number
greater than its argument we can generate a stream of primes as
follows:

\begin{axiom}
primes := generate(nextPrime,2)$Stream Integer
\end{axiom}

A stream is a structure that (potentially) has an infinite number
of distinct elements. Think of a stream as an “infinite list” where
elements are computed successively.

Axiom represents streams by a collection of already-computed
elements together with a function to compute the next element
“on demand.” Asking for the n-th element causes elements 1 through
n to be evaluated.

Streams can also be finite or cyclic. They are implemented by a
linked list structure similar to lists and have many of the same
operations. For example, first and rest are used to access elements
and successive nodes of a stream.

In contract, a **set** is a collection of elements where duplication
and order is irrelevant. Sets are always finite and have no
corresponding structure like streams for infinite collections.

By default, Axiom tries to compute and display the first ten elements
of a stream.Use:
\begin{axiom}
)set streams calculate 7
\end{axiom}

to change the default to something else, e.g. 7. This also affects the
number of terms computed and displayed for power series.

You can also create lists of streams, streams of lists and streams
of streams. Here is a stream of streams.
\begin{axiom}
[ [i/j for i in j+1..] for j in 1..]
\end{axiom}

Section 5.6 An Example: Streams of Primes

  Create a stream of primes.
\begin{axiom}
primes : Stream Integer := [i for i in 2.. | prime? i]
\end{axiom}

A more elegant way, however, is to use the generate operation as
show above.

Once the stream is generated, you might only be interested in
primes starting at a particular value.
\begin{axiom}
smallPrimes := [p for p in primes | p > 1000]
\end{axiom}

Here are the first 11 primes greater than 1000.
\begin{axiom}
[p for p in smallPrimes for i in 1..11]
\end{axiom}

Here is a stream of primes between 1000 and 1200.
\begin{axiom}
[p for p in smallPrimes while p < 1200]
\end{axiom}

Finally, we can convert a finite stream such as above to
a simple list.
\begin{axiom}
thesePrimes:=entries complete %
\end{axiom}

And now we can test for membership in the usual way.
\begin{axiom}
member?(1117,thesePrimes)
\end{axiom}

--
forwarded from http://page.axiom-developer.org/zope/mathaction/address@hidden




reply via email to

[Prev in Thread] Current Thread [Next in Thread]