PageRenderTime 1677ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/cpp/shared/expmv.h

https://bitbucket.org/gaberoo/expotree
C Header | 155 lines | 39 code | 14 blank | 102 comment | 0 complexity | 5776a04604e8a6babf7fb17df978b3cb MD5 | raw file
  1. /*
  2. * Copyright (c) 2012-2014, Gabriel Leventhal, ETH Zurich
  3. * All rights reserved.
  4. *
  5. * Original MATLAB implementation:
  6. * Copyright (c) 2010, Nick Higham and Awad Al-Mohy
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * * Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer
  16. * in the documentation and/or other materials provided with the
  17. * distribution.
  18. * * Neither the name of the ETH Zurich nor the names of its
  19. * contributors may be used to endorse or promote products derived
  20. * from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. * OWNER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  27. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  28. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  29. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. /*
  35. * input
  36. * -----
  37. *
  38. * t : time
  39. * A : matrix of derivatives
  40. * b : starting vector
  41. * M : taylor degree
  42. * prec : required accuracy (double, single, half)
  43. * shift : shift matrix (default true)
  44. * bal : balance matrix (default false)
  45. * N = none
  46. * P = permute only
  47. * S = scale only
  48. * B = scale + permute
  49. * full_term : default false
  50. * prnt : default false
  51. *
  52. *
  53. *
  54. * output
  55. * ------
  56. * mv : number of matrix-vector products
  57. * mvd : (mvd < mv) number of MV products that were used for norm
  58. * estimation
  59. *
  60. * ORIGINAL FUNCTION DESCRIPTION (MATLAB CODE)
  61. * ===========================================
  62. *
  63. * function [f,s,m,mv,mvd,unA] = ...
  64. * %EXPMV Matrix exponential times vector or matrix.
  65. * % [F,S,M,MV,MVD] = EXPMV(t,A,B,[],PREC) computes EXPM(t*A)*B without
  66. * % explicitly forming EXPM(t*A). PREC is the required accuracy, 'double',
  67. * % 'single' or 'half', and defaults to CLASS(A).
  68. * % A total of MV products with A or A^* are used, of which MVD are
  69. * % for norm estimation.
  70. * % The full syntax is
  71. * % [f,s,m,mv,mvd,unA] = expmv(t,A,b,M,prec,shift,bal,full_term,prnt).
  72. * % unA = 1 if the alpha_p were used instead of norm(A).
  73. * % If repeated invocation of EXPMV is required for several values of t
  74. * % or B, it is recommended to provide M as an external parameter as
  75. * % M = SELECT_TAYLOR_DEGREE(A,m_max,p_max,prec,shift,bal,true).
  76. * % This also allows choosing different m_max and p_max.
  77. *
  78. * % Reference: A. H. Al-Mohy and N. J. Higham, Computing the action of
  79. * % the matrix exponential, with an application to exponential
  80. * % integrators. MIMS EPrint 2010.30, The University of Manchester, 2010.
  81. *
  82. * % Awad H. Al-Mohy and Nicholas J. Higham, October 26, 2010.
  83. *
  84. *
  85. *
  86. */
  87. /*
  88. * PARAMETRS
  89. *
  90. * t (input) : scalar in y = exp(tA)*b
  91. * fmv (intput) : matrix-matrix product function
  92. * nf (input) : function to calculate matrix 1-norm
  93. * tf (input) : function to calculate trace
  94. * b (input) : matrix b in y = exp(tA)*b
  95. * M (input) : length of Taylor truncation
  96. * (will be calculated if M <= 0 or M > n)
  97. * prec (input) : desired precision
  98. * shift (input) : should the matrix be shifted?
  99. * bal (intput) : should the matrix be balanced?
  100. *
  101. */
  102. #ifndef __EXPMV_H__
  103. #define __EXPMV_H__
  104. #include <stdlib.h>
  105. #include <stdio.h>
  106. #include <math.h>
  107. #include <string.h>
  108. #include <R.h>
  109. typedef void (*matMat)(char trans, int n1, int n2, double alpha, double* x, double* y, void* pars);
  110. typedef double (*normFunc)(void* pars);
  111. typedef double (*traceFunc)(void* pars);
  112. double inf_norm(int n1, int n2, double* A);
  113. double find_absmax_array(int i1, int i2, const double* x);
  114. void expmv(double t, int n, matMat fmv, normFunc nf, traceFunc tf,
  115. double* b, int ncol, int tmrow, int tmcol, double* tm, int recalcm,
  116. char prec, double shift, char bal, int full_term, int prnt, int* info,
  117. int est_norm, int wrklen, double* wrk, int iwrklen, int* iwrk,
  118. void* pars);
  119. int select_taylor_degree(int n, matMat fmv, double t, double* b, int m,
  120. normFunc nf, int m_max, int p_max, char prec, double* alpha, double* eta,
  121. double* M, int* mv, int* unA, int shift, char bal, int force_estm,
  122. int wrklen, double* wrk, int iwrklen, int* iwrk, void* pars);
  123. void normAm(int n, matMat fmv, double t, int m, double* c, int* mv,
  124. int wrklen, double* wrk, int iwrklen, int* iwrk, void* pars);
  125. void afun_power(char trans, matMat fmv, double t, int n1, int n2,
  126. int m, double* x, double* wrk, void* pars);
  127. /* LAPACK ROUTINES */
  128. #if defined(MKL)
  129. #include <mkl.h>
  130. #else
  131. double dlange_(char* norm, int* M, int* N, double* A, int* LDA, void* WORK);
  132. void dlacon_(int* n, double* v, double* x, int* isgn, double* est, int* kase);
  133. int idamax_(int* n, double* x, int* incx);
  134. #endif
  135. double dscal_(int* n, double* alpha, double* x, int* incx);
  136. void daxpy_(int* n, double* alpha, double* x, int* incx, double* y, int* incy);
  137. double dnrm2_(int*,double*,int*);
  138. void dlacn1_(int* n, int* t, double* v, double* x, int* ldx, double* xold,
  139. int* ldxold, double* wrk, double* h, int* ind, int* indh, double* est,
  140. int* kase, int* iseed, int* info);
  141. #endif