/tags/cvs_final/octave-forge/extra/secs1d/src/Ubern.cc

# · C++ · 133 lines · 61 code · 25 blank · 47 comment · 7 complexity · d61a89612e52221427861b9657f3ed58 MD5 · raw file

  1. /*
  2. % This file is part of
  3. %
  4. % SECS2D - A 2-D Drift--Diffusion Semiconductor Device Simulator
  5. % -------------------------------------------------------------------
  6. % Copyright (C) 2004-2006 Carlo de Falco
  7. %
  8. %
  9. %
  10. % SECS2D is free software; you can redistribute it and/or modify
  11. % it under the terms of the GNU General Public License as published by
  12. % the Free Software Foundation; either version 2 of the License, or
  13. % (at your option) any later version.
  14. %
  15. % SECS2D is distributed in the hope that it will be useful,
  16. % but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. % GNU General Public License for more details.
  19. %
  20. % You should have received a copy of the GNU General Public License
  21. % along with SECS2D; if not, write to the Free Software
  22. % Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  23. % USA
  24. */
  25. #include <iostream>
  26. #include <octave/oct.h>
  27. ////////////////////////////////////////////
  28. // Ubern function
  29. // this function, though it does make use
  30. // of liboctave, is not an octve command
  31. // the wrapper to make the command is defined
  32. // below
  33. ////////////////////////////////////////////
  34. int Ubern(const double x, double &bp, double &bn )
  35. {
  36. double xlim=1e-2;
  37. int ii;
  38. double fp,fn,df,segno;
  39. double ax;
  40. ax=fabs(x);
  41. if (ax == 0.0) {
  42. bp=1.;
  43. bn=1.;
  44. goto theend ;
  45. }
  46. if (ax > 80.0){
  47. if (x >0.0){
  48. bp=0.0;
  49. bn=x;
  50. goto theend ;
  51. }else{
  52. bp=-x;
  53. bn=0.0;
  54. goto theend ;
  55. }
  56. }
  57. if (ax > xlim){
  58. bp=x/(exp(x)-1.0);
  59. bn=x+bp;
  60. goto theend ;
  61. }
  62. ii=1;
  63. fp=1.0;fn=1.0;df=1.0;segno=1.0;
  64. while (fabs(df) > 2.2204e-16){
  65. ii++;
  66. segno= -segno;
  67. df=df*x/ii;
  68. fp=fp+df;
  69. fn=fn+segno*df;
  70. bp=1/fp;
  71. bn=1/fn;
  72. }
  73. theend:
  74. return 0;
  75. }
  76. ////////////////////////////////////
  77. // WRAPPER
  78. ////////////////////////////////////
  79. // DEFUN_DLD and the macros that it
  80. // depends on are defined in the
  81. // files defun-dld.h, defun.h,
  82. // and defun-int.h.
  83. //
  84. // Note that the third parameter
  85. // (nargout) is not used, so it is
  86. // omitted from the list of arguments
  87. // to DEFUN_DLD in order to avoid
  88. // the warning from gcc about an
  89. // unused function parameter.
  90. ////////////////////////////////////
  91. DEFUN_DLD (Ubern, args, ,
  92. " [bp,bn]=Ubern(x)\n \
  93. computes Bernoulli function\n \
  94. B(x)=x/(exp(x)-1) corresponding to \n \
  95. to input values Z and -Z, recalling that\n \
  96. B(-Z)=Z+B(Z)\n")
  97. {
  98. // The list of values to return. See the declaration in oct-obj.h
  99. octave_value_list retval;
  100. NDArray X ( args(0).array_value() );
  101. octave_idx_type lx = X.length();
  102. NDArray BP(X),BN(X);
  103. for (octave_idx_type jj=0; jj<lx; jj++)
  104. Ubern(X(jj),BP(jj),BN(jj));
  105. retval (0) = BP;
  106. retval (1) = BN;
  107. return retval ;
  108. }