00001 /** 00002 \file cmatrix.h 00003 \brief 'c' functions for vector and matrix operations. 00004 \author Glenn D. MacGougan (GDM) 00005 \date 2009-01-08 00006 \version 0.07 Beta 00007 00008 \b Version \b Information \n 00009 This is the open source version (BSD license). The Professional Version 00010 is avaiable via http://www.zenautics.com. The Professional Version 00011 is highly optimized using SIMD and includes optimization for multi-core 00012 processors. 00013 00014 \b License \b Information \n 00015 Copyright (c) 2008, Glenn D. MacGougan, Zenautics Technologies Inc. \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 typedef int BOOL; 00057 00058 #ifndef FALSE 00059 #define FALSE (0) 00060 #endif 00061 #ifndef TRUE 00062 #define TRUE (1) 00063 #endif 00064 00065 /// \brief A complex data struct. 00066 typedef struct 00067 { 00068 double re; //!< The real part. 00069 double im; //!< The imaginary part. 00070 } stComplex; 00071 00072 /// \brief The deep level matrix struct. The matrix is either real or complex. 00073 typedef struct 00074 { 00075 unsigned nrows; //!< The number of rows in the matrix. 00076 unsigned ncols; //!< The number of columns in the matrix. 00077 BOOL isReal; //!< This indicates if is the matrix real or complex. 00078 double **data; //!< This is a pointer to an array of double column vectors. 00079 stComplex **cplx; //!< Thsi is a pointer to an array of complex column vectors. 00080 char *comment; //!< This is a comment string (if applicable). 00081 } MTX; 00082 00083 00084 /// \brief This function must be called first by users of cmatrix! 00085 /// 00086 /// \return TRUE if successful, FALSE otherwise. 00087 BOOL MTX_Initialize_MTXEngine(); 00088 00089 00090 /// \brief This function is used to set if matrices that are single 00091 /// elements (1x1) are treated as scalars for math operations 00092 /// or whether the regular matrix rules apply. THIS IS ENABLED 00093 /// BY DEFAULT. 00094 /// 00095 /// \return TRUE if successful, FALSE otherwise. 00096 BOOL MTX_Enable1x1MatricesForTreatmentAsScalars( BOOL enable ); 00097 00098 /// \brief Is this a null matrix? 00099 /// 00100 /// \return TRUE if the matrix is null, FALSE otherwise. 00101 BOOL MTX_isNull( const MTX *M ); 00102 00103 00104 /// \brief Are matrices A & B conformal for multiplication, real * real 00105 /// 00106 /// \return TRUE if successful, FALSE otherwise. 00107 BOOL MTX_isConformalForMultiplication( const MTX *A, const MTX *B ); 00108 00109 /// \brief Are matrices A & B conformat for addition/subtraction, real + real 00110 /// 00111 /// \return TRUE if successful, FALSE otherwise. 00112 BOOL MTX_isConformalForAddition( const MTX *A, const MTX *B ); 00113 00114 /// \brief Is this a square matrix? 00115 /// 00116 /// \return TRUE if successful, FALSE otherwise. 00117 BOOL MTX_isSquare( const MTX *A ); 00118 00119 /// \brief are A and B the same size? 00120 /// 00121 /// \return TRUE if successful, FALSE otherwise. 00122 BOOL MTX_isSameSize( const MTX *A, const MTX *B ); 00123 00124 /// \brief Initialize a MTX matrix struct to appropriate zero values. This must always be called for proper operation! 00125 /// \code 00126 /// MTX matrix; 00127 /// MTX_Init( &matrix ); 00128 /// \endcode 00129 /// 00130 /// \return TRUE if successful, FALSE otherwise. 00131 BOOL MTX_Init( MTX *M ); 00132 00133 00134 00135 /// \brief Set the matrix comment string 00136 /// 00137 /// \return TRUE if successful, FALSE otherwise. 00138 BOOL MTX_SetComment( MTX *M, const char *comment ); 00139 00140 /// \brief Clear the matrix data from memory if dynamically allocated. Zero the struct members. 00141 /// 00142 /// \return TRUE if successful, FALSE otherwise. 00143 BOOL MTX_Free( MTX *M ); 00144 00145 /// \brief Allocate matrix data (set to zero). 00146 /// 00147 /// \return TRUE if successful, FALSE otherwise. 00148 BOOL MTX_Calloc( MTX *M, const unsigned nrows, const unsigned ncols, const BOOL isReal ); 00149 00150 /// \brief Allocate matrix data (not set to zero). 00151 /// 00152 /// \return TRUE if successful, FALSE otherwise. 00153 BOOL MTX_Malloc( MTX *M, const unsigned nrows, const unsigned ncols, const BOOL isReal ); 00154 00155 /// \brief Set a scalar value in the matrix. 00156 /// 00157 /// \return TRUE if successful, FALSE otherwise. 00158 BOOL MTX_SetValue( MTX *M, const unsigned row, const unsigned col, const double value ); 00159 00160 /// \brief Set a complex value in the matrix. 00161 /// 00162 /// \return TRUE if successful, FALSE otherwise. 00163 BOOL MTX_SetComplexValue( MTX *M, const unsigned row, const unsigned col, const double re, const double im ); 00164 00165 /// \brief Matrix M = Re + Im*i, where Re and Im are real matrices. 00166 /// 00167 /// \return TRUE if successful, FALSE otherwise. 00168 BOOL MTX_Complex( MTX *M, const MTX *Re, const MTX *Im ); 00169 00170 /// \brief Set the specified column in Matrix M to Re + Im*i, where Re and Im are real matrices. 00171 /// The dimensions of M must already be valid. 00172 /// 00173 /// \return TRUE if successful, FALSE otherwise. 00174 BOOL MTX_SetComplexColumn( MTX *M, const unsigned col, const MTX *Re, const MTX *Im ); 00175 00176 /// \brief Convert a real matrix to a complex matrix 00177 /// 00178 /// \return TRUE if successful, FALSE otherwise. 00179 BOOL MTX_ConvertRealToComplex( MTX *M ); 00180 00181 /// \brief Convert a complex marix to a real matrix using only the imaginary component A = real(B). 00182 /// 00183 /// \return TRUE if successful, FALSE otherwise. 00184 BOOL MTX_ConvertComplexToReal( MTX *M ); 00185 00186 /// \brief Convert a complex marix to a real matrix using only the imaginary component A = imag(B). 00187 /// 00188 /// \return TRUE if successful, FALSE otherwise. 00189 BOOL MTX_ConvertComplexToImag( MTX *M ); 00190 00191 /// \brief Extract the real component of matrix M. 00192 /// 00193 /// \return TRUE if successful, FALSE otherwise. 00194 BOOL MTX_Real( const MTX *M, MTX *Re ); 00195 00196 /// \brief Check if the matrix contains only real values. 00197 /// Alter the matrix if it is stored as complex and only has real values. 00198 /// 00199 /// \return TRUE if successful, FALSE otherwise. 00200 BOOL MTX_isReal( MTX *M, BOOL *isReal ); 00201 00202 /// \brief Extract the real component of column col of matrix M. 00203 /// 00204 /// \return TRUE if successful, FALSE otherwise. 00205 BOOL MTX_RealColumn( const MTX *M, const unsigned col, MTX *Re ); 00206 00207 /// \brief Extract the imaginary component of matrix M. 00208 /// 00209 /// \return TRUE if successful, FALSE otherwise. 00210 BOOL MTX_Imag( const MTX *M, MTX *Im ); 00211 00212 /// \brief Extract the imaginary component of column col of matrix M. 00213 /// 00214 /// \return TRUE if successful, FALSE otherwise. 00215 BOOL MTX_ImagColumn( const MTX *M, const unsigned col, MTX *Im ); 00216 00217 /// \brief If M is a real matrix, Magnitude is a copy. 00218 /// If M is a complex matrix, Magnitude is a real matrix = sqrt( re*re + im*im ). 00219 /// 00220 /// \return TRUE if successful, FALSE otherwise. 00221 BOOL MTX_Magnitude( const MTX *M, MTX *Magnitude ); 00222 00223 /// \brief If M is a real matrix, Phase is a zero matrix. 00224 /// If M is a complex matrix, Phase is a real matrix = atan2(im,re). 00225 /// 00226 /// \return TRUE if successful, FALSE otherwise. 00227 BOOL MTX_Phase( const MTX *M, MTX *Phase ); 00228 00229 /// \brief If M is a real matrix, nothing is done. 00230 /// If M is a complex matrix, the conjugate is set. 00231 /// 00232 /// \return TRUE if successful, FALSE otherwise. 00233 BOOL MTX_Conjugate( MTX *M ); 00234 00235 /// \brief Remove a single column from the matrix. 00236 /// 00237 /// \return TRUE if successful, FALSE otherwise. 00238 BOOL MTX_RemoveColumn( MTX *M, const unsigned col ); 00239 00240 /// \brief remove all the columns 'after' the column index given. 00241 /// 00242 /// \return TRUE if successful, FALSE otherwise. 00243 BOOL MTX_RemoveColumnsAfterIndex( MTX *dst, const unsigned col ); 00244 00245 /// \brief insert a column into another matrix. 00246 /// 00247 /// \return TRUE if successful, FALSE otherwise. 00248 BOOL MTX_InsertColumn( MTX *dst, const MTX *src, const unsigned dst_col, const unsigned src_col ); 00249 00250 /// \brief Add a column to the Matrix. 00251 /// 00252 /// \return TRUE if successful, FALSE otherwise. 00253 BOOL MTX_AddColumn( MTX *dst, const MTX *src, const unsigned src_col ); 00254 00255 /// \brief Combine two matrices with the same nrows, A becomes A|B, 00256 /// 00257 /// \return TRUE if successful, FALSE otherwise. 00258 BOOL MTX_Concatonate( MTX *dst, const MTX *src ); 00259 00260 /// \brief A becomes A|0|0|0|.. etc 00261 /// 00262 /// \return TRUE if successful, FALSE otherwise. 00263 BOOL MTX_AddZeroValuedColumns( MTX *dst, const unsigned nr_new_cols ); 00264 00265 00266 /// \brief Redimension the matrix, original data is saved in place, new data is set to zero. 00267 /// 00268 /// \return TRUE if successful, FALSE otherwise. 00269 BOOL MTX_Redim( MTX *dst, const unsigned nrows, const unsigned ncols ); 00270 00271 /// \brief Resize the matrix, original data is lost, new data is set to zero, must specify if the matrix is real or complex. 00272 /// 00273 /// \return TRUE if successful, FALSE otherwise. 00274 BOOL MTX_Resize( MTX *dst, const unsigned nrows, const unsigned ncols, const BOOL isReal ); 00275 00276 00277 00278 /// \brief Copy the src data to dst matrix, resize dst if possible & necessary. 00279 /// 00280 /// \return TRUE if successful, FALSE otherwise. 00281 BOOL MTX_Copy( const MTX *src, MTX *dst ); 00282 00283 /// \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. 00284 /// 00285 /// \return TRUE if successful, FALSE otherwise. 00286 BOOL MTX_CopyIntoColumnWiseVector( const MTX *src, MTX *dst ); 00287 00288 /// \brief Set the dst matrix from the static 'c' style matrix indexed by mat[i*ncols + j]. 00289 /// 00290 /// \return TRUE if successful, FALSE otherwise. 00291 BOOL MTX_SetFromStaticMatrix( MTX *dst, const double mat[], const unsigned nrows, const unsigned ncols ); 00292 00293 /// \brief Copy the src data in column col to dst matrix, resize dst if possible & necessary. 00294 /// 00295 /// \return TRUE if successful, FALSE otherwise. 00296 BOOL MTX_CopyColumn( const MTX *src, const unsigned col, MTX *dst ); 00297 00298 /// \brief Copy the src data in row, row, to dst matrix, resize dst if possible & necessary. 00299 /// 00300 /// \return TRUE if successful, FALSE otherwise. 00301 BOOL MTX_CopyRow( const MTX *src, const unsigned row, MTX *dst ); 00302 00303 /// \brief Copy the src data in row 'row' (1xn) to dst matrix (nx1), resize dst if possible & necessary. 00304 /// dst becomes (nx1). 00305 /// 00306 /// \return TRUE if successful, FALSE otherwise. 00307 BOOL MTX_CopyRowIntoAColumnMatrix( const MTX *src, const unsigned row, MTX *dst ); 00308 00309 /// \brief Insert a submatrix (src) into dst, starting at indices dst(row,col). 00310 /// 00311 /// \return TRUE if successful, FALSE otherwise. 00312 BOOL MTX_InsertSubMatrix( MTX *dst, const MTX *src, const unsigned dst_row, const unsigned dst_col ); 00313 00314 /** 00315 \brief Extract a submatrix (dst) from this matrix from (inclusive) 00316 the rows and columns specified. 00317 \code 00318 MTX A; 00319 MTX B; 00320 BOOL result; 00321 MTX_Init( &A ); 00322 MTX_Init( &B ); 00323 00324 result = MTX_SetFromMatrixString( &A, "[1 2 3; 4 5 6; 7 8 9]" ); 00325 result = MTX_ExtractSubMatrix( &A, &B, 1, 0, 2, 2 ); 00326 // B == [4 5 6; 7 8 9] 00327 \endcode 00328 00329 \return TRUE if successful, FALSE otherwise. 00330 */ 00331 BOOL MTX_ExtractSubMatrix( 00332 const MTX* src, //!< The source matrix. 00333 MTX* dst, //!< The destination matrix to contain the submatrix. 00334 const unsigned from_row, //!< The zero-based index for the from row. 00335 const unsigned from_col, //!< The zero-based index for the from column. 00336 const unsigned to_row, //!< The zero-based index for the to row. 00337 const unsigned to_col //!< The zero-based index for the to column. 00338 ); 00339 00340 00341 /// \brief Zero the entire matrix. 00342 /// 00343 /// \return TRUE if successful, FALSE otherwise. 00344 BOOL MTX_Zero( MTX *dst ); 00345 00346 /// \brief Zero all elements in a specified column. 00347 /// 00348 /// \return TRUE if successful, FALSE otherwise. 00349 BOOL MTX_ZeroColumn( MTX *dst, const unsigned col ); 00350 00351 /// \brief Zero all elements in a specified row. 00352 /// 00353 /// \return TRUE if successful, FALSE otherwise. 00354 BOOL MTX_ZeroRow( MTX *dst, const unsigned row ); 00355 00356 /// \brief Fill the matrix with the given value. 00357 /// 00358 /// \return TRUE if successful, FALSE otherwise. 00359 BOOL MTX_Fill( MTX *dst, const double value ); 00360 00361 /// \brief Fill the matrix with the given complex value. 00362 /// 00363 /// \return TRUE if successful, FALSE otherwise. 00364 BOOL MTX_FillComplex( MTX *dst, const double re, const double im ); 00365 00366 /// \brief Fill the matrix column with the given value. 00367 /// 00368 /// \return TRUE if successful, FALSE otherwise. 00369 BOOL MTX_FillColumn( MTX *dst, const unsigned col, const double value ); 00370 00371 /// \brief Fill the matrix column with the given complex value. 00372 /// 00373 /// \return TRUE if successful, FALSE otherwise. 00374 BOOL MTX_FillColumnComplex( MTX *dst, const unsigned col, const double re, const double im ); 00375 00376 /// \brief Fill the matrix row with the given value. 00377 /// 00378 /// \return TRUE if successful, FALSE otherwise. 00379 BOOL MTX_FillRow( MTX *dst, const unsigned row, const double value ); 00380 00381 /// \brief Fill the matrix row with the given complex value. 00382 /// 00383 /// \return TRUE if successful, FALSE otherwise. 00384 BOOL MTX_FillRowComplex( MTX *dst, const unsigned row, const double re, const double im ); 00385 00386 /// \brief Reverse the order of elements of a column. 00387 /// 00388 /// \return TRUE if successful, FALSE otherwise. 00389 BOOL MTX_FlipColumn( MTX *M, const unsigned col ); 00390 00391 /// \brief Reverse the order of elements of a row. 00392 /// 00393 /// \return TRUE if successful, FALSE otherwise. 00394 BOOL MTX_FlipRow( MTX *M, const unsigned row ); 00395 00396 /// \brief Set the matrix to an identity. 00397 /// 00398 /// \return TRUE if successful, FALSE otherwise. 00399 BOOL MTX_Identity( MTX *dst ); 00400 00401 /// \brief Force this square matrix to be symmetric 00402 /// by M = (M + T.())/2 using minimal operations. 00403 /// 00404 /// \return TRUE if successful, FALSE otherwise. 00405 BOOL MTX_ForceSymmetric( MTX *M ); 00406 00407 /// \brief Transpose the matrix src into the matris dst. 00408 /// 00409 /// \return TRUE if successful, FALSE otherwise. 00410 BOOL MTX_Transpose( const MTX *src, MTX *dst ); 00411 00412 /// \brief Transpose the matrix as an inplace operation. 00413 /// 00414 /// \return TRUE if successful, FALSE otherwise. 00415 BOOL MTX_TransposeInplace( MTX *M ); 00416 00417 00418 /// \brief Round the matrix elements to the specified precision.\n 00419 /// e.g. precision = 0 1.8 -> 2\n 00420 /// e.g. precision = 1, 1.45 -> 1.5\n 00421 /// e.g. precision = 2 1.456 -> 1.46\n 00422 /// e.g. precision = 3, 1.4566 -> 1.457\n 00423 /// precision has a maximum of 32. After which no rounding occurs. 00424 /// 00425 /// \return TRUE if successful, FALSE otherwise. 00426 BOOL MTX_Round( MTX *M, const unsigned precision ); 00427 00428 /// \brief Round the matrix elements to the nearest integers towards minus infinity. 00429 /// 00430 /// \return TRUE if successful, FALSE otherwise. 00431 BOOL MTX_Floor( MTX *M ); 00432 00433 /// \brief Round the matrix elements to the nearest integers towards infinity. 00434 /// 00435 /// \return TRUE if successful, FALSE otherwise. 00436 BOOL MTX_Ceil( MTX *M ); 00437 00438 /// \brief Round the matrix elements to the nearest integers towards zero. 00439 /// Sometimes known as trunc(). 00440 /// 00441 /// \return TRUE if successful, FALSE otherwise. 00442 BOOL MTX_Fix( MTX *M ); 00443 00444 00445 /// \brief Set the destination matrix to be 1.0 minus the source matrix. 00446 /// 00447 /// \return TRUE if successful, FALSE otherwise. 00448 BOOL MTX_OneMinus( const MTX* src, MTX *dst ); 00449 00450 00451 /// \brief Determine the matrix file delimiter and if a comment line is available. 00452 /// 00453 /// \return TRUE if successful, FALSE otherwise. 00454 BOOL MTX_DetermineFileDelimiter( 00455 const char *path, //!< path to the input file 00456 char *delimiter, //!< delimiter, 'b' is binary 00457 BOOL *hasComment, //!< BOOL to indicate if a comment line is present 00458 char **comment //!< pointer to a string to store the comment line, *comment memory must be freed later. 00459 ); 00460 00461 /// \brief Determine the size of a file. 00462 /// 00463 /// \return TRUE if successful, FALSE otherwise. 00464 BOOL MTX_DetermineFileSize( const char *path, unsigned *size ); 00465 00466 /// \brief Determine the number of columns in the data string provided. 00467 /// 00468 /// \return TRUE if successful, FALSE otherwise. 00469 BOOL MTX_DetermineNumberOfColumnsInDataString( const char *datastr, unsigned *ncols ); 00470 00471 /// \brief Determine the number of columns in the complex data string provided. 00472 /// The delimiter is needed, 'w' indicates whitespace. 00473 /// 00474 /// \return TRUE if successful, FALSE otherwise. 00475 BOOL MTX_DetermineNumberOfColumnsInDataStringCplx( const char *datastr, const char delimiter, unsigned *ncols ); 00476 00477 00478 00479 /// \brief Read a real-only matrix from a file (ASCII formatted, any common delimiters). 00480 /// This function will also read in MTX BINARY formatted files. 00481 /// 00482 /// \return TRUE if successful, FALSE otherwise. 00483 BOOL MTX_ReadFromFileRealOnly( MTX *M, const char *path ); 00484 00485 00486 /// \brief Read either a real or complex matrix from a file (ASCII formatted, any common delimiters). 00487 /// This function will also read in MTX BINARY formatted files. 00488 /// 00489 /// \return TRUE if successful, FALSE otherwise. 00490 BOOL MTX_ReadFromFile( MTX *M, const char *path ); 00491 00492 00493 /// \brief Set the matrix from a matrix string. 00494 /// 00495 /// \return TRUE if successful, FALSE otherwise. 00496 BOOL MTX_SetFromMatrixString( MTX *M, const char *strMatrix ); 00497 00498 00499 00500 /// \brief Convert a value to a string with the specified width and precision. 00501 /// analogous to sprintf( ValueBuffer, "%'blank''-'width.precision'g'", value ); 00502 /// 00503 /// \return TRUE if successful, FALSE otherwise. 00504 BOOL MTX_ValueToString( 00505 const double value, //!< The double value to output. 00506 const unsigned width, //!< The width of the field. 00507 const unsigned precision, //!< The precision, %g style. 00508 const BOOL isReal, //!< The the value the real part or the imaginary part. 00509 const BOOL alignLeft, //!< Align the output left (for real data only). 00510 char *ValueBuffer, //!< The output buffer. 00511 const unsigned ValueBufferSize //!< The size of the output buffer. 00512 ); 00513 00514 /// \brief Print the matrix to a file with specifed width and precision. 00515 /// MTX_PrintAutoWidth is recommended over this function, "%'blank''-'width.precision'g'". 00516 /// 00517 /// \return TRUE if successful, FALSE otherwise. 00518 BOOL MTX_Print( const MTX *M, const char *path, const unsigned width, const unsigned precision, const BOOL append ); 00519 00520 /// \brief Print the matrix to a buffer of maxlength with specifed width and precision. 00521 /// MTX_PrintAutoWidth is recommended over this function, "%'blank''-'width.precision'g'". 00522 /// 00523 /// \return TRUE if successful, FALSE otherwise. 00524 BOOL MTX_Print_ToBuffer( const MTX *M, char *buffer, const unsigned maxlength, const unsigned width, const unsigned precision ); 00525 00526 /// \brief Print the matrix to a file with automatically determined column width. 00527 /// and the specified precision, uses "%'blank''-'autowidth.precision'g'". 00528 /// 00529 /// \return TRUE if successful, FALSE otherwise. 00530 BOOL MTX_PrintAutoWidth( const MTX *M, const char *path, const unsigned precision, const BOOL append ); 00531 00532 /// \brief Print the matrix to stdout with automatically determined column width. 00533 /// and the specified precision, uses "%'blank''-'autowidth.precision'g'". 00534 /// 00535 /// \return TRUE if successful, FALSE otherwise. 00536 BOOL MTX_PrintStdoutAutoWidth( const MTX *M, const unsigned precision ); 00537 00538 /// \brief Print the matrix to a buffer of maxlenth with automatically determined column width. 00539 /// and the specified precision, uses "%'blank''-'autowidth.precision'g'". 00540 /// 00541 /// \return TRUE if successful, FALSE otherwise. 00542 BOOL MTX_PrintAutoWidth_ToBuffer( const MTX *M, char *buffer, const unsigned maxlength, const unsigned precision ); 00543 00544 /// \brief Print the matrix to a file with specifed precision and delimiter. 00545 /// Use MTX_PrintAutoWidth if print using whitespace as a delimiter is required, uses "%.precision'g'" 00546 /// 00547 /// \return TRUE if successful, FALSE otherwise. 00548 BOOL MTX_PrintDelimited( const MTX *M, const char *path, const unsigned precision, const char delimiter, const BOOL append ); 00549 00550 /// \brief Print the matrix to a file with specifed precision and delimiter. 00551 /// Use MTX_PrintAutoWidth if print using whitespace as a delimiter is required, uses "%.precision'g'". 00552 /// 00553 /// \return TRUE if successful, FALSE otherwise. 00554 BOOL MTX_PrintDelimited_ToBuffer( const MTX *M, char *buffer, const unsigned maxlength, const unsigned precision, const char delimiter ); 00555 00556 /// \brief Print a row to a string buffer. 00557 /// 00558 /// \return TRUE if successful, FALSE otherwise. 00559 BOOL MTX_PrintRowToString( const MTX *M, const unsigned row, char *buffer, const unsigned maxlength, const int width, const int precision ); 00560 00561 00562 //// 00563 // Math operations 00564 00565 /// \brief Adds a scalar double to matrix M, ie: M += 5. 00566 /// 00567 /// \return TRUE if successful, FALSE otherwise. 00568 BOOL MTX_Add_Scalar( MTX *M, const double scalar ); 00569 00570 /// \brief Adds a scalar complex to matrix M, ie: M += (5 + 3i). 00571 /// 00572 /// \return TRUE if successful, FALSE otherwise. 00573 BOOL MTX_Add_ScalarComplex( MTX *M, const double re, const double im ); 00574 00575 /// \brief Subtracts a scalar double from matrix M, ie: M -= 5. 00576 /// 00577 /// \return TRUE if successful, FALSE otherwise. 00578 BOOL MTX_Subtract_Scalar( MTX *M, const double scalar ); 00579 00580 /// \brief Subtracts a scaler complex from matrix M, ie: M -= (5+3i). 00581 /// 00582 /// \return TRUE if successful, FALSE otherwise. 00583 BOOL MTX_Subtract_ScalarComplex( MTX *M, const double re, const double im ); 00584 00585 /// \brief Multiply M with a double scalar inplace, ie: M *= 5. 00586 /// 00587 /// \return TRUE if successful, FALSE otherwise. 00588 BOOL MTX_Multiply_Scalar( MTX *M, const double scalar ); 00589 00590 /// \brief Multiply M with a complex scalar inplace, ie: M *= (5+3i). 00591 /// 00592 /// \return TRUE if successful, FALSE otherwise. 00593 BOOL MTX_Multiply_ScalarComplex( MTX *M, const double re, const double im ); 00594 00595 /// \brief Divide M by scaler double inplace, ie: M /= 5. 00596 /// 00597 /// \return TRUE if successful, FALSE otherwise. 00598 BOOL MTX_Divide_Scalar( MTX *M, const double scalar ); 00599 00600 /// \brief Divide M by scaler complex inplace, ie: M /= (5+3i). 00601 /// 00602 /// \return TRUE if successful, FALSE otherwise. 00603 BOOL MTX_Divide_ScalarComplex( MTX *M, const double re, const double im ); 00604 00605 /// \brief Change the sign of all the data in the matrix. M *= -1. 00606 /// 00607 /// \return TRUE if successful, FALSE otherwise. 00608 BOOL MTX_Negate( MTX *M ); 00609 00610 00611 /// \brief Computes the absolute value of each element in the matrix. 00612 /// 00613 /// \return TRUE if successful, FALSE otherwise. 00614 BOOL MTX_Abs( MTX *M ); 00615 00616 /// \brief Compute the arc-cosine of each element of the matrix inplace. 00617 /// Complex results are obtained if elements are greater than abs(1). 00618 /// 00619 /// \return TRUE if successful, FALSE otherwise. 00620 BOOL MTX_acos( MTX *M ); 00621 00622 /// \brief Compute the phase angle in radians of the elements in the matrix. 00623 /// If all elements are real, the results are 0. 00624 /// 00625 /// \return TRUE if successful, FALSE otherwise. 00626 BOOL MTX_angle( MTX *M ); 00627 00628 /// \brief Compute the arc-sine of each element of the matrix inplace. 00629 /// Complex results are obtained if elements are greater than abs(1). 00630 /// 00631 /// \return TRUE if successful, FALSE otherwise. 00632 BOOL MTX_asin( MTX *M ); 00633 00634 00635 /// \brief Computes the value^2 of each element in the matrix. 00636 /// 00637 /// \return TRUE if successful, FALSE otherwise. 00638 BOOL MTX_Sqr( MTX *M ); 00639 00640 /// \brief Computes the sqrt(value) of each element in the matrix. 00641 /// A real matrix is converted to complex if any elements are negative. 00642 /// e.g. sqrt(-1) = -i. 00643 /// 00644 /// \return TRUE if successful, FALSE otherwise. 00645 BOOL MTX_Sqrt( MTX *M ); 00646 00647 /// \brief If real, computes the exp(value) of each element in the matrix. 00648 /// If complex, computes exp(M) = exp(real)*(cos(imag)+i*sin(imag)). 00649 /// 00650 /// \return TRUE if successful, FALSE otherwise. 00651 BOOL MTX_Exp( MTX *M ); 00652 00653 /// \brief Create an indentity matrix with nrows and ncols. 00654 /// 00655 /// \return TRUE if successful, FALSE otherwise. 00656 BOOL MTX_Eye( MTX *M, const unsigned nrows, const unsigned ncols ); 00657 00658 /// \brief Computes the natural logarithm, ln(value) of each element in the matrix. 00659 /// 00660 /// \return TRUE if successful, FALSE otherwise. 00661 BOOL MTX_Ln( MTX *M ); 00662 00663 /// \brief Raise all elements in src^(power_re + power_im*i) and store in dst. 00664 /// If power is just real, power_im = 0.0. 00665 /// 00666 /// \return TRUE if successful, FALSE otherwise. 00667 BOOL MTX_Pow( const MTX *src, MTX *dst, const double power_re, const double power_im ); 00668 00669 /// \brief Raise all elements in src^(power_re + power_im*i). 00670 /// If power is just real, power_im = 0.0. 00671 /// 00672 /// \return TRUE if successful, FALSE otherwise. 00673 BOOL MTX_PowInplace( MTX *src, const double power_re, const double power_im ); 00674 00675 00676 /// \brief Computes the arctan, atan(value) of each element in the matrix 00677 /// 00678 /// \return TRUE if successful, FALSE otherwise. 00679 BOOL MTX_atan( MTX *M ); 00680 00681 /// \brief Add +1.0 to all elements, e.g. M++. 00682 /// 00683 /// \return TRUE if successful, FALSE otherwise. 00684 BOOL MTX_Increment( MTX *M ); 00685 00686 /// \brief Subtract 1.0 from all elements, e.g. M--. 00687 /// 00688 /// \return TRUE if successful, FALSE otherwise. 00689 BOOL MTX_Decrement( MTX *M ); 00690 00691 /// \brief Add A += B, inplace. 00692 /// 00693 /// \return TRUE if successful, FALSE otherwise. 00694 BOOL MTX_Add_Inplace( MTX *A, const MTX *B ); 00695 00696 /// \brief Subtract A -= B, inplace. 00697 /// 00698 /// \return TRUE if successful, FALSE otherwise. 00699 BOOL MTX_Subtract_Inplace( MTX *A, const MTX *B ); 00700 00701 /// \brief Multiply A = B*A, inplace. 00702 /// 00703 /// \return TRUE if successful, FALSE otherwise. 00704 BOOL MTX_PreMultiply_Inplace( MTX *A, const MTX *B ); // A = B*A 00705 00706 /// \brief Multiply A = tranpose(B)*A, inplace. 00707 /// 00708 /// \return TRUE if successful, FALSE otherwise. 00709 BOOL MTX_TransposePreMultiply_Inplace( MTX *A, const MTX *B ); // A = tranpose(B)*A 00710 00711 /// \brief Multiply A = A*B, inplace. 00712 /// 00713 /// \return TRUE if successful, FALSE otherwise. 00714 BOOL MTX_PostMultiply_Inplace( MTX *A, const MTX* B ); // A = A*B 00715 00716 /// \brief Multiply A = A*transpose(B), inplace. 00717 /// 00718 /// \return TRUE if successful, FALSE otherwise. 00719 BOOL MTX_PostMultiplyTranspose_Inplace( MTX *A, const MTX* B ); // A = A*tranpose(B) 00720 00721 00722 /// \brief Dot multiply A .*= B, inplace (A.data[col][row] = A.data[col][row]*B.data[col][row]). 00723 /// 00724 /// \return TRUE if successful, FALSE otherwise. 00725 BOOL MTX_DotMultiply_Inplace( MTX *A, const MTX *B ); 00726 00727 /// \brief Dot divide A ./= B, inplace (A.data[col][row] = A.data[col][row]/B.data[col][row]). 00728 /// 00729 /// \return TRUE if successful, FALSE otherwise. 00730 BOOL MTX_DotDivide_Inplace( MTX *A, const MTX *B ); 00731 00732 00733 00734 /// \brief Add A = B+C. 00735 /// 00736 /// \return TRUE if successful, FALSE otherwise. 00737 BOOL MTX_Add( MTX *A, const MTX *B, const MTX *C ); 00738 00739 /// \brief Subtract A = B-C. 00740 /// 00741 /// \return TRUE if successful, FALSE otherwise. 00742 BOOL MTX_Subtract( MTX *A, const MTX *B, const MTX *C ); 00743 00744 /// \brief Multiply A = B*C. 00745 /// 00746 /// \return TRUE if successful, FALSE otherwise. 00747 BOOL MTX_Multiply( MTX *A, const MTX *B, const MTX *C ); 00748 00749 /// \brief Multiply A = transpose(B)*C. 00750 /// 00751 /// \return TRUE if successful, FALSE otherwise. 00752 BOOL MTX_TransposeMultiply( MTX *A, const MTX* B, const MTX* C ); 00753 00754 /// \brief Multiply A = B*transpose(C). 00755 /// 00756 /// \return TRUE if successful, FALSE otherwise. 00757 BOOL MTX_MultiplyTranspose( MTX *A, const MTX* B, const MTX* C ); // A = B*transpose(C) 00758 00759 /// \brief Rest if A == B to within the specified tolerance. 00760 /// 00761 /// \return TRUE if successful, FALSE otherwise. 00762 BOOL MTX_IsEqual( const MTX *A, const MTX *B, const double tolerance, BOOL *isEqual ); 00763 00764 00765 /// \brief Add this matrix and an identity matrix. Adds 1.0 to the diagonal even if not square. 00766 /// 00767 /// \return TRUE if successful, FALSE otherwise. 00768 BOOL MTX_AddIdentity( const MTX *src, MTX *dst ); 00769 00770 /// \brief Add this matrix and an identity matrix. Adds 1.0 to the diagonal even if not square. 00771 /// 00772 /// \return TRUE if successful, FALSE otherwise. 00773 BOOL MTX_AddIdentity_Inplace( MTX *src ); 00774 00775 /// \brief Subtract an identity matrix from this matrix. Subtracts 1.0 from the diagonal even if not square. 00776 /// 00777 /// \return TRUE if successful, FALSE otherwise. 00778 BOOL MTX_MinusIdentity( const MTX *src, MTX *dst ); 00779 00780 /// \brief Subtract an identity matrix from this matrix. Subtracts 1.0 from the diagonal even if not square. 00781 /// 00782 /// \return TRUE if successful, FALSE otherwise. 00783 BOOL MTX_MinusIdentity_Inplace( MTX *src ); 00784 00785 /// \brief Subtract this matrix from an identity matrix. Subtracts the diagonal from 1.0 even if not square. 00786 /// 00787 /// \return TRUE if successful, FALSE otherwise. 00788 BOOL MTX_IdentityMinus( const MTX *src, MTX *dst ); 00789 00790 /// \brief Subtract this matrix from an identity matrix. Subtracts the diagonal from 1.0 even if not square. 00791 /// 00792 /// \return TRUE if successful, FALSE otherwise. 00793 BOOL MTX_IdentityMinus_Inplace( MTX *src ); 00794 00795 00796 /// \brief Difference and approximte derivative for column col. 00797 /// The Diff is the column difference vector. 00798 /// diff = col[1:N-2] - col[0:N-1]. 00799 /// 00800 /// \return TRUE if successful, FALSE otherwise. 00801 BOOL MTX_ColumnDiff( const MTX *M, MTX *Diff, const unsigned col ); 00802 00803 /// \brief Difference and approximate derivative. 00804 /// The Diff matrix is composed of the column difference vectors. 00805 /// for(i=0:M-1){ diff_i = col_i[1:N-2] - col_i[0:N-1] } 00806 /// 00807 /// \return TRUE if successful, FALSE otherwise. 00808 BOOL MTX_Diff( const MTX *M, MTX *Diff ); 00809 00810 00811 00812 //// 00813 // Statistics 00814 00815 /// \brief Computes the maximum element in the specified column and its index. 00816 /// If the matrix is real, only the real value, re is set, im = 0. 00817 /// If the matrix is complex, both re and im are set. 00818 /// If there are several equal maximum elements, the first index from the beginning is returned. 00819 /// 00820 /// \return TRUE if successful, FALSE otherwise. 00821 BOOL MTX_MaxColIndex( const MTX *M, const unsigned col, double *re, double *im, unsigned *row ); 00822 00823 /// \brief Computes the maximum element in the specified row and its index. 00824 /// If the matrix is real, only the real value, re is set, im = 0. 00825 /// If the matrix is complex, both re and im are set. 00826 /// If there are several equal maximum elements, the first index from the beginning is returned. 00827 /// 00828 /// \return TRUE if successful, FALSE otherwise. 00829 BOOL MTX_MaxRowIndex( const MTX *M, const unsigned row, double *re, double *im, unsigned *col ); 00830 00831 00832 /// \brief Computes the minimum element in the specified column and its index. 00833 /// If the matrix is real, only the real value, re is set, im = 0. 00834 /// If the matrix is complex, both re and im are set. 00835 /// If there are several equal minimum elements, the first index from the beginning is returned. 00836 /// 00837 /// \return TRUE if successful, FALSE otherwise. 00838 BOOL MTX_MinColIndex( const MTX *M, const unsigned col, double *re, double *im, unsigned *row ); 00839 00840 /// \brief Computes the minimum element in the specified row and its index. 00841 /// If the matrix is real, only the real value, re is set, im = 0. 00842 /// If the matrix is complex, both re and im are set. 00843 /// If there are several equal minimum elements, the first index from the beginning is returned. 00844 /// 00845 /// \return TRUE if successful, FALSE otherwise. 00846 BOOL MTX_MinRowIndex( const MTX *M, const unsigned row, double *re, double *im, unsigned *col ); 00847 00848 00849 /// \brief Computes the absolute maximum element in the specified column and its index. 00850 /// If there are several equal maximum elements, the first index from the beginning is returned. 00851 /// 00852 /// \return TRUE if successful, FALSE otherwise. 00853 BOOL MTX_MaxAbsColIndex( const MTX *M, const unsigned col, double *value, unsigned *row ); 00854 00855 /// \brief Computes the absolue maximum element in the specified row and a its column index. 00856 /// If there are several equal maximum elements, the first index from the beginning is returned. 00857 /// 00858 /// \return TRUE if successful, FALSE otherwise. 00859 BOOL MTX_MaxAbsRowIndex( const MTX *M, const unsigned row, double *value, unsigned *col ); 00860 00861 /// \brief Computes the absolute minimum element in the specified column and its index. 00862 /// If there are several equal minimum elements, the first index from the beginning is returned. 00863 /// 00864 /// \return TRUE if successful, FALSE otherwise. 00865 BOOL MTX_MinAbsColIndex( const MTX *M, const unsigned col, double *value, unsigned *row ); 00866 00867 /// \brief Computes the absolute minimum element in the specified row and its index. 00868 /// If there are several equal minimum elements, the first index from the beginning is returned. 00869 /// 00870 /// \return TRUE if successful, FALSE otherwise. 00871 BOOL MTX_MinAbsRowIndex( const MTX *M, const unsigned row, double *value, unsigned *col ); 00872 00873 00874 /// \brief Computes the maximum element in the specified column. 00875 /// If the matrix is real, only the real value, re is set, im = 0. 00876 /// If the matrix is complex, both re and im are set. 00877 /// 00878 /// \return TRUE if successful, FALSE otherwise. 00879 BOOL MTX_MaxColumn( const MTX *M, const unsigned col, double *re, double *im ); 00880 00881 /// \brief Computes the maximum element in the specified row. 00882 /// If the matrix is real, only the real value, re is set, im = 0. 00883 /// If the matrix is complex, both re and im are set. 00884 /// 00885 /// \return TRUE if successful, FALSE otherwise. 00886 BOOL MTX_MaxRow( const MTX *M, const unsigned row, double *re, double *im ); 00887 00888 00889 /// \brief Computes the minimum element in the specified column. 00890 /// If the matrix is real, only the real value, re is set, im = 0. 00891 /// If the matrix is complex, both re and im are set. 00892 /// 00893 /// \return TRUE if successful, FALSE otherwise. 00894 BOOL MTX_MinColumn( const MTX *M, const unsigned col, double *re, double *im ); 00895 00896 00897 /// \brief Computes the minimum element in the specified row. 00898 /// If the matrix is real, only the real value, re is set, im = 0. 00899 /// If the matrix is complex, both re and im are set. 00900 /// 00901 /// \return TRUE if successful, FALSE otherwise. 00902 BOOL MTX_MinRow( const MTX *M, const unsigned row, double *re, double *im ); 00903 00904 00905 /// \brief Computes the absolute maximum element in the specified column. 00906 /// 00907 /// \return TRUE if successful, FALSE otherwise. 00908 BOOL MTX_MaxAbsColumn( const MTX *M, const unsigned col, double *value ); 00909 00910 /// \brief Computes the absolute maximum element in the specified row. 00911 /// 00912 /// \return TRUE if successful, FALSE otherwise. 00913 BOOL MTX_MaxAbsRow( const MTX *M, const unsigned row, double *value ); 00914 00915 00916 /// \brief Computes the absolute minimum element in the specified column. 00917 /// 00918 /// \return TRUE if successful, FALSE otherwise. 00919 BOOL MTX_MinAbsColumn( const MTX *M, const unsigned col, double *value ); 00920 00921 /// \brief Computes the absolute minimum element in the specified row. 00922 /// 00923 /// \return TRUE if successful, FALSE otherwise. 00924 BOOL MTX_MinAbsRow( const MTX *M, const unsigned row, double *value ); 00925 00926 00927 /// \brief Computes the absolute maximum element for the entire matrix and its row and column index. 00928 /// If there are several equal maximum elements, the first index from the beginning is returned. 00929 /// 00930 /// \return TRUE if successful, FALSE otherwise. 00931 BOOL MTX_MaxAbsIndex( const MTX *M, double* value, unsigned *row, unsigned *col ); 00932 00933 /// \brief Computes the maximum element for the entire matrix and its row and column index. 00934 /// If there are several equal maximum elements, the first index from the beginning is returned. 00935 /// 00936 /// \return TRUE if successful, FALSE otherwise. 00937 BOOL MTX_MaxIndex( const MTX *M, double *re, double *im, unsigned *row, unsigned *col ); 00938 00939 /// \brief Computes the absolute maximum element for the entire matrix. 00940 /// 00941 /// \return TRUE if successful, FALSE otherwise. 00942 BOOL MTX_MaxAbs( const MTX *M, double* value ); 00943 00944 /// \brief Computes the maximum element for the entire matrix. 00945 /// 00946 /// \return TRUE if successful, FALSE otherwise. 00947 BOOL MTX_Max( const MTX *M, double *re, double *im ); 00948 00949 00950 /// \brief Computes the absolute minimum element for the entire matrix and its row and column index. 00951 /// If there are several equal minimum elements, the first index from the beginning is returned. 00952 /// 00953 /// \return TRUE if successful, FALSE otherwise. 00954 BOOL MTX_MinAbsIndex( const MTX *M, double* value, unsigned *row, unsigned *col ); 00955 00956 /// \brief Computes the minimum element for the entire matrix and its row and column index. 00957 /// If there are several equal minimum elements, the first index from the beginning is returned. 00958 /// 00959 /// \return TRUE if successful, FALSE otherwise. 00960 BOOL MTX_MinIndex( const MTX *M, double *re, double *im, unsigned *row, unsigned *col ); 00961 00962 /// \brief Computes the absolute minimum element for the entire matrix. 00963 /// 00964 /// \return TRUE if successful, FALSE otherwise. 00965 BOOL MTX_MinAbs( const MTX *M, double* value ); 00966 00967 /// \brief Computes the minimum element for the entire matrix. 00968 /// 00969 /// \return TRUE if successful, FALSE otherwise. 00970 BOOL MTX_Min( const MTX *M, double *re, double *im ); 00971 00972 00973 /// \brief Computes the range of the data in the specified column. 00974 /// Range = MaxVal - MinVal. 00975 /// If the matrix is real, only the real value, re is set, im = 0. 00976 /// If the matrix is complex, both re and im are set. 00977 /// 00978 /// \return TRUE if successful, FALSE otherwise. 00979 BOOL MTX_ColumnRange( const MTX *M, const unsigned col, double *re, double *im ); 00980 00981 00982 /// \brief Computes the range of the data in the specified row. 00983 /// Range = MaxVal - MinVal. 00984 /// If the matrix is real, only the real value, re is set, im = 0. 00985 /// If the matrix is complex, both re and im are set. 00986 /// 00987 /// \return TRUE if successful, FALSE otherwise. 00988 BOOL MTX_RowRange( const MTX *M, const unsigned row, double *re, double *im ); 00989 00990 /// \brief Computes the range of the data in the matrix. 00991 /// Range = MaxVal - MinVal. 00992 /// If the matrix is real, only the real value, re is set, im = 0. 00993 /// If the matrix is complex, both re and im are set. 00994 /// 00995 /// \return TRUE if successful, FALSE otherwise. 00996 BOOL MTX_Range( const MTX *M, double *re, double *im ); 00997 00998 00999 /// \brief Computes the sum for the specified column. 01000 /// If the matrix is real, only the real value, re is set, im = 0. 01001 /// If the matrix is complex, both re and im are set. 01002 /// 01003 /// \return TRUE if successful, FALSE otherwise. 01004 BOOL MTX_ColumnSum( const MTX *M, const unsigned col, double *re, double *im ); 01005 01006 /// \brief Computes the sum of the absolute values for the specified column. 01007 /// 01008 /// \return TRUE if successful, FALSE otherwise. 01009 BOOL MTX_ColumnSumAbs( const MTX *M, const unsigned col, double *value ); 01010 01011 01012 /// \brief Computes the sum for the specified row. 01013 /// If the matrix is real, only the real value, re is set, im = 0. 01014 /// If the matrix is complex, both re and im are set. 01015 /// 01016 /// \return TRUE if successful, FALSE otherwise. 01017 BOOL MTX_RowSum( const MTX *M, const unsigned row, double *re, double *im ); 01018 01019 /// \brief Computes the sum of the data in the matrix . 01020 /// If the matrix is real, only the real value, re is set, im = 0. 01021 /// If the matrix is complex, both re and im are set. 01022 /// 01023 /// \return TRUE if successful, FALSE otherwise. 01024 BOOL MTX_Sum( const MTX *M, double *re, double *im ); 01025 01026 01027 01028 /// \brief Computes the sample mean for the specified column. 01029 /// If the matrix is real, only the real value, re is set, im = 0. 01030 /// If the matrix is complex, both re and im are set. 01031 /// 01032 /// \return TRUE if successful, FALSE otherwise. 01033 BOOL MTX_ColumnMean( const MTX *M, const unsigned col, double *re, double *im ); 01034 01035 /// \brief Computes the sample mean for the specified row. 01036 /// If the matrix is real, only the real value, re is set, im = 0. 01037 /// If the matrix is complex, both re and im are set. 01038 /// 01039 /// \return TRUE if successful, FALSE otherwise. 01040 BOOL MTX_RowMean( const MTX *M, const unsigned row, double *re, double *im ); 01041 01042 /// \brief Computes the sample mean for the matrix. 01043 /// If the matrix is real, only the real value, re is set, im = 0. 01044 /// If the matrix is complex, both re and im are set. 01045 /// 01046 /// \return TRUE if successful, FALSE otherwise. 01047 BOOL MTX_Mean( const MTX *M, double *re, double *im ); 01048 01049 01050 01051 01052 /// \brief Computes the sample standard deviation for the specified column. 01053 /// 01054 /// \return TRUE if successful, FALSE otherwise. 01055 BOOL MTX_ColumnStdev( const MTX *M, const unsigned col, double *value ); 01056 01057 /// \brief Computes the sample standard deviation for the specified row. 01058 /// 01059 /// \return TRUE if successful, FALSE otherwise. 01060 BOOL MTX_RowStdev( const MTX *M, const unsigned row, double *value ); 01061 01062 /// \brief Computes the sample standard deviation for the matrix. 01063 /// 01064 /// \return TRUE if successful, FALSE otherwise. 01065 BOOL MTX_Stdev( const MTX *M, double *value ); 01066 01067 01068 01069 /// \brief Computes the sample variance for the specified column. 01070 /// 01071 /// \return TRUE if successful, FALSE otherwise. 01072 BOOL MTX_ColumnVar( const MTX *M, const unsigned col, double *value ); 01073 01074 /// \brief Computes the sample variance for the specified row. 01075 /// 01076 /// \return TRUE if successful, FALSE otherwise. 01077 BOOL MTX_RowVar( const MTX *M, const unsigned row, double *value ); 01078 01079 /// \brief Computes the sample variance for the matrix. 01080 /// 01081 /// \return TRUE if successful, FALSE otherwise. 01082 BOOL MTX_Var( const MTX *M, double *value ); 01083 01084 01085 /// \brief Computes the norm of the specified column. 01086 /// If real, norm = sqrt( sum( val*val ) ). 01087 /// If complex, norm = sqrt( sum( val*conjugate(val) ) ). 01088 /// 01089 /// \return TRUE if successful, FALSE otherwise. 01090 BOOL MTX_ColumnNorm( const MTX *M, const unsigned col, double *value ); 01091 01092 /// \brief Computes the norm of the specified row. 01093 /// If real, norm = sqrt( sum( val*val ) ). 01094 /// If complex, norm = sqrt( sum( val*conjugate(val) ) ). 01095 /// 01096 /// \return TRUE if successful, FALSE otherwise. 01097 BOOL MTX_RowNorm( const MTX *M, const unsigned row, double *value ); 01098 01099 /// \brief Computes the norm of the matrix. 01100 /// If real, norm = sqrt( sum( val*val ) ). 01101 /// If complex, norm = sqrt( sum( val*conjugate(val) ) ). 01102 /// 01103 /// \return TRUE if successful, FALSE otherwise. 01104 BOOL MTX_Norm( const MTX *M, double *value ); 01105 01106 01107 /// \brief Computes the sample RMS value for the specified column. 01108 /// 01109 /// \return TRUE if successful, FALSE otherwise. 01110 BOOL MTX_ColumnRMS( const MTX *M, const unsigned col, double *value ); 01111 01112 /// \brief Computes the sample RMS value for the specified row. 01113 /// 01114 /// \return TRUE if successful, FALSE otherwise. 01115 BOOL MTX_RowRMS( const MTX *M, const unsigned row, double *value ); 01116 01117 /// \brief Computes the sample RMS value for the matrix. 01118 /// 01119 /// \return TRUE if successful, FALSE otherwise. 01120 BOOL MTX_RMS( const MTX *M, double *value ); 01121 01122 01123 /// \brief Computes the sample skewness value for the specified column. 01124 /// The skewness is the third central moment divided by the cube of the standard deviation. 01125 /// If the matrix is real, only the real value, re is set, im = 0. 01126 /// If the matrix is complex, both re and im are set. 01127 /// 01128 /// \return TRUE if successful, FALSE otherwise. 01129 BOOL MTX_ColumnSkewness( const MTX *M, const unsigned col, double *re, double* im ); 01130 01131 /// \brief Computes the sample skewness value for the specified row. 01132 /// The skewness is the third central moment divided by the cube of the standard deviation. 01133 /// If the matrix is real, only the real value, re is set, im = 0. 01134 /// If the matrix is complex, both re and im are set. 01135 /// 01136 /// \return TRUE if successful, FALSE otherwise. 01137 BOOL MTX_RowSkewness( const MTX *M, const unsigned row, double *re, double* im ); 01138 01139 /// \brief Computes the sample skewness value for the matrix. 01140 /// The skewness is the third central moment divided by the cube of the standard deviation. 01141 /// If the matrix is real, only the real value, re is set, im = 0. 01142 /// If the matrix is complex, both re and im are set. 01143 /// 01144 /// \return TRUE if successful, FALSE otherwise. 01145 BOOL MTX_Skewness( const MTX *M, double *re, double* im ); 01146 01147 01148 01149 /// \brief Computes the sample kurtosis value for the specified column. 01150 /// The kurtosis is the fourth central moment divided by fourth power of the standard deviation. 01151 /// If the matrix is real, only the real value, re is set, im = 0. 01152 /// If the matrix is complex, both re and im are set. 01153 /// To adjust the computed kurtosis value for bias, subtract 3 from the real component. 01154 /// Reference: http://en.wikipedia.org/wiki/Kurtosis. 01155 /// Reference: http://mathworld.wolfram.com/Kurtosis.html (kurtosis proper is computed). 01156 /// \return TRUE if successful, FALSE otherwise. 01157 // 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}. 01158 BOOL MTX_ColumnKurtosis( const MTX *M, const unsigned col, double *re, double *im ); 01159 01160 /// \brief Computes the sample kurtosis value for the specified row. 01161 /// The kurtosis is the fourth central moment divided by fourth power of the standard deviation. 01162 /// If the matrix is real, only the real value, re is set, im = 0. 01163 /// If the matrix is complex, both re and im are set. 01164 /// To adjust the computed kurtosis value for bias, subtract 3 from the real component. 01165 /// Reference: http://en.wikipedia.org/wiki/Kurtosis. 01166 /// Reference: http://mathworld.wolfram.com/Kurtosis.html (kurtosis proper is computed). 01167 /// 01168 /// \return TRUE if successful, FALSE otherwise. 01169 // 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}. 01170 BOOL MTX_RowKurtosis( const MTX *M, const unsigned row, double *re, double *im ); 01171 01172 01173 /// \brief Computes the sample kurtosis value for the matrix. 01174 /// The kurtosis is the fourth central moment divided by fourth power of the standard deviation. 01175 /// If the matrix is real, only the real value, re is set, im = 0. 01176 /// If the matrix is complex, both re and im are set. 01177 /// To adjust the computed kurtosis value for bias, subtract 3 from the real component. 01178 /// Reference: http://en.wikipedia.org/wiki/Kurtosis. 01179 /// Reference: http://mathworld.wolfram.com/Kurtosis.html (kurtosis proper is computed). 01180 /// 01181 /// \return TRUE if successful, FALSE otherwise. 01182 // 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}. 01183 BOOL MTX_Kurtosis( const MTX *M, double *re, double *im ); 01184 01185 01186 01187 01188 01189 //// 01190 // Matrix specific 01191 01192 /// \brief Computes the trace of M where M is a square matrix. 01193 /// Trace = Sum of diagonal elements. 01194 /// If the matrix is real, only the real value, re is set, im = 0. 01195 /// If the matrix is complex, both re and im are set. 01196 /// 01197 /// \return TRUE if successful, FALSE otherwise. 01198 BOOL MTX_Trace( const MTX *M, double *re, double *im ); 01199 01200 /// \brief Sets the diagonal elements of M into D as a column vector 01201 /// 01202 /// \return TRUE if successful, FALSE otherwise. 01203 BOOL MTX_Diagonal( const MTX *M, MTX *D ); 01204 01205 /// \brief Sorts each column of M in ascending order. 01206 /// If complex, sorts based on magnitude. 01207 /// 01208 /// \return TRUE if successful, FALSE otherwise. 01209 BOOL MTX_SortAscending( MTX *M ); 01210 01211 /// \brief Sorts each column of M in descending order. 01212 /// If complex, sorts based on magnitude. 01213 /// 01214 /// \return TRUE if successful, FALSE otherwise. 01215 BOOL MTX_SortDescending( MTX *M ); 01216 01217 /// \brief Sorts a specific column in ascending order. 01218 /// If complex, sorts based on magnitude. 01219 /// 01220 /// \return TRUE if successful, FALSE otherwise. 01221 BOOL MTX_SortColumnAscending( MTX *M, const unsigned col ); 01222 01223 /// \brief Sorts a specific column in descending order. 01224 /// If complex, sorts based on magnitude. 01225 /// 01226 /// \return TRUE if successful, FALSE otherwise. 01227 BOOL MTX_SortColumnDescending( MTX *M, const unsigned col ); 01228 01229 /// \brief Sorts a specific column in ascending order and fills a MTX column vector with the sorted index. 01230 /// The index vector will be resized if index->nrows != M->nrows 01231 /// If complex, sorts based on magnitude. 01232 /// 01233 /// \return TRUE if successful, FALSE otherwise. 01234 BOOL MTX_SortColumnIndexed( MTX *M, const unsigned col, MTX *index ); 01235 01236 /// \brief Sorts the entire matrix by a specific column. 01237 /// If complex, sorts based on magnitude. 01238 /// 01239 /// \return TRUE if successful, FALSE otherwise. 01240 BOOL MTX_SortByColumn( MTX *M, const unsigned col ); 01241 01242 01243 01244 /// \brief Saves a matrix to the specified file path using a proprietary compressed format. 01245 /// ADVANCED EDITION ONLY! 01246 /// \return TRUE if successful, FALSE otherwise. 01247 BOOL MTX_SaveCompressed( const MTX *M, const char *path ); 01248 01249 01250 /// \brief Loads a binary compressed matrix that was saved using the MTX_SaveCompressed function. 01251 /// 01252 /// \return TRUE if successful, FALSE otherwise. 01253 BOOL MTX_ReadCompressed( MTX *M, const char *path ); 01254 01255 /// \brief Get attributes of the compressed file. 01256 /// 01257 /// \return TRUE if successful, FALSE otherwise. 01258 BOOL MTX_GetCompressedFileAttributes( 01259 const char *path, 01260 unsigned* nrows, 01261 unsigned* ncols, 01262 BOOL* isReal 01263 ); 01264 01265 01266 /// \brief Read an ASCII matrix data file and save it using MTX_SaveCompressed. 01267 /// ADVANCED EDITION ONLY! 01268 /// 01269 /// \return TRUE if successful, FALSE otherwise. 01270 BOOL MTX_LoadAndSave( const char* infilepath, const char* outfilepath ); 01271 01272 /// \brief Read an ASCII matrix data file and save it using MTX_SaveCompressed. 01273 /// This version saves the data to the same base filename and uses the .mtx extension. 01274 /// ADVANCED EDITION ONLY! 01275 /// 01276 /// \return TRUE if successful, FALSE otherwise. 01277 BOOL MTX_LoadAndSaveQuick( const char* infilepath ); 01278 01279 01280 /// \brief Alter the matrix, M, so that its data is within the startTime to the startTime+duration 01281 /// and compensate for any rollovers in the time system (e.g. GPS time in seconds rolls over 01282 /// at 604800.0 s). This function assumes that time is one of the matrix columns and requires 01283 /// this index, the timeColumn. 01284 /// 01285 /// \return TRUE if successful, FALSE otherwise. 01286 BOOL MTX_TimeWindow( 01287 MTX* M, //!< Matrix to be altered 01288 const unsigned timeColumn, //!< The column containing time 01289 const double startTime, //!< The specified start time (inclusive) 01290 const double duration, //!< The duration to include 01291 const double rolloverTime ); //!< The potential time at which system time rolls over 01292 01293 /// \brief Alter the matrix, M, so that its data is within [startTime endTime]. 01294 /// This function assumes that time is one of the matrix columns and requires 01295 /// this index, the timeColumn. 01296 /// 01297 /// \return TRUE if successful, FALSE otherwise. 01298 BOOL MTX_TimeLimit( 01299 MTX* M, //!< Matrix to be altered 01300 const unsigned timeColumn, //!< The column containing time 01301 const double startTime, //!< The specified start time (inclusive) 01302 const double endTime ); //!< The duration to include 01303 01304 01305 /// \brief This function matches matrices in time with specified precision 01306 /// where time is a column of each matrix. This function also 01307 /// allows time to rollover at a specified interval. 01308 /// 01309 /// precision 0 = match to whole number \n 01310 /// precision 1 = match to nearest 0.1 \n 01311 /// precision 2 = match to nearest 0.01 \n 01312 /// etc. \n 01313 /// rolloverTime examples \n 01314 /// GPS time of week (s): rolloverTime= 604800.0 \n 01315 /// hours : rolloverTime = 24.0 \n 01316 /// minutes : rolloverTime = 60.0 \n 01317 /// 01318 /// The time data must be non-decreasing but the time may rollover 01319 /// by the specified amount. 01320 /// e.g. rolloverTime = 60.0 \n 01321 /// 0,1,2,3,4,...59,60,1,2,5,10,60,1,2,3... \n 01322 /// 01323 /// \return TRUE if successful, FALSE otherwise. 01324 BOOL MTX_TimeMatch( 01325 MTX *A, //!< The matrix with interpolation times 01326 const unsigned timeColumnA, //!< The zero based column index for matrix A 01327 MTX *B, //!< The matrix to be interpolated 01328 const unsigned timeColumnB, //!< The zero based column index for matrix B 01329 const unsigned precision, //!< The rounding precision used for time matching, 0 = whole, 1 = 0.1, 2 = 0.01, etc 01330 const double rolloverTime //!< The rollover time, e.g. 60 s for minute based timing, 0.0 means rollovers not allowed 01331 ); 01332 01333 01334 01335 /// \brief This function interpolates Matrix B values by the times defined 01336 /// in the column in Matrix A. Time must be increasing but times can 01337 /// rollover with the specified rolloverTime. 01338 /// 01339 /// This function returns A and B with the same number of rows and 01340 /// time aligned time columns. 01341 /// 01342 /// \return TRUE if successful, FALSE otherwise. 01343 BOOL MTX_Interpolate( 01344 MTX *A, //!< The matrix with interpolation times 01345 const unsigned timeColumnA, //!< The zero based column index for matrix A 01346 MTX *B, //!< The matrix to be interpolated 01347 const unsigned timeColumnB, //!< The zero based column index for matrix B 01348 const double maxInterpolationInterval, //!< The largest interpolation interval allowed 01349 const double rolloverTime //!< The rollover time, e.g. 60 s for minute based timing, 0.0 means rollovers not allowed 01350 ); 01351 01352 01353 /// \brief Compute the inverse, 1.0/x, inplace for each element 01354 /// of the matrix. 01355 /// 01356 /// \return TRUE if successful, FALSE otherwise. 01357 BOOL MTX_Inv( MTX *src ); 01358 01359 01360 /// \brief Compute the inplace inverse of the matrix. 01361 /// Uses fast closed form solutions for: 01362 /// Only for: 1x1, 2x2, 3x3 01363 /// 01364 /// If the matrix is singular, the original matrix is unchanged. 01365 /// 01366 /// \return TRUE if successful, FALSE if empty or has dimensions larger 01367 /// than 3x3, false if singular or not square 01368 BOOL MTX_InvertInPlaceClosedForm( MTX *M ); 01369 01370 /// \brief Compute the inplace inverse of the matrix. 01371 /// Uses fast closed form solutions for: 01372 /// Only for: 1x1, 2x2, 3x3 01373 /// 01374 /// \return TRUE if successful, FALSE if empty or has dimensions larger 01375 /// than 3x3, false if singular or not square 01376 BOOL MTX_InvertClosedForm( const MTX *M, MTX *dst ); 01377 01378 01379 /// \brief Compute the inplace inverse of a matrix. 01380 /// 01381 /// The matrix is first tested to determine if it is a symmetric 01382 /// positive-definite matrix. If so, Cholesky decomposition is used 01383 /// to facilitate the inversion of a lower triangular matrix. If the 01384 /// matrix is not symmetric and positive-definite robust inversion 01385 /// using gaussing elimination is attempted. 01386 /// 01387 /// 3x3 matrices or smaller dimensions are computed using 01388 /// MTX_InvertInPlaceClosedForm. 01389 /// 01390 /// If the matrix is singular, the original matrix is unchanged. 01391 /// 01392 /// \return TRUE if successful, FALSE otherwise. 01393 BOOL MTX_InvertInPlace( MTX *M ); 01394 01395 01396 /// \brief Compute the inverse of a matrix. 01397 /// 01398 /// The matrix is first tested to determine if it is a symmetric 01399 /// positive-definite matrix. If so, Cholesky decomposition is used 01400 /// to facilitate the inversion of a lower triangular matrix. If the 01401 /// matrix is not symmetric and positive-definite robust inversion 01402 /// using gaussing elimination is attempted. 01403 /// 01404 /// 3x3 matrices or smaller dimensions are computed using 01405 /// MTX_InvertClosedForm. 01406 /// 01407 /// If the matrix is singular, the original matrix is unchanged. 01408 /// 01409 /// \return TRUE if successful, FALSE otherwise. 01410 BOOL MTX_Invert( const MTX *src, MTX *dst ); 01411 01412 /// \brief Perfroms an inplace matrix inverse using Gaussian Elimination methods. 01413 /// 01414 /// \return TRUE if successful, FALSE otherwise. 01415 BOOL MTX_InvertInPlaceRobust( MTX *M ); 01416 01417 /// \brief Computes a moving average using N lead samples and M lagging samples 01418 /// for the specified column and stores it in dst. 01419 /// 01420 /// \return TRUE if successful, FALSE otherwise. 01421 BOOL MTX_ColumnMovAvg( const MTX *src, const unsigned col, const unsigned lead, const unsigned lag, MTX *dst ); 01422 01423 /// \brief Computes a moving average using N lead samples and M lagging samples 01424 /// for the matrix and stores it in dst. 01425 /// 01426 /// \return TRUE if successful, FALSE otherwise. 01427 BOOL MTX_MovAvg( const MTX *src, const unsigned lead, const unsigned lag, MTX *dst ); 01428 01429 01430 01431 /// \brief Computes: InvATA = inverse( transpose(A) * A ). 01432 /// 01433 /// \return TRUE if successful, FALSE otherwise. 01434 BOOL MTX_ATAInverse( const MTX *A, MTX *InvATA ); 01435 01436 /// \brief Compute the inplace inverse of a unit lower triangular matrix. 01437 /// An example unit lower triangular matrix is: \n 01438 /// A = [ 1 0 0; \n 01439 /// -2 2 0; \n 01440 /// 4 -3 3 ]; with \n 01441 /// inv(A) = [ 1 0 0; \n 01442 /// 1 1/2 0; \n 01443 /// -1/3 1/2 1/3 ]; \n 01444 /// 01445 /// \return TRUE if successful, FALSE otherwise. 01446 BOOL MTX_LowerTriangularInverseInplace( MTX *src ); 01447 01448 01449 /// \brief Computes the determinatnt of the square matrix M. 01450 /// If the matrix is real, only the real value, re is set, im = 0. 01451 /// If the matrix is complex, both re and im are set. 01452 /// 01453 /// \return TRUE if successful, FALSE otherwise. 01454 BOOL MTX_Det( const MTX *M, double *re, double *im ); 01455 01456 01457 /// \brief LU factorization. 01458 /// Performs a factorization to produce a unit lower triangular matrix, L, 01459 /// an upper triangular matrix, U, and permutation matrix P so that 01460 /// P*X = L*U. 01461 /// P, L and U are copmuted correctly if IsFullRank is set to true. 01462 /// 01463 /// \return TRUE if successful, FALSE otherwise. 01464 BOOL MTX_LUFactorization( const MTX *src, BOOL *IsFullRank, MTX *P, MTX *L, MTX *U ); 01465 01466 01467 /// \brief Retrieve the elements of the matrix specified by the index vectors. 01468 /// The index vectors must be nx1 real vectors. 01469 /// 01470 /// \return TRUE if successful, FALSE otherwise. 01471 BOOL MTX_IndexedValues( const MTX *src, const MTX *row_index, const MTX *col_index, MTX *dst ); 01472 01473 01474 /// \brief Set the elements of the matrix specified by the index vectors. 01475 /// The index vectors must be nx1 real vectors. 01476 /// 01477 /// \return TRUE if successful, FALSE otherwise. 01478 BOOL MTX_SetIndexedValues( MTX *dst, const MTX *row_index, const MTX *col_index, const MTX *src ); 01479 01480 01481 /// \brief Compute the Fast Fourier Transform of each columns in the src matrix and 01482 /// store it in the dst matrix. 01483 /// 01484 /// \return TRUE if successful, FALSE otherwise. 01485 BOOL MTX_FFT( const MTX *src, MTX *dst ); 01486 01487 /// \brief Compute the inverse Fast Fourier Transform of each columns in the src matrix and 01488 /// store it in the dst matrix. 01489 /// 01490 /// \return TRUE if successful, FALSE otherwise. 01491 BOOL MTX_IFFT( const MTX *src, MTX *dst ); 01492 01493 /// \brief Compute the Two-Dimensional Fast Fourier Transform of the src matrix and 01494 /// store it in the dst matrix. 01495 /// 01496 /// \return TRUE if successful, FALSE otherwise. 01497 BOOL MTX_FFT2( const MTX *src, MTX *dst ); 01498 01499 /// \brief Compute the Two-Dimensional Inverse Fast Fourier Transform of the src matrix and 01500 /// store it in the dst matrix. 01501 /// 01502 /// \return TRUE if successful, FALSE otherwise. 01503 BOOL MTX_IFFT2( const MTX *src, MTX *dst ); 01504 01505 01506 /// \brief Compute the inplace Fast Fourier Transform of each column of the matrix. 01507 /// 01508 /// \return TRUE if successful, FALSE otherwise. 01509 BOOL MTX_FFT_Inplace( MTX *src); 01510 01511 /// \brief Compute the inplace inverse Fast Fourier Transform of each column of the matrix. 01512 /// 01513 /// \return TRUE if successful, FALSE otherwise. 01514 BOOL MTX_IFFT_Inplace( MTX *src); 01515 01516 01517 /// \brief Compute the inplace two dimensional Fast Fourier Transform of the matrix. 01518 /// 01519 /// \return TRUE if successful, FALSE otherwise. 01520 BOOL MTX_FFT2_Inplace( MTX *src); 01521 01522 /// \brief Compute the inplace two dimensional inverse Fast Fourier Transform of the matrix. 01523 /// 01524 /// \return TRUE if successful, FALSE otherwise. 01525 BOOL MTX_IFFT2_Inplace( MTX *src); 01526 01527 01528 01529 /// \brief Compute the sine of each element in the matrix. Assumes elements are radians. 01530 /// 01531 /// \return TRUE if successful, FALSE otherwise. 01532 BOOL MTX_sin( MTX *src ); 01533 01534 /// \brief Compute the sin(pi*x)/(pi*) of each element in the matrix. 01535 /// 01536 /// \return TRUE if successful, FALSE otherwise. 01537 BOOL MTX_sinc( MTX *src ); 01538 01539 /// \brief Compute the hyperbolic sine of each element in the matrix. Assumes elements are radians. 01540 /// 01541 /// \return TRUE if successful, FALSE otherwise. 01542 BOOL MTX_sinh( MTX *src ); 01543 01544 /// \brief Compute the inverse hyperbolic sine of each element in the matrix. 01545 /// Results in radians. 01546 /// 01547 /// \return TRUE if successful, FALSE otherwise. 01548 BOOL MTX_asinh( MTX *src ); 01549 01550 /// \brief Compute the cosine of each element in the matrix. Assumes elements are radians. 01551 /// 01552 /// \return TRUE if successful, FALSE otherwise. 01553 BOOL MTX_cos( MTX *src ); 01554 01555 /// \brief Compute the hyperbolic cosine of each element in the matrix. Assumes elements are radians. 01556 /// 01557 /// \return TRUE if successful, FALSE otherwise. 01558 BOOL MTX_cosh( MTX *src ); 01559 01560 /// \brief Compute the inverse hyperbolic cosine of each element in the matrix. 01561 /// Results in radians. 01562 /// 01563 /// \return TRUE if successful, FALSE otherwise. 01564 BOOL MTX_acosh( MTX *src ); 01565 01566 /// \brief Compute the tangent of each element in the matrix. Assumes elements are radians. 01567 /// 01568 /// \return TRUE if successful, FALSE otherwise. 01569 BOOL MTX_tan( MTX *src ); 01570 01571 /// \brief Compute the hyperbolic tangent of each element in the matrix. Assumes elements are radians. 01572 /// 01573 /// \return TRUE if successful, FALSE otherwise. 01574 BOOL MTX_tanh( MTX *src ); 01575 01576 /// \brief Compute the inverse hyperbolic tangent of each element in the matrix. 01577 /// Results in radians. 01578 /// 01579 /// \return TRUE if successful, FALSE otherwise. 01580 BOOL MTX_atanh( MTX *src ); 01581 01582 01583 /// \brief Compute the cotangent of each element in the matrix. Assumes elements are radians. 01584 /// 01585 /// \return TRUE if successful, FALSE otherwise. 01586 BOOL MTX_cot( MTX *src ); 01587 01588 /// \brief Compute the hyperbolic cotangent of each element in the matrix. Assumes elements are radians. 01589 /// 01590 /// \return TRUE if successful, FALSE otherwise. 01591 BOOL MTX_coth( MTX *src ); 01592 01593 01594 01595 /// \brief Create a column vector [start:increment:end) beginning at start 01596 /// with step size of increment until less than or equal to end. 01597 /// Note that arguments must be real scalars. \n 01598 /// e.g. a = 2:2:9 = [2; 4; 6; 8;] \n 01599 /// e.g. b = 2:-2:-9 = [2; 0; -2; -4; -6; -9;] \n 01600 /// 01601 /// \return TRUE if successful, FALSE otherwise. 01602 BOOL MTX_Colon( MTX *dst, const double start, const double increment, const double end ); 01603 01604 01605 /** \brief A very efficient method to remove rows and columns from the matrix. 01606 * 01607 * \code 01608 * MTX A; 01609 * unsigned rows[2]; 01610 * unsigned cols[2]; 01611 * MTX_Init(&A); 01612 * MTX_Calloc( &A, 4, 4 ); 01613 * MTX_Identity( &A ); 01614 * A.data[0][0] = 100.0; 01615 * A.data[2][1] = 10.0; 01616 * A.data[1][2] = 20.0; 01617 * // remove the first row and column and the third row and column. 01618 * rows[0] = 0; 01619 * rows[1] = 2; 01620 * cols[0] = 0; 01621 * cols[1] = 2; 01622 * MTX_RemoveRowsAndColumns( &A, 2, (unsigned*)rows, 2 (unsigned*)cols ); 01623 * // A is now a 2x2 identity matrix. 01624 * \endcode 01625 * 01626 * \return TRUE if successful, FALSE otherwise. 01627 */ 01628 BOOL MTX_RemoveRowsAndColumns( 01629 MTX *src, //!< The pointer to the matrix object. 01630 const unsigned nrows, //!< The number of rows to remove (the length of the rows array). 01631 const unsigned rows[], //!< The array of row indices to remove. 01632 const unsigned ncols, //!< The number of columns to remove (the length of hte cols array). 01633 const unsigned cols[] 01634 ); 01635 01636 01637 /** 01638 \brief Sets the matrix as the NxN hilbert matrix. H_ij = 1.0 / (i+j-1.0) for i=1:N, j=1:N. 01639 01640 \code 01641 MTX H; 01642 BOOL result; 01643 MTX_Init( &H ); 01644 result = MTX_Hilbert( &H, 3); 01645 // H == "[1 1/2 1/3; 1/2 1/3 1/4; 1/3 1/4 1/5]"; 01646 \endcode 01647 01648 \return TRUE if successful, FALSE otherwise. 01649 */ 01650 BOOL MTX_Hilbert( MTX *src, const unsigned N ); 01651 01652 01653 /** 01654 \brief Produce a matrix that is composed of pseudo-random numbers. 01655 Values are elements are standard normal distribution with mean zero, 01656 variance of one and standard of deviation one. N(0,1) 01657 01658 \code 01659 MTX A; 01660 MTX_Init(&A); 01661 MTX_randn( 1000, 1 ); // create a standard-normal distributed random vector of 1000 rows by 1 column. 01662 \endcode 01663 01664 \return TRUE if successful, FALSE otherwise. 01665 */ 01666 BOOL MTX_randn( 01667 MTX* M, 01668 const unsigned nrows, 01669 const unsigned ncols, 01670 const unsigned seed 01671 ); 01672 01673 /** 01674 \brief Produce a matrix that is composed of pseudo-random numbers. 01675 Values are elements are uniform distribution [0,1]. 01676 01677 \code 01678 MTX A; 01679 MTX_Init(&A); 01680 MTX_rand( 1000, 1 ); // create a uniformly distributed random vector of 1000 rows by 1 column. 01681 \endcode 01682 01683 \return TRUE if successful, FALSE otherwise. 01684 */ 01685 BOOL MTX_rand( 01686 MTX* M, 01687 const unsigned nrows, 01688 const unsigned ncols, 01689 const unsigned seed 01690 ); 01691 01692 01693 /// \brief Test if a double value is NaN. 01694 BOOL MTX_IsNAN( double value ); 01695 01696 /// \brief Test if a double value is +INF. 01697 BOOL MTX_IsPostiveINF( double value ); 01698 01699 /// \brief Test if a double value is -INF. 01700 BOOL MTX_IsNegativeINF( double value ); 01701 01702 01703 /// \brief A quick plot, to a RLE compressed windows bitmap file, x_column vs y_column of the matrix M. 01704 /// \return TRUE if successful, FALSE otherwise. 01705 BOOL MTX_PlotQuick( MTX* M, const char* bmpfilename, const unsigned x_col, const unsigned y_col ); 01706 01707 01708 /// \brief These are the supported colors. 01709 typedef enum 01710 { 01711 MTX_WHITE = 0, 01712 MTX_BLACK = 1, 01713 MTX_BLUE = 2, 01714 MTX_GREEN = 3, 01715 MTX_PURPLE = 4, 01716 MTX_MAGENTA = 5, 01717 MTX_DARKBLUE = 6, 01718 MTX_INDIANRED = 7, 01719 MTX_BABYBLUE = 8, 01720 MTX_PAISLYBLUE = 9, 01721 MTX_LIGHTPURPLE = 10, 01722 MTX_DARKPURPLE = 11, 01723 MTX_GREYPURPLE = 12, 01724 MTX_BROWN = 13, 01725 MTX_RED = 14, 01726 MTX_PINK = 15, 01727 MTX_YELLOW = 16, 01728 MTX_ORANGE = 17, 01729 MTX_CYAN = 18, 01730 MTX_LIMEGREEN = 19, 01731 MTX_GREY = 20, 01732 MTX_LIGHTGREY = 21 01733 } MTX_enumColor; 01734 01735 typedef struct 01736 { 01737 MTX* M; //!< A pointer to the series data matrix. 01738 unsigned x_col; //!< The series x data column. 01739 unsigned y_col; //!< The series y data column. 01740 BOOL connected; //!< Are the data points connected. 01741 MTX_enumColor color; //!< The color of the data points/line. 01742 char* label; //!< The series label, NULL if none. 01743 char* units; //!< The series units, NULL if none. 01744 int precision; //!< The number of significant digits in the statistics. 01745 BOOL markOutlierData; //!< Should the outlier data be marked. 01746 } MTX_PLOT_structSeries; 01747 01748 01749 /// \brief A container struct used in MTX_structAxisOptions. 01750 typedef struct 01751 { 01752 BOOL doNotUseDefault; 01753 double val; 01754 } MTX_structAxisSubOption; 01755 01756 /// \brief A container struct for axis options. 01757 typedef struct 01758 { 01759 MTX_structAxisSubOption lowerlimit; 01760 MTX_structAxisSubOption upperlimit; 01761 MTX_structAxisSubOption tickstart; 01762 MTX_structAxisSubOption ticksize; 01763 MTX_structAxisSubOption tickend; 01764 }MTX_structAxisOptions; 01765 01766 01767 /// \brief Plot up to 12 series on one figure directly to an RLE compressed BITMAP file. 01768 /// \return TRUE if successful, FALSE otherwise. 01769 BOOL MTX_Plot( 01770 const char* bmpfilename, //!< The output RLE compressed BITMAP filename. 01771 const char* title, //!< The plot title (NULL if not used). 01772 const unsigned plot_height_cm, //!< The plot height in cm. 01773 const unsigned plot_width_cm, //!< The plot width in cm. 01774 const BOOL includeStats, //!< A boolean to indicate if statistics info should be included on the plot. 01775 const BOOL isXGridOn, //!< A boolean to indicate if the x grid lines are on. 01776 const BOOL isYGridOn, //!< A boolean to indicate if the y grid lines are on. 01777 const char* xlabel, //!< The x axis label (NULL if not used). 01778 const char* ylabel, //!< The y axis label (NULL if not used). 01779 const MTX_structAxisOptions x, //!< Limits and ticks for x. 01780 const MTX_structAxisOptions y, //!< Limits and ticks for y. 01781 const MTX_PLOT_structSeries* series, //!< A pointer to an array of series structs. 01782 const unsigned nrSeries //!< The number of series. 01783 ); 01784 01785 01786 /** 01787 \brief Swap the contents of matrix A with matrix B. 01788 01789 \code 01790 MTX A,B; 01791 MTX_Init( &A ); 01792 MTX_Init( &B ); 01793 MTX_Malloc( &A, 2, 2, TRUE ); 01794 MTX_Malloc( &B, 1, 1, TRUE ); 01795 MTX_Fill( &A, 88.0 ); 01796 MTX_Fill( &B, 44.0 ); 01797 MTX_Swap( &A, &B ); 01798 // A == [44], B == [88 88; 88 88] 01799 MTX_Free( &A ); 01800 MTX_Free( &B ); 01801 \endcode 01802 01803 \return TRUE if succesful, FALSE otherwise. 01804 */ 01805 BOOL MTX_Swap( MTX* A, MTX *B ); 01806 01807 01808 #ifdef _DEBUG 01809 01810 /* Take a filename and return a pointer to its final element. This 01811 function is called on __FILE__ to fix a MSVC nit where __FILE__ 01812 contains the full path to the file. This is bad, because it 01813 confuses users to find the home directory of the person who 01814 compiled the binary in their warrning messages. */ 01815 const char * _shortfile(const char *fname); 01816 #define _SHORT_FILE_ _shortfile(__FILE__) 01817 01818 #if defined(_MSC_VER) && _MSC_VER < 1300 /* Windows compilers before VC7 don't have __FUNCTION__ or __LINE__. */ 01819 #define MTX_ERROR_MSG( msg ) { const char *themsg = msg; if( themsg != NULL ){ printf( "\n%s, %s\n", _SHORT_FILE_, themsg ); }else{ printf( "\n%s, %s, %d, Unknown Error\n", __FILE__, __FUNCTION__, __LINE__ ); } } 01820 #else 01821 #define MTX_ERROR_MSG( msg ) { const char *themsg = msg; if( themsg != NULL ){ printf( "\n%s, %s, %d, %s\n", _SHORT_FILE_, __FUNCTION__, __LINE__, themsg ); }else{ printf( "\n%s, %s, %d, Unknown Error\n", __FILE__, __FUNCTION__, __LINE__ ); } } 01822 #endif 01823 01824 #else 01825 01826 const char* _shortfile(const char *fname); 01827 #define _SHORT_FILE_ _shortfile(__FILE__) 01828 #define MTX_ERROR_MSG( msg ) {} 01829 01830 #endif 01831 01832 01833 /// \brief Compute the LDLt decomposition of a square matrix. 01834 /// This method avoids using square roots and can be used 01835 /// for any square, full rank, symmetrical matrix. 01836 /// 01837 /// \return TRUE if succesful, FALSE otherwise. FALSE if not full rank. 01838 BOOL MTX_LDLt( 01839 MTX* src, //!< src = L*D*Lt 01840 MTX *L, //!< src = L*D*Lt 01841 MTX* d, //!< src = L*D*Lt, d it the vector diagonal of D. 01842 BOOL checkSymmetric //!< Option to enable/disable checking the src matrix for symmetry. Runs faster if the input is known to be symmetric. 01843 ); 01844 01845 /// \brief Compute the UDUt decomposition of a square matrix. 01846 /// This method avoids using square roots and can be used 01847 /// for any square, full rank, symmetrical matrix. 01848 /// 01849 /// \return TRUE if succesful, FALSE otherwise. FALSE if not full rank. 01850 BOOL MTX_UDUt( 01851 MTX* src, //!< src = U*D*Ut 01852 MTX *U, //!< src = U*D*Ut 01853 MTX* d, //!< src = U*D*Ut, d it the vector diagonal of D. 01854 BOOL checkSymmetric //!< Option to enable/disable checking the src matrix for symmetry. 01855 ); 01856 01857 01858 /** 01859 \brief Compute the error function (erf) for all values in the matrix inplace. \n 01860 erf(x) = 2/sqrt(pi) * [integral from 0 to x of]( e^(-t^2) )dt. 01861 01862 \return TRUE if successful, FALSE otherwise. 01863 */ 01864 BOOL MTX_erf_Inplace( MTX* src ); 01865 01866 01867 /** 01868 \brief Compute the inverse error function (erfinv) for all values in the matrix inplace. \n 01869 y = erf(x), compute x given y, i.e. x = erfinv(y). 01870 01871 \return TRUE if successful, FALSE otherwise. 01872 */ 01873 BOOL MTX_erfinv_Inplace( MTX* src ); 01874 01875 /** 01876 \brief Compute the complementary error function (erfc) for all values in the matrix inplace. 01877 erfc(x) = 1 - erf(x) = 2/sqrt(pi) * [integral from x to inf of]( e^(-t^2) )dt. 01878 01879 \return TRUE if successful, FALSE otherwise. 01880 */ 01881 BOOL MTX_erfc_Inplace( MTX* src ); 01882 01883 01884 /** 01885 \brief Compute the inverse complementary error function (erfcinv) for all values in the matrix inplace. 01886 01887 \return TRUE if successful, FALSE otherwise. 01888 */ 01889 BOOL MTX_erfcinv_Inplace( MTX* src ); 01890 01891 01892 /** 01893 \brief Set the index vector so that it contains are the indices of values that are equal 01894 to the value specified with the given tolerance from the column of the src matrix. 01895 01896 \return TRUE if successful, FALSE otherwise. 01897 */ 01898 BOOL MTX_find_column_values_equalto( 01899 const MTX* src, //!< The source matrix to search. 01900 const unsigned col, //!< The zero-based index of the column which is searched. 01901 MTX* indexVector, //!< This is the index vector corresponding to the equal values in the source matrix. 01902 const double re, //!< The real part of the equal to value. 01903 const double im, //!< The imaginary part of the equal to value. 01904 const double tolerance //!< The search tolerance. e.g. 1.0e-12. 01905 ); 01906 01907 /** 01908 \brief Set the index vector so that it contains are the indices of values that are not equal 01909 to the value specified with the given tolerance from the column of the src matrix. 01910 01911 \return TRUE if successful, FALSE otherwise. 01912 */ 01913 BOOL MTX_find_column_values_not_equalto( 01914 const MTX* src, //!< The source matrix to search. 01915 const unsigned col, //!< The zero-based index of the column which is searched. 01916 MTX* indexVector, //!< This is the index vector corresponding to the values that are not equal in the source matrix. 01917 const double re, //!< The real part of the value. 01918 const double im, //!< The imaginary part of the value. 01919 const double tolerance //!< The search tolerance. e.g. 1.0e-12. 01920 ); 01921 01922 01923 /** 01924 \brief Set the index vector so that it contains are the indices of values that are less then 01925 to the value specified with the given tolerance from the column of the src matrix. 01926 Complex matrix values are compared to the value by magnitude (i.e. sqrt(re*re+im*im)). 01927 01928 \return TRUE if successful, FALSE otherwise. 01929 */ 01930 BOOL MTX_find_column_values_less_than( 01931 const MTX* src, //!< The source matrix to search. 01932 const unsigned col, //!< The zero-based index of the column which is searched. 01933 MTX* indexVector, //!< This is the index vector of the values desired. 01934 const double value //!< The comparison value. 01935 ); 01936 01937 01938 /** 01939 \brief Set the index vector so that it contains are the indices of values that are more then 01940 to the value specified with the given tolerance from the column of the src matrix. 01941 Complex matrix values are compared to the value by magnitude (i.e. sqrt(re*re+im*im)). 01942 01943 \return TRUE if successful, FALSE otherwise. 01944 */ 01945 BOOL MTX_find_column_values_more_than( 01946 const MTX* src, //!< The source matrix to search. 01947 const unsigned col, //!< The zero-based index of the column which is searched. 01948 MTX* indexVector, //!< This is the index vector of the values desired. 01949 const double value //!< The comparison value. 01950 ); 01951 01952 01953 01954 01955 01956 #ifdef __cplusplus 01957 } 01958 #endif 01959 01960 01961 #endif // ZENUATICS_MTX_H 01962