Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a general function in mathematica?

How can I define a general function without the exact expression in Mathematica? For example, I don't need this:

a[x_, y_]:= 2x + 3y,

I need to work with a general parameter a(x,y) instead:

a[x_, y_]:= a[x, y].

Of course, this line doesn't work too well.

Edit1:

Well, I am using the parameter in another unknown function (unknown because it is defined only in the package), which works basically as a derivative, i.e. it's a very complicated mix of first and second derivatives.

This line

a[x_, y_]:= a[x, y],

actually works (kind of), because the parameter 'survives' the package as a function, except in the output I get the annoying "Hold[a[x,y]]" instead of just a[x,y], and I can't use the result as a function anymore.

like image 313
Dee Avatar asked Sep 21 '12 11:09

Dee


1 Answers

The way to "define" a function without specifying an expression is to not define it. Just use it.

Example:

D[f[x] g[x],x]
(*
==> g[x] f'[x] + f[x] g'[x]
*)

As you can see, I didn't define f or g, and yet Mathematica has no problems calculating with them.

Note that you can also make definitions using those functions. For example:

modify[a[x_,y_]]:=a[y,x+y]
modify[a[2,3]]
(*
==> a[3, 5]
*)

You can even define arithmetic operations on them. For example, you could define a function exp to symbolically calculate with exponentials (note the lower case, because Exp is already the built-in exponential function), and then define

exp/: exp[a_] exp[b_] := exp[a+b]
exp/: exp[a_]^n_Integer := exp[n a]

and then write

expression = 3 exp[x] exp[y+z]^3
(*
==> 3 exp[x + 3 (y + z)]
*)
like image 147
celtschk Avatar answered Sep 19 '22 18:09

celtschk