2
$\begingroup$

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?

EDIT:
Example

Solve[a[[j]] + a[[j + 1]] == a[[j - 1]] + a[[j - 2]], a[[j]]]
$\endgroup$
3
  • $\begingroup$ I don't want to do recursive stuff $\endgroup$
    – chr
    Commented Mar 17, 2019 at 20:39
  • 4
    $\begingroup$ Use Indexed[]. $\endgroup$ Commented Mar 17, 2019 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
    Commented Mar 17, 2019 at 23:49

1 Answer 1

0
$\begingroup$

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]

returns

{{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]]

returns

{{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.

$\endgroup$
2
  • $\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
    Commented Mar 20, 2019 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
    Commented Mar 20, 2019 at 17:19

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.