I want to do some stuff with symbolic arrays like this

a[[j]] + a[[j + 1]] == a[[j - 1]] + a[[j - 2]]

But upon declaring such an expression I get

Part::pkspec1: The expression j cannot be used as a part specification.

for all indices in the expression. It also does not work with the above expression when, for example, putting it into solve.

How can I do symbolic math with smybolic arrays?


Solve[a[[j]] + a[[j + 1]] == a[[j - 1]] + a[[j - 2]], a[[j]]]
  • $\begingroup$ I don't want to do recursive stuff $\endgroup$ – chr Mar 17 at 20:39
  • 4
    $\begingroup$ Use Indexed[]. $\endgroup$ – J. M. will be back soon Mar 17 at 20:45
  • $\begingroup$ Use Solve[a[j] + a[j + 1] == a[j - 1] + a[j - 2], a[j]], which solves for a[j]. $\endgroup$ – bill s Mar 17 at 23:49

You probably really want to use a[j] and RSolve[] instead. The code

RSolve[a[j] + a[1 + j] == a[-2 + j] + a[-1 + j], a[j], j]


{{a[j] -> (-1)^j*C[1] + (-1)^j*j*C[2] + C[3]}}

But you may still want to do it your way. The code

Solve[ a[j] + a[j + 1] == a[j - 1] + a[j - 2], a[j]]


{{a[j] -> a[-2 + j] + a[-1 + j] - a[1 + j]}}

which may be what you want. A third alternative mentioned in comments is to use Indexed[a,j] instead of a[[j]]. The function Indexed[] was introduced in 2014 with version 10.0 of Mathematica.

  • $\begingroup$ I had used this Notation before but I did not like the "wrong" semantics since single parantheses are for function calls but if does not work otherwise…. $\endgroup$ – chr Mar 20 at 16:34
  • $\begingroup$ Keep in mind that Mathematica does not have "functions" as in other computer languages.It has pattern matching, rewrite rules, evaluation rules, and symbolic expressions. You may think that f[x] = x^2 defines a "squaring function" but it doesn't. Even f[x_] := x^2 which is the closest equivalent is not a function. $\endgroup$ – Somos Mar 20 at 17:19

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.