Operations on functions

It's possible to add or multiply two functions, subtract one from another, divide one by another, and compose (chain) two functions (user defined, built-in, or defined in any other way). The result is called a pseudo function, and can be used anywhere a function is expected.

The operators for function addition and subtraction are + and -, while the operators for function multiplication and division are &* and &/ respectively. The operator for function composition is <>.

Operator Definition
h = f + g f(x) = f(x) + g(x)
h = f - g f(x) = f(x) - g(x)
h = f &* g f(x) = f(x) * g(x)
h = f &/ g f(x) = f(x) / g(x)
h = f <> g f(x) = f(g(x))
h = -f f(x) = -f(x)

For f + g, f - g, f &* g, and f &/ g, f and g should have the same signature, i.e., they take the same number of input arguments, and return two values that are compatible for addition, subtraction, multiplication, and division respectively.

For f <> g, the number of output arguments of g should be the same as the number of input arguments of f.

For example

>> f = sin + cos;
>> f(pi/4)
    1.414213562

>> p = sin &* cos;
>> p(pi/4)
    0.5

>> g = sin - cos;
>> g(pi/4)
    -1.110223025e-16

>> h = log <> sin;
>> h(pi/2)  
    0
Note that in the above, f and g don't have to be both `real' functions - they can be pseudo functions, or anything that can be used as functions, such as numbers or matrices. If either operand is a function, the operation will create a new pseudo function. For example
f = sin + 2
will create a function f(x) = sin(x) + 2x. Note that 2 as a function means x -> 2x instead of x -> 2.

oz 2009-12-22