% Copyright The Numerical Algorithms Group Limited 1992-94. All rights reserved. % !! DO NOT MODIFY THIS FILE BY HAND !! Created by ht.awk. \newcommand{\VectorXmpTitle}{Vector} \newcommand{\VectorXmpNumber}{9.85} % % ===================================================================== \begin{page}{VectorXmpPage}{9.85 Vector} % ===================================================================== \beginscroll The \spadtype{Vector} domain is used for storing data in a one-dimensional indexed data structure. A vector is a homogeneous data structure in that all the components of the vector must belong to the same \Language{} domain. Each vector has a fixed length specified by the user; vectors are not extensible. This domain is similar to the \spadtype{OneDimensionalArray} domain, except that when the components of a \spadtype{Vector} belong to a \spadtype{Ring}, arithmetic operations are provided. For more examples of operations that are defined for both \spadtype{Vector} and \spadtype{OneDimensionalArray}, see \downlink{`OneDimensionalArray'}{OneDimensionalArrayXmpPage}\ignore{OneDimensionalArray}. As with the \spadtype{OneDimensionalArray} domain, a \spadtype{Vector} can be created by calling the operation \spadfunFrom{new}{Vector}, its components can be accessed by calling the operations \spadfunFrom{elt}{Vector} and \spadfunFrom{qelt}{Vector}, and its components can be reset by calling the operations \spadfunFrom{setelt}{Vector} and \spadfunFromX{qsetelt}{Vector}. \xtc{ This creates a vector of integers of length \spad{5} all of whose components are \spad{12}. }{ \spadpaste{u : VECTOR INT := new(5,12) \bound{u}} } \xtc{ This is how you create a vector from a list of its components. }{ \spadpaste{v : VECTOR INT := vector([1,2,3,4,5]) \bound{v}} } \xtc{ Indexing for vectors begins at \spad{1}. The last element has index equal to the length of the vector, which is computed by \spadopFrom{\#}{Vector}. }{ \spadpaste{\#(v) \free{v}} } \xtc{ This is the standard way to use \spadfunFrom{elt}{Vector} to extract an element. Functionally, it is the same as if you had typed \spad{elt(v,2)}. }{ \spadpaste{v.2 \free{v}} } \xtc{ This is the standard way to use \spadfunFrom{setelt}{Vector} to change an element. It is the same as if you had typed \spad{setelt(v,3,99)}. }{ \spadpaste{v.3 := 99 \free{v}\bound{vdelta}} } \xtc{ Now look at \spad{v} to see the change. You can use \spadfunFrom{qelt}{Vector} and \spadfunFromX{qsetelt}{Vector} (instead of \spadfunFrom{elt}{Vector} and \spadfunFrom{setelt}{Vector}, respectively) but {\it only} when you know that the index is within the valid range. }{ \spadpaste{v \free{vdelta}} } \xtc{ When the components belong to a \spadtype{Ring}, \Language{} provides arithmetic operations for \spadtype{Vector}. These include left and right scalar multiplication. }{ \spadpaste{5 * v \free{vdelta}} } \xtc{ }{ \spadpaste{v * 7 \free{vdelta}} } \xtc{ }{ \spadpaste{w : VECTOR INT := vector([2,3,4,5,6]) \bound{w}} } \xtc{ Addition and subtraction are also available. }{ \spadpaste{v + w \free{vdelta w}} } \xtc{ Of course, when adding or subtracting, the two vectors must have the same length or an error message is displayed. }{ \spadpaste{v - w \free{vdelta w}} } For more information about other aggregate domains, see the following: \downlink{`List'}{ListXmpPage}\ignore{List}, \downlink{`Matrix'}{MatrixXmpPage}\ignore{Matrix}, \downlink{`OneDimensionalArray'}{OneDimensionalArrayXmpPage}\ignore{OneDimensionalArray}, \downlink{`Set'}{SetXmpPage}\ignore{Set}, \downlink{`Table'}{TableXmpPage}\ignore{Table}, and \downlink{`TwoDimensionalArray'}{TwoDimensionalArrayXmpPage}\ignore{TwoDimensionalArray}. Issue the system command \spadcmd{)show Vector} to display the full list of operations defined by \spadtype{Vector}. \endscroll \autobuttons \end{page} %