tomoto 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (420) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +3 -0
  3. data/LICENSE.txt +22 -0
  4. data/README.md +123 -0
  5. data/ext/tomoto/ext.cpp +245 -0
  6. data/ext/tomoto/extconf.rb +28 -0
  7. data/lib/tomoto.rb +12 -0
  8. data/lib/tomoto/ct.rb +11 -0
  9. data/lib/tomoto/hdp.rb +11 -0
  10. data/lib/tomoto/lda.rb +67 -0
  11. data/lib/tomoto/version.rb +3 -0
  12. data/vendor/EigenRand/EigenRand/Core.h +1139 -0
  13. data/vendor/EigenRand/EigenRand/Dists/Basic.h +111 -0
  14. data/vendor/EigenRand/EigenRand/Dists/Discrete.h +877 -0
  15. data/vendor/EigenRand/EigenRand/Dists/GammaPoisson.h +108 -0
  16. data/vendor/EigenRand/EigenRand/Dists/NormalExp.h +626 -0
  17. data/vendor/EigenRand/EigenRand/EigenRand +19 -0
  18. data/vendor/EigenRand/EigenRand/Macro.h +24 -0
  19. data/vendor/EigenRand/EigenRand/MorePacketMath.h +978 -0
  20. data/vendor/EigenRand/EigenRand/PacketFilter.h +286 -0
  21. data/vendor/EigenRand/EigenRand/PacketRandomEngine.h +624 -0
  22. data/vendor/EigenRand/EigenRand/RandUtils.h +413 -0
  23. data/vendor/EigenRand/EigenRand/doc.h +220 -0
  24. data/vendor/EigenRand/LICENSE +21 -0
  25. data/vendor/EigenRand/README.md +288 -0
  26. data/vendor/eigen/COPYING.BSD +26 -0
  27. data/vendor/eigen/COPYING.GPL +674 -0
  28. data/vendor/eigen/COPYING.LGPL +502 -0
  29. data/vendor/eigen/COPYING.MINPACK +52 -0
  30. data/vendor/eigen/COPYING.MPL2 +373 -0
  31. data/vendor/eigen/COPYING.README +18 -0
  32. data/vendor/eigen/Eigen/CMakeLists.txt +19 -0
  33. data/vendor/eigen/Eigen/Cholesky +46 -0
  34. data/vendor/eigen/Eigen/CholmodSupport +48 -0
  35. data/vendor/eigen/Eigen/Core +537 -0
  36. data/vendor/eigen/Eigen/Dense +7 -0
  37. data/vendor/eigen/Eigen/Eigen +2 -0
  38. data/vendor/eigen/Eigen/Eigenvalues +61 -0
  39. data/vendor/eigen/Eigen/Geometry +62 -0
  40. data/vendor/eigen/Eigen/Householder +30 -0
  41. data/vendor/eigen/Eigen/IterativeLinearSolvers +48 -0
  42. data/vendor/eigen/Eigen/Jacobi +33 -0
  43. data/vendor/eigen/Eigen/LU +50 -0
  44. data/vendor/eigen/Eigen/MetisSupport +35 -0
  45. data/vendor/eigen/Eigen/OrderingMethods +73 -0
  46. data/vendor/eigen/Eigen/PaStiXSupport +48 -0
  47. data/vendor/eigen/Eigen/PardisoSupport +35 -0
  48. data/vendor/eigen/Eigen/QR +51 -0
  49. data/vendor/eigen/Eigen/QtAlignedMalloc +40 -0
  50. data/vendor/eigen/Eigen/SPQRSupport +34 -0
  51. data/vendor/eigen/Eigen/SVD +51 -0
  52. data/vendor/eigen/Eigen/Sparse +36 -0
  53. data/vendor/eigen/Eigen/SparseCholesky +45 -0
  54. data/vendor/eigen/Eigen/SparseCore +69 -0
  55. data/vendor/eigen/Eigen/SparseLU +46 -0
  56. data/vendor/eigen/Eigen/SparseQR +37 -0
  57. data/vendor/eigen/Eigen/StdDeque +27 -0
  58. data/vendor/eigen/Eigen/StdList +26 -0
  59. data/vendor/eigen/Eigen/StdVector +27 -0
  60. data/vendor/eigen/Eigen/SuperLUSupport +64 -0
  61. data/vendor/eigen/Eigen/UmfPackSupport +40 -0
  62. data/vendor/eigen/Eigen/src/Cholesky/LDLT.h +673 -0
  63. data/vendor/eigen/Eigen/src/Cholesky/LLT.h +542 -0
  64. data/vendor/eigen/Eigen/src/Cholesky/LLT_LAPACKE.h +99 -0
  65. data/vendor/eigen/Eigen/src/CholmodSupport/CholmodSupport.h +639 -0
  66. data/vendor/eigen/Eigen/src/Core/Array.h +329 -0
  67. data/vendor/eigen/Eigen/src/Core/ArrayBase.h +226 -0
  68. data/vendor/eigen/Eigen/src/Core/ArrayWrapper.h +209 -0
  69. data/vendor/eigen/Eigen/src/Core/Assign.h +90 -0
  70. data/vendor/eigen/Eigen/src/Core/AssignEvaluator.h +935 -0
  71. data/vendor/eigen/Eigen/src/Core/Assign_MKL.h +178 -0
  72. data/vendor/eigen/Eigen/src/Core/BandMatrix.h +353 -0
  73. data/vendor/eigen/Eigen/src/Core/Block.h +452 -0
  74. data/vendor/eigen/Eigen/src/Core/BooleanRedux.h +164 -0
  75. data/vendor/eigen/Eigen/src/Core/CommaInitializer.h +160 -0
  76. data/vendor/eigen/Eigen/src/Core/ConditionEstimator.h +175 -0
  77. data/vendor/eigen/Eigen/src/Core/CoreEvaluators.h +1688 -0
  78. data/vendor/eigen/Eigen/src/Core/CoreIterators.h +127 -0
  79. data/vendor/eigen/Eigen/src/Core/CwiseBinaryOp.h +184 -0
  80. data/vendor/eigen/Eigen/src/Core/CwiseNullaryOp.h +866 -0
  81. data/vendor/eigen/Eigen/src/Core/CwiseTernaryOp.h +197 -0
  82. data/vendor/eigen/Eigen/src/Core/CwiseUnaryOp.h +103 -0
  83. data/vendor/eigen/Eigen/src/Core/CwiseUnaryView.h +128 -0
  84. data/vendor/eigen/Eigen/src/Core/DenseBase.h +611 -0
  85. data/vendor/eigen/Eigen/src/Core/DenseCoeffsBase.h +681 -0
  86. data/vendor/eigen/Eigen/src/Core/DenseStorage.h +570 -0
  87. data/vendor/eigen/Eigen/src/Core/Diagonal.h +260 -0
  88. data/vendor/eigen/Eigen/src/Core/DiagonalMatrix.h +343 -0
  89. data/vendor/eigen/Eigen/src/Core/DiagonalProduct.h +28 -0
  90. data/vendor/eigen/Eigen/src/Core/Dot.h +318 -0
  91. data/vendor/eigen/Eigen/src/Core/EigenBase.h +159 -0
  92. data/vendor/eigen/Eigen/src/Core/ForceAlignedAccess.h +146 -0
  93. data/vendor/eigen/Eigen/src/Core/Fuzzy.h +155 -0
  94. data/vendor/eigen/Eigen/src/Core/GeneralProduct.h +455 -0
  95. data/vendor/eigen/Eigen/src/Core/GenericPacketMath.h +593 -0
  96. data/vendor/eigen/Eigen/src/Core/GlobalFunctions.h +187 -0
  97. data/vendor/eigen/Eigen/src/Core/IO.h +225 -0
  98. data/vendor/eigen/Eigen/src/Core/Inverse.h +118 -0
  99. data/vendor/eigen/Eigen/src/Core/Map.h +171 -0
  100. data/vendor/eigen/Eigen/src/Core/MapBase.h +303 -0
  101. data/vendor/eigen/Eigen/src/Core/MathFunctions.h +1415 -0
  102. data/vendor/eigen/Eigen/src/Core/MathFunctionsImpl.h +101 -0
  103. data/vendor/eigen/Eigen/src/Core/Matrix.h +459 -0
  104. data/vendor/eigen/Eigen/src/Core/MatrixBase.h +529 -0
  105. data/vendor/eigen/Eigen/src/Core/NestByValue.h +110 -0
  106. data/vendor/eigen/Eigen/src/Core/NoAlias.h +108 -0
  107. data/vendor/eigen/Eigen/src/Core/NumTraits.h +248 -0
  108. data/vendor/eigen/Eigen/src/Core/PermutationMatrix.h +633 -0
  109. data/vendor/eigen/Eigen/src/Core/PlainObjectBase.h +1035 -0
  110. data/vendor/eigen/Eigen/src/Core/Product.h +186 -0
  111. data/vendor/eigen/Eigen/src/Core/ProductEvaluators.h +1112 -0
  112. data/vendor/eigen/Eigen/src/Core/Random.h +182 -0
  113. data/vendor/eigen/Eigen/src/Core/Redux.h +505 -0
  114. data/vendor/eigen/Eigen/src/Core/Ref.h +283 -0
  115. data/vendor/eigen/Eigen/src/Core/Replicate.h +142 -0
  116. data/vendor/eigen/Eigen/src/Core/ReturnByValue.h +117 -0
  117. data/vendor/eigen/Eigen/src/Core/Reverse.h +211 -0
  118. data/vendor/eigen/Eigen/src/Core/Select.h +162 -0
  119. data/vendor/eigen/Eigen/src/Core/SelfAdjointView.h +352 -0
  120. data/vendor/eigen/Eigen/src/Core/SelfCwiseBinaryOp.h +47 -0
  121. data/vendor/eigen/Eigen/src/Core/Solve.h +188 -0
  122. data/vendor/eigen/Eigen/src/Core/SolveTriangular.h +235 -0
  123. data/vendor/eigen/Eigen/src/Core/SolverBase.h +130 -0
  124. data/vendor/eigen/Eigen/src/Core/StableNorm.h +221 -0
  125. data/vendor/eigen/Eigen/src/Core/Stride.h +111 -0
  126. data/vendor/eigen/Eigen/src/Core/Swap.h +67 -0
  127. data/vendor/eigen/Eigen/src/Core/Transpose.h +403 -0
  128. data/vendor/eigen/Eigen/src/Core/Transpositions.h +407 -0
  129. data/vendor/eigen/Eigen/src/Core/TriangularMatrix.h +983 -0
  130. data/vendor/eigen/Eigen/src/Core/VectorBlock.h +96 -0
  131. data/vendor/eigen/Eigen/src/Core/VectorwiseOp.h +695 -0
  132. data/vendor/eigen/Eigen/src/Core/Visitor.h +273 -0
  133. data/vendor/eigen/Eigen/src/Core/arch/AVX/Complex.h +451 -0
  134. data/vendor/eigen/Eigen/src/Core/arch/AVX/MathFunctions.h +439 -0
  135. data/vendor/eigen/Eigen/src/Core/arch/AVX/PacketMath.h +637 -0
  136. data/vendor/eigen/Eigen/src/Core/arch/AVX/TypeCasting.h +51 -0
  137. data/vendor/eigen/Eigen/src/Core/arch/AVX512/MathFunctions.h +391 -0
  138. data/vendor/eigen/Eigen/src/Core/arch/AVX512/PacketMath.h +1316 -0
  139. data/vendor/eigen/Eigen/src/Core/arch/AltiVec/Complex.h +430 -0
  140. data/vendor/eigen/Eigen/src/Core/arch/AltiVec/MathFunctions.h +322 -0
  141. data/vendor/eigen/Eigen/src/Core/arch/AltiVec/PacketMath.h +1061 -0
  142. data/vendor/eigen/Eigen/src/Core/arch/CUDA/Complex.h +103 -0
  143. data/vendor/eigen/Eigen/src/Core/arch/CUDA/Half.h +674 -0
  144. data/vendor/eigen/Eigen/src/Core/arch/CUDA/MathFunctions.h +91 -0
  145. data/vendor/eigen/Eigen/src/Core/arch/CUDA/PacketMath.h +333 -0
  146. data/vendor/eigen/Eigen/src/Core/arch/CUDA/PacketMathHalf.h +1124 -0
  147. data/vendor/eigen/Eigen/src/Core/arch/CUDA/TypeCasting.h +212 -0
  148. data/vendor/eigen/Eigen/src/Core/arch/Default/ConjHelper.h +29 -0
  149. data/vendor/eigen/Eigen/src/Core/arch/Default/Settings.h +49 -0
  150. data/vendor/eigen/Eigen/src/Core/arch/NEON/Complex.h +490 -0
  151. data/vendor/eigen/Eigen/src/Core/arch/NEON/MathFunctions.h +91 -0
  152. data/vendor/eigen/Eigen/src/Core/arch/NEON/PacketMath.h +760 -0
  153. data/vendor/eigen/Eigen/src/Core/arch/SSE/Complex.h +471 -0
  154. data/vendor/eigen/Eigen/src/Core/arch/SSE/MathFunctions.h +562 -0
  155. data/vendor/eigen/Eigen/src/Core/arch/SSE/PacketMath.h +895 -0
  156. data/vendor/eigen/Eigen/src/Core/arch/SSE/TypeCasting.h +77 -0
  157. data/vendor/eigen/Eigen/src/Core/arch/ZVector/Complex.h +397 -0
  158. data/vendor/eigen/Eigen/src/Core/arch/ZVector/MathFunctions.h +137 -0
  159. data/vendor/eigen/Eigen/src/Core/arch/ZVector/PacketMath.h +945 -0
  160. data/vendor/eigen/Eigen/src/Core/functors/AssignmentFunctors.h +168 -0
  161. data/vendor/eigen/Eigen/src/Core/functors/BinaryFunctors.h +475 -0
  162. data/vendor/eigen/Eigen/src/Core/functors/NullaryFunctors.h +188 -0
  163. data/vendor/eigen/Eigen/src/Core/functors/StlFunctors.h +136 -0
  164. data/vendor/eigen/Eigen/src/Core/functors/TernaryFunctors.h +25 -0
  165. data/vendor/eigen/Eigen/src/Core/functors/UnaryFunctors.h +792 -0
  166. data/vendor/eigen/Eigen/src/Core/products/GeneralBlockPanelKernel.h +2156 -0
  167. data/vendor/eigen/Eigen/src/Core/products/GeneralMatrixMatrix.h +492 -0
  168. data/vendor/eigen/Eigen/src/Core/products/GeneralMatrixMatrixTriangular.h +311 -0
  169. data/vendor/eigen/Eigen/src/Core/products/GeneralMatrixMatrixTriangular_BLAS.h +145 -0
  170. data/vendor/eigen/Eigen/src/Core/products/GeneralMatrixMatrix_BLAS.h +122 -0
  171. data/vendor/eigen/Eigen/src/Core/products/GeneralMatrixVector.h +619 -0
  172. data/vendor/eigen/Eigen/src/Core/products/GeneralMatrixVector_BLAS.h +136 -0
  173. data/vendor/eigen/Eigen/src/Core/products/Parallelizer.h +163 -0
  174. data/vendor/eigen/Eigen/src/Core/products/SelfadjointMatrixMatrix.h +521 -0
  175. data/vendor/eigen/Eigen/src/Core/products/SelfadjointMatrixMatrix_BLAS.h +287 -0
  176. data/vendor/eigen/Eigen/src/Core/products/SelfadjointMatrixVector.h +260 -0
  177. data/vendor/eigen/Eigen/src/Core/products/SelfadjointMatrixVector_BLAS.h +118 -0
  178. data/vendor/eigen/Eigen/src/Core/products/SelfadjointProduct.h +133 -0
  179. data/vendor/eigen/Eigen/src/Core/products/SelfadjointRank2Update.h +93 -0
  180. data/vendor/eigen/Eigen/src/Core/products/TriangularMatrixMatrix.h +466 -0
  181. data/vendor/eigen/Eigen/src/Core/products/TriangularMatrixMatrix_BLAS.h +315 -0
  182. data/vendor/eigen/Eigen/src/Core/products/TriangularMatrixVector.h +350 -0
  183. data/vendor/eigen/Eigen/src/Core/products/TriangularMatrixVector_BLAS.h +255 -0
  184. data/vendor/eigen/Eigen/src/Core/products/TriangularSolverMatrix.h +335 -0
  185. data/vendor/eigen/Eigen/src/Core/products/TriangularSolverMatrix_BLAS.h +163 -0
  186. data/vendor/eigen/Eigen/src/Core/products/TriangularSolverVector.h +145 -0
  187. data/vendor/eigen/Eigen/src/Core/util/BlasUtil.h +398 -0
  188. data/vendor/eigen/Eigen/src/Core/util/Constants.h +547 -0
  189. data/vendor/eigen/Eigen/src/Core/util/DisableStupidWarnings.h +83 -0
  190. data/vendor/eigen/Eigen/src/Core/util/ForwardDeclarations.h +302 -0
  191. data/vendor/eigen/Eigen/src/Core/util/MKL_support.h +130 -0
  192. data/vendor/eigen/Eigen/src/Core/util/Macros.h +1001 -0
  193. data/vendor/eigen/Eigen/src/Core/util/Memory.h +993 -0
  194. data/vendor/eigen/Eigen/src/Core/util/Meta.h +534 -0
  195. data/vendor/eigen/Eigen/src/Core/util/NonMPL2.h +3 -0
  196. data/vendor/eigen/Eigen/src/Core/util/ReenableStupidWarnings.h +27 -0
  197. data/vendor/eigen/Eigen/src/Core/util/StaticAssert.h +218 -0
  198. data/vendor/eigen/Eigen/src/Core/util/XprHelper.h +821 -0
  199. data/vendor/eigen/Eigen/src/Eigenvalues/ComplexEigenSolver.h +346 -0
  200. data/vendor/eigen/Eigen/src/Eigenvalues/ComplexSchur.h +459 -0
  201. data/vendor/eigen/Eigen/src/Eigenvalues/ComplexSchur_LAPACKE.h +91 -0
  202. data/vendor/eigen/Eigen/src/Eigenvalues/EigenSolver.h +622 -0
  203. data/vendor/eigen/Eigen/src/Eigenvalues/GeneralizedEigenSolver.h +418 -0
  204. data/vendor/eigen/Eigen/src/Eigenvalues/GeneralizedSelfAdjointEigenSolver.h +226 -0
  205. data/vendor/eigen/Eigen/src/Eigenvalues/HessenbergDecomposition.h +374 -0
  206. data/vendor/eigen/Eigen/src/Eigenvalues/MatrixBaseEigenvalues.h +158 -0
  207. data/vendor/eigen/Eigen/src/Eigenvalues/RealQZ.h +654 -0
  208. data/vendor/eigen/Eigen/src/Eigenvalues/RealSchur.h +546 -0
  209. data/vendor/eigen/Eigen/src/Eigenvalues/RealSchur_LAPACKE.h +77 -0
  210. data/vendor/eigen/Eigen/src/Eigenvalues/SelfAdjointEigenSolver.h +870 -0
  211. data/vendor/eigen/Eigen/src/Eigenvalues/SelfAdjointEigenSolver_LAPACKE.h +87 -0
  212. data/vendor/eigen/Eigen/src/Eigenvalues/Tridiagonalization.h +556 -0
  213. data/vendor/eigen/Eigen/src/Geometry/AlignedBox.h +392 -0
  214. data/vendor/eigen/Eigen/src/Geometry/AngleAxis.h +247 -0
  215. data/vendor/eigen/Eigen/src/Geometry/EulerAngles.h +114 -0
  216. data/vendor/eigen/Eigen/src/Geometry/Homogeneous.h +497 -0
  217. data/vendor/eigen/Eigen/src/Geometry/Hyperplane.h +282 -0
  218. data/vendor/eigen/Eigen/src/Geometry/OrthoMethods.h +234 -0
  219. data/vendor/eigen/Eigen/src/Geometry/ParametrizedLine.h +195 -0
  220. data/vendor/eigen/Eigen/src/Geometry/Quaternion.h +814 -0
  221. data/vendor/eigen/Eigen/src/Geometry/Rotation2D.h +199 -0
  222. data/vendor/eigen/Eigen/src/Geometry/RotationBase.h +206 -0
  223. data/vendor/eigen/Eigen/src/Geometry/Scaling.h +170 -0
  224. data/vendor/eigen/Eigen/src/Geometry/Transform.h +1542 -0
  225. data/vendor/eigen/Eigen/src/Geometry/Translation.h +208 -0
  226. data/vendor/eigen/Eigen/src/Geometry/Umeyama.h +166 -0
  227. data/vendor/eigen/Eigen/src/Geometry/arch/Geometry_SSE.h +161 -0
  228. data/vendor/eigen/Eigen/src/Householder/BlockHouseholder.h +103 -0
  229. data/vendor/eigen/Eigen/src/Householder/Householder.h +172 -0
  230. data/vendor/eigen/Eigen/src/Householder/HouseholderSequence.h +470 -0
  231. data/vendor/eigen/Eigen/src/IterativeLinearSolvers/BasicPreconditioners.h +226 -0
  232. data/vendor/eigen/Eigen/src/IterativeLinearSolvers/BiCGSTAB.h +228 -0
  233. data/vendor/eigen/Eigen/src/IterativeLinearSolvers/ConjugateGradient.h +246 -0
  234. data/vendor/eigen/Eigen/src/IterativeLinearSolvers/IncompleteCholesky.h +400 -0
  235. data/vendor/eigen/Eigen/src/IterativeLinearSolvers/IncompleteLUT.h +462 -0
  236. data/vendor/eigen/Eigen/src/IterativeLinearSolvers/IterativeSolverBase.h +394 -0
  237. data/vendor/eigen/Eigen/src/IterativeLinearSolvers/LeastSquareConjugateGradient.h +216 -0
  238. data/vendor/eigen/Eigen/src/IterativeLinearSolvers/SolveWithGuess.h +115 -0
  239. data/vendor/eigen/Eigen/src/Jacobi/Jacobi.h +462 -0
  240. data/vendor/eigen/Eigen/src/LU/Determinant.h +101 -0
  241. data/vendor/eigen/Eigen/src/LU/FullPivLU.h +891 -0
  242. data/vendor/eigen/Eigen/src/LU/InverseImpl.h +415 -0
  243. data/vendor/eigen/Eigen/src/LU/PartialPivLU.h +611 -0
  244. data/vendor/eigen/Eigen/src/LU/PartialPivLU_LAPACKE.h +83 -0
  245. data/vendor/eigen/Eigen/src/LU/arch/Inverse_SSE.h +338 -0
  246. data/vendor/eigen/Eigen/src/MetisSupport/MetisSupport.h +137 -0
  247. data/vendor/eigen/Eigen/src/OrderingMethods/Amd.h +445 -0
  248. data/vendor/eigen/Eigen/src/OrderingMethods/Eigen_Colamd.h +1843 -0
  249. data/vendor/eigen/Eigen/src/OrderingMethods/Ordering.h +157 -0
  250. data/vendor/eigen/Eigen/src/PaStiXSupport/PaStiXSupport.h +678 -0
  251. data/vendor/eigen/Eigen/src/PardisoSupport/PardisoSupport.h +543 -0
  252. data/vendor/eigen/Eigen/src/QR/ColPivHouseholderQR.h +653 -0
  253. data/vendor/eigen/Eigen/src/QR/ColPivHouseholderQR_LAPACKE.h +97 -0
  254. data/vendor/eigen/Eigen/src/QR/CompleteOrthogonalDecomposition.h +562 -0
  255. data/vendor/eigen/Eigen/src/QR/FullPivHouseholderQR.h +676 -0
  256. data/vendor/eigen/Eigen/src/QR/HouseholderQR.h +409 -0
  257. data/vendor/eigen/Eigen/src/QR/HouseholderQR_LAPACKE.h +68 -0
  258. data/vendor/eigen/Eigen/src/SPQRSupport/SuiteSparseQRSupport.h +313 -0
  259. data/vendor/eigen/Eigen/src/SVD/BDCSVD.h +1246 -0
  260. data/vendor/eigen/Eigen/src/SVD/JacobiSVD.h +804 -0
  261. data/vendor/eigen/Eigen/src/SVD/JacobiSVD_LAPACKE.h +91 -0
  262. data/vendor/eigen/Eigen/src/SVD/SVDBase.h +315 -0
  263. data/vendor/eigen/Eigen/src/SVD/UpperBidiagonalization.h +414 -0
  264. data/vendor/eigen/Eigen/src/SparseCholesky/SimplicialCholesky.h +689 -0
  265. data/vendor/eigen/Eigen/src/SparseCholesky/SimplicialCholesky_impl.h +199 -0
  266. data/vendor/eigen/Eigen/src/SparseCore/AmbiVector.h +377 -0
  267. data/vendor/eigen/Eigen/src/SparseCore/CompressedStorage.h +258 -0
  268. data/vendor/eigen/Eigen/src/SparseCore/ConservativeSparseSparseProduct.h +352 -0
  269. data/vendor/eigen/Eigen/src/SparseCore/MappedSparseMatrix.h +67 -0
  270. data/vendor/eigen/Eigen/src/SparseCore/SparseAssign.h +216 -0
  271. data/vendor/eigen/Eigen/src/SparseCore/SparseBlock.h +603 -0
  272. data/vendor/eigen/Eigen/src/SparseCore/SparseColEtree.h +206 -0
  273. data/vendor/eigen/Eigen/src/SparseCore/SparseCompressedBase.h +341 -0
  274. data/vendor/eigen/Eigen/src/SparseCore/SparseCwiseBinaryOp.h +726 -0
  275. data/vendor/eigen/Eigen/src/SparseCore/SparseCwiseUnaryOp.h +148 -0
  276. data/vendor/eigen/Eigen/src/SparseCore/SparseDenseProduct.h +320 -0
  277. data/vendor/eigen/Eigen/src/SparseCore/SparseDiagonalProduct.h +138 -0
  278. data/vendor/eigen/Eigen/src/SparseCore/SparseDot.h +98 -0
  279. data/vendor/eigen/Eigen/src/SparseCore/SparseFuzzy.h +29 -0
  280. data/vendor/eigen/Eigen/src/SparseCore/SparseMap.h +305 -0
  281. data/vendor/eigen/Eigen/src/SparseCore/SparseMatrix.h +1403 -0
  282. data/vendor/eigen/Eigen/src/SparseCore/SparseMatrixBase.h +405 -0
  283. data/vendor/eigen/Eigen/src/SparseCore/SparsePermutation.h +178 -0
  284. data/vendor/eigen/Eigen/src/SparseCore/SparseProduct.h +169 -0
  285. data/vendor/eigen/Eigen/src/SparseCore/SparseRedux.h +49 -0
  286. data/vendor/eigen/Eigen/src/SparseCore/SparseRef.h +397 -0
  287. data/vendor/eigen/Eigen/src/SparseCore/SparseSelfAdjointView.h +656 -0
  288. data/vendor/eigen/Eigen/src/SparseCore/SparseSolverBase.h +124 -0
  289. data/vendor/eigen/Eigen/src/SparseCore/SparseSparseProductWithPruning.h +198 -0
  290. data/vendor/eigen/Eigen/src/SparseCore/SparseTranspose.h +92 -0
  291. data/vendor/eigen/Eigen/src/SparseCore/SparseTriangularView.h +189 -0
  292. data/vendor/eigen/Eigen/src/SparseCore/SparseUtil.h +178 -0
  293. data/vendor/eigen/Eigen/src/SparseCore/SparseVector.h +478 -0
  294. data/vendor/eigen/Eigen/src/SparseCore/SparseView.h +253 -0
  295. data/vendor/eigen/Eigen/src/SparseCore/TriangularSolver.h +315 -0
  296. data/vendor/eigen/Eigen/src/SparseLU/SparseLU.h +773 -0
  297. data/vendor/eigen/Eigen/src/SparseLU/SparseLUImpl.h +66 -0
  298. data/vendor/eigen/Eigen/src/SparseLU/SparseLU_Memory.h +226 -0
  299. data/vendor/eigen/Eigen/src/SparseLU/SparseLU_Structs.h +110 -0
  300. data/vendor/eigen/Eigen/src/SparseLU/SparseLU_SupernodalMatrix.h +301 -0
  301. data/vendor/eigen/Eigen/src/SparseLU/SparseLU_Utils.h +80 -0
  302. data/vendor/eigen/Eigen/src/SparseLU/SparseLU_column_bmod.h +181 -0
  303. data/vendor/eigen/Eigen/src/SparseLU/SparseLU_column_dfs.h +179 -0
  304. data/vendor/eigen/Eigen/src/SparseLU/SparseLU_copy_to_ucol.h +107 -0
  305. data/vendor/eigen/Eigen/src/SparseLU/SparseLU_gemm_kernel.h +280 -0
  306. data/vendor/eigen/Eigen/src/SparseLU/SparseLU_heap_relax_snode.h +126 -0
  307. data/vendor/eigen/Eigen/src/SparseLU/SparseLU_kernel_bmod.h +130 -0
  308. data/vendor/eigen/Eigen/src/SparseLU/SparseLU_panel_bmod.h +223 -0
  309. data/vendor/eigen/Eigen/src/SparseLU/SparseLU_panel_dfs.h +258 -0
  310. data/vendor/eigen/Eigen/src/SparseLU/SparseLU_pivotL.h +137 -0
  311. data/vendor/eigen/Eigen/src/SparseLU/SparseLU_pruneL.h +136 -0
  312. data/vendor/eigen/Eigen/src/SparseLU/SparseLU_relax_snode.h +83 -0
  313. data/vendor/eigen/Eigen/src/SparseQR/SparseQR.h +745 -0
  314. data/vendor/eigen/Eigen/src/StlSupport/StdDeque.h +126 -0
  315. data/vendor/eigen/Eigen/src/StlSupport/StdList.h +106 -0
  316. data/vendor/eigen/Eigen/src/StlSupport/StdVector.h +131 -0
  317. data/vendor/eigen/Eigen/src/StlSupport/details.h +84 -0
  318. data/vendor/eigen/Eigen/src/SuperLUSupport/SuperLUSupport.h +1027 -0
  319. data/vendor/eigen/Eigen/src/UmfPackSupport/UmfPackSupport.h +506 -0
  320. data/vendor/eigen/Eigen/src/misc/Image.h +82 -0
  321. data/vendor/eigen/Eigen/src/misc/Kernel.h +79 -0
  322. data/vendor/eigen/Eigen/src/misc/RealSvd2x2.h +55 -0
  323. data/vendor/eigen/Eigen/src/misc/blas.h +440 -0
  324. data/vendor/eigen/Eigen/src/misc/lapack.h +152 -0
  325. data/vendor/eigen/Eigen/src/misc/lapacke.h +16291 -0
  326. data/vendor/eigen/Eigen/src/misc/lapacke_mangling.h +17 -0
  327. data/vendor/eigen/Eigen/src/plugins/ArrayCwiseBinaryOps.h +332 -0
  328. data/vendor/eigen/Eigen/src/plugins/ArrayCwiseUnaryOps.h +552 -0
  329. data/vendor/eigen/Eigen/src/plugins/BlockMethods.h +1058 -0
  330. data/vendor/eigen/Eigen/src/plugins/CommonCwiseBinaryOps.h +115 -0
  331. data/vendor/eigen/Eigen/src/plugins/CommonCwiseUnaryOps.h +163 -0
  332. data/vendor/eigen/Eigen/src/plugins/MatrixCwiseBinaryOps.h +152 -0
  333. data/vendor/eigen/Eigen/src/plugins/MatrixCwiseUnaryOps.h +85 -0
  334. data/vendor/eigen/README.md +3 -0
  335. data/vendor/eigen/bench/README.txt +55 -0
  336. data/vendor/eigen/bench/btl/COPYING +340 -0
  337. data/vendor/eigen/bench/btl/README +154 -0
  338. data/vendor/eigen/bench/tensors/README +21 -0
  339. data/vendor/eigen/blas/README.txt +6 -0
  340. data/vendor/eigen/demos/mandelbrot/README +10 -0
  341. data/vendor/eigen/demos/mix_eigen_and_c/README +9 -0
  342. data/vendor/eigen/demos/opengl/README +13 -0
  343. data/vendor/eigen/unsupported/Eigen/CXX11/src/Tensor/README.md +1760 -0
  344. data/vendor/eigen/unsupported/README.txt +50 -0
  345. data/vendor/tomotopy/LICENSE +21 -0
  346. data/vendor/tomotopy/README.kr.rst +375 -0
  347. data/vendor/tomotopy/README.rst +382 -0
  348. data/vendor/tomotopy/src/Labeling/FoRelevance.cpp +362 -0
  349. data/vendor/tomotopy/src/Labeling/FoRelevance.h +88 -0
  350. data/vendor/tomotopy/src/Labeling/Labeler.h +50 -0
  351. data/vendor/tomotopy/src/TopicModel/CT.h +37 -0
  352. data/vendor/tomotopy/src/TopicModel/CTModel.cpp +13 -0
  353. data/vendor/tomotopy/src/TopicModel/CTModel.hpp +293 -0
  354. data/vendor/tomotopy/src/TopicModel/DMR.h +51 -0
  355. data/vendor/tomotopy/src/TopicModel/DMRModel.cpp +13 -0
  356. data/vendor/tomotopy/src/TopicModel/DMRModel.hpp +374 -0
  357. data/vendor/tomotopy/src/TopicModel/DT.h +65 -0
  358. data/vendor/tomotopy/src/TopicModel/DTM.h +22 -0
  359. data/vendor/tomotopy/src/TopicModel/DTModel.cpp +15 -0
  360. data/vendor/tomotopy/src/TopicModel/DTModel.hpp +572 -0
  361. data/vendor/tomotopy/src/TopicModel/GDMR.h +37 -0
  362. data/vendor/tomotopy/src/TopicModel/GDMRModel.cpp +14 -0
  363. data/vendor/tomotopy/src/TopicModel/GDMRModel.hpp +485 -0
  364. data/vendor/tomotopy/src/TopicModel/HDP.h +74 -0
  365. data/vendor/tomotopy/src/TopicModel/HDPModel.cpp +13 -0
  366. data/vendor/tomotopy/src/TopicModel/HDPModel.hpp +592 -0
  367. data/vendor/tomotopy/src/TopicModel/HLDA.h +40 -0
  368. data/vendor/tomotopy/src/TopicModel/HLDAModel.cpp +13 -0
  369. data/vendor/tomotopy/src/TopicModel/HLDAModel.hpp +681 -0
  370. data/vendor/tomotopy/src/TopicModel/HPA.h +27 -0
  371. data/vendor/tomotopy/src/TopicModel/HPAModel.cpp +21 -0
  372. data/vendor/tomotopy/src/TopicModel/HPAModel.hpp +588 -0
  373. data/vendor/tomotopy/src/TopicModel/LDA.h +144 -0
  374. data/vendor/tomotopy/src/TopicModel/LDACVB0Model.hpp +442 -0
  375. data/vendor/tomotopy/src/TopicModel/LDAModel.cpp +13 -0
  376. data/vendor/tomotopy/src/TopicModel/LDAModel.hpp +1058 -0
  377. data/vendor/tomotopy/src/TopicModel/LLDA.h +45 -0
  378. data/vendor/tomotopy/src/TopicModel/LLDAModel.cpp +13 -0
  379. data/vendor/tomotopy/src/TopicModel/LLDAModel.hpp +203 -0
  380. data/vendor/tomotopy/src/TopicModel/MGLDA.h +63 -0
  381. data/vendor/tomotopy/src/TopicModel/MGLDAModel.cpp +17 -0
  382. data/vendor/tomotopy/src/TopicModel/MGLDAModel.hpp +558 -0
  383. data/vendor/tomotopy/src/TopicModel/PA.h +43 -0
  384. data/vendor/tomotopy/src/TopicModel/PAModel.cpp +13 -0
  385. data/vendor/tomotopy/src/TopicModel/PAModel.hpp +467 -0
  386. data/vendor/tomotopy/src/TopicModel/PLDA.h +17 -0
  387. data/vendor/tomotopy/src/TopicModel/PLDAModel.cpp +13 -0
  388. data/vendor/tomotopy/src/TopicModel/PLDAModel.hpp +214 -0
  389. data/vendor/tomotopy/src/TopicModel/SLDA.h +54 -0
  390. data/vendor/tomotopy/src/TopicModel/SLDAModel.cpp +17 -0
  391. data/vendor/tomotopy/src/TopicModel/SLDAModel.hpp +456 -0
  392. data/vendor/tomotopy/src/TopicModel/TopicModel.hpp +692 -0
  393. data/vendor/tomotopy/src/Utils/AliasMethod.hpp +169 -0
  394. data/vendor/tomotopy/src/Utils/Dictionary.h +80 -0
  395. data/vendor/tomotopy/src/Utils/EigenAddonOps.hpp +181 -0
  396. data/vendor/tomotopy/src/Utils/LBFGS.h +202 -0
  397. data/vendor/tomotopy/src/Utils/LBFGS/LineSearchBacktracking.h +120 -0
  398. data/vendor/tomotopy/src/Utils/LBFGS/LineSearchBracketing.h +122 -0
  399. data/vendor/tomotopy/src/Utils/LBFGS/Param.h +213 -0
  400. data/vendor/tomotopy/src/Utils/LUT.hpp +82 -0
  401. data/vendor/tomotopy/src/Utils/MultiNormalDistribution.hpp +69 -0
  402. data/vendor/tomotopy/src/Utils/PolyaGamma.hpp +200 -0
  403. data/vendor/tomotopy/src/Utils/PolyaGammaHybrid.hpp +672 -0
  404. data/vendor/tomotopy/src/Utils/ThreadPool.hpp +150 -0
  405. data/vendor/tomotopy/src/Utils/Trie.hpp +220 -0
  406. data/vendor/tomotopy/src/Utils/TruncMultiNormal.hpp +94 -0
  407. data/vendor/tomotopy/src/Utils/Utils.hpp +337 -0
  408. data/vendor/tomotopy/src/Utils/avx_gamma.h +46 -0
  409. data/vendor/tomotopy/src/Utils/avx_mathfun.h +736 -0
  410. data/vendor/tomotopy/src/Utils/exception.h +28 -0
  411. data/vendor/tomotopy/src/Utils/math.h +281 -0
  412. data/vendor/tomotopy/src/Utils/rtnorm.hpp +2690 -0
  413. data/vendor/tomotopy/src/Utils/sample.hpp +192 -0
  414. data/vendor/tomotopy/src/Utils/serializer.hpp +695 -0
  415. data/vendor/tomotopy/src/Utils/slp.hpp +131 -0
  416. data/vendor/tomotopy/src/Utils/sse_gamma.h +48 -0
  417. data/vendor/tomotopy/src/Utils/sse_mathfun.h +710 -0
  418. data/vendor/tomotopy/src/Utils/text.hpp +49 -0
  419. data/vendor/tomotopy/src/Utils/tvector.hpp +543 -0
  420. metadata +531 -0
