How to define a general function in mathematica?
Asked Answered
F

1

9

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.

Fore answered 21/9, 2012 at 11:59 Comment(1)
What would be your next step ? What would you do with such a general parameter a(x,y) ?Dellinger
K
13

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)]
*)
Kaisership answered 22/9, 2012 at 8:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.