4
$\begingroup$

When I write W[f_] := Integrate[f[x], {x, 0, π}] I may rightly conclude that W[Sin] = 2, W[Cos] = 0, W[Log] = π (-1 + Log[π]) etc. But I am unable to define W[Sin + Cos] or W[Sin[Sin]] etc. Please help.

$\endgroup$
  • $\begingroup$ I could define g[x_] := Sin[Sin[x]] and then call W[g] but that is not what I want. $\endgroup$ – Quasar Supernova Sep 29 at 3:05
  • 2
    $\begingroup$ W[f_] := Integrate[f[x], {x, 0, Pi}]; W[Sin[#] + Cos[#] &] $\endgroup$ – cvgmt Sep 29 at 3:25
  • $\begingroup$ Is there something analogous to Hold[ Sin+Cos ] and then Distribute[ Sin+Cos ][x] and finally Release[ % ] ? The solutions given here work on a case by case basis. $\endgroup$ – Quasar Supernova Sep 29 at 4:15
  • 1
    $\begingroup$ @QuasarSupernova Maybe Through can do what you desire. But I think pure function is the most general way, rather than "case by case". $\endgroup$ – Αλέξανδρος Ζεγγ Sep 29 at 4:54
  • $\begingroup$ I find the title misleading: it's not what you are actually asking in the body of the question. Also, unusual choice of accepted answer. If that's indeed the one that answers your problem the best, then the question could have been phrased better. $\endgroup$ – Szabolcs Sep 29 at 11:58
4
$\begingroup$

The issue is that a functional operates on functions so in

W[f_] := Integrate[f[x], {x, 0, Pi}]

W needs to be fed a function. The expressions Sin+Cos or Sin[Sin] fail because these are not WL functions. Working in functional space we have the composition operators @* and \* but these are not sufficient when wanting to use WL's built-in functions which are geared to work with general expressions. But one way of co-opting WL's functions to operate in functional space is via FunctionalConstruct as follows:

FunctionalConstruct[op_, fs__] := Function[x, op @@ Through[{fs}[x]]];
FunctionalConstruct[op_] := op;

f = FunctionalConstruct[Plus, Sin, Cos];
ga = FunctionalConstruct[Sin, Sin];
gb = FunctionalConstruct[Sin@*Sin];

In some ways for these definite integrals it would be more natural to simply now write

Integrate[f, {0, Pi}]
Integrate[ga, {0, Pi}]
Integrate[gb, {0, Pi}]

in functional space but because in calculus it is often so useful to use dummy variables (indefinite integrals, turning expressions into desired functions w.r.t. x,y etc.) we get W to automatically apply the function to a dummy variable prior to performing the integral:

W[f]
W[ga]
W[gb]

2

Pi StruveH[0, 1]

Pi StruveH[0, 1]

| improve this answer | |
$\endgroup$
12
$\begingroup$

Make sure that you pass the right head/function to W. For example, Sin[Sin] @ x gives Sin[Sin][x], obviously unrecognizable by MMA, not to mention the aftermath evaluation of the integral. So one optional solution can be these

W[Sin[#] + Cos[#] &]
W[Sin @* Sin]

where @* is Composition.

| improve this answer | |
$\endgroup$
8
$\begingroup$

Sin is something that takes an argument---Sin[7] is a number.

In contrast, Sin+Cos is not something that takes an argument! Your functional is trying to evaluate eg. (Sin+Cos)[7] which, without help, it does not understand.

So, you need to turn the argument of W into something that properly takes argument.

You could say

g[x]:= Sin[x]+Cos[x]
W[g]

for example. Or, you can do it anonymously using pure functions (#),

W[Sin[#]+Cos[#]&]
| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ I think this answer addresses the problem in a straightforward way and proposes two concise solutions that are idiomatic and honestly it's what I would expect someone to use. $\endgroup$ – yosimitsu kodanuri Sep 29 at 6:32
4
$\begingroup$

For Sin + Cos you could define SubValues for CirclePlus:

CirclePlus[f_, g_][x] := f[x] + g[x]

Then:

W[Sin ⊕ Cos]

2

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.