@@ -0,0 +1,82 @@
1
+ #pragma once
2
+ #include <array>
3
+
4
+ namespace tomoto
5
+ {
6
+ namespace math
7
+ {
8
+ namespace detail
9
+ {
10
+ template<typename _Func, typename _Prec, size_t N, size_t S, size_t M, size_t T, size_t L, size_t U>
11
+ class LUT3
12
+ {
13
+ protected:
14
+ std::array<_Prec, N + M + L> points = {};
15
+ static constexpr _Prec P = (_Prec)(1. / S);
16
+ static constexpr _Prec Q = (_Prec)(1. / T);
17
+ static constexpr _Prec R = (_Prec)(1. / U);
18
+ LUT3()
19
+ {
20
+ _Func fun;
21
+ for (size_t i = 0; i < N; i++)
22
+ {
23
+ points[i] = fun(i ? i * P : (_Prec)0.0001);
24
+ }
25
+ for (size_t i = 0; i < M; i++)
26
+ {
27
+ points[i + N] = fun(i*Q + N * P);
28
+ }
29
+ for (size_t i = 0; i < L; i++)
30
+ {
31
+ points[i + N + M] = fun(i*R + N * P + M * Q);
32
+ }
33
+ }
34
+
35
+ _Prec _get(_Prec x) const
36
+ {
37
+ if (!std::isfinite(x)) return _Func{}.forNonFinite(x);
38
+ if (x < 0) return NAN;
39
+ if (x < _Func::smallThreshold) return _Func{}.forSmall(x);
40
+ if (x >= N * P + M * Q + (L - 1) * R) return _Func{}.forLarge(x);
41
+ size_t idx;
42
+ _Prec a;
43
+ _Prec nx = x;
44
+ if (x < N*P)
45
+ {
46
+ idx = (size_t)(nx / P);
47
+ a = (nx - idx * P) / P;
48
+ }
49
+ else
50
+ {
51
+ nx -= N * P;
52
+ if (nx < M*Q)
53
+ {
54
+ idx = (size_t)(nx / Q);
55
+ a = (nx - idx * Q) / Q;
56
+ idx += N;
57
+ }
58
+ else
59
+ {
60
+ nx -= M * Q;
61
+ idx = (size_t)(nx / R);
62
+ a = (nx - idx * R) / R;
63
+ idx += N + M;
64
+ }
65
+ }
66
+ return points[idx] + a * (points[idx + 1] - points[idx]);
67
+ }
68
+ public:
69
+ static const LUT3& getInst()
70
+ {
71
+ static LUT3 lg;
72
+ return lg;
73
+ }
74
+
75
+ static _Prec get(_Prec x)
76
+ {
77
+ return getInst()._get(x);
78
+ }
79
+ };
80
+ }
81
+ }
82
+ }
@@ -0,0 +1,69 @@
1
+ #pragma once
2
+ #include <Eigen/Dense>
3
+ #include <Eigen/Cholesky>
4
+ #include "serializer.hpp"
5
+
6
+ namespace tomoto
7
+ {
8
+ namespace math
9
+ {
10
+ template<typename _Ty = float>
11
+ struct MultiNormalDistribution
12
+ {
13
+ static constexpr _Ty log2pi = 1.83787706641;
14
+ Eigen::Matrix<_Ty, -1, 1> mean;
15
+ Eigen::Matrix<_Ty, -1, -1> cov, l;
16
+ _Ty logDet = 0;
17
+
18
+ MultiNormalDistribution(size_t k = 0) :
19
+ mean{ Eigen::Matrix<_Ty, -1, 1>::Zero(k) },
20
+ cov{ Eigen::Matrix<_Ty, -1, -1>::Identity(k, k) },
21
+ l{ Eigen::Matrix<_Ty, -1, -1>::Identity(k, k) }
22
+ {
23
+ }
24
+
25
+ _Ty getLL(const Eigen::Matrix<_Ty, -1, 1>& x) const
26
+ {
27
+ _Ty ll = -((x - mean).transpose() * cov.inverse() * (x - mean))[0] / 2;
28
+ ll -= log2pi * mean.size() / 2 + logDet;
29
+ return ll;
30
+ }
31
+
32
+ const Eigen::Matrix<_Ty, -1, -1>& getCovL() const
33
+ {
34
+ return l;
35
+ }
36
+
37
+ template<typename _List>
38
+ static MultiNormalDistribution<_Ty> estimate(_List list, size_t len)
39
+ {
40
+ MultiNormalDistribution<_Ty> newDist;
41
+ if (len)
42
+ {
43
+ newDist.mean = list(0);
44
+ for (size_t i = 1; i < len; ++i) newDist.mean += list(i);
45
+ newDist.mean /= len;
46
+ newDist.cov = Eigen::Matrix<_Ty, -1, -1>::Identity(newDist.mean.size(), newDist.mean.size());
47
+ for (size_t i = 0; i < len; ++i)
48
+ {
49
+ Eigen::Matrix<_Ty, -1, 1> o = list(i) - newDist.mean;
50
+ newDist.cov += o * o.transpose();
51
+ }
52
+ if (len > 1) newDist.cov /= len - 1;
53
+ }
54
+ newDist.l = newDist.cov.llt().matrixL();
55
+ newDist.logDet = newDist.l.diagonal().array().log().sum();
56
+ return newDist;
57
+ }
58
+
59
+ DEFINE_SERIALIZER_CALLBACK(onRead, mean, cov);
60
+ private:
61
+ void onRead()
62
+ {
63
+ l = cov.llt().matrixL();
64
+ logDet = l.diagonal().array().log().sum();
65
+ }
66
+ };
67
+
68
+ }
69
+ }
@@ -0,0 +1,200 @@
1
+ #pragma once
2
+ #include <vector>
3
+ #include <cmath>
4
+ #include <random>
5
+ #include <cassert>
6
+
7
+ namespace tomoto
8
+ {
9
+ namespace math
10
+ {
11
+ template<class _Real, class _RNG>
12
+ class PolyaGamma
13
+ {
14
+ static constexpr _Real __PI = 3.141592653589793238462643383279502884197;
15
+ static constexpr _Real HALFPISQ = 0.5 * __PI * __PI;
16
+ static constexpr _Real FOURPISQ = 4 * __PI * __PI;
17
+ static constexpr _Real __TRUNC = 0.64;
18
+ static constexpr _Real __TRUNC_RECIP = 1.0 / __TRUNC;
19
+
20
+ static _Real p_norm(_Real x)
21
+ {
22
+ return std::erf(x / std::sqrt((_Real)2)) / 2 + 0.5f;
23
+ }
24
+
25
+ static _Real draw_like_devroye(_Real Z, _RNG& r)
26
+ {
27
+ // Change the parameter.
28
+ Z = std::fabs(Z) * 0.5;
29
+
30
+ // Now sample 0.25 * J^*(1, Z := Z/2).
31
+ _Real fz = 0.125 * __PI*__PI + 0.5 * Z*Z;
32
+ // ... Problems with large Z? Try using q_over_p.
33
+ // double p = 0.5 * __PI * exp(-1.0 * fz * __TRUNC) / fz;
34
+ // double q = 2 * exp(-1.0 * Z) * pigauss(__TRUNC, Z);
35
+
36
+ _Real X = 0.0;
37
+ _Real S = 1.0;
38
+ _Real Y = 0.0;
39
+ // int iter = 0; If you want to keep track of iterations.
40
+
41
+ while (true)
42
+ {
43
+
44
+ // if (r.unif() < p/(p+q))
45
+ if (std::generate_canonical<_Real, sizeof(_Real) * 8>(r) < mass_texpon(Z))
46
+ X = __TRUNC + std::exponential_distribution<_Real>()(r) / fz;
47
+ else
48
+ X = rtigauss(Z, r);
49
+
50
+ S = a(0, X);
51
+ Y = std::generate_canonical<_Real, sizeof(_Real) * 8>(r) * S;
52
+ int n = 0;
53
+ bool go = true;
54
+
55
+ // Cap the number of iterations?
56
+ while (go)
57
+ {
58
+ ++n;
59
+ if (n % 2 == 1)
60
+ {
61
+ S = S - a(n, X);
62
+ if (Y <= S) return 0.25 * X;
63
+ }
64
+ else
65
+ {
66
+ S = S + a(n, X);
67
+ if (Y > S) go = false;
68
+ }
69
+
70
+ }
71
+ // Need Y <= S in event that Y = S, e.g. when X = 0.
72
+
73
+ }
74
+ }
75
+
76
+ static _Real a(int n, _Real x)
77
+ {
78
+ _Real K = (n + 0.5) * __PI;
79
+ _Real y = 0;
80
+ if (x > __TRUNC) {
81
+ y = K * std::exp(-0.5 * K*K * x);
82
+ }
83
+ else if (x > 0) {
84
+ _Real expnt = -1.5 * (std::log(0.5 * __PI) + std::log(x)) + std::log(K) - 2.0 * (n + 0.5)*(n + 0.5) / x;
85
+ y = std::exp(expnt);
86
+ // y = pow(0.5 * __PI * x, -1.5) * K * exp( -2.0 * (n+0.5)*(n+0.5) / x);
87
+ // ^- unstable for small x?
88
+ }
89
+ return y;
90
+ }
91
+
92
+ static _Real mass_texpon(_Real Z)
93
+ {
94
+ _Real t = __TRUNC;
95
+
96
+ _Real fz = 0.125 * __PI*__PI + 0.5 * Z*Z;
97
+ _Real b = std::sqrt(1.0 / t) * (t * Z - 1);
98
+ _Real a = std::sqrt(1.0 / t) * (t * Z + 1) * -1.0;
99
+
100
+ _Real x0 = log(fz) + fz * t;
101
+ _Real xb = x0 - Z + log(p_norm(b));
102
+ _Real xa = x0 + Z + log(p_norm(a));
103
+
104
+ _Real qdivp = 4 / __PI * (exp(xb) + exp(xa));
105
+
106
+ return 1.0 / (1.0 + qdivp);
107
+ }
108
+
109
+ static _Real rtigauss(_Real Z, _RNG& r)
110
+ {
111
+ Z = std::fabs(Z);
112
+ _Real t = __TRUNC;
113
+ _Real X = t + 1.0;
114
+ if (__TRUNC_RECIP > Z)
115
+ { // mu > t
116
+ _Real alpha = 0.0;
117
+ while (std::generate_canonical<_Real, sizeof(_Real) * 8>(r) > alpha)
118
+ {
119
+ // X = t + 1.0;
120
+ // while (X > t)
121
+ // X = 1.0 / r.gamma_rate(0.5, 0.5);
122
+ // Slightly faster to use truncated normal.
123
+ _Real E1 = std::exponential_distribution<_Real>()(r);
124
+ _Real E2 = std::exponential_distribution<_Real>()(r);
125
+ while (E1*E1 > 2 * E2 / t)
126
+ {
127
+ E1 = std::exponential_distribution<_Real>()(r);
128
+ E2 = std::exponential_distribution<_Real>()(r);
129
+ }
130
+ X = 1 + E1 * t;
131
+ X = t / (X * X);
132
+ alpha = std::exp(-0.5 * Z*Z * X);
133
+ }
134
+ }
135
+ else
136
+ {
137
+ _Real mu = 1.0 / Z;
138
+ while (X > t)
139
+ {
140
+ _Real Y = std::normal_distribution<_Real>()(r); Y *= Y;
141
+ _Real half_mu = 0.5 * mu;
142
+ _Real mu_Y = mu * Y;
143
+ X = mu + half_mu * mu_Y - half_mu * sqrt(4 * mu_Y + mu_Y * mu_Y);
144
+ if (std::generate_canonical<_Real, sizeof(_Real) * 8>(r) > mu / (mu + X))
145
+ X = mu * mu / X;
146
+ }
147
+ }
148
+ return X;
149
+ }
150
+
151
+ static _Real jj_m1(_Real b, _Real z)
152
+ {
153
+ z = std::fabs(z);
154
+ _Real m1 = 0.0;
155
+ if (z > 1e-12)
156
+ m1 = b * std::tanh(z) / z;
157
+ else
158
+ m1 = b * (1 - (1.0 / 3) * std::pow(z, 2) + (2.0 / 15) * std::pow(z, 4) - (17.0 / 315) * std::pow(z, 6));
159
+ return m1;
160
+ }
161
+
162
+ static _Real jj_m2(_Real b, _Real z)
163
+ {
164
+ z = std::fabs(z);
165
+ double m2 = 0.0;
166
+ if (z > 1e-12)
167
+ m2 = (b + 1) * b * std::pow(tanh(z) / z, 2) + b * ((std::tanh(z) - z) / std::pow(z, 3));
168
+ else
169
+ m2 = (b + 1) * b * std::pow(1 - (1.0 / 3) * std::pow(z, 2) + (2.0 / 15) * std::pow(z, 4) - (17.0 / 315) * std::pow(z, 6), 2) +
170
+ b * ((-1.0 / 3) + (2.0 / 15) * std::pow(z, 2) - (17.0 / 315) * std::pow(z, 4));
171
+ return m2;
172
+ }
173
+
174
+ public:
175
+ static _Real draw(size_t n, _Real z, _RNG& r)
176
+ {
177
+ _Real sum = 0.0;
178
+ for (size_t i = 0; i < n; ++i)
179
+ sum += draw_like_devroye(z, r);
180
+ return sum;
181
+ }
182
+
183
+ static _Real pg_m1(_Real b, _Real z)
184
+ {
185
+ return jj_m1(b, 0.5 * z) * 0.25;
186
+ }
187
+
188
+ static _Real pg_m2(_Real b, _Real z)
189
+ {
190
+ return jj_m2(b, 0.5 * z) * 0.0625;
191
+ }
192
+
193
+ };
194
+
195
+ template<class _Real, class _RNG> _Real drawPolyaGamma(size_t n, _Real z, _RNG& r)
196
+ {
197
+ return PolyaGamma<_Real, _RNG>::draw(n, z, r);
198
+ }
199
+ }
200
+ }
@@ -0,0 +1,672 @@
1
+ #pragma once
2
+ namespace tomoto
3
+ {
4
+ namespace math
5
+ {
6
+
7
+
8
+ const double PISQ = __PI * __PI;
9
+ const double HALFPI = 0.5 * __PI;
10
+
11
+ const double trunc_schedule[] = { // seq(1,4,by=0.01) -> 301 entries.
12
+ 0.64, 0.68, 0.72, 0.75, 0.78, 0.8, 0.83, 0.85, 0.87, 0.89,
13
+ 0.91, 0.93, 0.95, 0.96, 0.98, 1, 1.01, 1.03, 1.04, 1.06,
14
+ 1.07, 1.09, 1.1, 1.12, 1.13, 1.15, 1.16, 1.17, 1.19, 1.2,
15
+ 1.21, 1.23, 1.24, 1.25, 1.26, 1.28, 1.29, 1.3, 1.32, 1.33,
16
+ 1.34, 1.35, 1.36, 1.38, 1.39, 1.4, 1.41, 1.42, 1.44, 1.45,
17
+ 1.46, 1.47, 1.48, 1.5, 1.51, 1.52, 1.53, 1.54, 1.55, 1.57,
18
+ 1.58, 1.59, 1.6, 1.61, 1.62, 1.63, 1.65, 1.66, 1.67, 1.68,
19
+ 1.69, 1.7, 1.71, 1.72, 1.74, 1.75, 1.76, 1.77, 1.78, 1.79,
20
+ 1.8 , 1.81, 1.82, 1.84, 1.85, 1.86, 1.87, 1.88, 1.89, 1.9,
21
+ 1.91, 1.92, 1.93, 1.95, 1.96, 1.97, 1.98, 1.99, 2, 2.01,
22
+ 2.02, 2.03, 2.04, 2.05, 2.07, 2.08, 2.09, 2.1, 2.11, 2.12,
23
+ 2.13, 2.14, 2.15, 2.16, 2.17, 2.18, 2.19, 2.21, 2.22, 2.23,
24
+ 2.24, 2.25, 2.26, 2.27, 2.28, 2.29, 2.3, 2.31, 2.32, 2.33,
25
+ 2.35, 2.36, 2.37, 2.38, 2.39, 2.4, 2.41, 2.42, 2.43, 2.44,
26
+ 2.45, 2.46, 2.47, 2.48, 2.49, 2.51, 2.52, 2.53, 2.54, 2.55,
27
+ 2.56, 2.57, 2.58, 2.59, 2.6, 2.61, 2.62, 2.63, 2.64, 2.65,
28
+ 2.66, 2.68, 2.69, 2.7, 2.71, 2.72, 2.73, 2.74, 2.75, 2.76,
29
+ 2.77, 2.78, 2.79, 2.8, 2.81, 2.82, 2.83, 2.84, 2.85, 2.87,
30
+ 2.88, 2.89, 2.9, 2.91, 2.92, 2.93, 2.94, 2.95, 2.96, 2.97,
31
+ 2.98, 2.99, 3, 3.01, 3.02, 3.03, 3.04, 3.06, 3.07, 3.08,
32
+ 3.09, 3.1, 3.11, 3.12, 3.13, 3.14, 3.15, 3.16, 3.17, 3.18,
33
+ 3.19, 3.2, 3.21, 3.22, 3.23, 3.24, 3.25, 3.27, 3.28, 3.29,
34
+ 3.3, 3.31, 3.32, 3.33, 3.34, 3.35, 3.36, 3.37, 3.38, 3.39,
35
+ 3.4, 3.41, 3.42, 3.43, 3.44, 3.45, 3.46, 3.47, 3.49, 3.5,
36
+ 3.51, 3.52, 3.53, 3.54, 3.55, 3.56, 3.57, 3.58, 3.59, 3.6,
37
+ 3.61, 3.62, 3.63, 3.64, 3.65, 3.66, 3.67, 3.68, 3.69, 3.71,
38
+ 3.72, 3.73, 3.74, 3.75, 3.76, 3.77, 3.78, 3.79, 3.8, 3.81,
39
+ 3.82, 3.83, 3.84, 3.85, 3.86, 3.87, 3.88, 3.89, 3.9, 3.91,
40
+ 3.92, 3.93, 3.95, 3.96, 3.97, 3.98, 3.99, 4, 4.01, 4.02,
41
+ 4.03, 4.04, 4.05, 4.06, 4.07, 4.08, 4.09, 4.1, 4.11, 4.12, 4.13 };
42
+
43
+ //------------------------------------------------------------------------------
44
+
45
+ class PolyaGammaAlt
46
+ {
47
+
48
+ public:
49
+
50
+ // Draw.
51
+ double draw(double h, double z, RNG& r, int max_inner = 200)
52
+ {
53
+ assert(h >= 1);
54
+
55
+ double n = floor((h - 1.0) / 4.0);
56
+ double remain = h - 4.0 * n;
57
+
58
+ double x = 0.0;
59
+
60
+ for (int i = 0; i < (int)n; i++)
61
+ x += draw_abridged(4.0, z, r);
62
+ if (remain > 4.0)
63
+ x += draw_abridged(0.5 * remain, z, r) + draw_abridged(0.5 * remain, z, r);
64
+ else
65
+ x += draw_abridged(remain, z, r);
66
+
67
+ return x;
68
+ }
69
+
70
+ double draw_abridged(double h, double z, RNG& r, int max_inner = 200)
71
+ {
72
+ assert(h >= 1 && h <= 4);
73
+ // Change the parameter.
74
+ z = fabs(z) * 0.5;
75
+
76
+ int idx = (int)floor((h - 1.0)*100.0);
77
+ double trunc = trunc_schedule[idx];
78
+
79
+ // Now sample 0.25 * J^*(1, z := z/2).
80
+ double rate_z = 0.125 * __PI*__PI + 0.5 * z*z;
81
+ double weight_left = w_left(trunc, h, z);
82
+ double weight_right = w_right(trunc, h, z);
83
+ double prob_right = weight_right / (weight_right + weight_left);
84
+
85
+ // printf("prob_right: %g\n", prob_right);
86
+
87
+ double coef1_h = exp(h * log(2.0) - 0.5 * log(2.0 * __PI));
88
+ // double gamma_nh_over_n = RNG::Gamma(h);
89
+ double gnh_over_gn1_gh = 1.0; // Will fill in value on first call to a_coef_recursive.
90
+
91
+ int num_trials = 0;
92
+ int total_iter = 0;
93
+
94
+ while (num_trials < 10000) {
95
+ num_trials++;
96
+
97
+ double X = 0.0;
98
+ double Y = 0.0;
99
+
100
+ // if (r.unif() < p/(p+q))
101
+ double uu = std::generate_canonical<double, 64>(r);
102
+ if (uu < prob_right)
103
+ X = r.ltgamma(h, rate_z, trunc);
104
+ else
105
+ X = rtigauss(h, z, trunc, r);
106
+
107
+ // double S = a_coef(0, X, h);
108
+ double S = a_coef_recursive(0.0, X, h, coef1_h, gnh_over_gn1_gh);
109
+ double a_n = S;
110
+ // double a_n2 = S2;
111
+ // printf("a_n=%g, a_n2=%g\n", a_n, a_n2);
112
+ double gt = g_tilde(X, h, trunc);
113
+ Y = std::generate_canonical<double, 64>(r) * gt;
114
+
115
+ // printf("test gt: %g\n", g_tilde(trunc * 0.1, h, trunc));
116
+ // printf("X, Y, S, gt: %g, %g, %g, %g\n", X, Y, S, gt);
117
+
118
+ bool decreasing = false;
119
+
120
+ int n = 0;
121
+ bool go = true;
122
+
123
+ // Cap the number of iterations?
124
+ while (go && n < max_inner) {
125
+ total_iter++;
126
+
127
+ ++n;
128
+ double prev = a_n;
129
+ // a_n = a_coef(n, X, h);
130
+ a_n = a_coef_recursive((double)n, X, h, coef1_h, gnh_over_gn1_gh);
131
+ // printf("a_n=%g, a_n2=%g\n", a_n, a_n2);
132
+ decreasing = a_n <= prev;
133
+
134
+ if (n % 2 == 1) {
135
+ S = S - a_n;
136
+ if (Y <= S && decreasing) return 0.25 * X;
137
+ }
138
+ else {
139
+ S = S + a_n;
140
+ if (Y > S && decreasing) go = false;
141
+ }
142
+
143
+ }
144
+ // Need Y <= S in event that Y = S, e.g. when X = 0.
145
+
146
+ }
147
+
148
+ // We should never get here.
149
+ return -1.0;
150
+ } // draw
151
+
152
+ // Helper.
153
+ double a_coef(int n, double x, double h)
154
+ {
155
+ double d_n = 2.0 * (double)n + h;
156
+ double log_out = h * log(2.0) - std::lgamma(h) + std::lgamma(n + h)
157
+ - std::lgamma(n + 1) + log(d_n)
158
+ - 0.5 * log(2.0 * __PI * x * x * x) - 0.5 * d_n * d_n / x;
159
+ double out = exp(log_out);
160
+ // double out = exp(out) is a legal command. Weird.
161
+ return out;
162
+ }
163
+
164
+ double a_coef_recursive(double n, double x, double h, double coef_h, double& gamma_nh_over_n)
165
+ {
166
+ double d_n = 2.0 * (double)n + h;
167
+ // gamma_nh_over_n *= (n + h - 1) / n; // Can speed up further by separate function for a0 and an, n > 0.
168
+ if (n != 0)
169
+ gamma_nh_over_n *= (n + h - 1) / n;
170
+ else
171
+ gamma_nh_over_n = 1.0;
172
+ double coef = coef_h * gamma_nh_over_n;
173
+ double log_kernel = -0.5 * (log(x * x * x) + d_n * d_n / x) + log(d_n);
174
+ return coef * exp(log_kernel);
175
+ // double out = exp(out) is a legal command. Weird.
176
+ }
177
+
178
+ double g_tilde(double x, double h, double trunc)
179
+ {
180
+ double out = 0;
181
+ if (x > trunc)
182
+ out = exp(h * log(0.5 * __PI) + (h - 1) * log(x) - PISQ * 0.125 * x - std::lgamma(h));
183
+ else
184
+ out = h * exp(h * log(2.0) - 0.5 * log(2.0 * __PI * x * x * x) - 0.5 * h * h / x);
185
+ // out = h * pow(2, h) * pow(2 * __PI * pow(x,3), -0.5) * exp(-0.5 * pow(h,2) / x);
186
+ return out;
187
+ }
188
+
189
+ double pigauss(double x, double z, double lambda)
190
+ {
191
+ // z = 1 / mean
192
+ double b = sqrt(lambda / x) * (x * z - 1);
193
+ double a = sqrt(lambda / x) * (x * z + 1) * -1.0;
194
+ double y = p_norm(b) + exp(2 * lambda * z) * p_norm(a);
195
+ return y;
196
+ }
197
+
198
+ double rtigauss(double h, double z, double trunc, RNG& r)
199
+ {
200
+ z = fabs(z);
201
+ double mu = h / z;
202
+ double X = trunc + 1.0;
203
+ if (mu > trunc) { // mu > t
204
+ double alpha = 0.0;
205
+ while (std::generate_canonical<double, 64>(r) > alpha) {
206
+ X = rtinvchi2(h, trunc, r);
207
+ alpha = exp(-0.5 * z*z * X);
208
+ }
209
+ // printf("rtigauss, part i: %g\n", X);
210
+ }
211
+ else {
212
+ while (X > trunc) {
213
+ X = r.igauss(mu, h*h);
214
+ }
215
+ // printf("rtigauss, part ii: %g\n", X);
216
+ }
217
+ return X;
218
+ }
219
+
220
+ double w_left(double trunc, double h, double z)
221
+ {
222
+ double out = 0;
223
+ if (z != 0)
224
+ out = exp(h * (log(2.0) - z)) * pigauss(trunc, z / h, h*h);
225
+ else
226
+ out = exp(h * log(2.0)) * (1.0 - RNG::p_gamma_rate(1 / trunc, 0.5, 0.5*h*h));
227
+ return out;
228
+ }
229
+
230
+ double w_right(double trunc, double h, double z)
231
+ {
232
+ double lambda_z = PISQ * 0.125 + 0.5 * z * z;
233
+ double p = exp(h * log(HALFPI / lambda_z)) * (1.0 - RNG::p_gamma_rate(trunc, h, lambda_z));
234
+ return p;
235
+ }
236
+
237
+ };
238
+
239
+ double rtinvchi2(double h, double trunc, RNG& r)
240
+ {
241
+ double h2 = h * h;
242
+ double R = trunc / h2;
243
+ double X = 0.0;
244
+ // I need to consider using a different truncated normal sampler.
245
+ double E1 = std::exponential_distribution<>()(r); double E2 = std::exponential_distribution<>()(r);
246
+ while ((E1*E1) > (2 * E2 / R)) {
247
+ // printf("E %g %g %g %g\n", E1, E2, E1*E1, 2*E2/R);
248
+ E1 = std::exponential_distribution<>()(r); E2 = std::exponential_distribution<>()(r);
249
+ }
250
+ // printf("E %g %g \n", E1, E2);
251
+ X = 1 + E1 * R;
252
+ X = R / (X * X);
253
+ X = h2 * X;
254
+ return X;
255
+ }
256
+
257
+
258
+
259
+ struct FD {
260
+ double val;
261
+ double der;
262
+ };
263
+
264
+ struct Line {
265
+ double slope;
266
+ double icept;
267
+ };
268
+
269
+ // PolyaGamma approximation by SP.
270
+ class PolyaGammaSP
271
+ {
272
+
273
+ public:
274
+
275
+ int draw(double& d, double h, double z, RNG& r, int maxiter = 200)
276
+ {
277
+ if (n < 1) fprintf(stderr, "PolyaGammaSP::draw: n must be >= 1.\n");
278
+ z = 0.5 * fabs(z);
279
+
280
+ double xl = y_func(-1 * z*z); // Mode of phi - Left point.
281
+ double md = xl * 1.1; // Mid point.
282
+ double xr = xl * 1.2; // Right point.
283
+
284
+ // printf("xl, md, xr: %g, %g, %g\n", xl, md, xr);
285
+
286
+ // Inflation constants
287
+ // double vmd = yv.v_func(md);
288
+ double vmd = v_eval(md);
289
+ double K2md = 0.0;
290
+
291
+ if (fabs(vmd) >= 1e-6)
292
+ K2md = md * md + (1 - md) / vmd;
293
+ else
294
+ K2md = md * md - 1 / 3 - (2 / 15) * vmd;
295
+
296
+ double m2 = md * md;
297
+ double al = m2 * md / K2md;
298
+ double ar = m2 / K2md;
299
+
300
+ // printf("vmd, K2md, al, ar: %g, %g %g %g\n", vmd, K2md, al, ar);
301
+
302
+ // Tangent lines info.
303
+ Line ll, lr;
304
+ tangent_to_eta(xl, z, md, ll);
305
+ tangent_to_eta(xr, z, md, lr);
306
+
307
+ double rl = -1. * ll.slope;
308
+ double rr = -1. * lr.slope;
309
+ double il = ll.icept;
310
+ double ir = lr.icept;
311
+
312
+ // printf("rl, rr, il, ir: %g, %g, %g, %g\n", rl, rr, il, ir);
313
+
314
+ // Constants
315
+ double lcn = 0.5 * log(0.5 * n / __PI);
316
+ double rt2rl = sqrt(2 * rl);
317
+
318
+ // printf("sqrt(rl): %g\n", rt2rl);
319
+
320
+ // Weights
321
+ double wl, wr, wt, pl;
322
+
323
+ wl = exp(0.5 * log(al) - n * rt2rl + n * il + 0.5 * n * 1. / md) *
324
+ RNG::p_igauss(md, 1. / rt2rl, n);
325
+
326
+ wr = exp(0.5 * log(ar) + lcn - n * log(n * rr) + n * ir - n * log(md)) *
327
+ // yv.upperIncompleteGamma(md, n, n*rr);
328
+ std::lgamma(n) * (1.0 - RNG::p_gamma_rate(md, n, n*rr));
329
+
330
+ // printf("wl, wr: %g, %g\n", wl, wr);
331
+
332
+ wt = wl + wr;
333
+ pl = wl / wt;
334
+
335
+ // Sample
336
+ bool go = true;
337
+ int iter = 0;
338
+ double X = 2.0;
339
+ double F = 0.0;
340
+
341
+ while (go && iter < maxiter) {
342
+ // Put first so check on first pass.
343
+ #ifdef USE_R
344
+ if (iter % 1000 == 0) R_CheckUserInterrupt();
345
+ #endif
346
+
347
+ iter++;
348
+
349
+ double phi_ev;
350
+ if (std::generate_canonical<double, 64>(r) < pl) {
351
+ X = rtigauss(1. / rt2rl, n, md, r);
352
+ phi_ev = n * (il - rl * X) + 0.5 * n * ((1. - 1. / X) - (1. - 1. / md));
353
+ F = exp(0.5 * log(al) + lcn - 1.5 * log(X) + phi_ev);
354
+ }
355
+ else {
356
+ X = r.ltgamma(n, n*rr, md);
357
+ phi_ev = n * (ir - rr * X) + n * (log(X) - log(md));
358
+ F = exp(0.5 * log(ar) + lcn + phi_ev) / X;
359
+ }
360
+
361
+ double spa = sp_approx(X, n, z);
362
+
363
+ if (F * std::generate_canonical<double, 64>(r) < spa) go = false;
364
+
365
+ }
366
+
367
+ // return n * 0.25 * X;
368
+ d = n * 0.25 * X;
369
+ return iter;
370
+ }
371
+
372
+ protected:
373
+
374
+ // Helper.
375
+
376
+ double w_left(double trunc, double h, double z);
377
+ double w_right(double trunc, double h, double z);
378
+
379
+ void delta_func(double x, double mid, FD& delta)
380
+ {
381
+ if (x >= mid) {
382
+ delta.val = log(x) - log(mid);
383
+ delta.der = 1.0 / x;
384
+ }
385
+ else {
386
+ delta.val = 0.5 * (1 - 1.0 / x) - 0.5 * (1 - 1.0 / mid);
387
+ delta.der = 0.5 / (x*x);
388
+ }
389
+ }
390
+
391
+ double phi_func(double x, double z, FD& phi)
392
+ {
393
+ // double v = yv.v_func(x);
394
+ double v = v_eval(x);
395
+ double u = 0.5 * v;
396
+ double t = u + 0.5 * z*z;
397
+
398
+ phi.val = log(cosh(fabs(z))) - log(cos_rt(v)) - t * x;
399
+ phi.der = -1.0 * t;
400
+
401
+ return v;
402
+ }
403
+
404
+ double tangent_to_eta(double x, double z, double mid, Line& tl)
405
+ {
406
+ FD phi, delta, eta;
407
+ double v;
408
+
409
+ v = phi_func(x, z, phi);
410
+ delta_func(x, mid, delta);
411
+
412
+ eta.val = phi.val - delta.val;
413
+ eta.der = phi.der - delta.der;
414
+
415
+ // printf("v=%g\nphi=%g, phi.d=%g\ndelta=%g, delta.d=%g\neta=%g, eta.d=%g\n",
416
+ // v, phi.val, phi.der, delta.val, delta.der, eta.val, eta.der);
417
+
418
+ tl.slope = eta.der;
419
+ tl.icept = eta.val - eta.der * x;
420
+
421
+ return v;
422
+ }
423
+
424
+ double sp_approx(double x, double n, double z)
425
+ {
426
+ // double v = yv.v_func(x);
427
+ double v = v_eval(x);
428
+ double u = 0.5 * v;
429
+ double z2 = z * z;
430
+ double t = u + 0.5 * z2;
431
+ // double m = y_func(-1 * z2);
432
+
433
+ double phi = log(cosh(z)) - log(cos_rt(v)) - t * x;
434
+
435
+ double K2 = 0.0;
436
+ if (fabs(v) >= 1e-6)
437
+ K2 = x * x + (1 - x) / v;
438
+ else
439
+ K2 = x * x - 1 / 3 - (2 / 15) * v;
440
+
441
+ double log_spa = 0.5 * log(0.5 * n / __PI) - 0.5 * log(K2) + n * phi;
442
+ return exp(log_spa);
443
+ }
444
+
445
+ double cos_rt(double v)
446
+ {
447
+ double y = 0.0;
448
+ double r = sqrt(fabs(v));
449
+ if (v >= 0)
450
+ y = cos(r);
451
+ else
452
+ y = cosh(r);
453
+ return y;
454
+ }
455
+
456
+ // YV yv;
457
+
458
+ double rtigauss(double mu, double lambda, double trunc, RNG& r)
459
+ {
460
+ // mu = fabs(mu);
461
+ double X = trunc + 1.0;
462
+ if (trunc < mu) { // mu > t
463
+ double alpha = 0.0;
464
+ while (std::generate_canonical<double, 64>(r) > alpha) {
465
+ X = r.rtinvchi2(lambda, trunc);
466
+ alpha = exp(-0.5 * lambda / (mu*mu) * X);
467
+ }
468
+ // printf("rtigauss, part i: %g\n", X);
469
+ }
470
+ else {
471
+ while (X > trunc) {
472
+ X = r.igauss(mu, lambda);
473
+ }
474
+ // printf("rtigauss, part ii: %g\n", X);
475
+ }
476
+ return X;
477
+ }
478
+
479
+ double y_func(double v) // y = tan(sqrt(v)) / sqrt(v);
480
+ {
481
+ double tol = 1e-6;
482
+ double y = 0.0;
483
+ double r = sqrt(fabs(v));
484
+ if (v > tol)
485
+ y = tan(r) / r;
486
+ else if (v < -1 * tol)
487
+ y = tanh(r) / r;
488
+ else
489
+ y = 1 + (1 / 3) * v + (2 / 15) * v * v + (17 / 315) * v * v * v;
490
+ return y;
491
+ }
492
+
493
+ };
494
+
495
+
496
+ class PolyaGammaSmallB
497
+ {
498
+ public:
499
+ PolyaGammaSmallB() {}
500
+
501
+ // Draw.
502
+ double draw(double b, double z, RNG& r)
503
+ {
504
+ double x;
505
+ if (z == 0)
506
+ {
507
+ x = draw_invgamma_rej(b, r) / 4.0;
508
+ }
509
+ else
510
+ {
511
+ x = draw_invgauss_rej(b, z / 2.0, r) / 4.0;
512
+ }
513
+ return x;
514
+ }
515
+
516
+ private:
517
+
518
+ double draw_invgauss_rej(double b, double z, RNG& r)
519
+ {
520
+ bool success = false;
521
+ int niter = 0;
522
+
523
+ // fprintf(stderr, "b: %.3f\t z: %.3f\n", b, z);
524
+ double mu = b / fabs(z);
525
+ double lambda = b * b;
526
+
527
+ double x, u;
528
+
529
+ while (!success && niter < 100)
530
+ {
531
+ x = r.igauss(mu, lambda);
532
+ u = std::generate_canonical<double, 64>(r);
533
+ if (u < one_minus_psi(x, b))
534
+ {
535
+ success = true;
536
+ }
537
+ niter += 1;
538
+ }
539
+
540
+ if (!success)
541
+ {
542
+ throw std::runtime_error("InvGauss rejection sampler failed for MAXITER iterations.");
543
+ }
544
+
545
+ return x;
546
+ }
547
+
548
+ double draw_invgamma_rej(double b, RNG& r)
549
+ {
550
+ bool success = false;
551
+ int niter = 0;
552
+
553
+ double alpha = 0.5;
554
+ double beta = b * b / 2.0;
555
+
556
+ double x, u;
557
+
558
+ while (!success && niter < 100)
559
+ {
560
+ x = r.igamma(alpha, beta);
561
+ u = std::generate_canonical<double, 64>(r);
562
+ if (u < one_minus_psi(x, b))
563
+ {
564
+ success = true;
565
+ }
566
+ niter += 1;
567
+ }
568
+
569
+ if (!success)
570
+ {
571
+ throw std::runtime_error("InvGamma rejection sampler failed for MAXITER iterations.");
572
+ }
573
+
574
+ return x;
575
+ }
576
+
577
+ // Helper.
578
+ inline double one_minus_psi(double x, double b)
579
+ {
580
+ double omp = 1.0;
581
+ omp -= (2.0 + b) * exp(-2.*(b + 1.0) / x);
582
+ omp += (1.0 + b)*(4.0 + b) / 2.0 * exp(-4.0*(b + 2.0) / x);
583
+ omp -= (2.0 + b)*(1.0 + b)*(6.0 + b) / 6.0 * exp(-6.0*(b + 3.0) / x);
584
+ omp += (3.0 + b)*(2.0 + b)*(1.0 + b)*(8.0 + b) / 24.0 * exp(-8.0*(b + 4.0) / x);
585
+ omp -= (4.0 + b)*(3.0 + b)*(2.0 + b)*(1.0 + b)*(10.0 + b) / 120.0 * exp(-10.0*(b + 5.0) / x);
586
+ return omp;
587
+ }
588
+
589
+ };
590
+
591
+ template <typename Real>
592
+ class PolyaGammaHybrid
593
+ {
594
+ private:
595
+ RNG* rng;
596
+
597
+ public:
598
+ // Constructor and destructor
599
+ PolyaGammaHybrid(unsigned long seed);
600
+ ~PolyaGammaHybrid();
601
+
602
+
603
+ PolyaGamma dv;
604
+ PolyaGammaAlt al;
605
+ PolyaGammaSP sp;
606
+ PolyaGammaSmallB sb;
607
+
608
+ void set_trunc(int trunc);
609
+ Real draw(Real b, Real z);
610
+
611
+ };
612
+
613
+ // Constructor and Destructor
614
+ template <typename Real>
615
+ PolyaGammaHybrid<Real>::PolyaGammaHybrid(unsigned long seed)
616
+ {
617
+ rng = new RNG(seed);
618
+ }
619
+
620
+ template <typename Real>
621
+ PolyaGammaHybrid<Real>::~PolyaGammaHybrid()
622
+ {
623
+ delete rng;
624
+ }
625
+
626
+ // Plumbing
627
+ template <typename Real>
628
+ void PolyaGammaHybrid<Real>::set_trunc(int trunc)
629
+ {
630
+ dv.set_trunc(trunc);
631
+ }
632
+
633
+ // Draw
634
+ template <typename Real>
635
+ Real PolyaGammaHybrid<Real>::draw(Real b_, Real z_)
636
+ {
637
+ double x;
638
+
639
+ double b = (double)b_;
640
+ double z = (double)z_;
641
+
642
+ if (b > 170)
643
+ {
644
+ double m = dv.pg_m1(b, z);
645
+ double v = dv.pg_m2(b, z) - m * m;
646
+ x = (Real)rng->norm(m, sqrt(v));
647
+ }
648
+ else if (b > 13)
649
+ {
650
+ sp.draw(x, b, z, *rng);
651
+ }
652
+ else if (b == 1 || b == 2)
653
+ {
654
+ x = dv.draw((int)b, z, *rng);
655
+ }
656
+ else if (b > 1)
657
+ {
658
+ x = al.draw(b, z, *rng);
659
+ }
660
+ else if (b > 0)
661
+ {
662
+ x = sb.draw(b, z, *rng);
663
+ }
664
+ else
665
+ {
666
+ x = 0.0;
667
+ }
668
+
669
+ return (Real)x;
670
+ }
671
+ }
672
+ }