1
$\begingroup$

I'm brand new to Mathematica...should be an easy one. Instead of writing u[x] everywhere in the code, how do I assign the dependency early on like this:

u=u[x]

so that instead of writing this everywhere

D[u[x],x]

I can just write

D[u,x]

This is just a simple example but I find it annoying to have to bracket all the dependent variables all the time.

$\endgroup$
14
  • $\begingroup$ Give it a different name. Try f = u[x] and D[f, x] instead. Not really sure that I recommend doing things that way, but it works for your expressions. $\endgroup$
    – march
    Jul 10, 2015 at 2:27
  • $\begingroup$ yeah, that's what I'm doing now which works, but then I have all these dummy names and it's a little harder to keep track of. Why would you not recommend doing it this way? This seems like a trivial issue. Why would you not be able to assign variable dependency? It makes the code very messy for no apparent reason. $\endgroup$ Jul 10, 2015 at 2:32
  • 1
    $\begingroup$ @Guesswhoitis. Agree. My point is that the outcome depends on what you're actually doing after your last Clear and in what order. That may surely confuse a new user $\endgroup$ Jul 10, 2015 at 4:02
  • 3
    $\begingroup$ u[a_, b_, c_, d_, e_] := a^2 + b^3 - c^2 + d*e; uvars = {a, b, c, d, e}; D[u @@ uvars, uvars[[2]]] or D[u @@ uvars, b]...etc. $\endgroup$
    – ciao
    Jul 10, 2015 at 4:41
  • 1
    $\begingroup$ This will work in the specific case you gave: uvars=Sequence[a,b,c,d,e,f]; fn[uvars]=a^2+3b+4c+2d; D[fn[uvars],b] $\endgroup$ Jul 10, 2015 at 6:55

1 Answer 1

6
$\begingroup$

As you've seen, a definition like

u = u[x,y,z]

is not generally appropriate in Mathematica because the principle of the Mathematica evaluator is to repeatedly apply all known definitions to an expression until the result no longer changes. Here, the recursive definition is repeatedly applied, with no termination condition.

If you're just looking for a shorthand notation for u[x,y,z], a simple way to define one would be

u[] = u[x,y,z]

which sidesteps the recursion issue. (It defines a downvalue for u, rather than an ownvalue.) Now you can do

D[u[], x]

$u^{(1,0,0)}(x,y,z)$

Matlab has a whole "symbolic function" apparatus that doesn't exist in Mathematica, because it's unecessary -- any expression can act as a symbolic function. However, we can set up a similar behavior if we want. One possible way is to preprocess all input to add the arguments to bare function calls.

Set it up with

$SymbolicFunctions = {};
DeclareSymbolicFunction[func_[var__]] := AppendTo[$SymbolicFunctions, func[var]];
ClearSymbolicFunctions[] := $SymbolicFunctions = {};
SetAttributes[replaceSymbFuncs, HoldAll];
replaceSymbFuncs[input_] := Unevaluated@input /. 
    Flatten[{#[[0]][v__] -> #[[0]][v], #[[0]] -> #} & /@ $SymbolicFunctions];
$Pre = replaceSymbFuncs;

Now we can declare a symbolic function

DeclareSymbolicFunction[u[x, y, z]]

If we call the function with no arguments, the arguments are appended

u

$u(x,y,z)$

but if we call with different arguments, those are respected:

u[a, b, c]

$u(a,b,c)$

And it works inside D, for example:

D[u^2, x]

$2 u(x,y,z) u^{(1,0,0)}(x,y,z)$

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.