       Re: Sin*Cos + Log

• To: mathgroup at smc.vnet.net
• Subject: [mg113233] Re: Sin*Cos + Log
• From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
• Date: Wed, 20 Oct 2010 04:06:26 -0400 (EDT)

```Well, I can see three ways to go about this. One, which is convenient but perhaps carries some risks, is to redefine Plus and times so that they work as ring operations on functions. You can do it as follows:

Unprotect[Plus, Times];

(f_ + g_)[x_] :== f[x] + g[x]

(c_?NumericQ f)[x_] :== c f[x]

(f_*g_)[x_] :== f[x]*g[x]

Protect[Plus, Times];

Then

(Sin-2Cos*Log + Identity)[x]

x + Sin[x] - 2*Log[x]*Cos[x]

(You can add rules for powers too).

Another possible approach is based on judicious use of Through. For example, in your example this works:

(Through[#1, Times] & ) /@ Through[(Sin*Cos + Log)[x], Plus]

Log[x] + Sin[x]*Cos[x]

However, it's harder to make this work with expressions such as  Sin*Cos - 2 Log. The third approach involves more typing. Simply, instead of f==Sin*Cos+Log, built a "pure function":

f == (Sin[#]*Cos[#] - 2 Log[#]) &;
then simply

f[x]

Sin[x]*Cos[x] - 2*Log[x]

Andrzej Kozlowski

On 19 Oct 2010, at 11:54, Sam Takoy wrote:

> Hi,
>
> I'm working on a project that involves manipulating lots of functions.
> It would be much easier if I could manipulate functions without
> evaluating them and then evaluate them at the end. To this end, is there
> a way to endow
>
> f == Sin*Cos + Log
>
> with meaning and then somehow evaluate
>
> f[x]?
>