00001 /* 00002 COSTA: Problem solving environment for data assimilation 00003 Copyright (C) 2005 Nils van Velzen 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Lesser General Public 00007 License as published by the Free Software Foundation; either 00008 version 2.1 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Lesser General Public License for more details. 00014 00015 You should have received a copy of the GNU Lesser General Public 00016 License along with this library; if not, write to the Free Software 00017 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00018 */ 00019 00020 /** 00021 \file cta_vector.h 00022 \brief Interface description of the default COSTA vector componennt. For user implementation see cta_usr_vector.h. 00023 00024 Basic functions and utilities for vector operations on CTA_VECTOR objects. 00025 */ 00026 00027 #ifndef CTA_VECTOR_H 00028 #define CTA_VECTOR_H 00029 #include "cta_system.h" 00030 #include "cta_handles.h" 00031 #include "cta_datatypes.h" 00032 #include "cta_functions.h" 00033 00034 /* Function Handle */ 00035 typedef CTA_Handle CTA_Vector; 00036 typedef CTA_Handle CTA_VecClass; 00037 00038 /* parameters for different functions */ 00039 #define CTA_VECTOR_CREATE_SIZE ( 1) 00040 #define CTA_VECTOR_CREATE_INIT ( 2) 00041 #define CTA_VECTOR_GETVALS ( 3) 00042 #define CTA_VECTOR_SETVALS ( 4) 00043 #define CTA_VECTOR_SETCONST ( 5) 00044 #define CTA_VECTOR_SCAL ( 6) 00045 #define CTA_VECTOR_COPY ( 7) 00046 #define CTA_VECTOR_AXPY ( 8) 00047 #define CTA_VECTOR_DOT ( 9) 00048 #define CTA_VECTOR_NRM2 (10) 00049 #define CTA_VECTOR_AMAX (11) 00050 #define CTA_VECTOR_GETMAXLEN (12) 00051 #define CTA_VECTOR_FREE (13) 00052 #define CTA_VECTOR_GETVAL (14) 00053 #define CTA_VECTOR_SETVAL (15) 00054 #define CTA_VECTOR_EXPORT (16) 00055 #define CTA_VECTOR_PRINT_TABLE (17) 00056 #define CTA_VECTOR_APPENDVAL (18) 00057 #define CTA_VECTOR_ELMDIV (19) 00058 #define CTA_VECTOR_IMPORT (20) 00059 #define CTA_VECTOR_ELMPROD (21) 00060 #define CTA_VECTOR_ELMSQRT (22) 00061 #define CTA_VECTOR_NUMFUNC (23) 00062 00063 // CTA_Vector_Create Create a new COSTA-vector: 00064 // CTA_Vector_DefineClass ... 00065 00066 /* function interfaces */ 00067 #ifdef __cplusplus 00068 extern "C" { 00069 #endif 00070 /** \brief Create a new class (=implementation) of a COSTA vector component. 00071 * 00072 * \param name I name of the new vector class 00073 * \param h_func I COSTA function handles for functions that implement class. 00074 * Missing functions must have value CTA_NULL 00075 * \param hveccl O receives handle of new vector class 00076 * \return error status: CTA_OK if successful 00077 */ 00078 CTAEXPORT int CTA_Vector_DefineClass( 00079 const char *name, 00080 const CTA_Func h_func[CTA_VECTOR_NUMFUNC], 00081 CTA_VecClass *hveccl 00082 ); 00083 00084 /** \brief Duplicate a vector object. 00085 * 00086 * \note Only size, data type and type (class) are duplicated, the content is not 00087 * copied. 00088 * 00089 * \param hvector1 I handle of vector to be duplicated 00090 * \param hvector2 O receives handle of new duplicate vector, empty before calling 00091 * \return error status: CTA_OK if successful 00092 */ 00093 CTAEXPORT int CTA_Vector_Duplicate(CTA_Vector hvector1, CTA_Vector *hvector2); 00094 00095 /** \brief Create a new vector. 00096 * 00097 * \note 00098 * 00099 * \param hveccl I vector class of new vector 00100 * \param n I number of elements 00101 * \param datatype I data type of elements in vector 00102 * \param userdata IO user data for creation (depends on class) 00103 * \param hvector O receives handle of new vector 00104 * \return error status: CTA_OK if successful 00105 */ 00106 CTAEXPORT int CTA_Vector_Create(CTA_VecClass hveccl, const int n, CTA_Datatype datatype, 00107 CTA_Handle userdata, CTA_Vector *hvector); 00108 00109 /** \brief Get size of vector. 00110 * 00111 * \param hvec I handle of vector 00112 * \param n O receives number of elements 00113 * \return error status: CTA_OK if successful 00114 */ 00115 CTAEXPORT int CTA_Vector_GetSize(CTA_Vector hvec, int *n); 00116 00117 /** \brief Get a copy of a single element in the vector. 00118 * 00119 * \param hvec I handle of vector 00120 * \param i I index of element 00121 * \param vals O receives copy of value in vector, must exist before calling 00122 * \param datatype I data type of *vals, must be the same as data type of elements in vector 00123 * \return error status: CTA_OK if successful 00124 */ 00125 CTAEXPORT int CTA_Vector_GetVal(CTA_Vector hvec, int i, void *vals, 00126 CTA_Datatype datatype); 00127 00128 /** \brief Get a copy of all elements in the vector. 00129 * 00130 * \param hvec I handle of vector 00131 * \param vals O receives copy of all elements in vector, must exist before calling 00132 * \param n I length of array vals 00133 * \param datatype I data type of *vals, must be the same as data type of elements in vector 00134 * \return error status: CTA_OK if successful 00135 */ 00136 CTAEXPORT int CTA_Vector_GetVals(CTA_Vector hvec, void *vals, int n, 00137 CTA_Datatype datatype); 00138 00139 /** \brief Set a copy of an element in the vector. 00140 * 00141 * \note The value is copied in the vector. 00142 * 00143 * \param hvec IO handle of vector 00144 * \param i I index of element 00145 * \param val I new value that is copied into element at given index 00146 * \param datatype I data type of *val, must be the same as data type of elements in vector 00147 * \return error status: CTA_OK if successful 00148 */ 00149 CTAEXPORT int CTA_Vector_SetVal(CTA_Vector hvec, int i, void *val, 00150 CTA_Datatype datatype); 00151 00152 /** \brief Append a copy of an element to a vector. 00153 * 00154 * \note The value is copied into the vector, size of vector is increased. 00155 * 00156 * \param hvec IO handle of vector 00157 * \param val I value that needs to be added to vector 00158 * \param datatype I data type of *val, must be the same as data type of elements in vector 00159 * \return error status: CTA_OK if successful 00160 */ 00161 CTAEXPORT int CTA_Vector_AppendVal(CTA_Vector hvec, const void *val, CTA_Datatype datatype); 00162 00163 /** \brief Set all elementes in the vector. 00164 * 00165 * \note The values are copied in the vector. 00166 * 00167 * \param hvec IO handle of vector 00168 * \param vals I values that need to be copied to vector 00169 * \param n I number of elements in vals 00170 * \param datatype I data type of *vals, must be the same as data type of elements in vector 00171 * \return error status: CTA_OK if successful 00172 */ 00173 CTAEXPORT int CTA_Vector_SetVals(CTA_Vector hvec, void *vals, int n, 00174 CTA_Datatype datatype); 00175 00176 /** \brief Scale a vector. 00177 * 00178 * \note scale: x=alpha*x 00179 * 00180 * \param hvec IO handle of vector 00181 * \param alpha I scalar 00182 * \return error status: CTA_OK if successful 00183 */ 00184 CTAEXPORT int CTA_Vector_Scal(CTA_Vector hvec, double alpha); 00185 00186 /** \brief Copy a vector 00187 * 00188 * \note This function copies the vector content (y=x), but does not make a new vector. 00189 * 00190 * \param hvec_x I handle of sending vector 00191 * \param hvec_y O handle of receiving vector 00192 * \return error status: CTA_OK if successful 00193 */ 00194 CTAEXPORT int CTA_Vector_Copy(CTA_Vector hvec_x, CTA_Vector hvec_y); 00195 00196 /** \brief Operation axpy for two vectors x and y. 00197 * 00198 * \note axpy: y=alpha*x+y 00199 * 00200 * \param hvec_y IO handle of vector y 00201 * \param alpha I scalar 00202 * \param hvec_x I handle of vector x 00203 * \return error status: CTA_OK if successful 00204 */ 00205 CTAEXPORT int CTA_Vector_Axpy(CTA_Vector hvec_y, double alpha, CTA_Vector hvec_x); 00206 00207 /** \brief Dot product operation of two vectors. 00208 * 00209 * \note dot: dotprod=x^t*y 00210 * 00211 * \param hvec_x I handle of vector x 00212 * \param hvec_y I handle of vector y 00213 * \param dotprod O receives dot product 00214 * \return error status: CTA_OK if successful 00215 */ 00216 CTAEXPORT int CTA_Vector_Dot(CTA_Vector hvec_x, CTA_Vector hvec_y, double *dotprod); 00217 00218 /** \brief Compute 2-norm of vector. 00219 * 00220 * \note 2-norm: sqrt(x^t*x) 00221 * 00222 * \param hvec_x I handle of vector x 00223 * \param norm2 O receives 2-norm 00224 * \return error status: CTA_OK if successful 00225 */ 00226 CTAEXPORT int CTA_Vector_Nrm2(CTA_Vector hvec_x, double *norm2); 00227 00228 /** \brief Find index of element in vector with largest absolute value 00229 * 00230 * \param hvec_x I handle of vector 00231 * \param iloc O receives index of largest absolute value 00232 * \return error status: CTA_OK if successful 00233 */ 00234 CTAEXPORT int CTA_Vector_Amax(CTA_Vector hvec_x, int *iloc); 00235 00236 /** \brief Find largest length of elements in vector. 00237 * 00238 * \note e.g. length of string in a vector of strings 00239 * 00240 * \param hvec_x I handle of vector 00241 * \param maxlen O receives largest length of elements in vector 00242 * \return error status: CTA_OK if successful 00243 */ 00244 CTAEXPORT int CTA_Vector_GetMaxLen( 00245 CTA_Vector hvec_x, /* Handle of the first vector */ 00246 int *maxlen 00247 ); 00248 00249 /** \brief Export a vector to file, stdout or pack object. 00250 * 00251 * \Note CTA_DEFAULT_VECTOR supports exporting to:\n 00252 * file (usrdata a handle of COSTA file)\n 00253 * pack object (usrdata a handle of COSTA pack object)\n 00254 * 00255 * \param hvec I handle of vector 00256 * \param usrdata I configuration of output 00257 * \return error status: CTA_OK if successful 00258 */ 00259 CTAEXPORT int CTA_Vector_Export( 00260 CTA_Vector hvec, /* Handle of the vector */ 00261 CTA_Handle usrdata 00262 ); 00263 00264 /** \brief Import a vector. 00265 * 00266 * \note CTA_DEFAULT_VECTOR supports importing from pack object (usrdata[0] a pack handle). 00267 * \note Data type and size of vector can be changed due to this action 00268 * 00269 * \param hvec I handle of vector 00270 * \param usrdata I configuration of output 00271 * \return error status: CTA_OK if successful 00272 */ 00273 CTAEXPORT int CTA_Vector_Import( CTA_Vector hvec, CTA_Handle usrdata); 00274 00275 00276 /** \brief Print table, each column built up by a vector. 00277 * 00278 * \note CTA_DEFAULT_VECTOR TODO 00279 * 00280 * \param table I array of vector handles, these vectors form the table to be filled 00281 * \param ncolumns I number of columns in table 00282 * (number of vector handles in table) 00283 * \param vformats I handle of vector of string (size ncolumns) containing the 00284 * formats for printing each column (C-format) 00285 * \return error status: CTA_OK if successful 00286 */ 00287 CTAEXPORT int CTA_Vector_Print_Table(CTA_Vector* table, int ncolumns, 00288 CTA_Vector vformats); 00289 00290 /** \brief Free the vector object. 00291 * 00292 * 00293 * \Note hvec_x=CTA_NULL is allowed 00294 * 00295 * \param hvec_x IO handle of vector, replaced by CTA_NULL on return 00296 * \return error status: CTA_OK if successful 00297 */ 00298 CTAEXPORT int CTA_Vector_Free(CTA_Vector *hvec_x); 00299 00300 00301 /** \brief Get type of vector. 00302 * 00303 * \param hvec I handle of vector 00304 * \param datatype O receives data type of vector 00305 * \return error status: CTA_OK if successful 00306 */ 00307 CTAEXPORT int CTA_Vector_GetDatatype( 00308 CTA_Vector hvec, /* Handle of the vector */ 00309 CTA_Datatype *datatype /* Returned data type */ 00310 ); 00311 00312 /** \brief Set whole vector to one single value. 00313 * 00314 * \Note hvec=CTA_NULL is allowed, function returns error code 00315 * 00316 * \param hvec IO handle of vector 00317 * \param val I value that must be set 00318 * \param datatype I data type of *val, must be the same as data type of elements in vector 00319 * \return error status: CTA_OK if successful 00320 */ 00321 CTAEXPORT int CTA_Vector_SetConstant( 00322 CTA_Vector hvec, /* Handle of the vector */ 00323 void *val, /* value that must be set */ 00324 CTA_Datatype datatype /* Data type */ 00325 ); 00326 00327 /** \brief Element wise division 00328 * 00329 * \note y=x./y 00330 * 00331 * \param hvec_y IO handle of vector y 00332 * \param hvec_x I handle of vector x 00333 * \return error status: CTA_OK if successful 00334 */ 00335 CTAEXPORT int CTA_Vector_ElmDiv(CTA_Vector hvec_y, CTA_Vector hvec_x); 00336 00337 00338 /** \brief Element wise product 00339 * 00340 * \note y=x.y 00341 * 00342 * \param hvec_y IO handle of vector y 00343 * \param hvec_x I handle of vector x 00344 * \return error status: CTA_OK if successful 00345 */ 00346 CTAEXPORT int CTA_Vector_ElmProd(CTA_Vector hvec_y, CTA_Vector hvec_x); 00347 00348 /** \brief Element wise square root 00349 * 00350 * \note y=y.^0.5 00351 * 00352 * \param hvec_y IO handle of vector y 00353 * \return error status: CTA_OK if successful 00354 */ 00355 CTAEXPORT int CTA_Vector_ElmSqrt(CTA_Vector hvec_x); 00356 00357 00358 00359 00360 #ifdef __cplusplus 00361 } 00362 #endif 00363 #endif