I've upgraded the problem's solution but still not happy with the result.
Here are the points that bother me:
1) Do I use "Parallelize" correctly?
2) When "Parallelize" is ON, coefficients are not computed, i.e. "?fc" gives nothing. Why does this happen?
3) Double Do loop computes extra coeffs. I need (a,b) to run only through given pairs.
FindPairs::usage = "Placeholder";
FindPairs[function_, coeffs_, args_List, test_Integer: 0] := Module[
{
FUNCTION,
ARGS,
OUTPUT = {__, __},
a, b, c, d
},
{
(*Parallelize[*) (*<-- UNCOMMENT FOR PARALLEL COMPUTATION *)
(* PAIRS *)
ARGS = args;
FUNCTION = Im[List @@ Expand[TrigToExp[function]] /. {a_ E^b_ -> b}
/. {a_ ARGS[[1]] + b_ ARGS[[2]] -> {a, b}}
/. {a_ ARGS[[1]] -> {a, 0}}
/. {a_ ARGS[[2]] -> {0, a}}]
/. Im[a_] -> {0, 0};
OUTPUT[[1]] = DeleteDuplicates[Transpose[FUNCTION][[1]]];
OUTPUT[[2]] = DeleteDuplicates[Transpose[FUNCTION][[2]]];
(* COEFFICIENTS *)
FUNCTION = Expand[TrigToExp[function]];
Do[
{
coeffs[a, b] = Which[
(a == 0 && b == 0), Coefficient[FUNCTION /. E^a_ -> ARGS[[1]], ARGS[[1]], 0],
(a == 0 && (b > 0 || b < 0)), Coefficient[FUNCTION /. a_ E^(b_ + c_ ) -> 0, E^(I b ARGS[[2]])],
((a > 0 || a < 0) && b == 0), Coefficient[FUNCTION /. a_ E^(b_ + c_ ) -> 0, E^(I a ARGS[[1]])],
((a > 0 || a < 0) && (b > 0 || b < 0)), Coefficient[FUNCTION, E^(I a ARGS[[1]] + I b ARGS[[2]])]
];
},
{a, OUTPUT[[1]]},
{b, OUTPUT[[2]]}
];
OUTPUT
(*]*) (*<-- UNCOMMENT FOR PARALLEL COMPUTATION *)
}];
(* EXAMPLE *)
f=x3^(3/2)(A1 Cos[8 x1] + B1 Cos[9 x1] + C1 Cos[10 x1])(Cos[x2]+
Cos[3 x2])+f00+ D1 Cos[7 x1]+E1 Cos[11 x2]+F1 Cos[x1] Cos[5 x2];
range = FindPairs[f, fc, {x1, x2}][[1]]
f - Sum[fc[i, j] E^(I (i x1 + j x2)), {i, range[[1]]}, {j, range[[2]]}] // Simplify
?fc
Thanks,
I.M.