# Extending functions in mathematica

I am starting with Mathematica and I am willing to use it to perform some cumbersome computations for me.

Specifically, I want to obtain the duals of some convex cones and that requires extending some well-known functions. For instance, the functions

$$y\exp \left(\frac{x}{y} \right) \text{ and } x\log \left(\frac{x}{y} \right)$$

are of my interest and I would like to define $0\exp(\frac{x}{0})=0$ for each $x$ and $0\log(0)=0$, $0\log(\frac{0}{0})=0$, and $x\log(\frac{x}{0})=\infty$.

Can anyone help me to work this out?

• Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. – bbgodfrey Aug 6 '18 at 0:07

There are several ways to accomplish your goal, if I understand it correctly. For instance,

Clear[f];
f[x_, 0] := 0
f[x_, y_] := y Exp[x/y]

f[1, 1]
(* E *)

f[1, 0]
(* 0 *)


or

Clear[g]
g[x_, y_] := If[y == 0, 0, y Exp[x/y]]

g[1, 1]
(* E *)

g[1, 0]
(* 0 *)


The second function in the question can be handled similarly.

It seems that what you are interested in is how to attach 'special' definitions for a given function. By this I mean the need to allow eg $y e^\frac{x}{y}$ evaluate normally for all values of $x$ and values of $y$ other than $0$ while making it evaluate to $0$ whenever $y$ is $0$ (the other function can be treated in principle with the same approach so it won't be discussed further).

There are more than a handful of ways to achieve such an effect in MA/WL, two of which-the most straightforward IMHO-I'll display in what follows.

The first way to achieve the desired effect is to construct a function with different 'branches' for the different cases you are interested in.

To that effect you can use Piecewise:

f[x_,y_]:=Piecewise[{
{y e^(x/y), y!=0},
{0, y==0}},
$Failed ]  A couple of notes on f: Inputs are assumed to be numerical quantities. This is implicit but could be defined more rigorously if that is desired (potential ways to do that with minor adjustments in f would include PatternTest's on the inputs or incorporating numeric checks (eg using NumericQ) within Piecewise branches-there are still more ways to achieve this eg using Condition). Related to this is that f will return $Failed with symbolic inputs for y. More rigorous (and probably more demanding) definitions of f could involve Attribute NumericFunction but this is probably excessive for the current context.

Finally, similar results can be obtained with Which.

The second way to achieve similar effects with f above, is to make use of how MA/WL uses/matches patterns.

When evaluating an expression, all known definions are applied on the input until it no longer changes (this is a simplified way to put it but it suffices for the task at hand).

MA/WL uses specific definitions before more general ones during evaluation. To that effect g[x_,0]=0 is more specific than g[x_,y_]:=y Exp[x/y].

Evaluating these definitions will let MA/WL 'know' that expressions like g[_,0] are more specific than expressions like g[a,b] so when it encounters patterns like the former, it will supply the 'correct' replacement ie $0$ instead of the full definition of $y e^\frac{x}{y}$.

Obviously, numeric constraints and similar considerations to those presented in the first approach are relevant to this approach, too.