OSDN Git Service

FIRST REPOSITORY
[eos/hostdependOTHERS.git] / HP / util / HP / man / mann / vector.n
1 '\"
2 '\" Copyright 1991-1997 by Lucent Technologies, Inc.
3 '\"
4 '\" Permission to use, copy, modify, and distribute this software and its
5 '\" documentation for any purpose and without fee is hereby granted, provided
6 '\" that the above copyright notice appear in all copies and that both that the
7 '\" copyright notice and warranty disclaimer appear in supporting documentation,
8 '\" and that the names of Lucent Technologies any of their entities not be used
9 '\" in advertising or publicity pertaining to distribution of the software
10 '\" without specific, written prior permission.
11 '\"
12 '\" Lucent Technologies disclaims all warranties with regard to this software,
13 '\" including all implied warranties of merchantability and fitness.  In no event
14 '\" shall Lucent Technologies be liable for any special, indirect or
15 '\" consequential damages or any damages whatsoever resulting from loss of use,
16 '\" data or profits, whether in an action of contract, negligence or other
17 '\" tortuous action, arising out of or in connection with the use or performance
18 '\" of this software.  
19 '\"
20 '\" Vector command created by George Howlett.
21 '\"
22 '\"     # CS - begin code display
23 .de CS
24 .ft R
25 .nf
26 ..
27 '\"     # CE - end code display
28 .de CE
29 .ft R
30 .fi
31 ..
32 .HS vector BLT
33 .BS
34 '\" Note:  do not modify the .SH NAME line immediately below!
35 .SH NAME
36 vector \-  Vector data type for Tcl
37 .SH SYNOPSIS
38 \fBvector create \fIvecName \fR?\fIvecName\fR...? ?\fIswitches\fR? 
39 .sp
40 \fBvector destroy \fIvecName \fR?\fIvecName\fR...?
41 .sp
42 \fBvector expr \fIexpression\fR
43 .sp
44 \fBvector names \fR?\fIpattern\fR...?
45 .BE
46 .SH DESCRIPTION
47 The \fBvector\fR command creates a vector of floating point
48 values.  The vector's components can be manipulated in three ways:
49 through a Tcl array variable, a Tcl command, or the C API.
50 .SH INTRODUCTION
51 A vector is simply an ordered set of numbers.  The components of a
52 vector are real numbers, indexed by counting numbers.
53 .PP
54 Vectors are common data structures for many applications.  For
55 example, a graph may use two vectors to represent the X-Y
56 coordinates of the data plotted.  The graph will automatically
57 be redrawn when the vectors are updated or changed. By using vectors, 
58 you can separate
59 data analysis from the graph widget.  This makes it easier, for
60 example, to add data transformations, such as splines.  It's possible
61 to plot the same data to in multiple graphs, where each graph presents
62 a different view or scale of the data.
63 .PP
64 You could try to use Tcl's associative arrays as vectors.  Tcl arrays
65 are easy to use.  You can access individual elements randomly by
66 specifying the index, or the set the entire array by providing a list
67 of index and value pairs for each element.  The disadvantages of 
68 associative arrays as vectors lie in the fact they are implemented as
69 hash tables.
70 .TP 2
71 \(bu 
72 There's no implied ordering to the associative arrays.  If you used
73 vectors for plotting, you would want to insure the second component
74 comes after the first, an so on.  This isn't possible since arrays
75 are actually hash tables.  For example, you can't get a range of
76 values between two indices.  Nor can you sort an array.
77 .TP 2
78 \(bu
79 Arrays consume lots of memory when the number of elements becomes
80 large (tens of thousands).  This is because each element's index and
81 value are stored as strings in the hash table.
82 .TP 2
83 \(bu 
84 The C programming interface is unwieldy.  Normally with vectors, you
85 would like to view the Tcl array as you do a C array, as an array of
86 floats or doubles.  But with hash tables, you must convert both the
87 index and value to and from decimal strings, just to access
88 an element in the array.  This makes it cumbersome to perform operations on
89 the array as a whole.
90 .PP
91 The \fBvector\fR command tries to overcome these disadvantages while
92 still retaining the ease of use of Tcl arrays.  The \fBvector\fR
93 command creates both a new Tcl command and associate array which are
94 linked to the vector components.  You can randomly access vector
95 components though the elements of array.  Not have all indices are
96 generated for the array, so printing the array (using the \fBparray\fR
97 procedure) does not print out all the component values.  You can use
98 the Tcl command to access the array as a whole.  You can copy, append,
99 or sort vector using its command.  If you need greater performance, or
100 customized behavior, you can write your own C code to manage vectors.
101 .SH EXAMPLE
102 You create vectors using the \fBvector\fR command and its \fBcreate\fR
103 operation.
104 .DS
105 # Create a new vector. 
106 vector create y(50)
107 .DE
108 This creates a new vector named \f(CWy\fR.  It has fifty components, by
109 default, initialized to \f(CW0.0\fR.  In addition, both a Tcl command
110 and array variable, both named \f(CWy\fR, are created.  You can use
111 either the command or variable to query or modify components of the
112 vector.
113 .DS
114 # Set the first value. 
115 set y(0) 9.25
116 puts "y has [y length] components"
117 .DE
118 The array \f(CWy\fR can be used to read or set individual components of
119 the vector.  Vector components are indexed from zero.  The array index
120 must be a number less than the number of components.  For example,
121 it's an error if you try to set the 51st element of \f(CWy\fR.
122 .DS
123 # This is an error. The vector only has 50 components.
124 set y(50) 0.02
125 .DE
126 You can also specify a range of indices using a colon (:) to separate
127 the first and last indices of the range.
128 .DS
129 # Set the first six components of y 
130 set y(0:5) 25.2
131 .DE
132 If you don't include an index, then it will default to the first
133 and/or last component of the vector.
134 .DS
135 # Print out all the components of y 
136 puts "y = $y(:)"
137 .DE
138 There are special non-numeric indices.  The index \f(CWend\fR, specifies
139 the last component of the vector.  It's an error to use this index if
140 the vector is empty (length is zero).  The index \f(CW++end\fR can be
141 used to extend the vector by one component and initialize it to a specific 
142 value.  You can't read from the array using this index, though.
143 .DS
144 # Extend the vector by one component.
145 set y(++end) 0.02
146 .DE
147 The other special indices are \f(CWmin\fR and \f(CWmax\fR.  They return the
148 current smallest and largest components of the vector.  
149 .DS
150 # Print the bounds of the vector
151 puts "min=$y(min) max=$y(max)"
152 .DE
153 To delete components from a vector, simply unset the corresponding
154 array element. In the following example, the first component of
155 \f(CWy\fR is deleted.  All the remaining components of \f(CWy\fR will be
156 moved down by one index as the length of the vector is reduced by
157 one.
158 .DS
159 # Delete the first component
160 unset y(0)
161 puts "new first element is $y(0)"
162 .DE
163 The vector's Tcl command can also be used to query or set the vector.
164 .DS
165 # Create and set the components of a new vector
166 vector create x
167 x set { 0.02 0.04 0.06 0.08 0.10 0.12 0.14 0.16 0.18 0.20 }
168 .DE
169 Here we've created a vector \f(CWx\fR without a initial length specification.
170 In this case, the length is zero.  The \fBset\fR operation resets the vector,
171 extending it and setting values for each new component.  
172 .PP
173 There are several operations for vectors.  The \fBrange\fR operation
174 lists the components of a vector between two indices.
175 .DS
176 # List the components 
177 puts "x = [x range 0 end]"
178 .DE
179 You can search for a particular value using the \fBsearch\fR
180 operation.  It returns a list of indices of the components with the
181 same value.  If no component has the same value, it returns \f(CW""\fR.
182 .DS
183 # Find the index of the biggest component
184 set indices [x search $x(max)]
185 .DE
186 Other operations copy, append, or sort vectors.  You can append
187 vectors or new values onto an existing vector with the \fBappend\fR
188 operation.
189 .DS
190 # Append assorted vectors and values to x
191 x append x2 x3 { 2.3 4.5 } x4
192 .DE
193 The \fBsort\fR operation sorts the vector.  If any additional vectors
194 are specified, they are rearranged in the same order as the vector.
195 For example, you could use it to sort data points represented by x and
196 y vectors.
197 .DS
198 # Sort the data points
199 x sort y
200 .DE
201 The vector \f(CWx\fR is sorted while the components of \f(CWy\fR are 
202 rearranged so that the original x,y coordinate pairs are retained.
203 .PP
204 The \fBexpr\fR operation lets you perform arithmetic on vectors.  
205 The result is stored in the vector.
206 .DS
207 # Add the two vectors and a scalar
208 x expr { x + y }
209 x expr { x * 2 }
210 .DE
211 When a vector is modified, resized, or deleted, it may trigger
212 call-backs to notify the clients of the vector.  For example, when a
213 vector used in the \fBgraph\fR widget is updated, the vector
214 automatically notifies the widget that it has changed.  The graph can
215 then redrawn itself at the next idle point.  By default, the
216 notification occurs when Tk is next idle.  This way you can modify the
217 vector many times without incurring the penalty of the graph redrawing
218 itself for each change.  You can change this behavior using the
219 \fBnotify\fR operation.
220 .DS
221 # Make vector x notify after every change
222 x notify always
223         ...
224 # Never notify
225 x notify never
226         ...
227 # Force notification now
228 x notify now
229 .DE
230 To delete a vector, use the \fBvector delete\fR command.  
231 Both the vector and its corresponding Tcl command are destroyed.
232 .DS
233 # Remove vector x
234 vector destroy x
235 .DE
236 .SH SYNTAX
237 Vectors are created using the \fBvector create\fR operation.  
238 Th \fBcreate\fR operation can be invoked in one of three forms:
239 .TP
240 \fBvector create \fIvecName\fR
241 This creates a new vector \fIvecName\fR which initially has no components.
242 .TP
243 \fBvector create \fIvecName\fR(\fIsize\fR)
244 This second form creates a new vector which will contain \fIsize\fR
245 number of components.  The components will be indexed starting from
246 zero (0). The default value for the components is \f(CW0.0\fR.
247 .TP
248 \fBvector create \fIvecName\fR(\fIfirst\fR:\fIlast\fR)
249 The last form creates a new vector of indexed \fIfirst\fR through
250 \fIlast\fR.  \fIFirst\fR and \fIlast\fR can be any integer value
251 so long as \fIfirst\fR is less than \fIlast\fR.
252 .PP
253 Vector names must start with a letter and consist of letters, digits,
254 or underscores.  
255 .DS
256 # Error: must start with letter
257 vector create 1abc
258 .DE
259 You can automatically generate vector names using the
260 "\f(CW#auto\fR" vector name.  The \fBcreate\fR operation will generate a 
261 unique vector name.
262 .DS
263 set vec [vector create #auto]
264 puts "$vec has [$vec length] components"
265 .DE
266 .SS VECTOR INDICES
267 Vectors are indexed by integers.  You can access the individual vector
268 components via its array variable or Tcl command.  The string
269 representing the index can be an integer, a numeric expression, a
270 range, or a special keyword.
271 .PP
272 The index must lie within the current range of the vector, otherwise
273 an an error message is returned.  Normally the indices of a vector
274 are start from 0.  But you can use the \fBoffset\fR operation to
275 change a vector's indices on-the-fly.
276 .DS
277 puts $vecName(0)
278 vecName offset -5
279 puts $vecName(-5)
280 .DE
281 You can also use numeric expressions as indices.  The result
282 of the expression must be an integer value.  
283 .DS
284 set n 21
285 set vecName($n+3) 50.2
286 .DE
287 The following special non-numeric indices are available: \f(CWmin\fR, \f(CWmax\fR, \f(CWend\fR, and
288 \f(CW++end\fR.  
289 .DS
290 puts "min = $vecName($min)"
291 set vecName(end) -1.2
292 .DE
293 The indices \f(CWmin\fR and \f(CWmax\fR will return the minimum and maximum
294 values of the vector.  The index \f(CWend\fR returns the value of the 
295 last component in the vector.  The index \f(CW++end\fR is used to append
296 new value onto the vector.  It automatically extends the vector by
297 one component and sets its value.
298 .DS
299 # Append an new component to the end
300 set vecName(++end) 3.2
301 .DE
302 A range of indices can be indicated by a colon (:).  
303 .DS
304 # Set the first six components to 1.0
305 set vecName(0:5) 1.0
306 .DE
307 If no index is supplied the first or last component is assumed.
308 .DS
309 # Print the values of all the components
310 puts $vecName(:)
311 .DE
312 .SH VECTOR OPERATIONS
313 .TP
314 \fBvector create \fIvecName\fR?(\fIsize\fR)?... \fR?\fIswitches\fR? 
315 The \fBcreate\fR operation creates a new vector \fIvecName\fR.  Both a
316 Tcl command and array variable \fIvecName\fR are also created.  The
317 name \fIvecName\fR must be unique, so another Tcl command or array
318 variable can not already exist in that scope.  You can access the
319 components of the vector using its variable.  If you change a value in
320 the array, or unset an array element, the vector is updated to reflect
321 the changes.  When the variable \fIvecName\fR is unset, the vector and
322 its Tcl command are also destroyed.
323 .sp
324 The vector has optional switches that affect how the vector is created. They
325 are as follows:
326 .RS
327 .TP
328 \fB\-variable \fIvarName\fR
329 Specifies the name of a Tcl variable to be mapped to the vector. If
330 the variable already exists, it is first deleted, then recreated. 
331 If \fIvarName\fR is the empty string, then no variable will be mapped.
332 You can always map a variable back to the vector using the vector's 
333 \fBvariable\fR operation.
334 .TP
335 \fB\-command \fIcmdName\fR
336 Maps a Tcl command to the vector. The vector can be accessed using 
337 \fIcmdName\fR and one of the vector instance operations.  
338 A Tcl command by that name cannot already exist.
339 If \fIcmdName\fR is the empty string, no command mapping
340 will be made.
341 .TP
342 \fB\-watchunset \fIboolean\fR
343 Indicates that the vector should automatically delete itself if
344 the variable associated with the vector is unset.  By default,
345 the vector will not be deleted.  This is different from previous
346 releases.  Set \fIboolean\fR to "true" to get the old behavior.
347 .RE
348 .TP
349 \fBvector destroy \fIvecName\fR \fR?\fIvecName...\fR?
350 .TP
351 \fBvector expr \fIexpression\fR
352 .RS
353 All binary operators take vectors as operands (remember that numbers
354 are treated as one-component vectors).  The exact action of binary
355 operators depends upon the length of the second operand.  If the
356 second operand has only one component, then each element of the first
357 vector operand is computed by that value.  For example, the expression
358 "x * 2" multiples all elements of the vector x by 2.  If the second
359 operand has more than one component, both operands must be the same
360 length.  Each pair of corresponding elements are computed.  So "x + y"
361 adds the the first components of x and y together, the second, and so on.
362 .sp
363 The valid operators are listed below, grouped in decreasing order
364 of precedence:
365 .TP 20
366 \fB\-\0\0!\fR
367 Unary minus and logical NOT.  The unary minus flips the sign of each
368 component in the vector.  The logical not operator returns a vector of
369 whose values are 0.0 or 1.0.  For each non-zero component 1.0 is returned,
370 0.0 otherwise.
371 .TP 20
372 \fB^\fR
373 Exponentiation.  
374 .TP 20
375 \fB*\0\0/\0\0%\fR
376 Multiply, divide, remainder.  
377 .TP 20
378 \fB+\0\0\-\fR
379 Add and subtract.  
380 .TP 20
381 \fB<<\0\0>>\fR
382 Left and right shift.  Circularly shifts the values of the vector 
383 (not implemented yet).
384 .TP 20
385 \fB<\0\0>\0\0<=\0\0>=\fR
386 Boolean less, greater, less than or equal, and greater than or equal.
387 Each operator returns a vector of ones and zeros.  If the condition is true, 
388 1.0 is the component value, 0.0 otherwise.
389 .TP 20
390 \fB==\0\0!=\fR
391 Boolean equal and not equal.
392 Each operator returns a vector of ones and zeros.  If the condition is true, 
393 1.0 is the component value, 0.0 otherwise.
394 .TP 20
395 \fB|\fR
396 Bit-wise OR.  (Not implemented).
397 .TP 20
398 \fB&&\fR
399 Logical AND.  Produces a 1 result if both operands are non-zero, 0 otherwise.
400 .TP 20
401 \fB||\fR
402 Logical OR.  Produces a 0 result if both operands are zero, 1 otherwise.
403 .TP 20
404 \fIx\fB?\fIy\fB:\fIz\fR
405 If-then-else, as in C.  (Not implemented yet).
406 .LP
407 See the C manual for more details on the results produced by each
408 operator.  All of the binary operators group left-to-right within the
409 same precedence level.  
410 .sp
411 Several mathematical functions are supported for vectors.  Each of
412 the following functions invokes the math library function of the same name;
413 see the manual entries for the library functions for details on what
414 they do.  The operation is applied to all elements of the vector
415 returning the results. 
416 .DS
417 .ta 3c 6c 9c
418 \fBacos\fR      \fBcos\fR       \fBhypot\fR     \fBsinh\fR 
419 \fBasin\fR      \fBcosh\fR      \fBlog\fR       \fBsqrt\fR 
420 \fBatan\fR      \fBexp\fR       \fBlog10\fR     \fBtan\fR  
421 \fBceil\fR      \fBfloor\fR     \fBsin\fR       \fBtanh\fR 
422 .DE
423 Additional functions are:
424 .TP 1i
425 \fBabs\fR
426 Returns the absolute value of each component.
427 .TP 1i
428 \fBrandom\fR
429 Returns a vector of non-negative values uniformly distributed 
430 between [0.0, 1.0) using \fIdrand48\fR.
431 The seed comes from the internal clock of the machine or may be 
432 set manual with the srandom function.
433 .TP 1i
434 \fBround\fR
435 Rounds each component of the vector.
436 .TP 1i
437 \fBsrandom\fR
438 Initializes the random number generator using \fIsrand48\fR.
439 The high order 32-bits are set using the integral portion of the first 
440 vector component. All other components are ignored.  The low order 16-bits 
441 are set to an arbitrary value.
442 .PP
443 The following functions return a single value.
444 .TP 1i
445 \fBadev\fR 
446 Returns the average deviation (defined as the sum of the absolute values 
447 of the differences between component and the mean, divided by the length
448 of the vector).
449 .TP 1i
450 \fBkurtosis\fR
451 Returns the degree of peakedness (fourth moment) of the vector.
452 .TP 1i
453 \fBlength\fR
454 Returns the number of components in the vector.
455 .TP 1i
456 \fBmax\fR
457 Returns the vector's maximum value.
458 .TP 1i
459 \fBmean\fR
460 Returns the mean value of the vector.
461 .TP 1i
462 \fBmedian\fR
463 Returns the median of the vector.
464 .TP 1i
465 \fBmin\fR
466 Returns the vector's minimum value.
467 .TP 1i
468 \fBq1\fR
469 Returns the first quartile of the vector.
470 .TP 1i
471 \fBq3\fR
472 Returns the third quartile of the vector.
473 .TP 1i
474 \fBprod\fR 
475 Returns the product of the components.
476 .TP 1i
477 \fBsdev\fR 
478 Returns the standard deviation (defined as the square root of the variance)
479 of the vector.
480 .TP 1i
481 \fBskew\fR 
482 Returns the skewness (or third moment) of the vector.  This characterizes
483 the degree of asymmetry of the vector about the mean.
484 .TP 1i
485 \fBsum\fR 
486 Returns the sum of the components.
487 .TP 1i
488 \fBvar\fR
489 Returns the variance of the vector. The sum of the squared differences 
490 between each component and the mean is computed.  The variance is 
491 the sum divided by the length of the vector minus 1.
492 .PP
493 The last set returns a vector of the same length as the argument.
494 .TP 1i
495 \fBnorm\fR 
496 Scales the values of the vector to lie in the range [0.0..1.0].
497 .TP 1i
498 \fBsort\fR
499 Returns the vector components sorted in ascending order.
500 .RE
501 .TP
502 \fBvector names \fR?\fIpattern\fR?
503 .SH INSTANCE OPERATIONS
504 You can also use the vector's Tcl command to query or modify it.  The
505 general form is
506 .DS
507 \fIvecName \fIoperation\fR \fR?\fIarg\fR?...
508 .DE
509 Both \fIoperation\fR and its arguments determine the exact behavior of
510 the command.  The operations available for vectors are listed below.
511 .TP
512 \fIvecName \fBappend\fR \fIitem\fR ?\fIitem\fR?...
513 Appends the component values from \fIitem\fR to \fIvecName\fR.
514 \fIItem\fR can be either the name of a vector or a list of numeric
515 values.
516 .TP
517 \fIvecName \fBclear\fR 
518 Clears the element indices from the array variable associated with
519 \fIvecName\fR.  This doesn't affect the components of the vector.  By
520 default, the number of entries in the Tcl array doesn't match the
521 number of components in the vector.  This is because its too expensive
522 to maintain decimal strings for both the index and value for each
523 component.  Instead, the index and value are saved only when you read
524 or write an element with a new index.  This command removes the index
525 and value strings from the array.  This is useful when the vector is
526 large.
527 .TP
528 \fIvecName \fBdelete\fR \fIindex\fR ?\fIindex\fR?...
529 Deletes the \fIindex\fRth component from the vector \fIvecName\fR.
530 \fIIndex\fR is the index of the element to be deleted.  This is the
531 same as unsetting the array variable element \fIindex\fR.  The vector
532 is compacted after all the indices have been deleted.
533 .TP
534 \fIvecName \fBdup\fR \fIdestName\fR 
535 Copies \fIvecName\fR to \fIdestName\fR. \fIDestName\fR is the name of a
536 destination vector.  If a vector \fIdestName\fR already exists, it is
537 overwritten with the components of \fIvecName\fR.  Otherwise a 
538 new vector is created.
539 .TP
540 \fIvecName \fBexpr\fR \fIexpression\fR
541 Computes the expression and resets the values of the vector accordingly.
542 Both scalar and vector math operations are allowed.  All values in
543 expressions are either real numbers or names of vectors.  All numbers
544 are treated as one component vectors.
545 .TP
546 \fIvecName \fBlength\fR ?\fInewSize\fR?
547 Queries or resets the number of components in \fIvecName\fR.
548 \fINewSize\fR is a number specifying the new size of the vector.  If
549 \fInewSize\fR is smaller than the current size of \fIvecName\fR,
550 \fIvecName\fR is truncated.  If \fInewSize\fR is greater, the vector
551 is extended and the new components are initialized to \f(CW0.0\fR.  If
552 no \fInewSize\fR argument is present, the current length of the vector
553 is returned.
554 .TP
555 \fIvecName \fBmerge\fR \fIsrcName\fR ?\fIsrcName\fR?...
556 Returns a list of the merged vector components.  The list is formed by
557 merging the components of each vector at each index.
558 .TP
559 \fIvecName \fBnotify\fR \fIkeyword\fR
560 Controls how vector clients are notified of changes to the vector.  
561 The exact behavior is determined by \fIkeyword\fR.
562 .RS
563 .TP 0.75i
564 \f(CWalways\fR 
565 Indicates that clients are to be notified immediately whenever the
566 vector is updated.
567 .TP
568 \f(CWnever\fR
569 Indicates that no clients are to be notified.
570 .TP
571 \f(CWwhenidle\fR
572 Indicates that clients are to be notified at the next idle point
573 whenever the vector is updated.
574 .TP
575 \f(CWnow\fR
576 If any client notifications is currently pending, they are notified
577 immediately.
578 .TP
579 \f(CWcancel\fR
580 Cancels pending notifications of clients using the vector.
581 .TP
582 \f(CWpending\fR
583 Returns \f(CW1\fR if a client notification is pending, and \f(CW0\fR otherwise.
584 .RE
585 .TP
586 \fIvecName \fBoffset\fR ?\fIvalue\fR?
587 Shifts the indices of the vector by the amount specified by \fIvalue\fR.
588 \fIValue\fR is an integer number.  If no \fIvalue\fR argument is 
589 given, the current offset is returned.
590 .TP
591 \fIvecName \fBpopulate\fR \fIdestName\fR ?\fIdensity\fR?
592 Creates a vector \fIdestName\fR which is a superset of \fIvecName\fR.
593 \fIDestName\fR will include all the components of \fIvecName\fR, in
594 addition the interval between each of the original components will
595 contain a \fIdensity\fR number of new components, whose values are
596 evenly distributed between the original components values.  This is
597 useful for generating abscissas to be interpolated along a spline.
598 .TP
599 \fIvecName \fBrange\fR \fIfirstIndex\fR ?\fIlastIndex\fR?...
600 Returns a list of numeric values representing the vector components
601 between two indices. Both \fIfirstIndex\fR and \fIlastIndex\fR are 
602 indices representing the range of components to be returned. If 
603 \fIlastIndex\fR is less than \fIfirstIndex\fR, the components are
604 listed in reverse order.
605 .TP
606 \fIvecName \fBsearch\fR \fIvalue\fR ?\fIvalue\fR?  
607 Searches for a value or range of values among the components of
608 \fIvecName\fR.  If one \fIvalue\fR argument is given, a list of
609 indices of the components which equal \fIvalue\fR is returned.  If a
610 second \fIvalue\fR is also provided, then the indices of all
611 components which lie within the range of the two values are returned.
612 If no components are found, then \f(CW""\fR is returned.
613 .TP
614 \fIvecName \fBset\fR \fIitem\fR
615 Resets the components of the vector to \fIitem\fR. \fIItem\fR can
616 be either a list of numeric expressions or another vector.
617 .TP
618 \fIvecName \fBseq\fR \fIstart\fR ?\fIfinish\fR? ?\fIstep\fR?
619 Generates a sequence of values starting with the value \fIstart\fR.
620 \fIFinish\fR indicates the terminating value of the sequence.  
621 The vector is automatically resized to contain just the sequence.
622 If three arguments are present, \fIstep\fR designates the interval.  
623 .sp
624 With only two arguments (no \fIfinish\fR argument), the sequence will
625 continue until the vector is filled.  With one argument, the interval 
626 defaults to 1.0.
627 .TP
628 \fIvecName \fBsort\fR ?\fB-reverse\fR? ?\fIargName\fR?...  
629 Sorts the vector \fIvecName\fR in increasing order.  If the
630 \fB-reverse\fR flag is present, the vector is sorted in decreasing
631 order.  If other arguments \fIargName\fR are present, they are the
632 names of vectors which will be rearranged in the same manner as
633 \fIvecName\fR.  Each vector must be the same length as \fIvecName\fR.
634 You could use this to sort the x vector of a graph, while still
635 retaining the same x,y coordinate pairs in a y vector.
636 .TP
637 \fIvecName \fBvariable\fR \fIvarName\fR
638 Maps a Tcl variable to the vector, creating another means for 
639 accessing the vector.  The variable \fIvarName\fR can't already 
640 exist. This overrides any current variable mapping the vector
641 may have.
642 .RE
643 .SH C LANGUAGE API
644 You can create, modify, and destroy vectors from C code, using 
645 library routines.  
646 You need to include the header file \f(CWblt.h\fR. It contains the
647 definition of the structure \fBBlt_Vector\fR, which represents the
648 vector.  It appears below.
649 .DS
650 \fRtypedef struct {
651     double *\fIvalueArr\fR; 
652     int \fInumValues\fR;    
653     int \fIarraySize\fR;    
654     double \fImin\fR, \fImax\fR;  
655 } \fBBlt_Vector\fR;
656 .DE
657 The field \fIvalueArr\fR points to memory holding the vector
658 components.  The components are stored in a double precision array,
659 whose size size is represented by \fIarraySize\fR.  \fINumValues\fR is
660 the length of vector.  The size of the array is always equal to or
661 larger than the length of the vector.  \fIMin\fR and \fImax\fR are
662 minimum and maximum component values.
663 .SH LIBRARY ROUTINES
664 The following routines are available from C to manage vectors.
665 Vectors are identified by the vector name.
666 .PP
667 \fBBlt_CreateVector\fR 
668 .RS .25i
669 .TP 1i
670 Synopsis:
671 .CS 
672 int \fBBlt_CreateVector\fR (\fIinterp\fR, \fIvecName\fR, \fIlength\fR, \fIvecPtrPtr\fR)
673 .RS 1.25i
674 Tcl_Interp *\fIinterp\fR;
675 char *\fIvecName\fR;
676 int \fIlength\fR;
677 Blt_Vector **\fIvecPtrPtr\fR;
678 .RE
679 .CE
680 .TP
681 Description:
682 Creates a new vector \fIvecName\fR\fR with a length of \fIlength\fR.
683 \fBBlt_CreateVector\fR creates both a new Tcl command and array 
684 variable \fIvecName\fR.  Neither a command nor variable named 
685 \fIvecName\fR can already exist.  A pointer to the vector is 
686 placed into \fIvecPtrPtr\fR.
687 .TP
688 Results:
689 Returns \f(CWTCL_OK\fR if the vector is successfully created.  If
690 \fIlength\fR is negative, a Tcl variable or command \fIvecName\fR
691 already exists, or memory cannot be allocated for the vector, then
692 \f(CWTCL_ERROR\fR is returned and \fIinterp->result\fR will contain an
693 error message.
694 .RE
695 .sp
696 .PP
697 \fBBlt_DeleteVectorByName\fR 
698 .RS .25i
699 .TP 1i
700 Synopsis:
701 .CS
702 int \fBBlt_DeleteVectorByName\fR (\fIinterp\fR, \fIvecName\fR)
703 .RS 1.25i
704 Tcl_Interp *\fIinterp\fR;
705 char *\fIvecName\fR;
706 .RE
707 .CE
708 .TP 1i
709 Description:
710 Removes the vector \fIvecName\fR.  \fIVecName\fR is the name of a vector
711 which must already exist.  Both the Tcl command and array variable
712 \fIvecName\fR are destroyed.  All clients of the vector will be notified
713 immediately that the vector has been destroyed.
714 .TP
715 Results:
716 Returns \f(CWTCL_OK\fR if the vector is successfully deleted.  If
717 \fIvecName\fR is not the name a vector, then \f(CWTCL_ERROR\fR is returned
718 and \fIinterp->result\fR will contain an error message.
719 .RE
720 .sp
721 .PP
722 \fBBlt_DeleteVector\fR 
723 .RS .25i
724 .TP 1i
725 Synopsis:
726 .CS
727 int \fBBlt_DeleteVector\fR (\fIvecPtr\fR)
728 .RS 1.25i
729 Blt_Vector *\fIvecPtr\fR;
730 .RE
731 .CE
732 .TP 1i
733 Description:
734 Removes the vector pointed to by \fIvecPtr\fR.  \fIVecPtr\fR is a
735 pointer to a vector, typically set by \fBBlt_GetVector\fR or
736 \fBBlt_CreateVector\fR.  Both the Tcl command and array variable of
737 the vector are destroyed.  All clients of the vector will be notified
738 immediately that the vector has been destroyed.
739 .TP
740 Results:
741 Returns \f(CWTCL_OK\fR if the vector is successfully deleted.  If
742 \fIvecName\fR is not the name a vector, then \f(CWTCL_ERROR\fR is returned
743 and \fIinterp->result\fR will contain an error message.
744 .RE
745 .sp
746 .PP
747 \fBBlt_GetVector\fR 
748 .RS .25i
749 .TP 1i
750 Synopsis:
751 .CS
752 int \fBBlt_GetVector\fR (\fIinterp\fR, \fIvecName\fR, \fIvecPtrPtr\fR)
753 .RS 1.25i
754 Tcl_Interp *\fIinterp\fR;
755 char *\fIvecName\fR;
756 Blt_Vector **\fIvecPtrPtr\fR;
757 .RE
758 .CE
759 .TP 1i
760 Description:
761 Retrieves the vector \fIvecName\fR.  \fIVecName\fR is the name of a
762 vector which must already exist.  \fIVecPtrPtr\fR will point be set to
763 the address of the vector.
764 .TP
765 Results:
766 Returns \f(CWTCL_OK\fR if the vector is successfully retrieved.  If
767 \fIvecName\fR is not the name of a vector, then \f(CWTCL_ERROR\fR is
768 returned and \fIinterp->result\fR will contain an error message.
769 .RE
770 .sp
771 .PP
772 \fBBlt_ResetVector\fR 
773 .PP
774 .RS .25i
775 .TP 1i
776 Synopsis:
777 .CS
778 int \fBBlt_ResetVector\fR (\fIvecPtr\fR, \fIdataArr\fR, 
779         \fInumValues\fR, \fIarraySize\fR, \fIfreeProc\fR)
780 .RS 1.25i
781 Blt_Vector *\fIvecPtr\fR;
782 double *\fIdataArr\fR;
783 int *\fInumValues\fR;
784 int *\fIarraySize\fR;
785 Tcl_FreeProc *\fIfreeProc\fR;
786 .RE
787 .CE
788 .TP
789 Description: 
790 Resets the components of the vector pointed to by \fIvecPtr\fR.
791 Calling \fBBlt_ResetVector\fR will trigger the vector to dispatch
792 notifications to its clients. \fIDataArr\fR is the array of doubles
793 which represents the vector data. \fINumValues\fR is the number of
794 elements in the array. \fIArraySize\fR is the actual size of the array
795 (the array may be bigger than the number of values stored in
796 it). \fIFreeProc\fP indicates how the storage for the vector component
797 array (\fIdataArr\fR) was allocated.  It is used to determine how to
798 reallocate memory when the vector is resized or destroyed.  It must be
799 \f(CWTCL_DYNAMIC\fR, \f(CWTCL_STATIC\fR, \f(CWTCL_VOLATILE\fR, or a pointer
800 to a function to free the memory allocated for the vector array. If
801 \fIfreeProc\fR is \f(CWTCL_VOLATILE\fR, it indicates that \fIdataArr\fR
802 must be copied and saved.  If \fIfreeProc\fR is \f(CWTCL_DYNAMIC\fR, it
803 indicates that \fIdataArr\fR was dynamically allocated and that Tcl
804 should free \fIdataArr\fR if necessary.  \f(CWStatic\fR indicates that
805 nothing should be done to release storage for \fIdataArr\fR.
806 .TP
807 Results:
808 Returns \f(CWTCL_OK\fR if the vector is successfully resized.  If
809 \fInewSize\fR is negative, a vector \fIvecName\fR does not exist, or
810 memory cannot be allocated for the vector, then \f(CWTCL_ERROR\fR is
811 returned and \fIinterp->result\fR will contain an error message.
812 .RE
813 .sp
814 .PP
815 \fBBlt_ResizeVector\fR 
816 .RS .25i
817 .TP 1i
818 Synopsis:
819 .CS
820 int \fBBlt_ResizeVector\fR (\fIvecPtr\fR, \fInewSize\fR)
821 .RS 1.25i
822 Blt_Vector *\fIvecPtr\fR;
823 int \fInewSize\fR;
824 .RE
825 .CE
826 .TP
827 Description:
828 Resets the length of the vector pointed to by \fIvecPtr\fR to
829 \fInewSize\fR.  If \fInewSize\fR is smaller than the current size of
830 the vector, it is truncated.  If \fInewSize\fR is greater, the vector
831 is extended and the new components are initialized to \f(CW0.0\fR.
832 Calling \fBBlt_ResetVector\fR will trigger the vector to dispatch
833 notifications.
834 .TP
835 Results:
836 Returns \f(CWTCL_OK\fR if the vector is successfully resized.  If
837 \fInewSize\fR is negative or memory can not be allocated for the vector, 
838 then \f(CWTCL_ERROR\fR is returned and \fIinterp->result\fR will contain 
839 an error message.
840 .sp
841 .PP
842 \fBBlt_VectorExists\fR 
843 .RS .25i
844 .TP 1i
845 Synopsis:
846 .CS
847 int \fBBlt_VectorExists\fR (\fIinterp\fR, \fIvecName\fR)
848 .RS 1.25i
849 Tcl_Interp *\fIinterp\fR;
850 char *\fIvecName\fR;
851 .RE
852 .CE
853 .TP
854 Description:
855 Indicates if a vector named \fIvecName\fR exists in \fIinterp\fR.
856 .TP
857 Results:
858 Returns \f(CW1\fR if a vector \fIvecName\fR exists and \f(CW0\fR otherwise.
859 .RE
860 .sp
861 .PP
862 If your application needs to be notified when a vector changes, it can
863 allocate a unique \fIclient identifier\fR for itself.  Using this
864 identifier, you can then register a call-back to be made whenever the
865 vector is updated or destroyed.  By default, the call-backs are made at
866 the next idle point.  This can be changed to occur at the time the
867 vector is modified.  An application can allocate more than one
868 identifier for any vector.  When the client application is done with
869 the vector, it should free the identifier.
870 .PP
871 The call-back routine must of the following type.
872 .CS
873 .RS
874 .sp
875 typedef void (\fBBlt_VectorChangedProc\fR) (Tcl_Interp *\fIinterp\fR, 
876 .RS .25i
877 ClientData \fIclientData\fR, Blt_VectorNotify \fInotify\fR);
878 .RE
879 .sp
880 .RE
881 .CE
882 .fi
883 \fIClientData\fR is passed to this routine whenever it is called.  You
884 can use this to pass information to the call-back.  The \fInotify\fR 
885 argument indicates whether the vector has been updated of destroyed. It
886 is an enumerated type.
887 .CS
888 .RS
889 .sp
890 typedef enum {
891     \f(CWBLT_VECTOR_NOTIFY_UPDATE\fR=1,
892     \f(CWBLT_VECTOR_NOTIFY_DESTROY\fR=2
893 } \fBBlt_VectorNotify\fR;
894 .sp
895 .RE
896 .CE
897 .PP
898 \fBBlt_AllocVectorId\fR
899 .RS .25i
900 .TP 1i
901 Synopsis:
902 .CS
903 Blt_VectorId \fBBlt_AllocVectorId\fR (\fIinterp\fR, \fIvecName\fR)
904 .RS 1.25i
905 Tcl_Interp *\fIinterp\fR;
906 char *\fIvecName\fR;
907 .RE
908 .CE
909 .TP
910 Description:
911 Allocates an client identifier for with the vector \fIvecName\fR.
912 This identifier can be used to specify a call-back which is triggered
913 when the vector is updated or destroyed.
914 .TP
915 Results:
916 Returns a client identifier if successful.  If \fIvecName\fR is not
917 the name of a vector, then \f(CWNULL\fR is returned and
918 \fIinterp->result\fR will contain an error message.
919 .RE
920 .sp
921 .PP
922 \fBBlt_GetVectorById\fR 
923 .RS .25i
924 .TP 1i
925 Synopsis:
926 .CS
927 int \fBBlt_GetVector\fR (\fIinterp\fR, \fIclientId\fR, \fIvecPtrPtr\fR)
928 .RS 1.25i
929 Tcl_Interp *\fIinterp\fR;
930 Blt_VectorId \fIclientId\fR;
931 Blt_Vector **\fIvecPtrPtr\fR;
932 .RE
933 .CE
934 .TP 1i
935 Description:
936 Retrieves the vector used by \fIclientId\fR.  \fIClientId\fR is a valid
937 vector client identifier allocated by \fBBlt_AllocVectorId\fR.
938 \fIVecPtrPtr\fR will point be set to the address of the vector.
939 .TP
940 Results:
941 Returns \f(CWTCL_OK\fR if the vector is successfully retrieved.  
942 .RE
943 .sp
944 .PP
945 \fBBlt_SetVectorChangedProc\fR
946 .RS .25i
947 .TP 1i
948 Synopsis:
949 .CS
950 void \fBBlt_SetVectorChangedProc\fR (\fIclientId\fR, \fIproc\fR, \fIclientData\fR);
951 .RS 1.25i
952 Blt_VectorId \fIclientId\fR;
953 Blt_VectorChangedProc *\fIproc\fR;
954 ClientData *\fIclientData\fR;
955 .RE
956 .CE
957 .TP
958 Description: 
959 Specifies a call-back routine to be called whenever the vector
960 associated with \fIclientId\fR is updated or deleted.  \fIProc\fR is a
961 pointer to call-back routine and must be of the type
962 \fBBlt_VectorChangedProc\fR.  \fIClientData\fR is a one-word value to
963 be passed to the routine when it is invoked. If \fIproc\fR is
964 \f(CWNULL\fR, then the client is not notified.
965 .TP
966 Results:
967 The designated call-back procedure will be invoked when the vector is 
968 updated or destroyed.
969 .RE
970 .sp
971 .PP
972 \fBBlt_FreeVectorId\fR
973 .RS .25i
974 .TP 1i
975 Synopsis:
976 .CS
977 void \fBBlt_FreeVectorId\fR (\fIclientId\fR);
978 .RS 1.25i
979 Blt_VectorId \fIclientId\fR;
980 .RE
981 .CE
982 .TP
983 Description: 
984 Frees the client identifier.  Memory allocated for the identifier 
985 is released.  The client will no longer be notified when the
986 vector is modified.
987 .TP
988 Results:
989 The designated call-back procedure will be no longer be invoked when
990 the vector is updated or destroyed.
991 .RE
992 .sp
993 .PP
994 \fBBlt_NameOfVectorId\fR
995 .RS .25i
996 .TP 1i
997 Synopsis:
998 .CS
999 char *\fBBlt_NameOfVectorId\fR (\fIclientId\fR);
1000 .RS 1.25i
1001 Blt_VectorId \fIclientId\fR;
1002 .RE
1003 .CE
1004 .TP
1005 Description: 
1006 Retrieves the name of the vector associated with the client identifier
1007 \fIclientId\fR.  
1008 .TP
1009 Results:
1010 Returns the name of the vector associated with \fIclientId\fR.  If
1011 \fIclientId\fR is not an identifier or the vector has been destroyed, 
1012 \f(CWNULL\fR is returned.
1013 .RE
1014 .sp
1015 .PP
1016 \fBBlt_InstallIndexProc\fR
1017 .RS .25i
1018 .TP 1i
1019 Synopsis:
1020 .CS
1021 void \fBBlt_InstallIndexProc\fR (\fIindexName\fR, \fIprocPtr\fR)
1022 .RS 1.25i
1023 char *\fIindexName\fR;
1024 Blt_VectorIndexProc *\fIprocPtr\fR;
1025 .RE
1026 .CE
1027 .TP
1028 Description: 
1029 Registers a function to be called to retrieved the index \fIindexName\fR
1030 from the vector's array variable.  
1031 .sp
1032 typedef double Blt_VectorIndexProc(Vector *vecPtr);
1033 .sp
1034 The function will be passed a pointer to the vector.  The function must
1035 return a double representing the value at the index.
1036 .TP
1037 Results:
1038 The new index is installed into the vector.
1039 .RE
1040 .RE
1041 .SH C API EXAMPLE
1042 The following example opens a file of binary data and stores it in an
1043 array of doubles. The array size is computed from the size of the
1044 file. If the vector "data" exists, calling \fBBlt_VectorExists\fR,
1045 \fBBlt_GetVector\fR is called to get the pointer to the vector.
1046 Otherwise the routine \fBBlt_CreateVector\fR is called to create a new
1047 vector and returns a pointer to it. Just like the Tcl interface, both
1048 a new Tcl command and array variable are created when a new vector is
1049 created. It doesn't make any difference what the initial size of the
1050 vector is since it will be reset shortly. The vector is updated when
1051 \fBlt_ResetVector\fR is called.  Blt_ResetVector makes the changes
1052 visible to the Tcl interface and other vector clients (such as a graph
1053 widget).
1054 .sp
1055 .DS
1056 #include <tcl.h>
1057 #include <blt.h>                                
1058 ...
1059 Blt_Vector *vecPtr;
1060 double *newArr;
1061 FILE *f;
1062 struct stat statBuf;
1063 int numBytes, numValues;
1064
1065 f = fopen("binary.dat", "r");
1066 fstat(fileno(f), &statBuf);
1067 numBytes = (int)statBuf.st_size;
1068
1069 /* Allocate an array big enough to hold all the data */
1070 newArr = (double *)malloc(numBytes);
1071 numValues = numBytes / sizeof(double);
1072 fread((void *)newArr, numValues, sizeof(double), f);
1073 fclose(f);
1074
1075 if (Blt_VectorExists(interp, "data"))  {
1076     if (Blt_GetVector(interp, "data", &vecPtr) != TCL_OK) {
1077         return TCL_ERROR;
1078     }
1079 } else {
1080    if (Blt_CreateVector(interp, "data", 0, &vecPtr) != TCL_OK) {
1081         return TCL_ERROR;
1082    }
1083 }
1084 /* 
1085  * Reset the vector. Clients will be notified when Tk is idle. 
1086  * TCL_DYNAMIC tells the vector to free the memory allocated 
1087  * if it needs to reallocate or destroy the vector.
1088  */
1089 if (Blt_ResetVector(vecPtr, newArr, numValues, numValues, 
1090         TCL_DYNAMIC) != TCL_OK) {
1091     return TCL_ERROR;
1092 }
1093 .DE
1094 .SH "INCOMPATIBILITIES"
1095 In previous versions, if the array variable isn't global 
1096 (i.e. local to a Tcl procedure), the vector is automatically 
1097 destroyed when the procedure returns.
1098 .DS
1099 proc doit {} {
1100     # Temporary vector x
1101     vector x(10)
1102     set x(9) 2.0
1103       ...
1104 }
1105 .DE
1106 .PP
1107 This has changed.  Variables are not automatically destroyed when
1108 their variable is unset.  You can restore the old behavior by
1109 setting the "-watchunset" switch.
1110 .DE
1111 .SH KEYWORDS
1112 vector, graph, widget