00001 /** 00002 \file cmatrix.h 00003 \brief 'c' functions for vector and matrix operations. 00004 \author Glenn D. MacGougan (GDM) 00005 \date 2007-12-21 00006 \version 1.11 00007 00008 \b LICENSE \b INFORMATION \n 00009 Copyright (c) 2007, Glenn D. MacGougan, Zenautics Technologies Inc. \n 00010 00011 Redistribution pertains only to the following files and their contents. \n 00012 - Matrix.h\n 00013 - Matrix.cpp\n 00014 - cmatrix.h\n 00015 - cmatrix_basic.lib (for windows), cmatrix_basic_lib.a (for linux)\n 00016 00017 Redistribution and use in source and binary forms, with or without 00018 modification, of the specified files is permitted provided the following 00019 conditions are met: \n 00020 00021 - Redistributions of source code must retain the above copyright 00022 notice, this list of conditions and the following disclaimer. \n 00023 - Redistributions in binary form must reproduce the above copyright 00024 notice, this list of conditions and the following disclaimer in the 00025 documentation and/or other materials provided with the distribution. \n 00026 - The name(s) of the contributor(s) may not be used to endorse or promote 00027 products derived from this software without specific prior written 00028 permission. \n 00029 00030 THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS 00031 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00032 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00033 DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 00034 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00035 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00036 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00037 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00038 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00039 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00040 SUCH DAMAGE. 00041 00042 \b NOTES: \n 00043 This code was developed using rigourous unit testing for every function 00044 and operation. Despite any rigorous development process, bugs are 00045 inevitable. Please report bugs and suggested fixes to glenn@zenautics.com.\n 00046 */ 00047 00048 #ifndef ZENUATICS_MTX_H 00049 #define ZENUATICS_MTX_H 00050 00051 #ifdef __cplusplus 00052 extern "C" 00053 { 00054 #endif 00055 00056 00057 typedef int BOOL; 00058 00059 #ifndef FALSE 00060 #define FALSE (0) 00061 #endif 00062 #ifndef TRUE 00063 #define TRUE (1) 00064 #endif 00065 00066 /// \brief A complex data struct. 00067 typedef struct 00068 { 00069 double re; //!< The real part. 00070 double im; //!< The imaginary part. 00071 } stComplex; 00072 00073 /// \brief The deep level matrix struct. The matrix is either real or complex. 00074 typedef struct 00075 { 00076 unsigned nrows; //!< The number of rows in the matrix. 00077 unsigned ncols; //!< The number of columns in the matrix. 00078 BOOL isReal; //!< This indicates if is the matrix real or complex. 00079 double **data; //!< This is a pointer to an array of double column vectors. 00080 stComplex **cplx; //!< Thsi is a pointer to an array of complex column vectors. 00081 char *comment; //!< This is a comment string (if applicable). 00082 } MTX; 00083 00084 00085 /// \brief This function must be called first by users of cmatrix! 00086 /// 00087 /// \return TRUE if successful, FALSE otherwise. 00088 BOOL MTX_Initialize_MTXEngine(); 00089 00090 00091 /// \brief This function is used to set if matrices that are single 00092 /// elements (1x1) are treated as scalars for math operations 00093 /// or whether the regular matrix rules apply. THIS IS ENABLED 00094 /// BY DEFAULT. 00095 /// 00096 /// \return TRUE if successful, FALSE otherwise. 00097 BOOL MTX_Enable1x1MatricesForTreatmentAsScalars( BOOL enable ); 00098 00099 /// \brief Is this a null matrix? 00100 /// 00101 /// \return TRUE if the matrix is null, FALSE otherwise. 00102 BOOL MTX_isNull( const MTX *M ); 00103 00104 00105 /// \brief Are matrices A & B conformal for multiplication, real * real 00106 /// 00107 /// \return TRUE if successful, FALSE otherwise. 00108 BOOL MTX_isConformalForMultiplication( const MTX *A, const MTX *B ); 00109 00110 /// \brief Are matrices A & B conformat for addition/subtraction, real + real 00111 /// 00112 /// \return TRUE if successful, FALSE otherwise. 00113 BOOL MTX_isConformalForAddition( const MTX *A, const MTX *B ); 00114 00115 /// \brief Is this a square matrix? 00116 /// 00117 /// \return TRUE if successful, FALSE otherwise. 00118 BOOL MTX_isSquare( const MTX *A ); 00119 00120 /// \brief are A and B the same size? 00121 /// 00122 /// \return TRUE if successful, FALSE otherwise. 00123 BOOL MTX_isSameSize( const MTX *A, const MTX *B ); 00124 00125 /// \brief Initialize a MTX matrix struct to appropriate zero values. This must always be called for proper operation! 00126 /// \code 00127 /// MTX matrix; 00128 /// MTX_Init( &matrix ); 00129 /// \endcode 00130 /// 00131 /// \return TRUE if successful, FALSE otherwise. 00132 BOOL MTX_Init( MTX *M ); 00133 00134 00135 00136 /// \brief Set the matrix comment string 00137 /// 00138 /// \return TRUE if successful, FALSE otherwise. 00139 BOOL MTX_SetComment( MTX *M, const char *comment ); 00140 00141 /// \brief Clear the matrix data from memory if dynamically allocated. Zero the struct members. 00142 /// 00143 /// \return TRUE if successful, FALSE otherwise. 00144 BOOL MTX_Free( MTX *M ); 00145 00146 /// \brief Allocate matrix data (set to zero). 00147 /// 00148 /// \return TRUE if successful, FALSE otherwise. 00149 BOOL MTX_Calloc( MTX *M, const unsigned nrows, const unsigned ncols, const BOOL isReal ); 00150 00151 /// \brief Allocate matrix data (not set to zero). 00152 /// 00153 /// \return TRUE if successful, FALSE otherwise. 00154 BOOL MTX_Malloc( MTX *M, const unsigned nrows, const unsigned ncols, const BOOL isReal ); 00155 00156 /// \brief Set a scalar value in the matrix. 00157 /// 00158 /// \return TRUE if successful, FALSE otherwise. 00159 BOOL MTX_SetValue( MTX *M, const unsigned row, const unsigned col, const double value ); 00160 00161 /// \brief Set a complex value in the matrix. 00162 /// 00163 /// \return TRUE if successful, FALSE otherwise. 00164 BOOL MTX_SetComplexValue( MTX *M, const unsigned row, const unsigned col, const double re, const double im ); 00165 00166 /// \brief Matrix M = Re + Im*i, where Re and Im are real matrices. 00167 /// 00168 /// \return TRUE if successful, FALSE otherwise. 00169 BOOL MTX_Complex( MTX *M, const MTX *Re, const MTX *Im ); 00170 00171 /// \brief Set the specified column in Matrix M to Re + Im*i, where Re and Im are real matrices. 00172 /// The dimensions of M must already be valid. 00173 /// 00174 /// \return TRUE if successful, FALSE otherwise. 00175 BOOL MTX_SetComplexColumn( MTX *M, const unsigned col, const MTX *Re, const MTX *Im ); 00176 00177 /// \brief Convert a real matrix to a complex matrix 00178 /// 00179 /// \return TRUE if successful, FALSE otherwise. 00180 BOOL MTX_ConvertRealToComplex( MTX *M ); 00181 00182 /// \brief Convert a complex marix to a real matrix using only the imaginary component A = real(B). 00183 /// 00184 /// \return TRUE if successful, FALSE otherwise. 00185 BOOL MTX_ConvertComplexToReal( MTX *M ); 00186 00187 /// \brief Convert a complex marix to a real matrix using only the imaginary component A = imag(B). 00188 /// 00189 /// \return TRUE if successful, FALSE otherwise. 00190 BOOL MTX_ConvertComplexToImag( MTX *M ); 00191 00192 /// \brief Extract the real component of matrix M. 00193 /// 00194 /// \return TRUE if successful, FALSE otherwise. 00195 BOOL MTX_Real( const MTX *M, MTX *Re ); 00196 00197 /// \brief Check if the matrix contains only real values. 00198 /// Alter the matrix if it is stored as complex and only has real values. 00199 /// 00200 /// \return TRUE if successful, FALSE otherwise. 00201 BOOL MTX_isReal( MTX *M, BOOL *isReal ); 00202 00203 /// \brief Extract the real component of column col of matrix M. 00204 /// 00205 /// \return TRUE if successful, FALSE otherwise. 00206 BOOL MTX_RealColumn( const MTX *M, const unsigned col, MTX *Re ); 00207 00208 /// \brief Extract the imaginary component of matrix M. 00209 /// 00210 /// \return TRUE if successful, FALSE otherwise. 00211 BOOL MTX_Imag( const MTX *M, MTX *Im ); 00212 00213 /// \brief Extract the imaginary component of column col of matrix M. 00214 /// 00215 /// \return TRUE if successful, FALSE otherwise. 00216 BOOL MTX_ImagColumn( const MTX *M, const unsigned col, MTX *Im ); 00217 00218 /// \brief If M is a real matrix, Magnitude is a copy. 00219 /// If M is a complex matrix, Magnitude is a real matrix = sqrt( re*re + im*im ). 00220 /// 00221 /// \return TRUE if successful, FALSE otherwise. 00222 BOOL MTX_Magnitude( const MTX *M, MTX *Magnitude ); 00223 00224 /// \brief If M is a real matrix, Phase is a zero matrix. 00225 /// If M is a complex matrix, Phase is a real matrix = atan2(im,re). 00226 /// 00227 /// \return TRUE if successful, FALSE otherwise. 00228 BOOL MTX_Phase( const MTX *M, MTX *Phase ); 00229 00230 /// \brief If M is a real matrix, nothing is done. 00231 /// If M is a complex matrix, the conjugate is set. 00232 /// 00233 /// \return TRUE if successful, FALSE otherwise. 00234 BOOL MTX_Conjugate( MTX *M ); 00235 00236 /// \brief Remove a single column from the matrix. 00237 /// 00238 /// \return TRUE if successful, FALSE otherwise. 00239 BOOL MTX_RemoveColumn( MTX *M, const unsigned col ); 00240 00241 /// \brief remove all the columns 'after' the column index given. 00242 /// 00243 /// \return TRUE if successful, FALSE otherwise. 00244 BOOL MTX_RemoveColumnsAfterIndex( MTX *dst, const unsigned col ); 00245 00246 /// \brief insert a column into another matrix. 00247 /// 00248 /// \return TRUE if successful, FALSE otherwise. 00249 BOOL MTX_InsertColumn( MTX *dst, const MTX *src, const unsigned dst_col, const unsigned src_col ); 00250 00251 /// \brief Add a column to the Matrix. 00252 /// 00253 /// \return TRUE if successful, FALSE otherwise. 00254 BOOL MTX_AddColumn( MTX *dst, const MTX *src, const unsigned src_col ); 00255 00256 /// \brief Combine two matrices with the same nrows, A becomes A|B, 00257 /// 00258 /// \return TRUE if successful, FALSE otherwise. 00259 BOOL MTX_Concatonate( MTX *dst, const MTX *src ); 00260 00261 /// \brief Redimension the matrix, original data is saved in place, new data is set to zero. 00262 /// 00263 /// \return TRUE if successful, FALSE otherwise. 00264 BOOL MTX_Redim( MTX *dst, const unsigned nrows, const unsigned ncols ); 00265 00266 /// \brief Resize the matrix, original data is lost, new data is set to zero, must specify if the matrix is real or complex. 00267 /// 00268 /// \return TRUE if successful, FALSE otherwise. 00269 BOOL MTX_Resize( MTX *dst, const unsigned nrows, const unsigned ncols, const BOOL isReal ); 00270 00271 00272 00273 /// \brief Copy the src data to dst matrix, resize dst if possible & necessary. 00274 /// 00275 /// \return TRUE if successful, FALSE otherwise. 00276 BOOL MTX_Copy( const MTX *src, MTX *dst ); 00277 00278 /// \brief Copy the src matrix data [m cols x n rows] to dst vector [1 col x m*n rows], resize dst if possible & necessary. 00279 /// 00280 /// \return TRUE if successful, FALSE otherwise. 00281 BOOL MTX_CopyIntoColumnWiseVector( const MTX *src, MTX *dst ); 00282 00283 /// \brief Set the dst matrix from the static 'c' style matrix indexed by mat[i*ncols + j]. 00284 /// 00285 /// \return TRUE if successful, FALSE otherwise. 00286 BOOL MTX_SetFromStaticMatrix( MTX *dst, const double mat[], const unsigned nrows, const unsigned ncols ); 00287 00288 /// \brief Copy the src data in column col to dst matrix, resize dst if possible & necessary. 00289 /// 00290 /// \return TRUE if successful, FALSE otherwise. 00291 BOOL MTX_CopyColumn( const MTX *src, const unsigned col, MTX *dst ); 00292 00293 /// \brief Copy the src data in row, row, to dst matrix, resize dst if possible & necessary. 00294 /// 00295 /// \return TRUE if successful, FALSE otherwise. 00296 BOOL MTX_CopyRow( const MTX *src, const unsigned row, MTX *dst ); 00297 00298 /// \brief Copy the src data in row 'row' (1xn) to dst matrix (nx1), resize dst if possible & necessary. 00299 /// dst becomes (nx1). 00300 /// 00301 /// \return TRUE if successful, FALSE otherwise. 00302 BOOL MTX_CopyRowIntoAColumnMatrix( const MTX *src, const unsigned row, MTX *dst ); 00303 00304 /// \brief Insert a submatrix (src) into dst, starting at indices dst(row,col). 00305 /// 00306 /// \return TRUE if successful, FALSE otherwise. 00307 BOOL MTX_InsertSubMatrix( MTX *dst, const MTX *src, const unsigned dst_row, const unsigned dst_col ); 00308 00309 /// \brief Zero the entire matrix. 00310 /// 00311 /// \return TRUE if successful, FALSE otherwise. 00312 BOOL MTX_Zero( MTX *dst ); 00313 00314 /// \brief Zero all elements in a specified column. 00315 /// 00316 /// \return TRUE if successful, FALSE otherwise. 00317 BOOL MTX_ZeroColumn( MTX *dst, const unsigned col ); 00318 00319 /// \brief Zero all elements in a specified row. 00320 /// 00321 /// \return TRUE if successful, FALSE otherwise. 00322 BOOL MTX_ZeroRow( MTX *dst, const unsigned row ); 00323 00324 /// \brief Fill the matrix with the given value. 00325 /// 00326 /// \return TRUE if successful, FALSE otherwise. 00327 BOOL MTX_Fill( MTX *dst, const double value ); 00328 00329 /// \brief Fill the matrix with the given complex value. 00330 /// 00331 /// \return TRUE if successful, FALSE otherwise. 00332 BOOL MTX_FillComplex( MTX *dst, const double re, const double im ); 00333 00334 /// \brief Fill the matrix column with the given value. 00335 /// 00336 /// \return TRUE if successful, FALSE otherwise. 00337 BOOL MTX_FillColumn( MTX *dst, const unsigned col, const double value ); 00338 00339 /// \brief Fill the matrix column with the given complex value. 00340 /// 00341 /// \return TRUE if successful, FALSE otherwise. 00342 BOOL MTX_FillColumnComplex( MTX *dst, const unsigned col, const double re, const double im ); 00343 00344 /// \brief Fill the matrix row with the given value. 00345 /// 00346 /// \return TRUE if successful, FALSE otherwise. 00347 BOOL MTX_FillRow( MTX *dst, const unsigned row, const double value ); 00348 00349 /// \brief Fill the matrix row with the given complex value. 00350 /// 00351 /// \return TRUE if successful, FALSE otherwise. 00352 BOOL MTX_FillRowComplex( MTX *dst, const unsigned row, const double re, const double im ); 00353 00354 /// \brief Reverse the order of elements of a column. 00355 /// 00356 /// \return TRUE if successful, FALSE otherwise. 00357 BOOL MTX_FlipColumn( MTX *M, const unsigned col ); 00358 00359 /// \brief Reverse the order of elements of a row. 00360 /// 00361 /// \return TRUE if successful, FALSE otherwise. 00362 BOOL MTX_FlipRow( MTX *M, const unsigned row ); 00363 00364 /// \brief Set the matrix to an identity. 00365 /// 00366 /// \return TRUE if successful, FALSE otherwise. 00367 BOOL MTX_Identity( MTX *dst ); 00368 00369 00370 /// \brief Transpose the matrix src into the matris dst. 00371 /// 00372 /// \return TRUE if successful, FALSE otherwise. 00373 BOOL MTX_Transpose( const MTX *src, MTX *dst ); 00374 00375 /// \brief Transpose the matrix as an inplace operation. 00376 /// 00377 /// \return TRUE if successful, FALSE otherwise. 00378 BOOL MTX_TransposeInplace( MTX *M ); 00379 00380 00381 /// \brief Round the matrix elements to the specified precision.\n 00382 /// e.g. precision = 0 1.8 -> 2\n 00383 /// e.g. precision = 1, 1.45 -> 1.5\n 00384 /// e.g. precision = 2 1.456 -> 1.46\n 00385 /// e.g. precision = 3, 1.4566 -> 1.457\n 00386 /// precision has a maximum of 32. After which no rounding occurs. 00387 /// 00388 /// \return TRUE if successful, FALSE otherwise. 00389 BOOL MTX_Round( MTX *M, const unsigned precision ); 00390 00391 /// \brief Round the matrix elements to the nearest integers towards minus infinity. 00392 /// 00393 /// \return TRUE if successful, FALSE otherwise. 00394 BOOL MTX_Floor( MTX *M ); 00395 00396 /// \brief Round the matrix elements to the nearest integers towards infinity. 00397 /// 00398 /// \return TRUE if successful, FALSE otherwise. 00399 BOOL MTX_Ceil( MTX *M ); 00400 00401 /// \brief Round the matrix elements of X to the nearest integers towards zero. 00402 /// 00403 /// \return TRUE if successful, FALSE otherwise. 00404 BOOL MTX_Fix( MTX *M ); 00405 00406 00407 /// \brief Determine the matrix file delimiter and if a comment line is available. 00408 /// 00409 /// \return TRUE if successful, FALSE otherwise. 00410 BOOL MTX_DetermineFileDelimiter( 00411 const char *path, //!< path to the input file 00412 char *delimiter, //!< delimiter, 'b' is binary 00413 BOOL *hasComment, //!< BOOL to indicate if a comment line is present 00414 char **comment //!< pointer to a string to store the comment line, *comment memory must be freed later. 00415 ); 00416 00417 /// \brief Determine the size of a file. 00418 /// 00419 /// \return TRUE if successful, FALSE otherwise. 00420 BOOL MTX_DetermineFileSize( const char *path, unsigned *size ); 00421 00422 /// \brief Determine the number of columns in the data string provided. 00423 /// 00424 /// \return TRUE if successful, FALSE otherwise. 00425 BOOL MTX_DetermineNumberOfColumnsInDataString( const char *datastr, unsigned *ncols ); 00426 00427 /// \brief Determine the number of columns in the complex data string provided. 00428 /// The delimiter is needed, 'w' indicates whitespace. 00429 /// 00430 /// \return TRUE if successful, FALSE otherwise. 00431 BOOL MTX_DetermineNumberOfColumnsInDataStringCplx( const char *datastr, const char delimiter, unsigned *ncols ); 00432 00433 00434 00435 /// \brief Read a real-only matrix from a file (ASCII formatted, any common delimiters). 00436 /// This function will also read in MTX BINARY formatted files. 00437 /// 00438 /// \return TRUE if successful, FALSE otherwise. 00439 BOOL MTX_ReadFromFileRealOnly( MTX *M, const char *path ); 00440 00441 00442 /// \brief Read either a real or complex matrix from a file (ASCII formatted, any common delimiters). 00443 /// This function will also read in MTX BINARY formatted files. 00444 /// 00445 /// \return TRUE if successful, FALSE otherwise. 00446 BOOL MTX_ReadFromFile( MTX *M, const char *path ); 00447 00448 00449 /// \brief Set the matrix from a matrix string. 00450 /// 00451 /// \return TRUE if successful, FALSE otherwise. 00452 BOOL MTX_SetFromMatrixString( MTX *M, const char *strMatrix ); 00453 00454 00455 00456 /// \brief Convert a value to a string with the specified width and precision. 00457 /// analogous to sprintf( ValueBuffer, "%'blank''-'width.precision'g'", value ); 00458 /// 00459 /// \return TRUE if successful, FALSE otherwise. 00460 BOOL MTX_ValueToString( 00461 const double value, //!< The double value to output. 00462 const unsigned width, //!< The width of the field. 00463 const unsigned precision, //!< The precision, %g style. 00464 const BOOL isReal, //!< The the value the real part or the imaginary part. 00465 const BOOL alignLeft, //!< Align the output left (for real data only). 00466 char *ValueBuffer, //!< The output buffer. 00467 const unsigned ValueBufferSize //!< The size of the output buffer. 00468 ); 00469 00470 /// \brief Print the matrix to a file with specifed width and precision. 00471 /// MTX_PrintAutoWidth is recommended over this function, "%'blank''-'width.precision'g'". 00472 /// 00473 /// \return TRUE if successful, FALSE otherwise. 00474 BOOL MTX_Print( const MTX *M, const char *path, const unsigned width, const unsigned precision, const BOOL append ); 00475 00476 /// \brief Print the matrix to a buffer of maxlength with specifed width and precision. 00477 /// MTX_PrintAutoWidth is recommended over this function, "%'blank''-'width.precision'g'". 00478 /// 00479 /// \return TRUE if successful, FALSE otherwise. 00480 BOOL MTX_Print_ToBuffer( const MTX *M, char *buffer, const unsigned maxlength, const unsigned width, const unsigned precision ); 00481 00482 /// \brief Print the matrix to a file with automatically determined column width. 00483 /// and the specified precision, uses "%'blank''-'autowidth.precision'g'". 00484 /// 00485 /// \return TRUE if successful, FALSE otherwise. 00486 BOOL MTX_PrintAutoWidth( const MTX *M, const char *path, const unsigned precision, const BOOL append ); 00487 00488 /// \brief Print the matrix to stdout with automatically determined column width. 00489 /// and the specified precision, uses "%'blank''-'autowidth.precision'g'". 00490 /// 00491 /// \return TRUE if successful, FALSE otherwise. 00492 BOOL MTX_PrintStdoutAutoWidth( const MTX *M, const unsigned precision ); 00493 00494 /// \brief Print the matrix to a buffer of maxlenth with automatically determined column width. 00495 /// and the specified precision, uses "%'blank''-'autowidth.precision'g'". 00496 /// 00497 /// \return TRUE if successful, FALSE otherwise. 00498 BOOL MTX_PrintAutoWidth_ToBuffer( const MTX *M, char *buffer, const unsigned maxlength, const unsigned precision ); 00499 00500 /// \brief Print the matrix to a file with specifed precision and delimiter. 00501 /// Use MTX_PrintAutoWidth if print using whitespace as a delimiter is required, uses "%.precision'g'" 00502 /// 00503 /// \return TRUE if successful, FALSE otherwise. 00504 BOOL MTX_PrintDelimited( const MTX *M, const char *path, const unsigned precision, const char delimiter, const BOOL append ); 00505 00506 /// \brief Print the matrix to a file with specifed precision and delimiter. 00507 /// Use MTX_PrintAutoWidth if print using whitespace as a delimiter is required, uses "%.precision'g'". 00508 /// 00509 /// \return TRUE if successful, FALSE otherwise. 00510 BOOL MTX_PrintDelimited_ToBuffer( const MTX *M, char *buffer, const unsigned maxlength, const unsigned precision, const char delimiter ); 00511 00512 /// \brief Print a row to a string buffer. 00513 /// 00514 /// \return TRUE if successful, FALSE otherwise. 00515 BOOL MTX_PrintRowToString( const MTX *M, const unsigned row, char *buffer, const unsigned maxlength, const int width, const int precision ); 00516 00517 00518 //// 00519 // Math operations 00520 00521 /// \brief Adds a scalar double to matrix M, ie: M += 5. 00522 /// 00523 /// \return TRUE if successful, FALSE otherwise. 00524 BOOL MTX_Add_Scalar( MTX *M, const double scalar ); 00525 00526 /// \brief Adds a scalar complex to matrix M, ie: M += (5 + 3i). 00527 /// 00528 /// \return TRUE if successful, FALSE otherwise. 00529 BOOL MTX_Add_ScalarComplex( MTX *M, const double re, const double im ); 00530 00531 /// \brief Subtracts a scalar double from matrix M, ie: M -= 5. 00532 /// 00533 /// \return TRUE if successful, FALSE otherwise. 00534 BOOL MTX_Subtract_Scalar( MTX *M, const double scalar ); 00535 00536 /// \brief Subtracts a scaler complex from matrix M, ie: M -= (5+3i). 00537 /// 00538 /// \return TRUE if successful, FALSE otherwise. 00539 BOOL MTX_Subtract_ScalarComplex( MTX *M, const double re, const double im ); 00540 00541 /// \brief Multiply M with a double scalar inplace, ie: M *= 5. 00542 /// 00543 /// \return TRUE if successful, FALSE otherwise. 00544 BOOL MTX_Multiply_Scalar( MTX *M, const double scalar ); 00545 00546 /// \brief Multiply M with a complex scalar inplace, ie: M *= (5+3i). 00547 /// 00548 /// \return TRUE if successful, FALSE otherwise. 00549 BOOL MTX_Multiply_ScalarComplex( MTX *M, const double re, const double im ); 00550 00551 /// \brief Divide M by scaler double inplace, ie: M /= 5. 00552 /// 00553 /// \return TRUE if successful, FALSE otherwise. 00554 BOOL MTX_Divide_Scalar( MTX *M, const double scalar ); 00555 00556 /// \brief Divide M by scaler complex inplace, ie: M /= (5+3i). 00557 /// 00558 /// \return TRUE if successful, FALSE otherwise. 00559 BOOL MTX_Divide_ScalarComplex( MTX *M, const double re, const double im ); 00560 00561 00562 /// \brief Computes the absolute value of each element in the matrix. 00563 /// 00564 /// \return TRUE if successful, FALSE otherwise. 00565 BOOL MTX_Abs( MTX *M ); 00566 00567 /// \brief Compute the arc-cosine of each element of the matrix inplace. 00568 /// Complex results are obtained if elements are greater than abs(1). 00569 /// 00570 /// \return TRUE if successful, FALSE otherwise. 00571 BOOL MTX_acos( MTX *M ); 00572 00573 /// \brief Compute the phase angle in radians of the elements in the matrix. 00574 /// If all elements are real, the results are 0. If complex 00575 /// 00576 /// \return TRUE if successful, FALSE otherwise. 00577 BOOL MTX_angle( MTX *M ); 00578 00579 /// \brief Compute the arc-sine of each element of the matrix inplace. 00580 /// Complex results are obtained if elements are greater than abs(1). 00581 /// 00582 /// \return TRUE if successful, FALSE otherwise. 00583 BOOL MTX_asin( MTX *M ); 00584 00585 00586 /// \brief Computes the value^2 of each element in the matrix. 00587 /// 00588 /// \return TRUE if successful, FALSE otherwise. 00589 BOOL MTX_Sqr( MTX *M ); 00590 00591 /// \brief Computes the sqrt(value) of each element in the matrix. 00592 /// A real matrix is converted to complex if any elements are negative. 00593 /// e.g. sqrt(-1) = -i. 00594 /// 00595 /// \return TRUE if successful, FALSE otherwise. 00596 BOOL MTX_Sqrt( MTX *M ); 00597 00598 /// \brief If real, computes the exp(value) of each element in the matrix. 00599 /// If complex, computes exp(M) = exp(real)*(cos(imag)+i*sin(imag)). 00600 /// 00601 /// \return TRUE if successful, FALSE otherwise. 00602 BOOL MTX_Exp( MTX *M ); 00603 00604 /// \brief Create an indentity matrix with nrows and ncols. 00605 /// 00606 /// \return TRUE if successful, FALSE otherwise. 00607 BOOL MTX_Eye( MTX *M, const unsigned nrows, const unsigned ncols ); 00608 00609 /// \brief Computes the natural logarithm, ln(value) of each element in the matrix. 00610 /// 00611 /// \return TRUE if successful, FALSE otherwise. 00612 BOOL MTX_Ln( MTX *M ); 00613 00614 /// \brief Raise all elements in src^(power_re + power_im*i) and store in dst. 00615 /// If power is just real, power_im = 0.0. 00616 /// 00617 /// \return TRUE if successful, FALSE otherwise. 00618 BOOL MTX_Pow( const MTX *src, MTX *dst, const double power_re, const double power_im ); 00619 00620 /// \brief Raise all elements in src^(power_re + power_im*i). 00621 /// If power is just real, power_im = 0.0. 00622 /// 00623 /// \return TRUE if successful, FALSE otherwise. 00624 BOOL MTX_PowInplace( MTX *src, const double power_re, const double power_im ); 00625 00626 00627 /// \brief Computes the arctan, atan(value) of each element in the matrix 00628 /// 00629 /// \return TRUE if successful, FALSE otherwise. 00630 BOOL MTX_atan( MTX *M ); 00631 00632 /// \brief Add +1.0 to all elements, e.g. M++. 00633 /// 00634 /// \return TRUE if successful, FALSE otherwise. 00635 BOOL MTX_Increment( MTX *M ); 00636 00637 /// \brief Subtract 1.0 from all elements, e.g. M--. 00638 /// 00639 /// \return TRUE if successful, FALSE otherwise. 00640 BOOL MTX_Decrement( MTX *M ); 00641 00642 /// \brief Add A += B, inplace. 00643 /// 00644 /// \return TRUE if successful, FALSE otherwise. 00645 BOOL MTX_Add_Inplace( MTX *A, const MTX *B ); 00646 00647 /// \brief Subtract A -= B, inplace. 00648 /// 00649 /// \return TRUE if successful, FALSE otherwise. 00650 BOOL MTX_Subtract_Inplace( MTX *A, const MTX *B ); 00651 00652 /// \brief Multiply A = B*A, inplace. 00653 /// 00654 /// \return TRUE if successful, FALSE otherwise. 00655 BOOL MTX_PreMultiply_Inplace( MTX *A, const MTX *B ); // A = B*A 00656 00657 /// \brief Multiply A = A*B, inplace. 00658 /// 00659 /// \return TRUE if successful, FALSE otherwise. 00660 BOOL MTX_PostMultiply_Inplace( MTX *A, const MTX* B ); // A = A*B 00661 00662 00663 /// \brief Dot multiply A .*= B, inplace (A.data[col][row] = A.data[col][row]*B.data[col][row]). 00664 /// 00665 /// \return TRUE if successful, FALSE otherwise. 00666 BOOL MTX_DotMultiply_Inplace( MTX *A, const MTX *B ); 00667 00668 /// \brief Dot divide A ./= B, inplace (A.data[col][row] = A.data[col][row]/B.data[col][row]). 00669 /// 00670 /// \return TRUE if successful, FALSE otherwise. 00671 BOOL MTX_DotDivide_Inplace( MTX *A, const MTX *B ); 00672 00673 00674 00675 /// \brief Add A = B+C. 00676 /// 00677 /// \return TRUE if successful, FALSE otherwise. 00678 BOOL MTX_Add( MTX *A, const MTX *B, const MTX *C ); 00679 00680 /// \brief Subtract A = B-C. 00681 /// 00682 /// \return TRUE if successful, FALSE otherwise. 00683 BOOL MTX_Subtract( MTX *A, const MTX *B, const MTX *C ); 00684 00685 /// \brief Multiply A = B*C. 00686 /// 00687 /// \return TRUE if successful, FALSE otherwise. 00688 BOOL MTX_Multiply( MTX *A, const MTX *B, const MTX *C ); 00689 00690 /// \brief Rest if A == B to within the specified tolerance. 00691 /// 00692 /// \return TRUE if successful, FALSE otherwise. 00693 BOOL MTX_IsEqual( const MTX *A, const MTX *B, const double tolerance, BOOL *isEqual ); 00694 00695 00696 00697 /// \brief Difference and approximte derivative for column col. 00698 /// The Diff is the column difference vector. 00699 /// diff = col[1:N-2] - col[0:N-1]. 00700 /// 00701 /// \return TRUE if successful, FALSE otherwise. 00702 BOOL MTX_ColumnDiff( const MTX *M, MTX *Diff, const unsigned col ); 00703 00704 /// \brief Difference and approximate derivative. 00705 /// The Diff matrix is composed of the column difference vectors. 00706 /// for(i=0:M-1){ diff_i = col_i[1:N-2] - col_i[0:N-1] } 00707 /// 00708 /// \return TRUE if successful, FALSE otherwise. 00709 BOOL MTX_Diff( const MTX *M, MTX *Diff ); 00710 00711 00712 00713 //// 00714 // Statistics 00715 00716 /// \brief Computes the maximum element in the specified column and its index. 00717 /// If the matrix is real, only the real value, re is set, im = 0. 00718 /// If the matrix is complex, both re and im are set. 00719 /// If there are several equal maximum elements, the first index from the beginning is returned. 00720 /// 00721 /// \return TRUE if successful, FALSE otherwise. 00722 BOOL MTX_MaxColIndex( const MTX *M, const unsigned col, double *re, double *im, unsigned *row ); 00723 00724 /// \brief Computes the maximum element in the specified row and its index. 00725 /// If the matrix is real, only the real value, re is set, im = 0. 00726 /// If the matrix is complex, both re and im are set. 00727 /// If there are several equal maximum elements, the first index from the beginning is returned. 00728 /// 00729 /// \return TRUE if successful, FALSE otherwise. 00730 BOOL MTX_MaxRowIndex( const MTX *M, const unsigned row, double *re, double *im, unsigned *col ); 00731 00732 00733 /// \brief Computes the minimum element in the specified column and its index. 00734 /// If the matrix is real, only the real value, re is set, im = 0. 00735 /// If the matrix is complex, both re and im are set. 00736 /// If there are several equal minimum elements, the first index from the beginning is returned. 00737 /// 00738 /// \return TRUE if successful, FALSE otherwise. 00739 BOOL MTX_MinColIndex( const MTX *M, const unsigned col, double *re, double *im, unsigned *row ); 00740 00741 /// \brief Computes the minimum element in the specified row and its index. 00742 /// If the matrix is real, only the real value, re is set, im = 0. 00743 /// If the matrix is complex, both re and im are set. 00744 /// If there are several equal minimum elements, the first index from the beginning is returned. 00745 /// 00746 /// \return TRUE if successful, FALSE otherwise. 00747 BOOL MTX_MinRowIndex( const MTX *M, const unsigned row, double *re, double *im, unsigned *col ); 00748 00749 00750 /// \brief Computes the absolute maximum element in the specified column and its index. 00751 /// If there are several equal maximum elements, the first index from the beginning is returned. 00752 /// 00753 /// \return TRUE if successful, FALSE otherwise. 00754 BOOL MTX_MaxAbsColIndex( const MTX *M, const unsigned col, double *value, unsigned *row ); 00755 00756 /// \brief Computes the absolue maximum element in the specified row and a its column index. 00757 /// If there are several equal maximum elements, the first index from the beginning is returned. 00758 /// 00759 /// \return TRUE if successful, FALSE otherwise. 00760 BOOL MTX_MaxAbsRowIndex( const MTX *M, const unsigned row, double *value, unsigned *col ); 00761 00762 /// \brief Computes the absolute minimum element in the specified column and its index. 00763 /// If there are several equal minimum elements, the first index from the beginning is returned. 00764 /// 00765 /// \return TRUE if successful, FALSE otherwise. 00766 BOOL MTX_MinAbsColIndex( const MTX *M, const unsigned col, double *value, unsigned *row ); 00767 00768 /// \brief Computes the absolute minimum element in the specified row and its index. 00769 /// If there are several equal minimum elements, the first index from the beginning is returned. 00770 /// 00771 /// \return TRUE if successful, FALSE otherwise. 00772 BOOL MTX_MinAbsRowIndex( const MTX *M, const unsigned row, double *value, unsigned *col ); 00773 00774 00775 /// \brief Computes the maximum element in the specified column. 00776 /// If the matrix is real, only the real value, re is set, im = 0. 00777 /// If the matrix is complex, both re and im are set. 00778 /// 00779 /// \return TRUE if successful, FALSE otherwise. 00780 BOOL MTX_MaxColumn( const MTX *M, const unsigned col, double *re, double *im ); 00781 00782 /// \brief Computes the maximum element in the specified row. 00783 /// If the matrix is real, only the real value, re is set, im = 0. 00784 /// If the matrix is complex, both re and im are set. 00785 /// 00786 /// \return TRUE if successful, FALSE otherwise. 00787 BOOL MTX_MaxRow( const MTX *M, const unsigned row, double *re, double *im ); 00788 00789 00790 /// \brief Computes the minimum element in the specified column. 00791 /// If the matrix is real, only the real value, re is set, im = 0. 00792 /// If the matrix is complex, both re and im are set. 00793 /// 00794 /// \return TRUE if successful, FALSE otherwise. 00795 BOOL MTX_MinColumn( const MTX *M, const unsigned col, double *re, double *im ); 00796 00797 00798 /// \brief Computes the minimum element in the specified row. 00799 /// If the matrix is real, only the real value, re is set, im = 0. 00800 /// If the matrix is complex, both re and im are set. 00801 /// 00802 /// \return TRUE if successful, FALSE otherwise. 00803 BOOL MTX_MinRow( const MTX *M, const unsigned row, double *re, double *im ); 00804 00805 00806 /// \brief Computes the absolute maximum element in the specified column. 00807 /// 00808 /// \return TRUE if successful, FALSE otherwise. 00809 BOOL MTX_MaxAbsColumn( const MTX *M, const unsigned col, double *value ); 00810 00811 /// \brief Computes the absolute maximum element in the specified row. 00812 /// 00813 /// \return TRUE if successful, FALSE otherwise. 00814 BOOL MTX_MaxAbsRow( const MTX *M, const unsigned row, double *value ); 00815 00816 00817 /// \brief Computes the absolute minimum element in the specified column. 00818 /// 00819 /// \return TRUE if successful, FALSE otherwise. 00820 BOOL MTX_MinAbsColumn( const MTX *M, const unsigned col, double *value ); 00821 00822 /// \brief Computes the absolute minimum element in the specified row. 00823 /// 00824 /// \return TRUE if successful, FALSE otherwise. 00825 BOOL MTX_MinAbsRow( const MTX *M, const unsigned row, double *value ); 00826 00827 00828 /// \brief Computes the absolute maximum element for the entire matrix and its row and column index. 00829 /// If there are several equal maximum elements, the first index from the beginning is returned. 00830 /// 00831 /// \return TRUE if successful, FALSE otherwise. 00832 BOOL MTX_MaxAbsIndex( const MTX *M, double* value, unsigned *row, unsigned *col ); 00833 00834 /// \brief Computes the maximum element for the entire matrix and its row and column index. 00835 /// If there are several equal maximum elements, the first index from the beginning is returned. 00836 /// 00837 /// \return TRUE if successful, FALSE otherwise. 00838 BOOL MTX_MaxIndex( const MTX *M, double *re, double *im, unsigned *row, unsigned *col ); 00839 00840 /// \brief Computes the absolute maximum element for the entire matrix. 00841 /// 00842 /// \return TRUE if successful, FALSE otherwise. 00843 BOOL MTX_MaxAbs( const MTX *M, double* value ); 00844 00845 /// \brief Computes the maximum element for the entire matrix. 00846 /// 00847 /// \return TRUE if successful, FALSE otherwise. 00848 BOOL MTX_Max( const MTX *M, double *re, double *im ); 00849 00850 00851 /// \brief Computes the absolute minimum element for the entire matrix and its row and column index. 00852 /// If there are several equal minimum elements, the first index from the beginning is returned. 00853 /// 00854 /// \return TRUE if successful, FALSE otherwise. 00855 BOOL MTX_MinAbsIndex( const MTX *M, double* value, unsigned *row, unsigned *col ); 00856 00857 /// \brief Computes the minimum element for the entire matrix and its row and column index. 00858 /// If there are several equal minimum elements, the first index from the beginning is returned. 00859 /// 00860 /// \return TRUE if successful, FALSE otherwise. 00861 BOOL MTX_MinIndex( const MTX *M, double *re, double *im, unsigned *row, unsigned *col ); 00862 00863 /// \brief Computes the absolute minimum element for the entire matrix. 00864 /// 00865 /// \return TRUE if successful, FALSE otherwise. 00866 BOOL MTX_MinAbs( const MTX *M, double* value ); 00867 00868 /// \brief Computes the minimum element for the entire matrix. 00869 /// 00870 /// \return TRUE if successful, FALSE otherwise. 00871 BOOL MTX_Min( const MTX *M, double *re, double *im ); 00872 00873 00874 /// \brief Computes the range of the data in the specified column. 00875 /// Range = MaxVal - MinVal. 00876 /// If the matrix is real, only the real value, re is set, im = 0. 00877 /// If the matrix is complex, both re and im are set. 00878 /// 00879 /// \return TRUE if successful, FALSE otherwise. 00880 BOOL MTX_ColumnRange( const MTX *M, const unsigned col, double *re, double *im ); 00881 00882 00883 /// \brief Computes the range of the data in the specified row. 00884 /// Range = MaxVal - MinVal. 00885 /// If the matrix is real, only the real value, re is set, im = 0. 00886 /// If the matrix is complex, both re and im are set. 00887 /// 00888 /// \return TRUE if successful, FALSE otherwise. 00889 BOOL MTX_RowRange( const MTX *M, const unsigned row, double *re, double *im ); 00890 00891 /// \brief Computes the range of the data in the matrix. 00892 /// Range = MaxVal - MinVal. 00893 /// If the matrix is real, only the real value, re is set, im = 0. 00894 /// If the matrix is complex, both re and im are set. 00895 /// 00896 /// \return TRUE if successful, FALSE otherwise. 00897 BOOL MTX_Range( const MTX *M, double *re, double *im ); 00898 00899 00900 /// \brief Computes the sum for the specified column. 00901 /// If the matrix is real, only the real value, re is set, im = 0. 00902 /// If the matrix is complex, both re and im are set. 00903 /// 00904 /// \return TRUE if successful, FALSE otherwise. 00905 BOOL MTX_ColumnSum( const MTX *M, const unsigned col, double *re, double *im ); 00906 00907 /// \brief Computes the sum of the absolute values for the specified column. 00908 /// 00909 /// \return TRUE if successful, FALSE otherwise. 00910 BOOL MTX_ColumnSumAbs( const MTX *M, const unsigned col, double *value ); 00911 00912 00913 /// \brief Computes the sum for the specified row. 00914 /// If the matrix is real, only the real value, re is set, im = 0. 00915 /// If the matrix is complex, both re and im are set. 00916 /// 00917 /// \return TRUE if successful, FALSE otherwise. 00918 BOOL MTX_RowSum( const MTX *M, const unsigned row, double *re, double *im ); 00919 00920 /// \brief Computes the sum of the data in the matrix . 00921 /// If the matrix is real, only the real value, re is set, im = 0. 00922 /// If the matrix is complex, both re and im are set. 00923 /// 00924 /// \return TRUE if successful, FALSE otherwise. 00925 BOOL MTX_Sum( const MTX *M, double *re, double *im ); 00926 00927 00928 00929 /// \brief Computes the sample mean for the specified column. 00930 /// If the matrix is real, only the real value, re is set, im = 0. 00931 /// If the matrix is complex, both re and im are set. 00932 /// 00933 /// \return TRUE if successful, FALSE otherwise. 00934 BOOL MTX_ColumnMean( const MTX *M, const unsigned col, double *re, double *im ); 00935 00936 /// \brief Computes the sample mean for the specified row. 00937 /// If the matrix is real, only the real value, re is set, im = 0. 00938 /// If the matrix is complex, both re and im are set. 00939 /// 00940 /// \return TRUE if successful, FALSE otherwise. 00941 BOOL MTX_RowMean( const MTX *M, const unsigned row, double *re, double *im ); 00942 00943 /// \brief Computes the sample mean for the matrix. 00944 /// If the matrix is real, only the real value, re is set, im = 0. 00945 /// If the matrix is complex, both re and im are set. 00946 /// 00947 /// \return TRUE if successful, FALSE otherwise. 00948 BOOL MTX_Mean( const MTX *M, double *re, double *im ); 00949 00950 00951 00952 00953 /// \brief Computes the sample standard deviation for the specified column. 00954 /// 00955 /// \return TRUE if successful, FALSE otherwise. 00956 BOOL MTX_ColumnStdev( const MTX *M, const unsigned col, double *value ); 00957 00958 /// \brief Computes the sample standard deviation for the specified row. 00959 /// 00960 /// \return TRUE if successful, FALSE otherwise. 00961 BOOL MTX_RowStdev( const MTX *M, const unsigned row, double *value ); 00962 00963 /// \brief Computes the sample standard deviation for the matrix. 00964 /// 00965 /// \return TRUE if successful, FALSE otherwise. 00966 BOOL MTX_Stdev( const MTX *M, double *value ); 00967 00968 00969 00970 /// \brief Computes the sample variance for the specified column. 00971 /// 00972 /// \return TRUE if successful, FALSE otherwise. 00973 BOOL MTX_ColumnVar( const MTX *M, const unsigned col, double *value ); 00974 00975 /// \brief Computes the sample variance for the specified row. 00976 /// 00977 /// \return TRUE if successful, FALSE otherwise. 00978 BOOL MTX_RowVar( const MTX *M, const unsigned row, double *value ); 00979 00980 /// \brief Computes the sample variance for the matrix. 00981 /// 00982 /// \return TRUE if successful, FALSE otherwise. 00983 BOOL MTX_Var( const MTX *M, double *value ); 00984 00985 00986 /// \brief Computes the norm of the specified column. 00987 /// If real, norm = sqrt( sum( val*val ) ). 00988 /// If complex, norm = sqrt( sum( val*conjugate(val) ) ). 00989 /// 00990 /// \return TRUE if successful, FALSE otherwise. 00991 BOOL MTX_ColumnNorm( const MTX *M, const unsigned col, double *value ); 00992 00993 /// \brief Computes the norm of the specified row. 00994 /// If real, norm = sqrt( sum( val*val ) ). 00995 /// If complex, norm = sqrt( sum( val*conjugate(val) ) ). 00996 /// 00997 /// \return TRUE if successful, FALSE otherwise. 00998 BOOL MTX_RowNorm( const MTX *M, const unsigned row, double *value ); 00999 01000 /// \brief Computes the norm of the matrix. 01001 /// If real, norm = sqrt( sum( val*val ) ). 01002 /// If complex, norm = sqrt( sum( val*conjugate(val) ) ). 01003 /// 01004 /// \return TRUE if successful, FALSE otherwise. 01005 BOOL MTX_Norm( const MTX *M, double *value ); 01006 01007 01008 /// \brief Computes the sample RMS value for the specified column. 01009 /// 01010 /// \return TRUE if successful, FALSE otherwise. 01011 BOOL MTX_ColumnRMS( const MTX *M, const unsigned col, double *value ); 01012 01013 /// \brief Computes the sample RMS value for the specified row. 01014 /// 01015 /// \return TRUE if successful, FALSE otherwise. 01016 BOOL MTX_RowRMS( const MTX *M, const unsigned row, double *value ); 01017 01018 /// \brief Computes the sample RMS value for the matrix. 01019 /// 01020 /// \return TRUE if successful, FALSE otherwise. 01021 BOOL MTX_RMS( const MTX *M, double *value ); 01022 01023 01024 /// \brief Computes the sample skewness value for the specified column. 01025 /// The skewness is the third central moment divided by the cube of the standard deviation. 01026 /// If the matrix is real, only the real value, re is set, im = 0. 01027 /// If the matrix is complex, both re and im are set. 01028 /// 01029 /// \return TRUE if successful, FALSE otherwise. 01030 BOOL MTX_ColumnSkewness( const MTX *M, const unsigned col, double *re, double* im ); 01031 01032 /// \brief Computes the sample skewness value for the specified row. 01033 /// The skewness is the third central moment divided by the cube of the standard deviation. 01034 /// If the matrix is real, only the real value, re is set, im = 0. 01035 /// If the matrix is complex, both re and im are set. 01036 /// 01037 /// \return TRUE if successful, FALSE otherwise. 01038 BOOL MTX_RowSkewness( const MTX *M, const unsigned row, double *re, double* im ); 01039 01040 /// \brief Computes the sample skewness value for the matrix. 01041 /// The skewness is the third central moment divided by the cube of the standard deviation. 01042 /// If the matrix is real, only the real value, re is set, im = 0. 01043 /// If the matrix is complex, both re and im are set. 01044 /// 01045 /// \return TRUE if successful, FALSE otherwise. 01046 BOOL MTX_Skewness( const MTX *M, double *re, double* im ); 01047 01048 01049 01050 /// \brief Computes the sample kurtosis value for the specified column. 01051 /// The kurtosis is the fourth central moment divided by fourth power of the standard deviation. 01052 /// If the matrix is real, only the real value, re is set, im = 0. 01053 /// If the matrix is complex, both re and im are set. 01054 /// To adjust the computed kurtosis value for bias, subtract 3 from the real component. 01055 /// Reference: http://en.wikipedia.org/wiki/Kurtosis. 01056 /// Reference: http://mathworld.wolfram.com/Kurtosis.html (kurtosis proper is computed). 01057 /// \return TRUE if successful, FALSE otherwise. 01058 // g_2 = \frac{m_4}{m_{2}^2} = \frac{n\,\sum_{i=1}^n (x_i - \overline{x})^4}{\left(\sum_{i=1}^n (x_i - \overline{x})^2\right)^2}. 01059 BOOL MTX_ColumnKurtosis( const MTX *M, const unsigned col, double *re, double *im ); 01060 01061 /// \brief Computes the sample kurtosis value for the specified row. 01062 /// The kurtosis is the fourth central moment divided by fourth power of the standard deviation. 01063 /// If the matrix is real, only the real value, re is set, im = 0. 01064 /// If the matrix is complex, both re and im are set. 01065 /// To adjust the computed kurtosis value for bias, subtract 3 from the real component. 01066 /// Reference: http://en.wikipedia.org/wiki/Kurtosis. 01067 /// Reference: http://mathworld.wolfram.com/Kurtosis.html (kurtosis proper is computed). 01068 /// 01069 /// \return TRUE if successful, FALSE otherwise. 01070 // g_2 = \frac{m_4}{m_{2}^2} = \frac{n\,\sum_{i=1}^n (x_i - \overline{x})^4}{\left(\sum_{i=1}^n (x_i - \overline{x})^2\right)^2}. 01071 BOOL MTX_RowKurtosis( const MTX *M, const unsigned row, double *re, double *im ); 01072 01073 01074 /// \brief Computes the sample kurtosis value for the matrix. 01075 /// The kurtosis is the fourth central moment divided by fourth power of the standard deviation. 01076 /// If the matrix is real, only the real value, re is set, im = 0. 01077 /// If the matrix is complex, both re and im are set. 01078 /// To adjust the computed kurtosis value for bias, subtract 3 from the real component. 01079 /// Reference: http://en.wikipedia.org/wiki/Kurtosis. 01080 /// Reference: http://mathworld.wolfram.com/Kurtosis.html (kurtosis proper is computed). 01081 /// 01082 /// \return TRUE if successful, FALSE otherwise. 01083 // g_2 = \frac{m_4}{m_{2}^2} = \frac{n\,\sum_{i=1}^n (x_i - \overline{x})^4}{\left(\sum_{i=1}^n (x_i - \overline{x})^2\right)^2}. 01084 BOOL MTX_Kurtosis( const MTX *M, double *re, double *im ); 01085 01086 01087 01088 01089 01090 //// 01091 // Matrix specific 01092 01093 /// \brief Computes the trace of M where M is a square matrix. 01094 /// Trace = Sum of diagonal elements. 01095 /// If the matrix is real, only the real value, re is set, im = 0. 01096 /// If the matrix is complex, both re and im are set. 01097 /// 01098 /// \return TRUE if successful, FALSE otherwise. 01099 BOOL MTX_Trace( const MTX *M, double *re, double *im ); 01100 01101 /// \brief Sets the diagonal elements of M into D as a column vector 01102 /// 01103 /// \return TRUE if successful, FALSE otherwise. 01104 BOOL MTX_Diagonal( const MTX *M, MTX *D ); 01105 01106 /// \brief Sorts each column of M in ascending order. 01107 /// If complex, sorts based on magnitude. 01108 /// 01109 /// \return TRUE if successful, FALSE otherwise. 01110 BOOL MTX_SortAscending( MTX *M ); 01111 01112 /// \brief Sorts each column of M in descending order. 01113 /// If complex, sorts based on magnitude. 01114 /// 01115 /// \return TRUE if successful, FALSE otherwise. 01116 BOOL MTX_SortDescending( MTX *M ); 01117 01118 /// \brief Sorts a specific column in ascending order. 01119 /// If complex, sorts based on magnitude. 01120 /// 01121 /// \return TRUE if successful, FALSE otherwise. 01122 BOOL MTX_SortColumnAscending( MTX *M, const unsigned col ); 01123 01124 /// \brief Sorts a specific column in descending order. 01125 /// If complex, sorts based on magnitude. 01126 /// 01127 /// \return TRUE if successful, FALSE otherwise. 01128 BOOL MTX_SortColumnDescending( MTX *M, const unsigned col ); 01129 01130 /// \brief Sorts a specific column in ascending order and fills a MTX column vector with the sorted index. 01131 /// The index vector will be resized if index->nrows != M->nrows 01132 /// If complex, sorts based on magnitude. 01133 /// 01134 /// \return TRUE if successful, FALSE otherwise. 01135 BOOL MTX_SortColumnIndexed( MTX *M, const unsigned col, MTX *index ); 01136 01137 /// \brief Sorts the entire matrix by a specific column. 01138 /// If complex, sorts based on magnitude. 01139 /// 01140 /// \return TRUE if successful, FALSE otherwise. 01141 BOOL MTX_SortByColumn( MTX *M, const unsigned col ); 01142 01143 01144 01145 /// \brief Saves a matrix to the specified file path using a proprietary compressed format. 01146 /// ADVANCED EDITION ONLY! 01147 /// \return TRUE if successful, FALSE otherwise. 01148 BOOL MTX_SaveCompressed( const MTX *M, const char *path ); 01149 01150 01151 /// \brief Loads a binary compressed matrix that was saved using the MTX_SaveCompressed function. 01152 /// 01153 /// \return TRUE if successful, FALSE otherwise. 01154 BOOL MTX_ReadCompressed( MTX *M, const char *path ); 01155 01156 /// \brief Get attributes of the compressed file. 01157 /// 01158 /// \return TRUE if successful, FALSE otherwise. 01159 BOOL MTX_GetCompressedFileAttributes( 01160 const char *path, 01161 unsigned* nrows, 01162 unsigned* ncols, 01163 BOOL* isReal 01164 ); 01165 01166 01167 /// \brief Read an ASCII matrix data file and save it using MTX_SaveCompressed. 01168 /// ADVANCED EDITION ONLY! 01169 /// 01170 /// \return TRUE if successful, FALSE otherwise. 01171 BOOL MTX_LoadAndSave( const char* infilepath, const char* outfilepath ); 01172 01173 /// \brief Read an ASCII matrix data file and save it using MTX_SaveCompressed. 01174 /// This version saves the data to the same base filename and uses the .mtx extension. 01175 /// ADVANCED EDITION ONLY! 01176 /// 01177 /// \return TRUE if successful, FALSE otherwise. 01178 BOOL MTX_LoadAndSaveQuick( const char* infilepath ); 01179 01180 01181 /// \brief Alter the matrix, M, so that its data is within the startTime to the startTime+duration 01182 /// and compensate for any rollovers in the time system (e.g. GPS time in seconds rolls over 01183 /// at 604800.0 s). This function assumes that time is one of the matrix columns and requires 01184 /// this index, the timeColumn. 01185 /// 01186 /// \return TRUE if successful, FALSE otherwise. 01187 BOOL MTX_TimeWindow( 01188 MTX* M, //!< Matrix to be altered 01189 const unsigned timeColumn, //!< The column containing time 01190 const double startTime, //!< The specified start time (inclusive) 01191 const double duration, //!< The duration to include 01192 const double rolloverTime ); //!< The potential time at which system time rolls over 01193 01194 /// \brief Alter the matrix, M, so that its data is within [startTime endTime]. 01195 /// This function assumes that time is one of the matrix columns and requires 01196 /// this index, the timeColumn. 01197 /// 01198 /// \return TRUE if successful, FALSE otherwise. 01199 BOOL MTX_TimeLimit( 01200 MTX* M, //!< Matrix to be altered 01201 const unsigned timeColumn, //!< The column containing time 01202 const double startTime, //!< The specified start time (inclusive) 01203 const double endTime ); //!< The duration to include 01204 01205 01206 /// \brief This function matches matrices in time with specified precision 01207 /// where time is a column of each matrix. This function also 01208 /// allows time to rollover at a specified interval. 01209 /// 01210 /// precision 0 = match to whole number \n 01211 /// precision 1 = match to nearest 0.1 \n 01212 /// precision 2 = match to nearest 0.01 \n 01213 /// etc. \n 01214 /// rolloverTime examples \n 01215 /// GPS time of week (s): rolloverTime= 604800.0 \n 01216 /// hours : rolloverTime = 24.0 \n 01217 /// minutes : rolloverTime = 60.0 \n 01218 /// 01219 /// The time data must be non-decreasing but the time may rollover 01220 /// by the specified amount. 01221 /// e.g. rolloverTime = 60.0 \n 01222 /// 0,1,2,3,4,...59,60,1,2,5,10,60,1,2,3... \n 01223 /// 01224 /// \return TRUE if successful, FALSE otherwise. 01225 BOOL MTX_TimeMatch( 01226 MTX *A, //!< The matrix with interpolation times 01227 const unsigned timeColumnA, //!< The zero based column index for matrix A 01228 MTX *B, //!< The matrix to be interpolated 01229 const unsigned timeColumnB, //!< The zero based column index for matrix B 01230 const unsigned precision, //!< The rounding precision used for time matching, 0 = whole, 1 = 0.1, 2 = 0.01, etc 01231 const double rolloverTime //!< The rollover time, e.g. 60 s for minute based timing, 0.0 means rollovers not allowed 01232 ); 01233 01234 01235 01236 /// \brief This function interpolates Matrix B values by the times defined 01237 /// in the column in Matrix A. Time must be increasing but times can 01238 /// rollover with the specified rolloverTime. 01239 /// 01240 /// This function returns A and B with the same number of rows and 01241 /// time aligned time columns. 01242 /// 01243 /// \return TRUE if successful, FALSE otherwise. 01244 BOOL MTX_Interpolate( 01245 MTX *A, //!< The matrix with interpolation times 01246 const unsigned timeColumnA, //!< The zero based column index for matrix A 01247 MTX *B, //!< The matrix to be interpolated 01248 const unsigned timeColumnB, //!< The zero based column index for matrix B 01249 const double maxInterpolationInterval, //!< The largest interpolation interval allowed 01250 const double rolloverTime //!< The rollover time, e.g. 60 s for minute based timing, 0.0 means rollovers not allowed 01251 ); 01252 01253 01254 /// \brief Compute the inverse, 1.0/x, inplace for each element 01255 /// of the matrix. 01256 /// 01257 /// \return TRUE if successful, FALSE otherwise. 01258 BOOL MTX_Inv( MTX *src ); 01259 01260 01261 /// \brief Compute the inplace inverse of the matrix. 01262 /// Uses fast closed form solutions for: 01263 /// Only for: 1x1, 2x2, 3x3 01264 /// 01265 /// If the matrix is singular, the original matrix is unchanged. 01266 /// 01267 /// \return TRUE if successful, FALSE if empty or has dimensions larger 01268 /// than 3x3, false if singular or not square 01269 BOOL MTX_InvertInPlaceClosedForm( MTX *M ); 01270 01271 01272 01273 /// \brief Compute the inplace inverse of a postive definite matrix. 01274 /// 01275 /// The matrix is first tested to determine if it is a symmetric 01276 /// positive-definite matrix. If so, Cholesky decomposition is used 01277 /// to facilitate the inversion of a lower triangular matrix. If the 01278 /// matrix is not symmetric and positive-definite robust inversion 01279 /// using gaussing elimination is attempted. 01280 /// 01281 /// 3x3 matrices or smaller dimensions are computed using 01282 /// MTX_InvertInPlaceClosedForm. 01283 /// 01284 /// If the matrix is singular, the original matrix is unchanged. 01285 /// 01286 /// \return TRUE if successful, FALSE otherwise. 01287 BOOL MTX_InvertInPlace( MTX *M ); 01288 01289 01290 /// \brief Perfroms an inplace inverse using Gaussian Elimination methods. 01291 /// 01292 /// \return TRUE if successful, FALSE otherwise. 01293 BOOL MTX_InvertInPlaceRobust( MTX *M ); 01294 01295 01296 /// \brief Computes a moving average using N lead samples and M lagging samples 01297 /// for the specified column and stores it in dst. 01298 /// 01299 /// \return TRUE if successful, FALSE otherwise. 01300 BOOL MTX_ColumnMovAvg( const MTX *src, const unsigned col, const unsigned lead, const unsigned lag, MTX *dst ); 01301 01302 /// \brief Computes a moving average using N lead samples and M lagging samples 01303 /// for the matrix and stores it in dst. 01304 /// 01305 /// \return TRUE if successful, FALSE otherwise. 01306 BOOL MTX_MovAvg( const MTX *src, const unsigned lead, const unsigned lag, MTX *dst ); 01307 01308 01309 01310 /// \brief Computes: InvATA = inverse( transpose(A) * A ). 01311 /// 01312 /// \return TRUE if successful, FALSE otherwise. 01313 BOOL MTX_ATAInverse( const MTX *A, MTX *InvATA ); 01314 01315 /// \brief Compute the inplace inverse of a unit lower triangular matrix. 01316 /// An example unit lower triangular matrix is: \n 01317 /// A = [ 1 0 0; \n 01318 /// -2 2 0; \n 01319 /// 4 -3 3 ]; with \n 01320 /// inv(A) = [ 1 0 0; \n 01321 /// 1 1/2 0; \n 01322 /// -1/3 1/2 1/3 ]; \n 01323 /// 01324 /// \return TRUE if successful, FALSE otherwise. 01325 BOOL MTX_LowerTriangularInverseInplace( MTX *src ); 01326 01327 01328 /// \brief Computes the determinatnt of the square matrix M. 01329 /// If the matrix is real, only the real value, re is set, im = 0. 01330 /// If the matrix is complex, both re and im are set. 01331 /// 01332 /// \return TRUE if successful, FALSE otherwise. 01333 BOOL MTX_Det( const MTX *M, double *re, double *im ); 01334 01335 01336 /// \brief LU factorization. 01337 /// Performs a factorization to produce a unit lower triangular matrix, L, 01338 /// an upper triangular matrix, U, and permutation matrix P so that 01339 /// P*X = L*U. 01340 /// P, L and U are copmuted correctly if IsFullRank is set to true. 01341 /// 01342 /// \return TRUE if successful, FALSE otherwise. 01343 BOOL MTX_LUFactorization( const MTX *src, BOOL *IsFullRank, MTX *P, MTX *L, MTX *U ); 01344 01345 01346 /// \brief Retrieve the elements of the matrix specified by the index vectors. 01347 /// The index vectors must be nx1 real vectors. 01348 /// 01349 /// \return TRUE if successful, FALSE otherwise. 01350 BOOL MTX_IndexedValues( const MTX *src, const MTX *row_index, const MTX *col_index, MTX *dst ); 01351 01352 01353 /// \brief Set the elements of the matrix specified by the index vectors. 01354 /// The index vectors must be nx1 real vectors. 01355 /// 01356 /// \return TRUE if successful, FALSE otherwise. 01357 BOOL MTX_SetIndexedValues( MTX *dst, const MTX *row_index, const MTX *col_index, const MTX *src ); 01358 01359 01360 /// \brief Compute the Fast Fourier Transform of each columns in the src matrix and 01361 /// store it in the dst matrix. 01362 /// 01363 /// \return TRUE if successful, FALSE otherwise. 01364 BOOL MTX_FFT( const MTX *src, MTX *dst ); 01365 01366 /// \brief Compute the inverse Fast Fourier Transform of each columns in the src matrix and 01367 /// store it in the dst matrix. 01368 /// 01369 /// \return TRUE if successful, FALSE otherwise. 01370 BOOL MTX_IFFT( const MTX *src, MTX *dst ); 01371 01372 /// \brief Compute the inplace Fast Fourier Transform of each column of the matrix. 01373 /// 01374 /// \return TRUE if successful, FALSE otherwise. 01375 BOOL MTX_FFT_Inplace( MTX *src); 01376 01377 /// \brief Compute the inplace inverse Fast Fourier Transform of each column of the matrix. 01378 /// 01379 /// \return TRUE if successful, FALSE otherwise. 01380 BOOL MTX_IFFT_Inplace( MTX *src); 01381 01382 01383 01384 /// \brief Compute the sine of each element in the matrix. Assumes elements are radians. 01385 /// 01386 /// \return TRUE if successful, FALSE otherwise. 01387 BOOL MTX_sin( MTX *src ); 01388 01389 /// \brief Compute the sin(pi*x)/(pi*) of each element in the matrix. 01390 /// 01391 /// \return TRUE if successful, FALSE otherwise. 01392 BOOL MTX_sinc( MTX *src ); 01393 01394 /// \brief Compute the hyperbolic sine of each element in the matrix. Assumes elements are radians. 01395 /// 01396 /// \return TRUE if successful, FALSE otherwise. 01397 BOOL MTX_sinh( MTX *src ); 01398 01399 /// \brief Compute the inverse hyperbolic sine of each element in the matrix. 01400 /// Results in radians. 01401 /// 01402 /// \return TRUE if successful, FALSE otherwise. 01403 BOOL MTX_asinh( MTX *src ); 01404 01405 /// \brief Compute the cosine of each element in the matrix. Assumes elements are radians. 01406 /// 01407 /// \return TRUE if successful, FALSE otherwise. 01408 BOOL MTX_cos( MTX *src ); 01409 01410 /// \brief Compute the hyperbolic cosine of each element in the matrix. Assumes elements are radians. 01411 /// 01412 /// \return TRUE if successful, FALSE otherwise. 01413 BOOL MTX_cosh( MTX *src ); 01414 01415 /// \brief Compute the inverse hyperbolic cosine of each element in the matrix. 01416 /// Results in radians. 01417 /// 01418 /// \return TRUE if successful, FALSE otherwise. 01419 BOOL MTX_acosh( MTX *src ); 01420 01421 /// \brief Compute the tangent of each element in the matrix. Assumes elements are radians. 01422 /// 01423 /// \return TRUE if successful, FALSE otherwise. 01424 BOOL MTX_tan( MTX *src ); 01425 01426 /// \brief Compute the hyperbolic tangent of each element in the matrix. Assumes elements are radians. 01427 /// 01428 /// \return TRUE if successful, FALSE otherwise. 01429 BOOL MTX_tanh( MTX *src ); 01430 01431 /// \brief Compute the inverse hyperbolic tangent of each element in the matrix. 01432 /// Results in radians. 01433 /// 01434 /// \return TRUE if successful, FALSE otherwise. 01435 BOOL MTX_atanh( MTX *src ); 01436 01437 01438 /// \brief Compute the cotangent of each element in the matrix. Assumes elements are radians. 01439 /// 01440 /// \return TRUE if successful, FALSE otherwise. 01441 BOOL MTX_cot( MTX *src ); 01442 01443 /// \brief Compute the hyperbolic cotangent of each element in the matrix. Assumes elements are radians. 01444 /// 01445 /// \return TRUE if successful, FALSE otherwise. 01446 BOOL MTX_coth( MTX *src ); 01447 01448 01449 01450 /// \brief Create a column vector [start:increment:end) beginning at start 01451 /// with step size of increment until less than or equal to end. 01452 /// Note that arguments must be real scalars. \n 01453 /// e.g. a = 2:2:9 = [2; 4; 6; 8;] \n 01454 /// e.g. b = 2:-2:-9 = [2; 0; -2; -4; -6; -9;] \n 01455 /// 01456 /// \return TRUE if successful, FALSE otherwise. 01457 BOOL MTX_Colon( MTX *dst, const double start, const double increment, const double end ); 01458 01459 01460 /** \brief A very efficient method to remove rows and columns from the matrix. 01461 * 01462 * \code 01463 * MTX A; 01464 * unsigned rows[2]; 01465 * unsigned cols[2]; 01466 * MTX_Init(&A); 01467 * MTX_Calloc( &A, 4, 4 ); 01468 * MTX_Identity( &A ); 01469 * A.data[0][0] = 100.0; 01470 * A.data[2][1] = 10.0; 01471 * A.data[1][2] = 20.0; 01472 * // remove the first row and column and the third row and column. 01473 * rows[0] = 0; 01474 * rows[1] = 2; 01475 * cols[0] = 0; 01476 * cols[1] = 2; 01477 * MTX_RemoveRowsAndColumns( &A, 2, (unsigned*)rows, 2 (unsigned*)cols ); 01478 * // A is now a 2x2 identity matrix. 01479 * \endcode 01480 * 01481 * \return TRUE if successful, FALSE otherwise. 01482 */ 01483 BOOL MTX_RemoveRowsAndColumns( 01484 MTX *src, //!< The pointer to the matrix object. 01485 const unsigned nrows, //!< The number of rows to remove (the length of the rows array). 01486 const unsigned rows[], //!< The array of row indices to remove. 01487 const unsigned ncols, //!< The number of columns to remove (the length of hte cols array). 01488 const unsigned cols[] 01489 ); 01490 01491 01492 #ifdef ___MTX_RANDN_READY 01493 /** \brief Produce a matrix that is composed of pseudo-random numbers. 01494 * The seed state is based on the system clock or alternatively by the seed 01495 * parameter, a positive integer can set the seed state upon generation. 01496 * Elements are chosen from a normal distribution with mean zero, variance of 01497 * one and standard of deviation one. 01498 * 01499 * \code 01500 * MTX A; 01501 * MTX_Init(&A); 01502 * MTX_randn( 1000, 1 ); // create a random vector of 1000 rows by 1 column. 01503 * \endcode 01504 * 01505 * \return TRUE if successful, FALSE otherwise. 01506 */ 01507 BOOL MTX_randn( 01508 MTX* M, 01509 const unsigned nrows, 01510 const unsigned ncols, 01511 const BOOL useSeed, 01512 const unsigned seed 01513 ); 01514 #endif 01515 01516 /// \brief Test if a double value is NaN. 01517 BOOL MTX_IsNAN( double value ); 01518 01519 /// \brief Test if a double value is +INF. 01520 BOOL MTX_IsPostiveINF( double value ); 01521 01522 /// \brief Test if a double value is -INF. 01523 BOOL MTX_IsNegativeINF( double value ); 01524 01525 01526 #ifdef __cplusplus 01527 } 01528 #endif 01529 01530 01531 #endif // ZENUATICS_MTX_H 01532 01533 01534 01535 01536