Hello,
I need to make the following code very fast:
B[a_, m_, t_] := Module[{n, p, q, seq, s, f, b},
n = 1 - m; p = m/a; q = m (1 - 1/a);
seq[i_, j_] :=
Sequence @@
Flatten[Table[{i == K || j == t - K, s[i, j, K]}, {K, 1,
Round[t/2]}], 1];
s[i_, j_, K_] :=
Sum[Binomial[i, i - k] Binomial[t - i, j - i + k] (p q)^
k ((n + p) (n + q))^(K - k), {k, 0, K}];
f[i_, j_] :=
If[(t - i - j) >= 0, (n + p)^(t - i - j), (n + q)^-(t - i - j)];
b[i_, j_] := Which[
j == t, p^(j - i) f[i, j] Binomial[t, i],
i == t, q^(i - j) f[i, j] Binomial[t, j],
i <= j,
p^(j - i) f[i, j] Binomial[t, i] Which[Evaluate[seq[i, j]]],
i > j,
q^(i - j) f[i, j] Binomial[t, j] Which[Evaluate[seq[j, i]]]];
Return[Table[b[i, j], {i, 1, t}, {j, 1, t}]]];
It generates a t x t numerical matrix for an integer a and a real number m between 0 and 1. For low t it is fast enough but I need it for t=2000. I estimated that this would take years on a desktop computer… So I guess I need to compile the whole thing to C-code and possibly parallelize it. The problem is the seq function. It says that it cannot be compiled because the expression “Sequence” is not allowed. It generates a sequence of conditions of length t/2 and also uses the function s inside it. How can it be written in C-compatible manner? And is it possible to compile a function that uses another inside it? So I have a lot of questions. Unfortunately, my programming skills are only basic so any help would greatly be appreciated.