4
$\begingroup$

I am trying to define a function f[x1,x2,...,xn] with n integer but not specified. And then I would like this funktion to behave properly under the derivative operation, so that D[f[x1,x2,...,xn],xi] actually results in an expression which unambiguously displays that it is the first derivative in respect to the i-th variable. Up until now I had little success in employing the Mathematica function D[x_,y_] and have been simply substituting it for a place holder d[x_,y_]. However, this is not very convenient, since those placeholders do not respect commutativity. Maybe there is such functionality already, which I do not know of? Or maybe one can implement it? Thank you for any suggestion!

EDIT:

Please note, I am not looking to define custom functions of a finite number of variables tied to the derivative operation. What I am actually interested in, is defining a function which is dependent on an unspecified number of variables n. And introducing a formalism such that taking derivatives in respect to one of these variables of an unspecified index i still works consistently.

EDIT2:

To explain what I mean in more detail, I would like to be able to define a function f[v] in which I would like to treat v as a 1-dimensional list v={v1,v2,v3,...,vn}. However, I would like to keep the amount of elements in v arbitrary (without specifying to a certain length). And then I would like to define a derivative operation, which would allow expressions like D[f[v],v1] or D[f[v],v2] and even D[f[v],vi] which should not evaluate to zero, but to some meaningful expression, from which I still could extract the information which derivative it is, and with respect to which of the n variables.

$\endgroup$
  • 1
    $\begingroup$ Remember that you can use Derivative[n][f] $\endgroup$ – Dr. belisarius Oct 26 '14 at 4:01
  • $\begingroup$ When you try D[f[x1, x2, xn], x2] you do get an unambiguous result, right? So I think you have to state the problem more clearly with an example. $\endgroup$ – Jens Oct 26 '14 at 17:32
  • $\begingroup$ The answer in the tagged question above does not answer my actual question. I will edit my question to make it more clear. $\endgroup$ – Kagaratsch Oct 27 '14 at 17:30
  • $\begingroup$ This may be related to or perhaps is a duplicate of symbolic-derivative-of-n-term-product. $\endgroup$ – Sjoerd C. de Vries Oct 27 '14 at 18:49
  • 1
    $\begingroup$ Derivative[n][f] denotes an unspecified number of derivatives with respect to a single variable. What I need is a notation that denotes an unspecified number of derivatives with respect to an unspecified number of variables unambiguously, which clearly cannot be captured by merely Derivative[n][f]. $\endgroup$ – Kagaratsch Oct 27 '14 at 22:35
2
$\begingroup$

I hope this toy example helps.

First I define some function of an arbitrary number of arguments, such that

f[v1, v2, v3,...] == v1 + 2 v2 + 3 v3 + ...

I do this with

f[v__] := Plus @@ MapIndexed[#1 First@#2 &, {v}, {1}]

I can then, without knowing the exact number of arguments, take a derivative with respect to a given argument by

i = 3
Derivative[Sequence@@(ConstantArray[0,i-1]~Join~{1})][f]
(* 3 & *)

Unfortunately, it doesn't allow a purely symbolic approach. I can't clear the i and do this:

Clear[i]
Derivative[Sequence@@(ConstantArray[0,i-1]~Join~{1})][f]
(* i & <--- ain't happening *)

Also problems arise if the derivative is dependent on the number of arguments. e.g.

f[v__] := Times @@ MapIndexed[Exp[#1] First@#2 &, {v}, {1}]
Derivative[0, 1][f]
(* 2 E^(#1 + #2) & *)
Derivative[0, 1, 0][f]
(* 6 E^(#1 + #2 + #3) & *)
$\endgroup$
  • $\begingroup$ And now I realize, I'm necroposting. Hopefully my activity will attract someone with a better solution. $\endgroup$ – LLlAMnYP Apr 27 '15 at 14:21
  • $\begingroup$ Necroposting is certainly not frowned upon here, long as you make a meaningful contribution. $\endgroup$ – J. M. will be back soon May 27 '15 at 14:39
  • $\begingroup$ @LLlAMnYP I think you expressed very clearly what I think the OP is asking for when you say you "can't clear the i"? If we could clear the i that would be the solution I think. Maybe it would be worth adding that into the question? Just trying to get some attention to this as I would love to have a solution. $\endgroup$ – tortortor Jan 27 '18 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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