# How to produce functions [closed]

I want to produce n functions. For example, assume that by giving n=3, 3 definite integrals will be created, like the following:

In[1]:= n=3;

(The algorithm and Processing )

Out[1]:={ Integrate[f1,{x,x1,x2}], Integrate[f2,{x,x3,x4}], Integrate[f3,{x,x5,x6}]}


OR:

Out[1]:=  Integrate[f1,{x,x1,x2}]+ Integrate[f2,{x,x3,x4}]+ Integrate[f3,{x,x5,x6}]


What is the elegant way to do this calculation and produce different functions ?!

-

## closed as unclear what you're asking by m_goldberg, Artes, belisarius, rasher, Verbeia♦Apr 14 at 23:26

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

It's not clear to my why Table wouldn't work for you. Can you give a specific example of what your input is and what output you need? –  Szabolcs Apr 14 at 17:08
Perhaps you need Apply, e.g. Integrate[ #1, {x, #2, #3}]& @@@ {{f1[x], x1, x2}, {f2[x], x3, x4}, {f3[x], x5, x6}}. Then you needn't specify n. –  Artes Apr 14 at 17:17
@ Szabolcs : In fact, I have a formulation and I want to use a code to add some terms ( like I've mentioned above ) into my formulation. n is the number of those terms. I hope that I could clarify my question. –  Shellp Apr 14 at 17:41
Is this closer? Array[Integrate[Symbol["f"<>ToString[#]][x],{x,Symbol["x"<>ToString[2#-1]],Symb‌​ol["x"<>ToString[2#]]}]&,4] –  chuy Apr 14 at 18:27
he answered the literal question, but its not likely the best approach to your problem. Using f[1][x],f[2][x].. for example would be cleaner and avoid messing with symbol names. –  george2079 Apr 14 at 19:28

Clear[fu, fus]
With[{uq = Unique["fus"]}
,
fu[k_] := uq[[k]];
uq =
Function[{x}, #] & /@
Table[
Integrate[(x^k Sin[x])/Exp[k x^2], x]
,
{k, 1, 5}
]
]


I picked this example because this integral does not produce a nice result for a fixed k. It can be reasonable to want to make a few functions here, as it may be nice to avoid the integrals being calculated again and again. This is very similar to memoization, except everything is calculated up front, rather than when needed.

Usage

We can ask a value like this

fu[2][1] // N // Chop


-0.0379245

Below is the definition of one of the functions

f[2]

Function[{x},
1/128 E^(-x (I + 2 x)) (-4 - 4 E^(2 I x) - 16 I x + 16 I E^(2 I x) x -
3 E^(1/8 (I + 4 x)^2) Sqrt[2 \[Pi]] Erfi[(1 - 4 I x)/(2 Sqrt[2])] -
3 E^(1/8 (I + 4 x)^2) Sqrt[
2 \[Pi]] Erfi[(1 + 4 I x)/(2 Sqrt[2])])]


Others reasons for posting this answer are to show an alternative to using SubValues and to using strings to generate symbols.

Alternative

Clear@fu;
ReleaseHold@
Hold[SetDelayed][
Hold[fu[k_, x_]],

Hold[Part][
Table[
Integrate[(x^k Sin[x])/Exp[k x^2], x]
,
{k, 1, 5}
]
,
Hold[k]
]
]


usage

fu[2, 1] // N // Chop


-0.0379245

Note: It seems like I am the only one who uses Hold and ReleaseHold in this way. The key is to ignore the Holds while reading and realise that the only thing that is different w.r.t. a usual definition is the order of evaluation. The advantage of this alternative here is that we do not need another symbol to store our definitions.

P.S.

Sorry if Function[{x}, #] &/@... makes your head hurt :P

-
Thank you Jacob !! It's really great. –  Shellp Apr 15 at 5:20