Is there a way to create and manipulate a function of a sum of arbitrary length? Specifically, I'd like to create a function like the following:

f[T_]:= Sum[exp[Sum[Q[[i,j]]T[[i]],{i,1,nn}]], {j, 1, N}]

and I'd like to be able to take derivatives of it with respect to the Ti's, and have it return an answer in the form of a sum. Can this be done in simple Mathematica or do I need to use some additional software, like Xact?


2 Answers 2


It's hard to tell exactly what you are looking for, but here is something to get you started:

n = 5; nn = 10;
Q = RandomInteger[{0, 10}, {nn, n}];
T = Array[t, 5];
D[Exp[Q.T], {T}]

I've replaced sums with a matrix multiply since Q enters linearly. Note that the derivative is taken with respect to all n entries of the T variables.


I think you will find that Mathematica provides little support for vectors of "arbitrary length"

and that operators, like taking the derivative, when applied to a sum tend to not operate inside the sum

and that Mathematica will tend to not return results as a sum if there is any way it can evaluate that sum, because Mathematica is what is called "an infinite evaluation system" which means it will perform every evaluation that it can and tend to not leave expressions in unevaluated or partially evaluated form.

So I think I understand what you want to accomplish and I think Mathematica will resist trying to give you your result in the form that you want, with or without other software tools. You can try to to write code and functions to attempt to overcome Mathematica's default behavior, but this is often more challenging than letting Mathematica behave in the default way.

  • $\begingroup$ Ah, I see. Thanks for your answer, I sort of suspected this was the case. Any suggestions for better ways to manipulate sums with variable length? $\endgroup$
    – naomig
    Commented Nov 16, 2018 at 22:33
  • $\begingroup$ I don't know what you are trying to do so it is very difficult for me to offer firm suggestions. Is there any chance that you might be able to create a single term of your arbitrary sum and then perform all of the operations on that term? You may need to be careful with the boundary cases of j==1 and j==N or j==Infinity. $\endgroup$
    – Bill
    Commented Nov 16, 2018 at 22:48

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.