| 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 |
.so man.macros |
| 23 |
.TH vector n BLT_VERSION BLT "BLT Built-In Commands" |
| 24 |
.BS |
| 25 |
'\" Note: do not modify the .SH NAME line immediately below! |
| 26 |
.SH NAME |
| 27 |
vector \- Vector data type for Tcl |
| 28 |
.SH SYNOPSIS |
| 29 |
\fBvector configure \fIoption value ...\fR |
| 30 |
.sp |
| 31 |
\fBvector create \fIvecName \fR?\fIvecName\fR...? ?\fIswitches\fR? |
| 32 |
.sp |
| 33 |
\fBvector destroy \fIvecName \fR?\fIvecName\fR...? |
| 34 |
.sp |
| 35 |
\fBvector expr \fIexpression\fR |
| 36 |
.sp |
| 37 |
\fBvector names \fR?\fIpattern\fR...? |
| 38 |
.sp |
| 39 |
\fBvector op\fR \fIoperation vecName\fR ?\fIarg\fR?... |
| 40 |
.BE |
| 41 |
.SH DESCRIPTION |
| 42 |
The \fBvector\fR command creates a vector of floating point |
| 43 |
values. The vector's components can be manipulated in three ways: |
| 44 |
through a Tcl array variable, a Tcl command, or the C API. |
| 45 |
.SH INTRODUCTION |
| 46 |
A vector is simply an ordered set of numbers. The components of a |
| 47 |
vector are real numbers, indexed by counting numbers. |
| 48 |
.PP |
| 49 |
Vectors are common data structures for many applications. For |
| 50 |
example, a graph may use two vectors to represent the X-Y |
| 51 |
coordinates of the data plotted. The graph will automatically |
| 52 |
be redrawn when the vectors are updated or changed. By using vectors, |
| 53 |
you can separate |
| 54 |
data analysis from the graph widget. This makes it easier, for |
| 55 |
example, to add data transformations, such as splines. It's possible |
| 56 |
to plot the same data to in multiple graphs, where each graph presents |
| 57 |
a different view or scale of the data. |
| 58 |
.PP |
| 59 |
You could try to use Tcl's associative arrays as vectors. Tcl arrays |
| 60 |
are easy to use. You can access individual elements randomly by |
| 61 |
specifying the index, or the set the entire array by providing a list |
| 62 |
of index and value pairs for each element. The disadvantages of |
| 63 |
associative arrays as vectors lie in the fact they are implemented as |
| 64 |
hash tables. |
| 65 |
.TP 2 |
| 66 |
\(bu |
| 67 |
There's no implied ordering to the associative arrays. If you used |
| 68 |
vectors for plotting, you would want to insure the second component |
| 69 |
comes after the first, an so on. This isn't possible since arrays |
| 70 |
are actually hash tables. For example, you can't get a range of |
| 71 |
values between two indices. Nor can you sort an array. |
| 72 |
.TP 2 |
| 73 |
\(bu |
| 74 |
Arrays consume lots of memory when the number of elements becomes |
| 75 |
large (tens of thousands). This is because each element's index and |
| 76 |
value are stored as strings in the hash table. |
| 77 |
.TP 2 |
| 78 |
\(bu |
| 79 |
The C programming interface is unwieldy. Normally with vectors, you |
| 80 |
would like to view the Tcl array as you do a C array, as an array of |
| 81 |
floats or doubles. But with hash tables, you must convert both the |
| 82 |
index and value to and from decimal strings, just to access |
| 83 |
an element in the array. This makes it cumbersome to perform operations on |
| 84 |
the array as a whole. |
| 85 |
.PP |
| 86 |
The \fBvector\fR command tries to overcome these disadvantages while |
| 87 |
still retaining the ease of use of Tcl arrays. The \fBvector\fR |
| 88 |
command creates both a new Tcl command and associate array which are |
| 89 |
linked to the vector components. You can randomly access vector |
| 90 |
components though the elements of array. Not all indices are |
| 91 |
generated for the array, so printing the array (using the \fBparray\fR |
| 92 |
procedure) does not print out all the component values. You can use |
| 93 |
the Tcl command to access the array as a whole. You can copy, append, |
| 94 |
or sort vector using its command. If you need greater performance, or |
| 95 |
customized behavior, you can write your own C code to manage vectors. |
| 96 |
.SH EXAMPLE |
| 97 |
You create vectors using the \fBvector\fR command and its \fBcreate\fR |
| 98 |
operation. |
| 99 |
.CS |
| 100 |
# Create a new vector. |
| 101 |
vector create y(50) |
| 102 |
.CE |
| 103 |
This creates a new vector named \fBy\fR. It has fifty components, by |
| 104 |
default, initialized to \fB0.0\fR. In addition, both a Tcl command |
| 105 |
and array variable, both named \fBy\fR, are created. You can use |
| 106 |
either the command or variable to query or modify components of the |
| 107 |
vector. |
| 108 |
.CS |
| 109 |
# Set the first value. |
| 110 |
set y(0) 9.25 |
| 111 |
puts "y has [y length] components" |
| 112 |
.CE |
| 113 |
The array \fBy\fR can be used to read or set individual components of |
| 114 |
the vector. Vector components are indexed from zero. The array index |
| 115 |
must be a number less than the number of components. For example, |
| 116 |
it's an error if you try to set the 51st element of \fBy\fR. |
| 117 |
.CS |
| 118 |
# This is an error. The vector only has 50 components. |
| 119 |
set y(50) 0.02 |
| 120 |
.CE |
| 121 |
You can also specify a range of indices using a colon (:) to separate |
| 122 |
the first and last indices of the range. |
| 123 |
.CS |
| 124 |
# Set the first six components of y |
| 125 |
set y(0:5) 25.2 |
| 126 |
.CE |
| 127 |
If you don't include an index, then it will default to the first |
| 128 |
and/or last component of the vector. |
| 129 |
.CS |
| 130 |
# Print out all the components of y |
| 131 |
puts "y = $y(:)" |
| 132 |
.CE |
| 133 |
There are special non-numeric indices. The index \fBend\fR, specifies |
| 134 |
the last component of the vector. It's an error to use this index if |
| 135 |
the vector is empty (length is zero). The index \fB++end\fR can be |
| 136 |
used to extend the vector by one component and initialize it to a specific |
| 137 |
value. You can't read from the array using this index, though. |
| 138 |
.CS |
| 139 |
# Extend the vector by one component. |
| 140 |
set y(++end) 0.02 |
| 141 |
.CE |
| 142 |
The other special indices are \fBmin\fR and \fBmax\fR. They return the |
| 143 |
current smallest and largest components of the vector. |
| 144 |
.CS |
| 145 |
# Print the bounds of the vector |
| 146 |
puts "min=$y(min) max=$y(max)" |
| 147 |
.CE |
| 148 |
To delete components from a vector, simply unset the corresponding |
| 149 |
array element. In the following example, the first component of |
| 150 |
\fBy\fR is deleted. All the remaining components of \fBy\fR will be |
| 151 |
moved down by one index as the length of the vector is reduced by |
| 152 |
one. |
| 153 |
.CS |
| 154 |
# Delete the first component |
| 155 |
unset y(0) |
| 156 |
puts "new first element is $y(0)" |
| 157 |
.CE |
| 158 |
The vector's Tcl command can also be used to query or set the vector. |
| 159 |
.CS |
| 160 |
# Create and set the components of a new vector |
| 161 |
vector create x |
| 162 |
x set { 0.02 0.04 0.06 0.08 0.10 0.12 0.14 0.16 0.18 0.20 } |
| 163 |
.CE |
| 164 |
Here we've created a vector \fBx\fR without a initial length specification. |
| 165 |
In this case, the length is zero. The \fBset\fR operation resets the vector, |
| 166 |
extending it and setting values for each new component. |
| 167 |
.PP |
| 168 |
There are several operations for vectors. The \fBrange\fR operation |
| 169 |
lists the components of a vector between two indices. |
| 170 |
.CS |
| 171 |
# List the components |
| 172 |
puts "x = [x range 0 end]" |
| 173 |
.CE |
| 174 |
You can search for a particular value using the \fBsearch\fR |
| 175 |
operation. It returns a list of indices of the components with the |
| 176 |
same value. If no component has the same value, it returns \fB""\fR. |
| 177 |
.CS |
| 178 |
# Find the index of the biggest component |
| 179 |
set indices [x search $x(max)] |
| 180 |
.CE |
| 181 |
Other operations copy, append, or sort vectors. You can append |
| 182 |
vectors or new values onto an existing vector with the \fBappend\fR |
| 183 |
operation. |
| 184 |
.CS |
| 185 |
# Append assorted vectors and values to x |
| 186 |
x append x2 x3 { 2.3 4.5 } x4 |
| 187 |
.CE |
| 188 |
The \fBsort\fR operation sorts the vector. If any additional vectors |
| 189 |
are specified, they are rearranged in the same order as the vector. |
| 190 |
For example, you could use it to sort data points represented by x and |
| 191 |
y vectors. |
| 192 |
.CS |
| 193 |
# Sort the data points |
| 194 |
x sort y |
| 195 |
.CE |
| 196 |
The vector \fBx\fR is sorted while the components of \fBy\fR are |
| 197 |
rearranged so that the original x,y coordinate pairs are retained. |
| 198 |
.PP |
| 199 |
The \fBexpr\fR operation lets you perform arithmetic on vectors. |
| 200 |
The result is stored in the vector. |
| 201 |
.CS |
| 202 |
# Add the two vectors and a scalar |
| 203 |
x expr { x + y } |
| 204 |
x expr { x * 2 } |
| 205 |
.CE |
| 206 |
When a vector is modified, resized, or deleted, it may trigger |
| 207 |
call-backs to notify the clients of the vector. For example, when a |
| 208 |
vector used in the \fBgraph\fR widget is updated, the vector |
| 209 |
automatically notifies the widget that it has changed. The graph can |
| 210 |
then redrawn itself at the next idle point. By default, the |
| 211 |
notification occurs when Tk is next idle. This way you can modify the |
| 212 |
vector many times without incurring the penalty of the graph redrawing |
| 213 |
itself for each change. You can change this behavior using the |
| 214 |
\fBnotify\fR operation. |
| 215 |
.CS |
| 216 |
# Make vector x notify after every change |
| 217 |
x notify always |
| 218 |
... |
| 219 |
# Never notify |
| 220 |
x notify never |
| 221 |
... |
| 222 |
# Force notification now |
| 223 |
x notify now |
| 224 |
|
| 225 |
# Set Tcl callback for update of Tktable widget .t. |
| 226 |
x notify callback {.t conf -padx [.t cget -padx]; .t reread} |
| 227 |
|
| 228 |
.CE |
| 229 |
To delete a vector, use the \fBvector delete\fR command. |
| 230 |
Both the vector and its corresponding Tcl command are destroyed. |
| 231 |
.CS |
| 232 |
# Remove vector x |
| 233 |
vector destroy x |
| 234 |
.CE |
| 235 |
The psuedo vector \fBlast\fR can be used at the end of an |
| 236 |
expression to implement running totals. |
| 237 |
During execution it resolves to the result from the previous |
| 238 |
vector element evaluation. |
| 239 |
.CS |
| 240 |
vector create A(10) |
| 241 |
vector create B(10) |
| 242 |
vector create S(10) |
| 243 |
vector create T(10) |
| 244 |
S expr A+B |
| 245 |
T expr S+last; # Running total |
| 246 |
.CE |
| 247 |
.SH SYNTAX |
| 248 |
Vectors are created using the \fBvector create\fR operation. |
| 249 |
Th \fBcreate\fR operation can be invoked in one of three forms: |
| 250 |
.TP |
| 251 |
\fBvector create \fIvecName\fR |
| 252 |
This creates a new vector \fIvecName\fR which initially has no components. |
| 253 |
.TP |
| 254 |
\fBvector create \fIvecName\fR(\fIsize\fR) |
| 255 |
This second form creates a new vector which will contain \fIsize\fR |
| 256 |
number of components. The components will be indexed starting from |
| 257 |
zero (0). The default value for the components is \fB0.0\fR. |
| 258 |
.TP |
| 259 |
\fBvector create \fIvecName\fR(\fIrows,columns\fR) |
| 260 |
This form allows creation of a matrix with the specified columns and |
| 261 |
\fIrows*columns\fR elements. |
| 262 |
See the \fImatrix\fR section for more details. |
| 263 |
.TP |
| 264 |
\fBvector create \fIvecName\fR(\fIfirst\fR:\fIlast\fR) |
| 265 |
The last form creates a new vector of indexed \fIfirst\fR through |
| 266 |
\fIlast\fR. \fIFirst\fR and \fIlast\fR can be any integer value |
| 267 |
so long as \fIfirst\fR is less than \fIlast\fR. |
| 268 |
.PP |
| 269 |
Vector names must start with a letter and consist of letters, digits, |
| 270 |
or underscores. |
| 271 |
.CS |
| 272 |
# Error: must start with letter |
| 273 |
vector create 1abc |
| 274 |
.CE |
| 275 |
You can automatically generate vector names using the |
| 276 |
"\fB#auto\fR" vector name. The \fBcreate\fR operation will generate a |
| 277 |
unique vector name. |
| 278 |
.CS |
| 279 |
set vec [vector create #auto] |
| 280 |
puts "$vec has [$vec length] components" |
| 281 |
.CE |
| 282 |
.SS VECTOR INDICES |
| 283 |
Vectors are indexed by integers. You can access the individual vector |
| 284 |
components via its array variable or Tcl command. The string |
| 285 |
representing the index can be an integer, a numeric expression, a |
| 286 |
range, or a special keyword. |
| 287 |
.PP |
| 288 |
The index must lie within the current range of the vector, otherwise |
| 289 |
an an error message is returned. Normally the indices of a vector |
| 290 |
are start from 0. But you can use the \fBoffset\fR operation to |
| 291 |
change a vector's indices on-the-fly. |
| 292 |
.CS |
| 293 |
puts $vecName(0) |
| 294 |
vecName offset -5 |
| 295 |
puts $vecName(-5) |
| 296 |
.CE |
| 297 |
When \fImatrix numcols\fR is > 1, 2D indexes are supported using |
| 298 |
ROW,COL form. |
| 299 |
.CS |
| 300 |
vecName matrix numcols 3 |
| 301 |
puts vecName(0,2) |
| 302 |
.CE |
| 303 |
You can also use numeric expressions as indices. The result |
| 304 |
of the expression must be an integer value. |
| 305 |
.CS |
| 306 |
set n 21 |
| 307 |
set vecName($n+3) 50.2 |
| 308 |
.CE |
| 309 |
The following special non-numeric indices are available: \fBmin\fR, \fBmax\fR, \fBend\fR, and |
| 310 |
\fB++end\fR. |
| 311 |
.CS |
| 312 |
puts "min = $vecName($min)" |
| 313 |
set vecName(end) -1.2 |
| 314 |
.CE |
| 315 |
The indices \fBmin\fR and \fBmax\fR will return the minimum and maximum |
| 316 |
values of the vector. Also available are: \fBprod\fR, \fBsum\fR, |
| 317 |
and \fBmean\fR. |
| 318 |
The index \fBend\fR returns the value of the |
| 319 |
last component in the vector. |
| 320 |
he index \fBend,0\fR returns the value of the |
| 321 |
last row in column 0 of the vector. |
| 322 |
The index \fB++end\fR is used to append |
| 323 |
new value onto the vector. It automatically extends the vector by |
| 324 |
numcols and sets its value. |
| 325 |
.CS |
| 326 |
# Append an new component to the end |
| 327 |
set vecName(++end) 3.2 |
| 328 |
.CE |
| 329 |
A range of indices can be indicated by a colon (:). |
| 330 |
.CS |
| 331 |
# Set the first six components to 1.0 |
| 332 |
set vecName(0:5) 1.0 |
| 333 |
.CE |
| 334 |
If no index is supplied the first or last component is assumed. |
| 335 |
.CS |
| 336 |
# Print the values of all the components |
| 337 |
puts $vecName(:) |
| 338 |
.CE |
| 339 |
.SH VECTOR OPERATIONS |
| 340 |
.TP |
| 341 |
\fBvector configure \fI? -flush bool -watchunset bool -oldcreate bool -maxsize int -novariable bool -nocommand bool?\fR |
| 342 |
The \fBconfigure\fR operation sets the default options used |
| 343 |
in creating vectors: these options are global to the interpreter. |
| 344 |
The \fI\-maxsize\fR option, when non-zero, limits creation size. |
| 345 |
The \fI\-oldcreate\fR enable the creation shortcut: \fBvector vec1 vec2 ...\fR. |
| 346 |
See the create command for details on the others. |
| 347 |
By default, these are all disabled or zero. |
| 348 |
.RE |
| 349 |
.TP |
| 350 |
\fBvector create \fIvecName\fR?(\fIsize\fR)?... \fR?\fIswitches\fR? |
| 351 |
The \fBcreate\fR operation creates a new vector \fIvecName\fR. The |
| 352 |
\fIsize\fR may be an integer, a START:END range or ROW,COL (see matrix). |
| 353 |
This creates both a |
| 354 |
Tcl command and array variable called \fIvecName\fR. The |
| 355 |
name \fIvecName\fR must be unique, so another Tcl command or array |
| 356 |
variable can not already exist in the current scope. You may access the |
| 357 |
components of the vector using the variable. If you change a value in |
| 358 |
the array, or unset an array element, the vector is updated to reflect |
| 359 |
the changes. When the variable \fIvecName\fR is unset, the vector and |
| 360 |
its Tcl command are also destroyed. |
| 361 |
.sp |
| 362 |
The vector has optional switches that affect how the vector is created. They |
| 363 |
are as follows: |
| 364 |
.RS |
| 365 |
.TP |
| 366 |
\fB\-variable \fIvarName\fR |
| 367 |
Specifies the name of a Tcl variable to be mapped to the vector. If |
| 368 |
the variable already exists, it is first deleted, then recreated. |
| 369 |
If \fIvarName\fR is the empty string, then no variable will be mapped. |
| 370 |
You can always map a variable back to the vector using the vector's |
| 371 |
\fBvariable\fR operation. |
| 372 |
.TP |
| 373 |
\fB\-command \fIcmdName\fR |
| 374 |
Maps a Tcl command to the vector. The vector can be accessed using |
| 375 |
\fIcmdName\fR and one of the vector instance operations. |
| 376 |
A Tcl command by that name cannot already exist. |
| 377 |
If \fIcmdName\fR is the empty string, no command mapping |
| 378 |
will be made. |
| 379 |
.TP |
| 380 |
\fB\-watchunset \fIboolean\fR |
| 381 |
Indicates that the vector should automatically delete itself if |
| 382 |
the variable associated with the vector is unset. By default, |
| 383 |
the vector will not be deleted. This is different from previous |
| 384 |
releases. Set \fIboolean\fR to "true" to get the old behavior. |
| 385 |
.TP |
| 386 |
\fB\-flush \fIboolean\fR |
| 387 |
Indicates that the vector should automatically flush the cached |
| 388 |
variable elements which unsets all the elements of the Tcl array |
| 389 |
variable associated with the vector, freeing memory associated |
| 390 |
with the variable. This includes both the hash table and the hash keys. |
| 391 |
The down side is that this effectively flushes the caching of vector |
| 392 |
elements in the array. This means that the subsequent reads |
| 393 |
of the array will require a decimal to string conversion. |
| 394 |
By default, flushing is disabled. |
| 395 |
.RE |
| 396 |
.TP |
| 397 |
\fBvector destroy \fIvecName\fR \fR?\fIvecName...\fR? |
| 398 |
Destroy vectors. |
| 399 |
.TP |
| 400 |
\fBvector expr \fIexpression\fR |
| 401 |
.RS |
| 402 |
All binary operators take vectors as operands (remember that numbers |
| 403 |
are treated as one-component vectors).The exact action of binary |
| 404 |
operators depends upon the length of the second operand. If the |
| 405 |
second operand has only one component, then each element of the first |
| 406 |
vector operand is computed by that value. For example, the expression |
| 407 |
"x * 2" multiples all elements of the vector x by 2. If the second |
| 408 |
operand has more than one component, both operands must be the same |
| 409 |
length. Each pair of corresponding elements are computed. So "x + y" |
| 410 |
adds the the first components of x and y together, the second, and so on. |
| 411 |
.sp |
| 412 |
The valid operators are listed below, grouped in decreasing order |
| 413 |
of precedence: |
| 414 |
.TP 20 |
| 415 |
\fB\-\0\0!\fR |
| 416 |
Unary minus and logical NOT. The unary minus flips the sign of each |
| 417 |
component in the vector. The logical not operator returns a vector of |
| 418 |
whose values are 0.0 or 1.0. For each non-zero component 1.0 is returned, |
| 419 |
0.0 otherwise. |
| 420 |
.TP 20 |
| 421 |
\fB^\fR |
| 422 |
Exponentiation. |
| 423 |
.TP 20 |
| 424 |
\fB*\0\0/\0\0%\fR |
| 425 |
Multiply, divide, remainder. |
| 426 |
.TP 20 |
| 427 |
\fB+\0\0\-\fR |
| 428 |
Add and subtract. |
| 429 |
.TP 20 |
| 430 |
\fB<<\0\0>>\fR |
| 431 |
Left and right shift. Circularly shifts the values of the vector |
| 432 |
.TP 20 |
| 433 |
\fB<\0\0>\0\0<=\0\0>=\fR |
| 434 |
Boolean less, greater, less than or equal, and greater than or equal. |
| 435 |
Each operator returns a vector of ones and zeros. If the condition is true, |
| 436 |
1.0 is the component value, 0.0 otherwise. |
| 437 |
.TP 20 |
| 438 |
\fB==\0\0!=\fR |
| 439 |
Boolean equal and not equal. |
| 440 |
Each operator returns a vector of ones and zeros. If the condition is true, |
| 441 |
1.0 is the component value, 0.0 otherwise. |
| 442 |
.TP 20 |
| 443 |
\fB&&\fR |
| 444 |
Logical AND. Produces a 1 result if both operands are non-zero, 0 otherwise. |
| 445 |
.TP 20 |
| 446 |
\fB||\fR |
| 447 |
Logical OR. Produces a 0 result if both operands are zero, 1 otherwise. |
| 448 |
.TP 20 |
| 449 |
\fIx\fB?\fIy\fB:\fIz\fR |
| 450 |
If-then-else, as in C. |
| 451 |
.LP |
| 452 |
.sp |
| 453 |
See the C manual for more details on the results produced by each |
| 454 |
operator. All of the binary operators group left-to-right within the |
| 455 |
same precedence level. |
| 456 |
.sp |
| 457 |
Several mathematical functions are supported for vectors. Each of |
| 458 |
the following functions invokes the math library function of the same name; |
| 459 |
see the manual entries for the library functions for details on what |
| 460 |
they do. The operation is applied to all elements of the vector |
| 461 |
returning the results. All functions take a vector operand. |
| 462 |
If no vector operand is used in the call, the current |
| 463 |
vector is assumed. eg. |
| 464 |
.CS |
| 465 |
vector create aVec |
| 466 |
aVec seq 0 100 |
| 467 |
aVec expr {2*abs(aVec)-1} |
| 468 |
aVec length 100 |
| 469 |
aVec expr {2*row()} |
| 470 |
vector expr {2*row()} ; # ERROR! |
| 471 |
.CE |
| 472 |
.CS |
| 473 |
.ta 3c 6c 9c |
| 474 |
\fBacos\fR \fBcos\fR \fBhypot\fR \fBsinh\fR |
| 475 |
\fBasin\fR \fBcosh\fR \fBlog\fR \fBsqrt\fR |
| 476 |
\fBatan\fR \fBexp\fR \fBlog10\fR \fBtan\fR |
| 477 |
\fBceil\fR \fBfloor\fR \fBsin\fR \fBtanh\fR |
| 478 |
.CE |
| 479 |
Additional functions are: |
| 480 |
.TP 1i |
| 481 |
\fBabs\fR |
| 482 |
Returns the absolute value of each component. |
| 483 |
.TP 1i |
| 484 |
\fBrandom\fR |
| 485 |
Returns a vector of non-negative values uniformly distributed |
| 486 |
between [0.0, 1.0) using \fIdrand48\fR. |
| 487 |
The seed comes from the internal clock of the machine or may be |
| 488 |
set manual with the srandom function. |
| 489 |
.TP 1i |
| 490 |
\fBround\fR |
| 491 |
Rounds each component of the vector. |
| 492 |
.TP 1i |
| 493 |
\fBsrandom\fR |
| 494 |
Initializes the random number generator using \fIsrand48\fR. |
| 495 |
The high order 32-bits are set using the integral portion of the first |
| 496 |
vector component. All other components are ignored. The low order 16-bits |
| 497 |
are set to an arbitrary value. |
| 498 |
.PP |
| 499 |
The following functions return a single value. |
| 500 |
.TP 1i |
| 501 |
\fBadev\fR |
| 502 |
Returns the average deviation (defined as the sum of the absolute values |
| 503 |
of the differences between component and the mean, divided by the length |
| 504 |
of the vector). |
| 505 |
.TP 1i |
| 506 |
\fBkurtosis\fR |
| 507 |
Returns the degree of peakedness (fourth moment) of the vector. |
| 508 |
.TP 1i |
| 509 |
\fBlength\fR |
| 510 |
Returns the number of components in the vector. |
| 511 |
.TP 1i |
| 512 |
\fBmax\fR |
| 513 |
Returns the vector's maximum value. |
| 514 |
.TP 1i |
| 515 |
\fBmean\fR |
| 516 |
Returns the mean value of the vector. |
| 517 |
.TP 1i |
| 518 |
\fBmedian\fR |
| 519 |
Returns the median of the vector. |
| 520 |
.TP 1i |
| 521 |
\fBmin\fR |
| 522 |
Returns the vector's minimum value. |
| 523 |
.TP 1i |
| 524 |
\fBq1\fR |
| 525 |
Returns the first quartile of the vector. |
| 526 |
.TP 1i |
| 527 |
\fBq3\fR |
| 528 |
Returns the third quartile of the vector. |
| 529 |
.TP 1i |
| 530 |
\fBprod\fR |
| 531 |
Returns the product of the components. |
| 532 |
.TP 1i |
| 533 |
\fBsdev\fR |
| 534 |
Returns the standard deviation (defined as the square root of the variance) |
| 535 |
of the vector. |
| 536 |
.TP 1i |
| 537 |
\fBskew\fR |
| 538 |
Returns the skewness (or third moment) of the vector. This characterizes |
| 539 |
the degree of asymmetry of the vector about the mean. |
| 540 |
.TP 1i |
| 541 |
\fBsum\fR |
| 542 |
Returns the sum of the components. |
| 543 |
.TP 1i |
| 544 |
\fBvar\fR |
| 545 |
Returns the variance of the vector. The sum of the squared differences |
| 546 |
between each component and the mean is computed. The variance is |
| 547 |
the sum divided by the length of the vector minus 1. |
| 548 |
.PP |
| 549 |
This last set of functions returns a vector of the same length as the argument. |
| 550 |
.TP 1i |
| 551 |
\fBinvert\fR |
| 552 |
Returns vector with elements in reversed order. |
| 553 |
.TP 1i |
| 554 |
\fBnorm\fR |
| 555 |
Scales the values of the vector to lie in the range [0.0..1.0]. |
| 556 |
.TP 1i |
| 557 |
\fBrow\fR |
| 558 |
Psuedo function to get the current row. |
| 559 |
.TP 1i |
| 560 |
\fBsort\fR |
| 561 |
Returns the vector components sorted in ascending order. |
| 562 |
.TP 1i |
| 563 |
\fBshift(nVec,N)\fR |
| 564 |
This is the only function taking a second arg. |
| 565 |
It provides a version of \fInvec\fR shifted by N places. |
| 566 |
When N is a scalar or vector with only one element, |
| 567 |
shift fills vacant area with 0. Otherwise the second element of |
| 568 |
\fInVec\fR is used for the fill value. |
| 569 |
One use for this is providing running totals. |
| 570 |
.RE |
| 571 |
.TP |
| 572 |
\fBvector names \fR?\fIpattern\fR? |
| 573 |
Return names of all defined vectors. |
| 574 |
.RE |
| 575 |
.TP |
| 576 |
\fBvector op\fR \fIoperation vecName\fR ?\fIarg\fR?... |
| 577 |
Invoke instance operation. Supported operations are defined in the next section. |
| 578 |
Op is the only way to invoke instance operation sub-commands |
| 579 |
when -command is defined as empty in |
| 580 |
a vector. It also allows writing vector code that is checkable |
| 581 |
by a syntax checkers. eg. |
| 582 |
.CS |
| 583 |
|
| 584 |
vector create v1 |
| 585 |
v1 op append {1 2 3} |
| 586 |
v1 op modify 1 2.1 |
| 587 |
|
| 588 |
.CE |
| 589 |
.RE |
| 590 |
.SH INSTANCE OPERATIONS |
| 591 |
You can also use the vector's Tcl command to query or modify it. The |
| 592 |
general form is |
| 593 |
.DS |
| 594 |
\fIvecName \fIoperation\fR \fR?\fIarg\fR?... |
| 595 |
.DE |
| 596 |
Note this is equivalent to the form: |
| 597 |
.DS |
| 598 |
\fBvector op\fR \fIoperation vecName\fR ?\fIarg\fR?... |
| 599 |
.DE |
| 600 |
Both \fIoperation\fR and its arguments determine the exact behavior of |
| 601 |
the command. The operations available for vectors are listed below. |
| 602 |
.TP |
| 603 |
\fIvecName \fB+\fR \fIitem\fR |
| 604 |
\fIvecName \fB-\fR \fIitem\fR |
| 605 |
\fIvecName \fB*\fR \fIitem\fR |
| 606 |
\fIvecName \fB/\fR \fIitem\fR |
| 607 |
Perform binary op and return result as a list. |
| 608 |
.TP |
| 609 |
\fIvecName \fBappend\fR \fIitem\fR ?\fIitem\fR?... |
| 610 |
Appends the component values from \fIitem\fR to \fIvecName\fR. |
| 611 |
\fIItem\fR can be either the name of a vector or a list of numeric |
| 612 |
values. |
| 613 |
.TP |
| 614 |
\fIvecName \fBbinread\fR \fIchannel\fR ?\fIlength\fR? ?\fIswitches\fR? |
| 615 |
Reads binary values from a Tcl channel. Values are either appended |
| 616 |
to the end of the vector or placed at a given index (using the |
| 617 |
\fB\-at\fR option), overwriting existing values. Data is read until EOF |
| 618 |
is found on the channel or a specified number of values \fIlength\fR |
| 619 |
are read (note that this is not necessarily the same as the number of |
| 620 |
bytes). The following switches are supported: |
| 621 |
.RS |
| 622 |
.TP |
| 623 |
\fB\-swap\fR |
| 624 |
Swap bytes and words. The default endian is the host machine. |
| 625 |
.TP |
| 626 |
\fB\-at \fIindex\fR |
| 627 |
New values will start at vector index \fIindex\fR. This will |
| 628 |
overwrite any current values. |
| 629 |
.TP |
| 630 |
\fB\-format\fR \fIformat\fR |
| 631 |
Specifies the format of the data. \fIFormat\fR can be one of the |
| 632 |
following: "i1", "i2", "i4", "i8", "u1, "u2", "u4", "u8", "r4", |
| 633 |
"r8", or "r16". The number indicates the number of bytes |
| 634 |
required for each value. The letter indicates the type: "i" for signed, |
| 635 |
"u" for unsigned, "r" or real. The default format is "r16". |
| 636 |
.RE |
| 637 |
.TP |
| 638 |
\fIvecName \fBbinwrite\fR \fIchannel\fR ?\fIlength\fR? ?\fI\-at index\fR? |
| 639 |
Like \fBbinread\fR, but writes data. |
| 640 |
.TP |
| 641 |
\fIvecName \fBclear\fR |
| 642 |
Clears the element indices from the array variable associated with |
| 643 |
\fIvecName\fR. This doesn't affect the components of the vector. By |
| 644 |
default, the number of entries in the Tcl array doesn't match the |
| 645 |
number of components in the vector. This is because its too expensive |
| 646 |
to maintain decimal strings for both the index and value for each |
| 647 |
component. Instead, the index and value are saved only when you read |
| 648 |
or write an element with a new index. This command removes the index |
| 649 |
and value strings from the array. This is useful when the vector is |
| 650 |
large. |
| 651 |
.TP |
| 652 |
\fIvecName \fBdelete\fR \fIindex\fR ?\fIindex\fR?... |
| 653 |
Deletes the \fIindex\fRth component from the vector \fIvecName\fR. |
| 654 |
\fIIndex\fR is the index of the element to be deleted. This is the |
| 655 |
same as unsetting the array variable element \fIindex\fR. The vector |
| 656 |
is compacted after all the indices have been deleted. |
| 657 |
.TP |
| 658 |
\fIvecName \fBdup\fR \fIdestName\fR |
| 659 |
Copies \fIvecName\fR to \fIdestName\fR. \fIDestName\fR is the name of a |
| 660 |
destination vector. If a vector \fIdestName\fR already exists, it is |
| 661 |
overwritten with the components of \fIvecName\fR. Otherwise a |
| 662 |
new vector is created. |
| 663 |
.TP |
| 664 |
\fIvecName \fBexpr\fR \fIexpression\fR |
| 665 |
Computes the expression and resets the values of the vector accordingly. |
| 666 |
Both scalar and vector math operations are allowed. All values in |
| 667 |
expressions are either real numbers or names of vectors. All numbers |
| 668 |
are treated as one component vectors. |
| 669 |
.TP |
| 670 |
\fIvecName \fBindex\fR \fIindex\fR ?\fIvalue\fR?... |
| 671 |
Get/set individual vector values. This provides element |
| 672 |
updating when \fI\-variable\fR is set to empty. |
| 673 |
.TP |
| 674 |
\fIvecName \fBinsert\fR \fIindex\fR \fIitem\fR ?\fIitem\fR?... |
| 675 |
Inserts the component values from \fIitem\fR to \fIvecName\fR at |
| 676 |
\fIindex\fR \fIItem\fR can be either the name of a vector or |
| 677 |
a list of numeric values. |
| 678 |
.TP |
| 679 |
\fIvecName \fBlength\fR ?\fInewSize\fR? |
| 680 |
Queries or resets the number of components in \fIvecName\fR. |
| 681 |
\fINewSize\fR is a number specifying the new size of the vector. If |
| 682 |
\fInewSize\fR is smaller than the current size of \fIvecName\fR, |
| 683 |
\fIvecName\fR is truncated. If \fInewSize\fR is greater, the vector |
| 684 |
is extended and the new components are initialized to \fB0.0\fR. If |
| 685 |
no \fInewSize\fR argument is present, the current length of the vector |
| 686 |
is returned. |
| 687 |
.TP |
| 688 |
\fIvecName \fBmatrix \fI ...\fR |
| 689 |
Matrix provides a 2D array view into 1D data. It provides indexing operations |
| 690 |
in ROW,COL form making it suitable for use with TkTable. |
| 691 |
Data storage remains unchanged: vectors are still just a single long array. |
| 692 |
For example, here are two ways to create a 3 column by 10 row matrix: |
| 693 |
.CS |
| 694 |
|
| 695 |
vector create aVec(10,3) |
| 696 |
vector create bVec(30) |
| 697 |
bVec matrix numcols 3 |
| 698 |
set aVec(0,0) 99 |
| 699 |
set bVec(29,2) -99 |
| 700 |
aVec append {5 6 7}; # aVec now has 11 rows. |
| 701 |
aVec append 1 2; # Now aVec has 13 rows! |
| 702 |
|
| 703 |
.CE |
| 704 |
Note that data is appended only in increments of numcols. |
| 705 |
Elements 0-2 make up the first row, 3-5 the second, etc. |
| 706 |
Elements will appear only in increments of the column size. |
| 707 |
.RS |
| 708 |
.TP 0.75i |
| 709 |
\fIvecName \fBmatrix copy \fIdstcolumn\fR \fIsrccolumn\fR \fI?srcVec?\fR |
| 710 |
Copy a column of element values |
| 711 |
to column \fIdstcolumn\fR from \fIsrccolumn\fR. |
| 712 |
If vector \fIsrcVec\fR is given, and not the same as \fIvecName\fR, the |
| 713 |
columns numbers must be different. |
| 714 |
If the \fIsrcVec\fR column is longer, \fIvecName\fR will be extended. |
| 715 |
If shorter, remaining destination values are not overwritten. |
| 716 |
.TP |
| 717 |
\fIvecName \fBmatrix delete \fIcolumn\fR. |
| 718 |
Delete elements in a column. |
| 719 |
Note that \fBnumcols\fR, which must be greater than 1, will be decremented. |
| 720 |
.TP |
| 721 |
\fIvecName \fBmatrix get \fIcolumn\fR |
| 722 |
Get the element in a column: this number must be |
| 723 |
less than \fBnumcols\fR. |
| 724 |
Note that \fBnumcols\fR must be non-zero. |
| 725 |
.TP |
| 726 |
\fIvecName \fBmatrix insert \fIcolumn\fR \fI?initvalue?\fR . |
| 727 |
Insert a new column of elements at column (default 0). |
| 728 |
The new column is initialized |
| 729 |
with \fIinitvalue\fR, or \fI0.0\fR if not specified. |
| 730 |
Note that \fBnumcols\fR will be incremented. |
| 731 |
.TP |
| 732 |
\fIvecName \fBmatrix multiply \fIsrcVec\fR ?\fIdstVec\fR? |
| 733 |
Perform matrix multiplication using \fBsrcVec\fR, |
| 734 |
placing results either in \fBdstVec\fR, or returned as a list. |
| 735 |
The numrows of \fIsrcVec\fR must equal numcols in |
| 736 |
\fIvecName\fR. One application for multiply is coordinate transformation. |
| 737 |
.TP |
| 738 |
\fIvecName \fBmatrix numcols \fI?size?\fR |
| 739 |
Get or set the number of columns for a vectors data. Values >1 enable |
| 740 |
array variables to accept 2d matrix indexes. |
| 741 |
For example with a numcols of 10, \fB$vec1(1,2)\fR refers to the |
| 742 |
13th element in the vector. A vectors size is also constrained |
| 743 |
to multiples of numcols, as is it's offset. |
| 744 |
By default, numcols is 1. |
| 745 |
.TP |
| 746 |
\fIvecName \fBmatrix numrows \fI?size?\fR |
| 747 |
Get or set the length of rows in a columns for a vector. |
| 748 |
By default, this is just the \fIvector length/numcols\fR. |
| 749 |
Setting this value simply provides a convenient way to increase or decrease |
| 750 |
the vector size by multiples of \fInumcols\fR. |
| 751 |
.TP |
| 752 |
\fIvecName \fBmatrix set \fIcolumn\fR \fI?valuelist?\fR |
| 753 |
Set value elements in a column: this number must be |
| 754 |
less than \fBnumcols\fR. The \fIvaluelist\fR is a list values. |
| 755 |
If this list is shorter than the column, it's last |
| 756 |
value is used for all remaining columns. The column gets set to |
| 757 |
the values of \fIitem\fR, or \fI0.0\fR by default. |
| 758 |
.TP |
| 759 |
\fIvecName \fBmatrix shift \fIcolumn\fR \fIamount\fR ?\fIstartoffset\fR? |
| 760 |
Shifts the values of a column by integer in\fIamount\fR. A negative |
| 761 |
value shifts upward. |
| 762 |
The \fIstartoffset\fR indicates where to start shifting from. |
| 763 |
.TP |
| 764 |
\fIvecName \fBmatrix sort \fIcolumn\fR \fI?-reverse?\fR |
| 765 |
Sort the vector by the given column. |
| 766 |
.TP |
| 767 |
\fIvecName \fBmatrix transpose\fR |
| 768 |
Transpose all columns with rows in matrix. |
| 769 |
Note that this is a no-op if \fBnumcols\fR is 1. Otherwise, |
| 770 |
numcols will change to \fBvectorLength/numcols\fR. |
| 771 |
.RE |
| 772 |
.TP |
| 773 |
\fIvecName \fBmerge\fR \fIsrcName\fR ?\fIsrcName\fR?... |
| 774 |
Merges the named vectors into a single vector. The resulting |
| 775 |
vector is formed by merging the components of each source vector |
| 776 |
one index at a time. |
| 777 |
.TP |
| 778 |
\fIvecName \fBnotify\fR ?\fIkeyword\fR? ?\fIscript\fR? |
| 779 |
Queries or controls how vector clients are notified of changes |
| 780 |
to the vector. Also allows setting a notifier callback. |
| 781 |
The exact behavior is determined by \fIkeyword\fR. |
| 782 |
.RS |
| 783 |
.TP 0.75i |
| 784 |
\fBalways\fR |
| 785 |
Indicates that clients are to be notified immediately whenever the |
| 786 |
vector is updated. |
| 787 |
.TP |
| 788 |
\fBnever\fR |
| 789 |
Indicates that no clients are to be notified. |
| 790 |
.TP |
| 791 |
\fBwhenidle\fR |
| 792 |
Indicates that clients are to be notified at the next idle point |
| 793 |
whenever the vector is updated. |
| 794 |
.TP |
| 795 |
\fBnow\fR |
| 796 |
If any client notifications is currently pending, they are notified |
| 797 |
immediately. |
| 798 |
.TP |
| 799 |
\fBcancel\fR |
| 800 |
Cancels pending notifications of clients using the vector. |
| 801 |
.TP |
| 802 |
\fBpending\fR |
| 803 |
Returns \fB1\fR if a client notification is pending, and \fB0\fR otherwise. |
| 804 |
.TP |
| 805 |
\fBcallback\fR ?\fIscript\fR? |
| 806 |
Query or set a Tcl callback script that is evaluated when a vector is updated. |
| 807 |
.RE |
| 808 |
.TP |
| 809 |
\fIvecName \fBpopulate\fR \fIdestName\fR ?\fIdensity\fR? |
| 810 |
Creates a vector \fIdestName\fR which is a superset of \fIvecName\fR. |
| 811 |
\fIDestName\fR will include all the components of \fIvecName\fR, in |
| 812 |
addition the interval between each of the original components will |
| 813 |
contain a \fIdensity\fR number of new components, whose values are |
| 814 |
evenly distributed between the original components values. This is |
| 815 |
useful for generating abscissas to be interpolated along a spline. |
| 816 |
.TP |
| 817 |
\fIvecName \fBrange\fR \fIfirstIndex\fR ?\fIlastIndex\fR?... |
| 818 |
Returns a list of numeric values representing the vector components |
| 819 |
between two indices. Both \fIfirstIndex\fR and \fIlastIndex\fR are |
| 820 |
indices representing the range of components to be returned. If |
| 821 |
\fIlastIndex\fR is less than \fIfirstIndex\fR, the components are |
| 822 |
listed in reverse order. |
| 823 |
.TP |
| 824 |
\fIvecName \fBsearch\fR \fIvalue\fR ?\fIvalue\fR? |
| 825 |
Searches for a value or range of values among the components of |
| 826 |
\fIvecName\fR. If one \fIvalue\fR argument is given, a list of |
| 827 |
indices of the components which equal \fIvalue\fR is returned. If a |
| 828 |
second \fIvalue\fR is also provided, then the indices of all |
| 829 |
components which lie within the range of the two values are returned. |
| 830 |
If no components are found, then \fB""\fR is returned. |
| 831 |
.TP |
| 832 |
\fIvecName \fBset\fR \fIitem\fR |
| 833 |
Resets the components of the vector to \fIitem\fR. \fIItem\fR can |
| 834 |
be either a list of numeric expressions or another vector. |
| 835 |
.TP |
| 836 |
\fIvecName \fBseq\fR \fIstart\fR ?\fIfinish\fR? ?\fIstep\fR? |
| 837 |
Generates a sequence of values starting with the value \fIstart\fR. |
| 838 |
\fIFinish\fR indicates the terminating value of the sequence. |
| 839 |
The vector is automatically resized to contain just the sequence. |
| 840 |
If three arguments are present, \fIstep\fR designates the interval. |
| 841 |
.sp |
| 842 |
With only two arguments (no \fIfinish\fR argument), the sequence will |
| 843 |
continue until the vector is filled. With one argument, the interval |
| 844 |
defaults to 1.0. |
| 845 |
.TP |
| 846 |
\fIvecName \fBsort\fR ?\fB-reverse\fR? ?\fIargName\fR?... |
| 847 |
Sorts the vector \fIvecName\fR in increasing order. If the |
| 848 |
\fB-reverse\fR flag is present, the vector is sorted in decreasing |
| 849 |
order. If other arguments \fIargName\fR are present, they are the |
| 850 |
names of vectors which will be rearranged in the same manner as |
| 851 |
\fIvecName\fR. Each vector must be the same length as \fIvecName\fR. |
| 852 |
You could use this to sort the x vector of a graph, while still |
| 853 |
retaining the same x,y coordinate pairs in a y vector. |
| 854 |
.TP |
| 855 |
\fIvecName \fBsplit\fR \fIdstName\fR ?\fIdstName\fR?... |
| 856 |
Split the vector into a multiple vectors. The resulting |
| 857 |
N vectors each contain the mod-Nth element from source. |
| 858 |
.TP |
| 859 |
\fIvecName \fBvariable\fR \fIvarName\fR |
| 860 |
Maps a Tcl variable to the vector, creating another means for |
| 861 |
accessing the vector. The variable \fIvarName\fR can't already |
| 862 |
exist. This overrides any current variable mapping the vector |
| 863 |
may have. |
| 864 |
.RE |
| 865 |
.SH C LANGUAGE API |
| 866 |
You can create, modify, and destroy vectors from C code, using |
| 867 |
library routines. |
| 868 |
You need to include the header file \fBblt.h\fR. It contains the |
| 869 |
definition of the structure \fBBlt_Vector\fR, which represents the |
| 870 |
vector. It appears below. |
| 871 |
.CS |
| 872 |
\fRtypedef struct { |
| 873 |
double *\fIvalueArr\fR; |
| 874 |
int \fInumValues\fR; |
| 875 |
int \fIarraySize\fR; |
| 876 |
double \fImin\fR, \fImax\fR; |
| 877 |
} \fBBlt_Vector\fR; |
| 878 |
.CE |
| 879 |
The field \fIvalueArr\fR points to memory holding the vector |
| 880 |
components. The components are stored in a double precision array, |
| 881 |
whose size size is represented by \fIarraySize\fR. \fINumValues\fR is |
| 882 |
the length of vector. The size of the array is always equal to or |
| 883 |
larger than the length of the vector. \fIMin\fR and \fImax\fR are |
| 884 |
minimum and maximum component values. |
| 885 |
.SH LIBRARY ROUTINES |
| 886 |
The following routines are available from C to manage vectors. |
| 887 |
Vectors are identified by the vector name. |
| 888 |
.PP |
| 889 |
\fBBlt_CreateVector\fR |
| 890 |
.RS .25i |
| 891 |
.TP 1i |
| 892 |
Synopsis: |
| 893 |
.CS |
| 894 |
int \fBBlt_CreateVector\fR (\fIinterp\fR, \fIvecName\fR, \fIlength\fR, \fIvecPtrPtr\fR) |
| 895 |
.RS 1.25i |
| 896 |
Tcl_Interp *\fIinterp\fR; |
| 897 |
char *\fIvecName\fR; |
| 898 |
int \fIlength\fR; |
| 899 |
Blt_Vector **\fIvecPtrPtr\fR; |
| 900 |
.RE |
| 901 |
.CE |
| 902 |
.TP |
| 903 |
Description: |
| 904 |
Creates a new vector \fIvecName\fR\fR with a length of \fIlength\fR. |
| 905 |
\fBBlt_CreateVector\fR creates both a new Tcl command and array |
| 906 |
variable \fIvecName\fR. Neither a command nor variable named |
| 907 |
\fIvecName\fR can already exist. A pointer to the vector is |
| 908 |
placed into \fIvecPtrPtr\fR. |
| 909 |
.TP |
| 910 |
Results: |
| 911 |
Returns \fBTCL_OK\fR if the vector is successfully created. If |
| 912 |
\fIlength\fR is negative, a Tcl variable or command \fIvecName\fR |
| 913 |
already exists, or memory cannot be allocated for the vector, then |
| 914 |
\fBTCL_ERROR\fR is returned and \fIinterp->result\fR will contain an |
| 915 |
error message. |
| 916 |
.RE |
| 917 |
.sp |
| 918 |
.PP |
| 919 |
\fBBlt_DeleteVectorByName\fR |
| 920 |
.RS .25i |
| 921 |
.TP 1i |
| 922 |
Synopsis: |
| 923 |
.CS |
| 924 |
int \fBBlt_DeleteVectorByName\fR (\fIinterp\fR, \fIvecName\fR) |
| 925 |
.RS 1.25i |
| 926 |
Tcl_Interp *\fIinterp\fR; |
| 927 |
char *\fIvecName\fR; |
| 928 |
.RE |
| 929 |
.CE |
| 930 |
.TP 1i |
| 931 |
Description: |
| 932 |
Removes the vector \fIvecName\fR. \fIVecName\fR is the name of a vector |
| 933 |
which must already exist. Both the Tcl command and array variable |
| 934 |
\fIvecName\fR are destroyed. All clients of the vector will be notified |
| 935 |
immediately that the vector has been destroyed. |
| 936 |
.TP |
| 937 |
Results: |
| 938 |
Returns \fBTCL_OK\fR if the vector is successfully deleted. If |
| 939 |
\fIvecName\fR is not the name a vector, then \fBTCL_ERROR\fR is returned |
| 940 |
and \fIinterp->result\fR will contain an error message. |
| 941 |
.RE |
| 942 |
.sp |
| 943 |
.PP |
| 944 |
\fBBlt_DeleteVector\fR |
| 945 |
.RS .25i |
| 946 |
.TP 1i |
| 947 |
Synopsis: |
| 948 |
.CS |
| 949 |
int \fBBlt_DeleteVector\fR (\fIvecPtr\fR) |
| 950 |
.RS 1.25i |
| 951 |
Blt_Vector *\fIvecPtr\fR; |
| 952 |
.RE |
| 953 |
.CE |
| 954 |
.TP 1i |
| 955 |
Description: |
| 956 |
Removes the vector pointed to by \fIvecPtr\fR. \fIVecPtr\fR is a |
| 957 |
pointer to a vector, typically set by \fBBlt_GetVector\fR or |
| 958 |
\fBBlt_CreateVector\fR. Both the Tcl command and array variable of |
| 959 |
the vector are destroyed. All clients of the vector will be notified |
| 960 |
immediately that the vector has been destroyed. |
| 961 |
.TP |
| 962 |
Results: |
| 963 |
Returns \fBTCL_OK\fR if the vector is successfully deleted. If |
| 964 |
\fIvecName\fR is not the name a vector, then \fBTCL_ERROR\fR is returned |
| 965 |
and \fIinterp->result\fR will contain an error message. |
| 966 |
.RE |
| 967 |
.sp |
| 968 |
.PP |
| 969 |
\fBBlt_GetVector\fR |
| 970 |
.RS .25i |
| 971 |
.TP 1i |
| 972 |
Synopsis: |
| 973 |
.CS |
| 974 |
int \fBBlt_GetVector\fR (\fIinterp\fR, \fIvecName\fR, \fIvecPtrPtr\fR) |
| 975 |
.RS 1.25i |
| 976 |
Tcl_Interp *\fIinterp\fR; |
| 977 |
char *\fIvecName\fR; |
| 978 |
Blt_Vector **\fIvecPtrPtr\fR; |
| 979 |
.RE |
| 980 |
.CE |
| 981 |
.TP 1i |
| 982 |
Description: |
| 983 |
Retrieves the vector \fIvecName\fR. \fIVecName\fR is the name of a |
| 984 |
vector which must already exist. \fIVecPtrPtr\fR will point be set to |
| 985 |
the address of the vector. |
| 986 |
.TP |
| 987 |
Results: |
| 988 |
Returns \fBTCL_OK\fR if the vector is successfully retrieved. If |
| 989 |
\fIvecName\fR is not the name of a vector, then \fBTCL_ERROR\fR is |
| 990 |
returned and \fIinterp->result\fR will contain an error message. |
| 991 |
.RE |
| 992 |
.sp |
| 993 |
.PP |
| 994 |
\fBBlt_ResetVector\fR |
| 995 |
.PP |
| 996 |
.RS .25i |
| 997 |
.TP 1i |
| 998 |
Synopsis: |
| 999 |
.CS |
| 1000 |
int \fBBlt_ResetVector\fR (\fIvecPtr\fR, \fIdataArr\fR, |
| 1001 |
\fInumValues\fR, \fIarraySize\fR, \fIfreeProc\fR) |
| 1002 |
.RS 1.25i |
| 1003 |
Blt_Vector *\fIvecPtr\fR; |
| 1004 |
double *\fIdataArr\fR; |
| 1005 |
int *\fInumValues\fR; |
| 1006 |
int *\fIarraySize\fR; |
| 1007 |
Tcl_FreeProc *\fIfreeProc\fR; |
| 1008 |
.RE |
| 1009 |
.CE |
| 1010 |
.TP |
| 1011 |
Description: |
| 1012 |
Resets the components of the vector pointed to by \fIvecPtr\fR. |
| 1013 |
Calling \fBBlt_ResetVector\fR will trigger the vector to dispatch |
| 1014 |
notifications to its clients. \fIDataArr\fR is the array of doubles |
| 1015 |
which represents the vector data. \fINumValues\fR is the number of |
| 1016 |
elements in the array. \fIArraySize\fR is the actual size of the array |
| 1017 |
(the array may be bigger than the number of values stored in |
| 1018 |
it). \fIFreeProc\fP indicates how the storage for the vector component |
| 1019 |
array (\fIdataArr\fR) was allocated. It is used to determine how to |
| 1020 |
reallocate memory when the vector is resized or destroyed. It must be |
| 1021 |
\fBTCL_DYNAMIC\fR, \fBTCL_STATIC\fR, \fBTCL_VOLATILE\fR, or a pointer |
| 1022 |
to a function to free the memory allocated for the vector array. If |
| 1023 |
\fIfreeProc\fR is \fBTCL_VOLATILE\fR, it indicates that \fIdataArr\fR |
| 1024 |
must be copied and saved. If \fIfreeProc\fR is \fBTCL_DYNAMIC\fR, it |
| 1025 |
indicates that \fIdataArr\fR was dynamically allocated and that Tcl |
| 1026 |
should free \fIdataArr\fR if necessary. \fBStatic\fR indicates that |
| 1027 |
nothing should be done to release storage for \fIdataArr\fR. |
| 1028 |
.TP |
| 1029 |
Results: |
| 1030 |
Returns \fBTCL_OK\fR if the vector is successfully resized. If |
| 1031 |
\fInewSize\fR is negative, a vector \fIvecName\fR does not exist, or |
| 1032 |
memory cannot be allocated for the vector, then \fBTCL_ERROR\fR is |
| 1033 |
returned and \fIinterp->result\fR will contain an error message. |
| 1034 |
.RE |
| 1035 |
.sp |
| 1036 |
.PP |
| 1037 |
\fBBlt_ResizeVector\fR |
| 1038 |
.RS .25i |
| 1039 |
.TP 1i |
| 1040 |
Synopsis: |
| 1041 |
.CS |
| 1042 |
int \fBBlt_ResizeVector\fR (\fIvecPtr\fR, \fInewSize\fR) |
| 1043 |
.RS 1.25i |
| 1044 |
Blt_Vector *\fIvecPtr\fR; |
| 1045 |
int \fInewSize\fR; |
| 1046 |
.RE |
| 1047 |
.CE |
| 1048 |
.TP |
| 1049 |
Description: |
| 1050 |
Resets the length of the vector pointed to by \fIvecPtr\fR to |
| 1051 |
\fInewSize\fR. If \fInewSize\fR is smaller than the current size of |
| 1052 |
the vector, it is truncated. If \fInewSize\fR is greater, the vector |
| 1053 |
is extended and the new components are initialized to \fB0.0\fR. |
| 1054 |
Calling \fBBlt_ResetVector\fR will trigger the vector to dispatch |
| 1055 |
notifications. |
| 1056 |
.TP |
| 1057 |
Results: |
| 1058 |
Returns \fBTCL_OK\fR if the vector is successfully resized. If |
| 1059 |
\fInewSize\fR is negative or memory can not be allocated for the vector, |
| 1060 |
then \fBTCL_ERROR\fR is returned and \fIinterp->result\fR will contain |
| 1061 |
an error message. |
| 1062 |
.sp |
| 1063 |
.PP |
| 1064 |
\fBBlt_VectorExists\fR |
| 1065 |
.RS .25i |
| 1066 |
.TP 1i |
| 1067 |
Synopsis: |
| 1068 |
.CS |
| 1069 |
int \fBBlt_VectorExists\fR (\fIinterp\fR, \fIvecName\fR) |
| 1070 |
.RS 1.25i |
| 1071 |
Tcl_Interp *\fIinterp\fR; |
| 1072 |
char *\fIvecName\fR; |
| 1073 |
.RE |
| 1074 |
.CE |
| 1075 |
.TP |
| 1076 |
Description: |
| 1077 |
Indicates if a vector named \fIvecName\fR exists in \fIinterp\fR. |
| 1078 |
.TP |
| 1079 |
Results: |
| 1080 |
Returns \fB1\fR if a vector \fIvecName\fR exists and \fB0\fR otherwise. |
| 1081 |
.RE |
| 1082 |
.sp |
| 1083 |
.PP |
| 1084 |
If your application needs to be notified when a vector changes, it can |
| 1085 |
allocate a unique \fIclient identifier\fR for itself. Using this |
| 1086 |
identifier, you can then register a call-back to be made whenever the |
| 1087 |
vector is updated or destroyed. By default, the call-backs are made at |
| 1088 |
the next idle point. This can be changed to occur at the time the |
| 1089 |
vector is modified. An application can allocate more than one |
| 1090 |
identifier for any vector. When the client application is done with |
| 1091 |
the vector, it should free the identifier. |
| 1092 |
.PP |
| 1093 |
The call-back routine must of the following type. |
| 1094 |
.CS |
| 1095 |
.RS |
| 1096 |
.sp |
| 1097 |
typedef void (\fBBlt_VectorChangedProc\fR) (Tcl_Interp *\fIinterp\fR, |
| 1098 |
.RS .25i |
| 1099 |
ClientData \fIclientData\fR, Blt_VectorNotify \fInotify\fR); |
| 1100 |
.RE |
| 1101 |
.sp |
| 1102 |
.RE |
| 1103 |
.CE |
| 1104 |
.fi |
| 1105 |
\fIClientData\fR is passed to this routine whenever it is called. You |
| 1106 |
can use this to pass information to the call-back. The \fInotify\fR |
| 1107 |
argument indicates whether the vector has been updated of destroyed. It |
| 1108 |
is an enumerated type. |
| 1109 |
.CS |
| 1110 |
.RS |
| 1111 |
.sp |
| 1112 |
typedef enum { |
| 1113 |
\fBBLT_VECTOR_NOTIFY_UPDATE\fR=1, |
| 1114 |
\fBBLT_VECTOR_NOTIFY_DESTROY\fR=2 |
| 1115 |
} \fBBlt_VectorNotify\fR; |
| 1116 |
.sp |
| 1117 |
.RE |
| 1118 |
.CE |
| 1119 |
.PP |
| 1120 |
\fBBlt_AllocVectorId\fR |
| 1121 |
.RS .25i |
| 1122 |
.TP 1i |
| 1123 |
Synopsis: |
| 1124 |
.CS |
| 1125 |
Blt_VectorId \fBBlt_AllocVectorId\fR (\fIinterp\fR, \fIvecName\fR) |
| 1126 |
.RS 1.25i |
| 1127 |
Tcl_Interp *\fIinterp\fR; |
| 1128 |
char *\fIvecName\fR; |
| 1129 |
.RE |
| 1130 |
.CE |
| 1131 |
.TP |
| 1132 |
Description: |
| 1133 |
Allocates an client identifier for with the vector \fIvecName\fR. |
| 1134 |
This identifier can be used to specify a call-back which is triggered |
| 1135 |
when the vector is updated or destroyed. |
| 1136 |
.TP |
| 1137 |
Results: |
| 1138 |
Returns a client identifier if successful. If \fIvecName\fR is not |
| 1139 |
the name of a vector, then \fBNULL\fR is returned and |
| 1140 |
\fIinterp->result\fR will contain an error message. |
| 1141 |
.RE |
| 1142 |
.sp |
| 1143 |
.PP |
| 1144 |
\fBBlt_GetVectorById\fR |
| 1145 |
.RS .25i |
| 1146 |
.TP 1i |
| 1147 |
Synopsis: |
| 1148 |
.CS |
| 1149 |
int \fBBlt_GetVector\fR (\fIinterp\fR, \fIclientId\fR, \fIvecPtrPtr\fR) |
| 1150 |
.RS 1.25i |
| 1151 |
Tcl_Interp *\fIinterp\fR; |
| 1152 |
Blt_VectorId \fIclientId\fR; |
| 1153 |
Blt_Vector **\fIvecPtrPtr\fR; |
| 1154 |
.RE |
| 1155 |
.CE |
| 1156 |
.TP 1i |
| 1157 |
Description: |
| 1158 |
Retrieves the vector used by \fIclientId\fR. \fIClientId\fR is a valid |
| 1159 |
vector client identifier allocated by \fBBlt_AllocVectorId\fR. |
| 1160 |
\fIVecPtrPtr\fR will point be set to the address of the vector. |
| 1161 |
.TP |
| 1162 |
Results: |
| 1163 |
Returns \fBTCL_OK\fR if the vector is successfully retrieved. |
| 1164 |
.RE |
| 1165 |
.sp |
| 1166 |
.PP |
| 1167 |
\fBBlt_SetVectorChangedProc\fR |
| 1168 |
.RS .25i |
| 1169 |
.TP 1i |
| 1170 |
Synopsis: |
| 1171 |
.CS |
| 1172 |
void \fBBlt_SetVectorChangedProc\fR (\fIclientId\fR, \fIproc\fR, \fIclientData\fR); |
| 1173 |
.RS 1.25i |
| 1174 |
Blt_VectorId \fIclientId\fR; |
| 1175 |
Blt_VectorChangedProc *\fIproc\fR; |
| 1176 |
ClientData *\fIclientData\fR; |
| 1177 |
.RE |
| 1178 |
.CE |
| 1179 |
.TP |
| 1180 |
Description: |
| 1181 |
Specifies a call-back routine to be called whenever the vector |
| 1182 |
associated with \fIclientId\fR is updated or deleted. \fIProc\fR is a |
| 1183 |
pointer to call-back routine and must be of the type |
| 1184 |
\fBBlt_VectorChangedProc\fR. \fIClientData\fR is a one-word value to |
| 1185 |
be passed to the routine when it is invoked. If \fIproc\fR is |
| 1186 |
\fBNULL\fR, then the client is not notified. |
| 1187 |
.TP |
| 1188 |
Results: |
| 1189 |
The designated call-back procedure will be invoked when the vector is |
| 1190 |
updated or destroyed. |
| 1191 |
.RE |
| 1192 |
.sp |
| 1193 |
.PP |
| 1194 |
\fBBlt_FreeVectorId\fR |
| 1195 |
.RS .25i |
| 1196 |
.TP 1i |
| 1197 |
Synopsis: |
| 1198 |
.CS |
| 1199 |
void \fBBlt_FreeVectorId\fR (\fIclientId\fR); |
| 1200 |
.RS 1.25i |
| 1201 |
Blt_VectorId \fIclientId\fR; |
| 1202 |
.RE |
| 1203 |
.CE |
| 1204 |
.TP |
| 1205 |
Description: |
| 1206 |
Frees the client identifier. Memory allocated for the identifier |
| 1207 |
is released. The client will no longer be notified when the |
| 1208 |
vector is modified. |
| 1209 |
.TP |
| 1210 |
Results: |
| 1211 |
The designated call-back procedure will be no longer be invoked when |
| 1212 |
the vector is updated or destroyed. |
| 1213 |
.RE |
| 1214 |
.sp |
| 1215 |
.PP |
| 1216 |
\fBBlt_NameOfVectorId\fR |
| 1217 |
.RS .25i |
| 1218 |
.TP 1i |
| 1219 |
Synopsis: |
| 1220 |
.CS |
| 1221 |
char *\fBBlt_NameOfVectorId\fR (\fIclientId\fR); |
| 1222 |
.RS 1.25i |
| 1223 |
Blt_VectorId \fIclientId\fR; |
| 1224 |
.RE |
| 1225 |
.CE |
| 1226 |
.TP |
| 1227 |
Description: |
| 1228 |
Retrieves the name of the vector associated with the client identifier |
| 1229 |
\fIclientId\fR. |
| 1230 |
.TP |
| 1231 |
Results: |
| 1232 |
Returns the name of the vector associated with \fIclientId\fR. If |
| 1233 |
\fIclientId\fR is not an identifier or the vector has been destroyed, |
| 1234 |
\fBNULL\fR is returned. |
| 1235 |
.RE |
| 1236 |
.sp |
| 1237 |
.PP |
| 1238 |
\fBBlt_InstallIndexProc\fR |
| 1239 |
.RS .25i |
| 1240 |
.TP 1i |
| 1241 |
Synopsis: |
| 1242 |
.CS |
| 1243 |
void \fBBlt_InstallIndexProc\fR (\fIindexName\fR, \fIprocPtr\fR) |
| 1244 |
.RS 1.25i |
| 1245 |
char *\fIindexName\fR; |
| 1246 |
Blt_VectorIndexProc *\fIprocPtr\fR; |
| 1247 |
.RE |
| 1248 |
.CE |
| 1249 |
.TP |
| 1250 |
Description: |
| 1251 |
Registers a function to be called to retrieved the index \fIindexName\fR |
| 1252 |
from the vector's array variable. |
| 1253 |
.sp |
| 1254 |
typedef double Blt_VectorIndexProc(Vector *vecPtr); |
| 1255 |
.sp |
| 1256 |
The function will be passed a pointer to the vector. The function must |
| 1257 |
return a double representing the value at the index. |
| 1258 |
.TP |
| 1259 |
Results: |
| 1260 |
The new index is installed into the vector. |
| 1261 |
.RE |
| 1262 |
.RE |
| 1263 |
.SH C API EXAMPLE |
| 1264 |
The following example opens a file of binary data and stores it in an |
| 1265 |
array of doubles. The array size is computed from the size of the |
| 1266 |
file. If the vector "data" exists, calling \fBBlt_VectorExists\fR, |
| 1267 |
\fBBlt_GetVector\fR is called to get the pointer to the vector. |
| 1268 |
Otherwise the routine \fBBlt_CreateVector\fR is called to create a new |
| 1269 |
vector and returns a pointer to it. Just like the Tcl interface, both |
| 1270 |
a new Tcl command and array variable are created when a new vector is |
| 1271 |
created. It doesn't make any difference what the initial size of the |
| 1272 |
vector is since it will be reset shortly. The vector is updated when |
| 1273 |
\fBlt_ResetVector\fR is called. Blt_ResetVector makes the changes |
| 1274 |
visible to the Tcl interface and other vector clients (such as a graph |
| 1275 |
widget). |
| 1276 |
.sp |
| 1277 |
.CS |
| 1278 |
#include <tcl.h> |
| 1279 |
#include <blt.h> |
| 1280 |
... |
| 1281 |
Blt_Vector *vecPtr; |
| 1282 |
double *newArr; |
| 1283 |
FILE *f; |
| 1284 |
struct stat statBuf; |
| 1285 |
int numBytes, numValues; |
| 1286 |
|
| 1287 |
f = fopen("binary.dat", "r"); |
| 1288 |
fstat(fileno(f), &statBuf); |
| 1289 |
numBytes = (int)statBuf.st_size; |
| 1290 |
|
| 1291 |
/* Allocate an array big enough to hold all the data */ |
| 1292 |
newArr = (double *)malloc(numBytes); |
| 1293 |
numValues = numBytes / sizeof(double); |
| 1294 |
fread((void *)newArr, numValues, sizeof(double), f); |
| 1295 |
fclose(f); |
| 1296 |
|
| 1297 |
if (Blt_VectorExists(interp, "data")) { |
| 1298 |
if (Blt_GetVector(interp, "data", &vecPtr) != TCL_OK) { |
| 1299 |
return TCL_ERROR; |
| 1300 |
} |
| 1301 |
} else { |
| 1302 |
if (Blt_CreateVector(interp, "data", 0, &vecPtr) != TCL_OK) { |
| 1303 |
return TCL_ERROR; |
| 1304 |
} |
| 1305 |
} |
| 1306 |
/* |
| 1307 |
* Reset the vector. Clients will be notified when Tk is idle. |
| 1308 |
* TCL_DYNAMIC tells the vector to free the memory allocated |
| 1309 |
* if it needs to reallocate or destroy the vector. |
| 1310 |
*/ |
| 1311 |
if (Blt_ResetVector(vecPtr, newArr, numValues, numValues, |
| 1312 |
TCL_DYNAMIC) != TCL_OK) { |
| 1313 |
return TCL_ERROR; |
| 1314 |
} |
| 1315 |
.CE |
| 1316 |
.SH "INCOMPATIBILITIES" |
| 1317 |
In previous versions, if the array variable isn't global |
| 1318 |
(i.e. local to a Tcl procedure), the vector is automatically |
| 1319 |
destroyed when the procedure returns. |
| 1320 |
.CS |
| 1321 |
proc doit {} { |
| 1322 |
# Temporary vector x |
| 1323 |
vector x(10) |
| 1324 |
set x(9) 2.0 |
| 1325 |
... |
| 1326 |
} |
| 1327 |
.CE |
| 1328 |
.PP |
| 1329 |
This has changed. Variables are not automatically destroyed when |
| 1330 |
their variable is unset. You can restore the old behavior by |
| 1331 |
setting the "-watchunset" switch. |
| 1332 |
.CE |
| 1333 |
.SH KEYWORDS |
| 1334 |
vector, graph, widget |