# How do mathematicians handle functions of functions that may change?

E.g. let $f(x) =$ some function. Now define $h(x) = f(g(x))$. Now suppose the definition of $g(x)$ changes around in a discussion. Do we still refer to $h(x)$ as the original $h(x)$ or only when we do something like $h(g)(x) = f(g(x))$ ?

Reason: I'm writing a math lib that uses lazy expression eval. Now if I say (in D code):

    Var x, y;

Function f = new Function;
Function g = new Function;
f = x^2 + y^2;      // f is defined as f(x,y) = x^2 + y^2
g = x^3 + y^3;      // g similarly

Function h = f(x,y) + g(x,y);
// I've decided to let f(x,y) return the expression x^2 + y^2
//
Now if I did
f = x^4 + y^5;
//h would not be affected since h's expr points to the old f expr still

// to make a copy of the expression, in case f, g change later, you'd do:
h = h.dup;
// or earlier do:
h = (f(x,y) + g(x,y)).dup;   // dup means duplicate or make an exact memory copy of an object
// Now if the internal expression of f is changed, h will not change, again

//But if I said:
h = f + g;
// That could mean that h(x,y) = (f + g)(x,y) -AND- that h changes when f and g are re-assigned


I might have answered my own question in there. The expressions will be coded as trees with operators at the nodes and edges going down to operands or more operators, and so on. Are my concerns making sense to anyone? I'm going to go ahead and post this, in case my little brainstorm helps others.

## EDIT:

Came up with some answers. Let's work in d code, and assume that our types are all classes, so this means A a; will mean a is a reference type (like an address where the data of A is stored). When we decide how to design something, sometimes we see that certain things would happen if we chose one way and we then apply the "K.I.S.S." principle. So let's look at the use of the library:

If Function has a setDomain() or setCodomain() method, then functions built from other functions:

h = f + g; (where references to f obj and g obj are stored in h's expression tree)

may become invalid. On the other hand, we would like the ability of a functions domain to change, so instead resolving all such h functions that are dependent, let's return a new function with the new domain.

So, for Function objects declared as from Z and on R, we'll allow their definition to change in all places except their domain and codomain. While still retaining this use case:

"hmm.. let's define $e(x)$ = (some infinite sum)" "I wonder what this function does to matrices if it were defined on them, let's see..." "Create matrix M over the reals and" auto e2 = new Function(e(M));

$e(x)$ which was declared in code as from R to R will substitute a matrix in and make sure every operation we have in R can be done on M's space, and return a new Function with new domain if it does.

The setDomain(), setCodomain() functions will be replaced with factories that return the new function between new sets.

## EDIT 2

I think I got now. If we say h = f + g, do we mean that h is a function of two functions, or that h is a function on the same domain(s) as f and g? So what would calling h(a,b) produce? The first way would produce h(a,b) = a + b, and the second h(a,b) = (f + g)(a,b). But the second way is already handled by this syntax: h = f(x,y) + g(x,y). So h = f + g should mean function space addition and you'd have to call h(f,g)(a,b) to get (f+g)(a,b). I might run into some other issues by not specifying the input variables of h, as in h(x,y) = f(x,y) + g(x,y); But I can't do that directly in D code the way I have it now and with my limited understanding of the language - i.e. h(x,y) returns h's expression, and h is what I need to assign to. But if I overload Expression's assign operator, I could do it that way. One question is then is that how I want the whole library to work?

## EDIT 3

All right. EDIT 2, didn't work either. h = f + g implying h is the function space addition operator, doesn't mesh with math unless I have multiple D classes that interpret that statement in different ways. I think h = f + g most comonly means h is an element of the same function space as f and g, and h(f,g) = f + g means h is a binary function on the space of f and g. And the notation f = x^2 + y^3 could be interpreted that f is in the same space as x and y. So I'm going to need something that specifies that f is a function of x and y. Last edit.

-
Mathematicians don't change their functions. This is more of a computer science question. –  Qiaochu Yuan Mar 27 '12 at 19:35
What about functions of mappings? Where the variable is the mapping. But you're saying this doesn't change the first function? But instead changes the output (which could be a function). I want to try to mimick this idea or make sure it meshes with the library useage - it's for mathematicians, and is going to be as accurate as D allows at the library API level, and can be perfectly accurate at the end user / command interpreter level. –  Enjoys Math Mar 27 '12 at 19:45
Also, the change happens symbolically sometimes. In one section of an article f = blah. And in the context of another article f = blah2. I'm just preparing for the worst case that someone wants to re-use the f symbol. And if the API-level user wants to not change a function they can use const / immutable keywords of D. –  Enjoys Math Mar 27 '12 at 19:47
To add to Quaochu's answer, mathematicians tend to think of functions as immutable objects that live in a function space, rather than machines that do things. You can imagine that out of the gigantic space of all possible functions, you have singled out a specific one and decided to call it "h". Then later you decided to reuse the label "h" to refer to a different but related function. –  Nick Alger Mar 27 '12 at 20:01
Mathematical objects are like values in a program written in a pure functional language, like Haskell. They simply do not change—that's why Haskell does not have variables: it simply does not need them! –  Mariano Suárez-Alvarez Mar 27 '12 at 20:42