LAMMP 4.1.0
Lamina High-Precision Arithmetic Library
载入中...
搜索中...
未找到
multinomial.c
浏览该文件的文档.
1/*
2 * LAMMP - Copyright (C) 2025-2026 HJimmyK(Jericho Knox)
3 * This file is part of lammp, under the GNU LGPL v2 license.
4 * See LICENSE in the project root for the full license text.
5 */
6
7#include "../../../include/lammp/impl/ele_mul.h"
8#include "../../../include/lammp/impl/prime_table.h"
9#include "../../../include/lammp/impl/longlong.h"
10#include "../../../include/lammp/impl/mparam.h"
11
13 *n = 0;
14 uint i = 0;
15 for (; i < m; ++i) *n += r[i];
16
17 double logr = lgamma(*n + 1.0);
18
19 for (i = 0; i < m; ++i)
20 logr -= lgamma(r[i] + 1.0);
21
22 logr /= LOG2_;
23
24 mp_size_t rn = ceil(logr / LIMB_BITS) + 2;
25 return rn;
26}
27
28static inline uint count_factors(fac_ptr fac, uint nfactors, uint n, const uintp r, uint m, uint p) {
29 uint pn = n;
30 uint e = 0;
31 ulong inv = MP_ULONG_MAX / p + 1;
32 while (pn > 0) {
33 _udiv32by32_q_preinv(pn, pn, inv);
34 e += pn;
35 }
36 for (uint i = 0; i < m; ++i) {
37 uint pn = r[i];
38 while (pn > 0) {
39 _udiv32by32_q_preinv(pn, pn, inv);
40 e -= pn;
41 }
42 }
43 if (e > 0) {
44 fac[nfactors].f = p;
45 fac[nfactors++].j = e;
46 }
47 return nfactors;
48}
49
50static inline uint factor_size_int(mp_size_t rn, uint n) {
51 /*
52 使用类似组合数的思路来估计缓冲区大小。
53 */
54 uint approx1 = rn * 8;
55 uint approx2 = lmmp_prime_size_(n);
56 return approx1 < approx2 ? approx1 : approx2;
57}
58
59static inline uint factor_size_short(mp_size_t rn) {
60 return rn * 10;
61}
62
64 mp_ptr restrict dst,
65 mp_size_t rn,
66 uint n,
67 const uintp restrict r,
68 uint m
69) {
70 if (n < ODD_FACTORIAL_SIZE) {
71 lmmp_odd_nPr_ushort_(dst, rn, n, n);
72 mp_limb_t t = 0;
73 for (uint i = 0; i < m; ++i) {
74 lmmp_odd_nPr_ushort_(&t, 1, r[i], r[i]);
75 dst[0] /= t;
76 }
77 return 1;
78 } else {
80 uint primen = lmmp_prime_cnt16_(n);
81 uint nfactors = factor_size_short(rn);
82 nfactors = primen < nfactors ? primen : nfactors;
83 fac_ptr restrict fac = TALLOC_TYPE(nfactors, fac_t);
84 nfactors = 0;
85 for (uint i = 1; i < primen; ++i) {
87 nfactors = count_factors(fac, nfactors, n, r, m, p);
88 }
89
90 rn = lmmp_factors_mul_(dst, rn, fac, nfactors);
91
93 return rn;
94 }
95}
96
97static mp_size_t lmmp_odd_multinomial_uint_(mp_ptr restrict dst, mp_size_t rn, uint n, const uintp restrict r, uint m) {
100 uint nfactors = factor_size_int(rn, n);
101 fac_ptr restrict fac = BALLOC_TYPE(nfactors, fac_t);
102
103 nfactors = 0;
104 prime_cache_t cache;
105 lmmp_prime_cache_init_(&cache, n);
106 while (cache.is_end == 0) {
108 for (uint i = 0; i < cache.size; ++i) {
109 nfactors = count_factors(fac, nfactors, n, r, m, cache.pp[i]);
110 }
111 }
113
114 rn = lmmp_factors_mul_(dst, rn, fac, nfactors);
115
117 return rn;
118}
119
120#define MULTINOMIAL_SHORT_LIMIT (0xffff)
121#define MULTINOMIAL_INT_LIMIT (0xffffffff)
122
123mp_size_t lmmp_multinomial_(mp_ptr restrict dst, mp_size_t rn, uint n, const uintp restrict r, uint m) {
124 mp_size_t shl = n - lmmp_limb_popcnt_(n);
125 for (uint j = 0; j < m; ++j) {
126 shl += lmmp_limb_popcnt_(r[j]);
127 shl -= r[j];
128 }
129 mp_size_t shw = shl / LIMB_BITS;
130 shl %= LIMB_BITS;
131 lmmp_zero(dst, shw);
132 if (n <= MULTINOMIAL_SHORT_LIMIT) {
133 rn = lmmp_odd_multinomial_ushort_(dst + shw, rn - shw, n, r, m);
134 } else {
135 rn = lmmp_odd_multinomial_uint_(dst + shw, rn - shw, n, r, m);
136 }
137
138 if (shl > 0) {
139 dst[shw + rn] = lmmp_shl_(dst + shw, dst + shw, rn, shl);
140 rn += shw + 1;
141 rn -= dst[rn - 1] == 0 ? 1 : 0;
142 } else {
143 rn += shw;
144 }
145 return rn;
146}
uint f
Definition ele_mul.h:118
uint j
Definition ele_mul.h:119
mp_size_t lmmp_factors_mul_(mp_ptr dst, mp_size_t rn, fac_ptr fac, uint nfactors)
计算因子的累乘,并将结果放入dst中
mp_limb_t * mp_ptr
Definition lmmp.h:215
#define lmmp_zero(dst, n)
Definition lmmp.h:366
uint64_t mp_size_t
Definition lmmp.h:212
uint64_t mp_limb_t
Definition lmmp.h:211
#define LIMB_BITS
Definition lmmp.h:221
mp_limb_t lmmp_shl_(mp_ptr dst, mp_srcptr numa, mp_size_t na, mp_size_t shl)
大数左移操作 [dst,na] = [numa,na]<<shl,dst的低shl位填充0
Definition shl.c:9
int lmmp_limb_popcnt_(mp_limb_t x)
计算一个64位无符号整数中1的个数
Definition tiny.c:20
#define _udiv32by32_q_preinv(q, n0, dinv)
Definition longlong.h:466
#define LOG2_
Definition mparam.h:165
#define MP_ULONG_MAX
Definition mparam.h:140
#define ODD_FACTORIAL_SIZE
Definition mparam.h:152
static mp_size_t lmmp_odd_multinomial_ushort_(mp_ptr restrict dst, mp_size_t rn, uint n, const uintp restrict r, uint m)
Definition multinomial.c:63
static uint factor_size_int(mp_size_t rn, uint n)
Definition multinomial.c:50
static uint factor_size_short(mp_size_t rn)
Definition multinomial.c:59
static uint count_factors(fac_ptr fac, uint nfactors, uint n, const uintp r, uint m, uint p)
Definition multinomial.c:28
mp_size_t lmmp_multinomial_size_(const uintp r, uint m, ulong *restrict n)
Definition multinomial.c:12
#define MULTINOMIAL_SHORT_LIMIT
static mp_size_t lmmp_odd_multinomial_uint_(mp_ptr restrict dst, mp_size_t rn, uint n, const uintp restrict r, uint m)
Definition multinomial.c:97
mp_size_t lmmp_multinomial_(mp_ptr restrict dst, mp_size_t rn, uint n, const uintp restrict r, uint m)
mp_size_t lmmp_odd_nPr_ushort_(mp_ptr dst, mp_size_t rn, ulong n, ulong r)
计算 nPr 排列数的奇数部分
uint32_t uint
Definition numth.h:35
uint32_t * uintp
Definition numth.h:43
uint64_t ulong
Definition numth.h:36
void lmmp_prime_cache_free_(prime_cache_t *cache)
释放素数表缓存
ulong lmmp_prime_size_(ulong n)
估计 n 范围内的素数数量
Definition prime_table.c:11
void lmmp_prime_cache_next_(prime_cache_t *cache)
素数表缓存更新(从小到大遍历全局质数表)
const ushort prime_short_table[6542]
void lmmp_prime_int_table_init_(uint n)
初始化全局素数表
Definition prime_table.c:70
ushort lmmp_prime_cnt16_(ushort n)
计算小于等于 n 的素数数量
void lmmp_prime_cache_init_(prime_cache_t *cache, uint n)
初始化素数表缓存
#define TEMP_DECL
Definition tmp_alloc.h:72
#define TEMP_FREE
Definition tmp_alloc.h:93
#define TALLOC_TYPE(n, type)
Definition tmp_alloc.h:91
#define TEMP_B_DECL
Definition tmp_alloc.h:75
#define BALLOC_TYPE(n, type)
Definition tmp_alloc.h:89
#define TEMP_B_FREE
Definition tmp_alloc.h:100