VectorLibSite Index:OptiVec homeMatrixLib CMATH Download Order Update Support |
VectorLib4. VectorLib Functions and Routines: A Short Overview
4.1 Generation, Initialization and De-Allocation of VectorsWith VectorLib, you may use static arrays (like, for example, float a[100];) as well as dynamically allocated ones (see chapter 2.3). We recommend, however, that you use the more flexible vector types defined by VectorLib, using dynamic allocation. The following functions manage dynamically allocated vectors:
You should always take proper care to de-allocate the memory of vectors which are no longer needed. Internally, the allocated vectors are written into a table to keep track of the allocated memory. If you try to free a vector that has never been or is no longer allocated, you get a warning message, and nothing is freed. Performance tips:
The following functions are used to initialize or re-initialize vectors that have already been created:
The following functions are used to access and modify single vector elements:
The following functions generate windows for use in spectral analysis:
Complex vectors may be initialized by these functions:
Back to VectorLib Table of Contents OptiVec home 4.2 Index-oriented Manipulations
Operations performed only on a sampled sub-set of elements of a vector are provided by the VF_subvector_... family, where the omission mark stands for a suffix denoting the desired operation:
Searching tables for specific values is accomplished by:
Interpolations are performed by:
Back to VectorLib Table of Contents OptiVec home 4.3 Data-Type InterconversionsThe first thing that has to be said about the floating-point data-type interconversions is: do not use them too extensively. Decide which accuracy is appropriate for your application, and then use consistently either the VF_, or the VD_, or the VE_ version of the functions you need. Nevertheless, every data type can be converted into every other, in case it is necessary. Only a few examples are given; the rest should be obvious:
The conversion of floating-point numbers into integers is performed by the following functions, differing in the way a possible fractional part is treated:
These operations are treated as mathematical functions and are further described in chapter 4.6.1. Back to VectorLib Table of Contents OptiVec home 4.4 More about Integer ArithmeticsAlthough the rules of integer arithmetics are quite straightforward, it appears appropriate to recall that all integer operations are implicitly performed modulo 2n, where n is the number of bits the numbers are represented with. This means that any result, falling outside the range of the respective data type, is made to fall inside the range by loosing the highest bits. The effect is the same as if as many times 2n had been added to (or subtracted from) the "correct" result as necessary to reach the legal range.For example, in the data type short / SmallInt, the result of the multiplication 5 * 20000 is -31072. The reason for this seemingly wrong negative result is that the "correct" result, 100000, falls outside the range of short numbers which is -32768 <= x <= +32767. short / SmallInt is a 16-bit type, so n = 16, and 2n = 65536. In order to make the result fall into the specified range, the processor "subtracts" 2 * 65536 = 131072 from 100000, yielding -31072. Note that overflowing intermediate results cannot be "cured" by any following operation. For example, (5 * 20000) / 4 is not (as one might hope) 25000, but rather -7768. Back to VectorLib Table of Contents OptiVec home 4.5 Basic Functions of Complex VectorsThe following functions are available for the basic treatment of cartesian complex vectors:
The corresponding functions for polar coordinates are:
Back to VectorLib Table of Contents OptiVec home 4.6 Mathematical FunctionsLacking a more well-founded definition, we denote as "mathematical" all those functions which calculate each single element of a vector from the corresponding element of another vector by a more or less simple mathematical formula:Yi = f( Xi ). Except for the "basic arithmetics" functions, they are defined only for the floating-point data types. Most of these mathematical functions are vectorized versions of scalar ANSI C or Pascal functions or derived from them. In C/C++, errors are handled by _matherr and _matherrl. In Pascal/Delphi, OptiVec allows the user to control error handling by means of the function V_setFPErrorHandling. In addition to this error handling "by element", the return values of the VectorLib math functions show if all elements have been processed successfully. In C/C++, the return value is of the data-type int, in Pascal/Delphi, it is IntBool. (We do not yet use the newly introduced data type bool for this return value in C/C++, in order to make VectorLib compatible also with older versions of C compilers.) If a math function worked error-free, the return value is FALSE (0), otherwise it is TRUE (any non-zero number). Back to VectorLib Table of Contents OptiVec home 4.6.1 RoundingSome of the functions converting floating-point into integer vectors have already been noted above. The result of these rounding operations may either be left in the original floating-point format, or it may be converted into one of the integer types. The following functions store the result in the original floating-point format:
The following functions store the result as integers (type int):
The target type may also be any of the other integer data types. A few examples should suffice:
Back to VectorLib Table of Contents OptiVec home 4.6.2 ComparisonsFunctions performing comparisons are generally named VF_cmp... (where further letters and/or numbers specify the type of comparison desired). Every element of a vector can be compared either to 0, or to a constant C, or to the corresponding element of another vector. There are two possibilities: either the comparison is performed with the three possible answers "greater than", "equal to" or "less than". In this case, the results are stored as floating-point numbers (0.0, 1.0, or -1.0). Examples are:
The other possibility is to test if one of the following conditions is fulfilled: "greater than", "greater than or equal to", "equal to", "not equal to", "less than", or "less than or equal to". Here, the answers will be "TRUE" or "FALSE" (1.0 or 0.0). Examples are
Alternatively, the indices of the elements for which the answer was "TRUE" may be stored in an index-array, as in:
While the basic comparison functions check against one boundary, there is a number of functions checking if a vector elements falls into a certain range:
The following functions test if one or more values can be found in a vector:
Back to VectorLib Table of Contents OptiVec home 4.6.3 Direct Bit-ManipulationFor the integer data types, a number of bit-wise operations is available, which can be used, e.g., for fast multiplication and divisions by integer powers of 2.
4.6.4 Basic Arithmetics, AccumulationsAs before, only the VF_ function is explicitly named, but the VD_ and VE_ functions exist as well; if it makes sense, the same is true for the complex and for the integer-type versions:
Besides these basic operations, several frequently-used combinations of addition and division have been included, not to forget the Pythagoras formula:
All functions in the right column of the above two sections also exist in an expanded form (with the prefix VFx_...) in which the function is not evaluated for Xi itself, but for the expression (a * Xi + b), e.g.
The simple algebraic functions exist also in yet another special form, with the result being scaled by some arbitrary factor. This scaled form gets the prefix VFs_:
Other simple operations include:
While, in general, all OptiVec functions are for input and output vectors of the same type, the arithmetic functions exist also for mixed-type operations between the floating-point and the integer types. The result is always stored in the floating-point type. Examples are:
Similarly, there exists a family of functions for the accumulation of data in either the same type or in higher-precision data types. Some examples are:
Additionally, within the floating-point data-types, you can accumulate two vectors at once:
Again only within the floating-point data-types, you can also accumulate squares and products:
Back to VectorLib Table of Contents OptiVec home 4.6.5 Geometrical Vector ArithmeticsIn its geometrical interpretation, a vector is a pointer, with its elements representing the coordinates of a point in n-dimensional space. There are a few functions for geometrical vector arithmetics, namely
If, on the other hand, two real input vectors X and Y, or one complex input vector XY, define the coordinates of several points in a planar coordinate system, there is a function to rotate these coordinates:
Back to VectorLib Table of Contents OptiVec home 4.6.6 PowersThe following functions raise arbitrary numbers to specified powers. The extra-fast "unprotected versions" can be employed in situations where you are absolutely sure that all input elements yield valid results. Due to the much more efficient vectorization permitted by the absence of error checks, the unprotected functions are up to 1.8 times as fast as the protected versions. (This is true from the Pentium CPU on; on older computers, almost nothing is gained.) Be, however, aware of the price you have to pay for this increase in speed: in case of an overflow error, the program will crash without any warning.
The following group of functions is used to raise specified numbers to arbitrary powers:
All of these functions exist also in the expanded "VFx_" form, like VFx_square: Yi = (a * Xi + b)² The expanded form of the unprotected functions has the prefix VFux_. The complex-number equivalents are available as well, both for cartesian and polar coordinates. Additionally, two special cases are covered:
Back to VectorLib Table of Contents OptiVec home 4.6.7 Exponentials and Hyperbolic FunctionsA variety of functions are derived from the exponential function VF_exp (which itself has already been mentioned in the last section).
The vectorized hyperbolic functions are available as:
Back to VectorLib Table of Contents OptiVec home 4.6.8 Logarithms
Again, the cartesian-complex equivalents exist as well. The polar-complex versions, however, are special in that their output is always in cartesian coordinates:
As a special form of the decadic logarithm, the Optical Density is made available by a family of functions of which some examples are contained in the following table:
Back to VectorLib Table of Contents OptiVec home 4.6.9 Trigonometric FunctionsThe basic trigonometric functions are available in two variants. The first variant follows the usual rules of error handling for math functions, whereas the second is for situations where you know beforehand that all input arguments will be in the range -2p <= Xi <= +2p. If you choose to employ these extra-fast reduced-range functions, you really have to be absolutely sure about your input vectors, as these functions will crash without warning in the case of any input number outside the range specified above. The reduced-range functions are available only for the sine and cosine, as all other trigonometric functions need error checking and handling anyway, even in this range.
The following functions yield the squares of the trigonometric functions in a more efficient way than by first calculating the basic functions and squaring afterwards:
A very efficient way to calculate the trigonometric functions for arguments representable as rational multiples of p (PI) is supplied by the trigonometric functions with the suffix "rpi" (meaning "rational multiple of p"). Here, r = p / q, where q is constant and p is given by the input vector elements:
Even more efficient versions use tables to obtain frequently-used values; these versions are denoted by the suffixes "rpi2" (multiples of p divided by an integer power of 2) and "rpi3" (multiples of p over an integer multiple of 3). Examples are:
Two more special trigonometric functions are:
Vectorized inverse trigonometric functions are available as
Back to VectorLib Table of Contents OptiVec home 4.7 AnalysisThere is a number of functions probing the analytical properties of data arrays:
The complex equivalents of the last group of functions are:
Sums, products, etc. are available by functions grouped as statistical building blocks and summarized in chapter 4.9. To determine the center of gravity of a vector, you have the choice between the following two functions:
Back to VectorLib Table of Contents OptiVec home 4.8 Signal Processing:
The following list of functions is available for signal processing applications:
|
VF_FFTtoC | forward Fast Fourier Transform (FFT) of a real vector; the result is a cartesian complex vector |
VF_FFT | forward and backward FFT of a real vector; the result of the forward FFT is packed into a real vector of the same size as the input vector |
VCF_FFT | forward and backward FFT of a complex vector |
MF_Rows_FFT | FFT along the rows of a matrix; this function may be used for batch-processing of several vectors of identical size, stored as the rows of a matrix |
MF_Cols_FFT | FFT along the columns of a matrix; this function may be used for batch-processing of several vectors of identical size, stored as the columns of a matrix |
VF_convolve / VF_convolvewEdit | convolution with a given response function |
VF_deconvolve / VF_deconvolvewEdit | deconvolution, assuming a given response function |
VF_filter | spectral filtering |
VF_spectrum | spectral analysis |
VF_xspectrum | cross-spectral density of two signals (complex one-sided variant) |
VF_xspectrumAbs | cross-spectral density of two signals (absolute values) |
VF_coherence | coherence function between two signals |
VF_autocorr | autocorrelation function of a signal |
VF_xcorr | cross-correlation function of two signals |
VF_setRspEdit | set default editing threshold for the filter in convolutions and deconvolutions (decides over the treatment of "lost" frequencies) |
VF_getRspEdit | retrieve the current default editing threshold |
Although they do not use Fourier transform methods, the functions VF_biquad (bi-quadratic audio filtering) and VF_smooth (crude form of frequency filtering which removes high-frequency noise) should be mentioned here.
Back to VectorLib Table of Contents OptiVec home
VF_sum | sum of all elements |
VI_fsum | sum of all elements of an integer vector, accumulated as a floating point number in double or extended precision |
VF_prod | product of all elements |
VF_ssq | sum-of-squares of all elements |
VF_sumabs | sum of absolute values of all elements |
VF_rms | root-of-the-mean-square of all elements |
VF_runsum | running sum |
VF_runprod | running product |
VF_sumdevC | sum over the deviations from a preset constant, sum( |Xi-C| ) |
VF_sumdevV | sum over the deviations from another vector, sum( |Xi-Yi| ) |
VF_sumdevVwSaturation | sum over the deviations from another vector, sum( |Xi-Yi| ) with saturation of possible overflow to HUGE_VAL |
VF_subV_sumabs | difference between two vectors and sum over the absolute values of the results |
VF_avdevC | average deviation from a preset constant, 1/N * sum( |Xi-C| ) |
VF_avdevV | average deviation from another vector, 1 / N * sum( |Xi-Yi| ) |
VF_ssqdevC | sum-of-squares of the deviations from a preset constant, sum( (Xi - C)² ) |
VF_ssqdevV | sum-of-squares of the deviations from another vector, sum( (Xi - Yi)² ) |
VF_ssqdevVwSaturation | sum-of-squares of the deviations from another vector with saturation of possible overflow to HUGE_VAL |
VF_subV_ssq | difference between two vectors and sum-of-squares over the results |
VF_chi2 | chi-square merit function |
VF_chi2wSaturation | chi-square merit function with saturation of possible overflow to HUGE_VAL |
VF_subV_chi2 | difference between two vectors and chi-square merit function |
VF_chiabs | "robust" merit function, similar to VF_chi2, but based on absolute instead of squared deviations |
VF_chiabswSaturation | The same as VF_chiabs, but with saturation of possible overflow to HUGE_VAL |
VF_subV_chiabs | difference between two vectors and chiabs merit function |
VF_mean | equally-weighted mean (or average) of all elements |
VF_meanwW | "mean with weights" of all elements |
VF_meanabs | equally-weighted mean (or average) of the absolute values of all elements |
VF_selected_mean | averages only those vector elements which fall into a specified range, thus allowing to exclude outlier points from the calculation of the mean |
VF_varianceC | variance of a distribution with respect to a preset constant value |
VF_varianceCwW | the same with non-equal weighting |
VF_varianceV | variance of one distribution with respect to another |
VF_varianceVwW | the same with non-equal weighting |
VF_meanvar | mean and variance of a distribution simultaneously |
VF_meanvarwW | the same with non-equal weighting |
VF_median | median of a distribution |
VF_corrcoeff | linear correlation coefficient of two distributions |
VF_distribution | bins data into a discrete one-dimensional distribution function |
VF_min_max_mean_stddev | simultaneous calculation of the minimum, maximum, mean, and standard deviation of a one-dimensional distribution |
Back to VectorLib Table of Contents OptiVec home
A detailed description of the various data-fitting concepts is given elsewhere. Therefore, at this place, the available X-Y fitting functions are only summarized in the following table:
VF_linregress | equally-weighted linear regression on X-Y data |
VF_linregresswW | the same with non-equal weighting |
VF_polyfit | fitting of one X-Y data set to a polynomial |
VF_polyfitwW | the same for non-equal data-point weighting |
VF_linfit | fitting of one X-Y data set to an arbitrary function linear in its parameters |
VF_linfitwW | the same for non-equal data-point weighting |
VF_setLinfitNeglect | set threshold to neglect (i.e. set equal to zero) a fitting parameter A[i], if its significance is smaller than the threshold |
VF_getLinfitNeglect | retrieve current significance threshold |
VF_nonlinfit | fitting of one X-Y data set to an arbitrary, possibly non-linear function |
VF_nonlinfitwW | the same for non-equal data-point weighting |
VF_multiLinfit | fitting of multiple X-Y data sets to one common linear function |
VF_multiLinfitwW | the same for non-equal data-point weighting |
VF_multiNonlinfit | fitting of multiple X-Y data sets to one common nonlinear function |
VF_multiNonlinfitwW | the same for non-equal data-point weighting |
Back to VectorLib Table of Contents OptiVec home
VF_cprint | print the elements of a vector to the screen (or "console" - hence the "c" in the name) into the current text window, automatically detecting its height and width. After printing one page, the user is prompted to continue. (Only for console applications) |
VF_print | is similar to VF_cprint in that the output is directed to the screen, but there is no automatic detection of the screen data. The symbolic constant V_consoleWindowWidth (defined in <VecLib.h> or in the unit VecLib with a default value of 150) determines the linewidth, and no division into pages is made. (Only for console applications) |
VF_fprint | print a vector to a stream. |
VF_chexprint | Similar to VF_cprint, but printed in hexadecimal format. |
VF_hexprint | Similar to VF_print, but printed in hexadecimal format. |
VF_fhexprint | Similar to VF_fprint, but printed in hexadecimal format. |
VF_write | write data in ASCII format in a stream |
VF_read | read a vector from an ASCII file |
VF_nwrite | write n vectors of the same data type as the columns of a table into a stream |
VF_nread | read the columns of a table into n vectors of the same type |
VF_store | store data in binary format |
VF_recall | retrieve data in binary format |
VF_setWriteFormat | define a certain number format |
VF_setWriteSeparate | define a separation string between successive elements, written by VF_write |
VF_setNWriteSeparate | define a separation string between the columns written by VF_nwrite |
V_setRadix | define a radix different from the standard of 10 for the whole-number variants of the V..read functions |
Back to VectorLib Table of Contents OptiVec home
V_initPlot | initialize VectorLib graphics functions. No shut-down is needed at the end, since the Windows graphics functions always remain accessible. V_initPlot automatically reserves a part of the screen for plotting operations. This part comprises about 2/3 of the screen on the right side. Above, one line is left for a heading. Below, a few lines are left empty. To change this default plotting region, call V_setPlotRegion after V_initPlot. |
V_initPrint | initialize VectorLib graphics functions and direct them to a printer. By default, one whole page is reserved for plotting. In order to change this, call V_setPlotRegion after V_initPrint. |
V_setPlotRegion | set a plotting region different from the default |
VF_xyAutoPlot | display an automatically-scaled plot of an X-Y vector pair |
VF_yAutoPlot | plot a single Y-vector, using the index as X-axis |
VF_xy2AutoPlot | plot two X-Y pairs at once, scaling the axes in such a way that both vectors fit into the same coordinate system |
VF_y2AutoPlot | the same for two Y-vectors, plotted against their indices |
VF_xyDataPlot | plot one additional set of X-Y data |
VF_yDataPlot | plot one additional Y vector over its index |
VCF_autoPlot | plot one cartesian complex vector |
VCF_2AutoPlot | plot two cartesian complex vectors simultaneously |
VCF_dataPlot | plot one additional cartesian complex vector |
V_continuePlot | go back to the viewport of the last plot and restore its scalings |
V_getCoordSystem | get a copy of the scalings and position of the current coordinate system |
V_setCoordSystem | restore the scalings and position of a coordinate system; these must have been stored previously, using V_getCoordSystem |
Continue with chapter 5. Error Handling
Back to VectorLib Table of Contents
OptiVec home
Copyright © 1996-2022 OptiCode - Dr. Martin Sander Software Development