5

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.

AsukaMinato
  • 9,758
  • 1
  • 14
  • 40
Quasar Supernova
  • 1,678
  • 8
  • 13
  • I could define g[x_] := Sin[Sin[x]] and then call W[g] but that is not what I want. – Quasar Supernova Sep 29 '20 at 03:05
  • 2
    W[f_] := Integrate[f[x], {x, 0, Pi}]; W[Sin[#] + Cos[#] &] – cvgmt Sep 29 '20 at 03:25
  • 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. – Quasar Supernova Sep 29 '20 at 04:15
  • 1
    @QuasarSupernova Maybe Through can do what you desire. But I think pure function is the most general way, rather than "case by case". – Αλέξανδρος Ζεγγ Sep 29 '20 at 04:54
  • 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. – Szabolcs Sep 29 '20 at 11:58

4 Answers4

13

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.

9

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[#]&]
evanb
  • 6,026
  • 18
  • 30
  • 1
    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. – yosimitsu kodanuri Sep 29 '20 at 06:32
6

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]

Ronald Monson
  • 6,076
  • 26
  • 46
4

For Sin + Cos you could define SubValues for CirclePlus:

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

Then:

W[Sin ⊕ Cos]

2

Carl Woll
  • 130,679
  • 6
  • 243
  • 355