0
$\begingroup$

This question seems easy but I am struggling with it now, I tried a few ways and check this solution as well but couldn't help me, please check and help if you are able:
So I need to save the sum of array elements in single variable and I tried these approaches, I show you two of them here. (I must have x as variable to store result, and initialize the array as well)

Array [A, 3]
A = {1, 2, 5}
R2 = x == Sum[A[index], {index, i, 3}]  && i == 1
Simplify[Reduce[R2]]

And Also

R2 = x == Sum[A[index], {index, i, 3}] && A[1] == 1 && A[2] == 2 && A[3] == 5 && i == 1
Simplify[Reduce[R2]]

this is part of final answer I get

x == A[1] + A[2] + A[3]

It shows me a simple equation not final answer, but I expect a simple answer x== 8 or x-> 8, but it does not solve the SUM, seems I am missing something.

EDITED: based on @thorimur answer I completed my question. Actually the reason i used Reduce and Sum is: 1- I used reduce because we have more complicated expressions which Sum is part of that. 2- I used sum because not all the time we have initialized array. For some times, i need parametric array which must be shown in Sum. this is the more complete statement.

R2 = x == Sum[A[index], {index, i, 3}]  && i == 1
R3=Simplify[Reduce[
  Exists[{i,x,A},  R2 && // Array initialization // i == 1 ]]]

I know this does not work but this is my intention. we need a statement as R3

$\endgroup$

1 Answer 1

3
$\begingroup$

A couple things:

  • Mathematica generally doesn't require you to initialize arrays before constructing them or anything like that; you can just construct them! Array[A, 3] does not give any definitions to the variable A; it merely outputs {A[1], A[2], A[3]} without changing any variables. Only the next line does anything to affect the symbol A, and it defines A as {1,2,5}.
  • Single-brackets are for function application. So, in the above Sum, it expects A to be a function that takes in index. To access parts of a list, use double brackets. E.g. with A = {1,2,5}, A[[3]] evaluates to 5.
  • Sum is typically used for mathematical sums, like $\sum_{n=1}^\infty \frac{1}{n^2}$, which would simply be written as Sum[1/n^2, {n, 1, Infinity}]. In the above, you could have simply replaced i with its value. It's not used for adding up elements of lists, generally.
  • Reduce is essentially for solving equations, like Reduce[x^2 == 1, x]—you wouldn't expect to use Reduce in manipulating data structures, like you want to do here.
  • Note that the solution you linked is about getting a list of successive sums, not simply adding up the elements of the list! The function Total is used for that.

So, the following is sufficient:

A = {1, 2, 5}
x = Total[A]

There are also several ways to add up a range of indices but not all of them. One of them is indeed Sum[A[[index]], {index, indexmin, indexmax}], but you could also simply do Total[A[[indexmin ;; indexmax]]]; ;; specifies a span of indices (inclusive). So, for example, if A = {1,2,5}, A[[2;;3]] evaluates to {2,5}.

Also, if you have a list of indices—say you want to add up the elements at indices indexlist = {2, 4, 8}—you can simply feed a whole list to [[ ]] and get out a list of the elements at those indices, then apply Total. E.g. indexlist = {2, 4, 8}, A[[indexlist]] gets you {A[[2]], A[[4]], A[[8]]}. Then you can Total that list.

To create a symbolic array, though, you'll want to be sure to give your array and its components different names. For example, A0 = Array[A, 3]. Note that A0[[i]] now evaluates to A[i]. I'm still not totally sure what you need, but note that you might not need to do any initialization at all. I could write Sum[A[i], {i, 1, 3}] and get A[1] + A[2] + A[3] without needing to define an array A0 of these A anywhere. Mathematica's symbolic nature is a strength here.

Hope this helps; let me know if it doesn't quite get you where you need to be, or if anything I said is unclear! :)

$\endgroup$
9
  • $\begingroup$ Thanks a lot for your kind response, the reason I used Reduce is that, I have more complicated expressions which Sum is just part of that. for some statements, i have a parametric array . so we used SUM. please see my edited as it gets long here. thanks $\endgroup$
    – Azzurro94
    Apr 27, 2021 at 20:33
  • $\begingroup$ can we rewrite Total with Sum, I might need to get sum of a range of indices not all, thanks $\endgroup$
    – Azzurro94
    Apr 27, 2021 at 22:22
  • $\begingroup$ I'm not totally sure what you mean by parametric array, but I added a couple ways to extract and total sublists of lists! I don't quite know what your added code does, though. (I'm also not sure why we're multiplying and dividing by 1 at various places) $\endgroup$
    – thorimur
    Apr 27, 2021 at 22:47
  • $\begingroup$ Ah, wait, I think I understand. I think that would typically be called a symbolic array in Mathematica! $\endgroup$
    – thorimur
    Apr 27, 2021 at 22:49
  • $\begingroup$ added another potential section, but I'm still not sure it addresses what you need. Let me know if it doesn't! (I might need a fuller description of what problem you're actually trying to solve.) $\endgroup$
    – thorimur
    Apr 27, 2021 at 22:55

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.