# Tag Info

17

Sum is meant for working with symbolic sums (check the examples in the docs to see what I mean). What you are looking for here is Total: Total[a] (* ==> {b+d,c+e} *) Before we had this shorthand, we used Plus with Apply: Plus @@ a (* ==> {b+d,c+e} *) The most important difference between using Total and Plus is that Total is optimized for ...

16

The documentation for Product[] gives a nice example that you can adapt to your needs: With[{j = 2, n = 6}, Sum[(a - Subscript[a, i])/(Subscript[a, i] - Subscript[a, j]), {i, Complement[Range[0, n], {j}]}]] (a - Subscript[a, 0])/(Subscript[a, 0] - Subscript[a, 2]) + (a - Subscript[a, 1])/(Subscript[a, 1] - Subscript[a, 2]) + (a - Subscript[a, ...

13

Accumulate is absolutely the most idiomatic and appropriate answer here. However since Mathematica is very powerful at list manipulation, it might be illustrative to show you couple of other ways of doing the same thing, just so you learn to think outside of mainstream procedural ways. 1. Using FoldList: This is a functional way of doing exactly what you ...

13

In general Mathematica cannot compute symbolically infinite sums over primes because of the lack of appropriate mathematical tools. However there are infinite products over primes which are basically well understood on the mathematical level. One famous example is the Euler formula for the Riemann zeta function, one of the most beautiful (and mysterious ...

12

Here is the simplest answer: sum[n_] := Sum[i x[i], {i, 1, n}] x /: D[x[i_], x[j_], NonConstants -> {x}] := KroneckerDelta[i, j] D[sum[n], x[2], NonConstants -> x] $\begin{cases} 2 & n>1 \\ 1-n & \text{True} \end{cases}$ The trick here is the use of the NonConstants option of the derivative operator. This then has to be ...

12

According to the documentation: $\sum _i^{i_{\max }} f$ is by default interpreted as Sum[f {$i$, $i_{\max }$}] so we can abuse this: You can use Sequence to provide di too because: $\sum _{i=i_{\min }}^{i_{\max }} f$ is by default interpreted as Sum[f {$i$, $i_{\min }$, $i_{\max }$}] ssch has found undocumented but useful pattern that is ...

11

Note that Mathematica can do the symbolic sums: Sum[1/(x^2 + 1), {x, 1, Infinity}] gives $$\frac{1}{2} (-1 + \pi \coth[\pi])$$ and Sum[1/(x^2 + x + 1), {x, 1, Infinity}] gives $$-\frac{i \left(\psi ^{(0)}\left(1+\sqrt[3]{-1}\right)-\psi ^{(0)}\left(1-(-1)^{2/3}\right)\right)}{\sqrt{3}}$$ (where $\psi^{(0)}(x)$ is PolyGamma[0,x], and in fact ...

10

This is a common limitation experienced in versions prior to 9. As Oleksandr explains: Are you by chance using Mathematica 8? Integers were based on 32-bit machine values in previous versions, and this was changed to 64-bit only in version 9. As a result, Range[2^31, 2^31 + 1] returns a packed array only in the most recent version. Fold automatically ...

9

You can use Defer to see how to properly enter your "summation" type notation. Defer[1 + Sum[Sum[1/((k + 2) k!), {k, n, Infinity}], {n, 0, Infinity}]] You can then enter that output to see that it works. You must've entered something different.

9

Instead of creating a list of all tuples and then selecting those whose total is n-1, you could start with the IntegerPartitions of n-1, pad them to length n with zeroes, and create all the permutations: getvecs[n_] := Flatten[Permutations[PadRight[#, n]] & /@ IntegerPartitions[n - 1], 1] I would also suggest using Position for the support function: ...

9

If we transpose the indices of $v$ and $w$ so that $v'_{abe} = v_{aeb}$ and $w'_{ecd} = w_{ced}$, then we can compute $u = v' \cdot w'$: u === Transpose[v, {1, 3, 2}] . Transpose[w, {2, 1, 3}] (* True *) We can use a similar trick to compute $q$ if we reorder $v_{dea} \to v''_{ade}$, except that this time the $d$ and $e$ indices in $v_{ade}''$ and ...

8

Number theory questions are always a huge accumulator for up votes. :) From my experience I can say that the builtin MangoldtLambda function is pretty slow. So let's define a Mangoldt function on our own. The Mangoldt function is defined by: $\Lambda(n) \equiv \left\{ \begin{array}{1 1} ln\ p & \quad \text{if n =$p^k$for p a prime}\\ 0 & \quad ... 7 There are several methods in Sum which may help in various cases. To find your second sum symbolically in apparently real form you can try, e.g. Sum[1/(1 + x + x^2), {x, 1, Infinity}, Method -> "HypergeometricTermPFQ"] However there are still other ways to get the same result without methods specified, e.g. : s = Sum[1/(1 + x + x^2), {x, 1, ... 7 If your elements are in lists the fastest way is to use array operations. In the present case of an outer product one index, let's say "i", will not be expanded, on the other you want to thread. To operate on a list the function needs the attribute Listable. The Times function, as many other internal ones, is already listable, that is Times[{1,2,3},x] = ... 7 A brute force solution is to check all possible values of this function. num = {1/10, 1/2, 4/7, 3/5, 2/3}; pow = {0, 1, 2, 3, 4}; To obtain value for one combination use the Inner function Inner[Power, num, pow, Plus] (* => 2222701/992250 *) Then we apply function Inner[Power, num, #, Plus]& on all permutations prm = Permutations[pow]; val = ... 6 The sum is a little strange, because the multinomial coefficient makes sense only when$k_1+k_2+\ldots+k_n=m$. I will assume this restriction is (implicitly) intended and that$n$is fixed. (If not, a variation of the following solution will work.) Notice that the set $$\{0 \le k_1 \le k_2 \le \ldots \le k_n \le m\}$$ is in one-to-one correspondence ... 6 Since the sum goes over the last index of$e$and the first index of$A$, it is directly done by using Dot: dim = 5; e = Array[\[ScriptE], Table[dim, {dim}]]; a = Array[\[ScriptA], Table[dim, {2}]]; c = e.a; Here I defined the arrays with the appropriate dimensions but suppressed the output because it's too long for five dimensions. Another ... 6 If you know in advance that your sum is already convergent, you can skip the convergence check. Also, it sometimes helps to change the Method used by NSum[]. Here, I use the Shanks transformation as the summation method: Plot[n/2 NSum[2^(-j) (1 - 2^(-j))^(n - 1), {j, 1, ∞}, Method -> {"WynnEpsilon", Degree -> 2, "ExtraTerms" -> 30}, ... 6 First of all, as you seem to indicate, you don't have the problem with accuracy if you use := (SetDelayed) in your function definitions instead of = (Set), although, as you say, with this change it takes longer to evaluate f. The accuracy improves because Mathematica will calculate the Hermite polynomials accurately when x is Real. Using Set for the ... 6 As of version 9 this has changed http://reference.wolfram.com/mathematica/Compatibility/tutorial/Utilities/FilterOptions.html So this package needs to use FilterRules. I Downloaded this package, and changed Summa.m according to the above, and now it loads ok. Changed every place it said FilterOptions to Sequence@@FilterRules. Run few tests from the ... 5 The problem has to do with precision in numerical approximations. During the evaluation of your function Mathematica has to deal with really huge numbers, so you have to make sure that during this computation everything is evaluated up to a very high precision. One way to achieve that is by specifying the initial argument with high precision. For example, if ... 5 Since If has attribute HoldRest, the i will not be inserted into the latter parts during the evaluation. Consider this example showing the same effect: Sum[s[i, Hold[i]], {i, 1, 2}] (* s[1, Hold[i]] + s[2, Hold[i]] *) I think the best practices way of getting what you are asking for is to use With to ensure i gets inserted: Sum[With[{i = i}, If[x[i] < ... 5 J. M.'s method is appropriate for sums (and products) that will be computed iteratively. It is however quite inappropriate for sums that can be computed symbolically. You could use Piecewise in the latter case. Here is an illustration using a very simple function: Sum[5 i, {i, Range[1*^7] ~Delete~ 777}] // Timing {2.543, 250000024996115} ... 5 In Mathematica version 9, you can do these kinds of things much more naturally. Here are the two quantities that you wanted: u = TensorContract[TensorProduct[v, w], {2, 5}]; q = TensorContract[TensorProduct[v, w], {{1, 4}, {2, 5}}]; The contraction is performed on the tensor product in which the first three indices belong to the factor v and the last ... 5 A variation: mySet = {1/10, 1/2, 4/7, 3/5, 2/3}; perms = Permutations[mySet]; powersums = Total[Transpose @ perms^Range[0, 4]]; Extract[perms, Position[powersums, Max[powersums]]] {{1/10, 4/7, 2/3, 3/5, 1/2}} Another variation (beware: slower on large sets): Last @ Sort @ With[{perms = Permutations[{1/10, 1/2, 4/7, 3/5, 2/3}]}, ... 5 In our case a simple and direct approach would be defining a list of rules. Here is an example: rules = { c_ Sum[n a[n] c_^(n-1), {n, 0, Infinity}] :> Sum[n c^n a[n], {n, 0, Infinity}], α_ Sum[a[n] c_^n, {n, 0, Infinity}] + Sum[n a[n] c_^n, {n, 0, Infinity}] :> Sum[(α + n) a[n] c^n, {n, 0, Infinity}]}; Let's define an appropriate ... 5 If we are to deal with big numbers we should exploit symbolic capabilities of the system. This Sum[ k Boole[Or @@ Divisible[k, {2, 3, 5, 7}]], {k, n} ] would be useful for n < 10^7 but for n > 10^9 we have to provide a neat modification. We can observe that Sum is needed only for a small subset of the whole range$\{1, \cdots , n\}\$. Since we have: ...

5

b[0] := 1; b[k_] := b[k] = N[b[k - 1] + 1/(b[k - 1]^2 + b[k - 1] + 1)] f[x_] := f[x] = f[x - 1] + (1/(b[x]^2 + b[x] + 1)) f[0] := 0 Manipulate[ ListPlot[Table[1/x^(1/3) f[x], {x, 1, a}], MaxPlotPoints -> 100, Epilog -> {Green, Line[{{1, 3^(1/3)}, {a, 3^(1/3)}}]}, PlotRange -> {0, 4}], {a, 10, 100000, 1}]

4

You seem to be writing a tortured definition. As already commented you need Pattern in the left-hand-side of the definition. There are better tools than For in nearly all cases. Return is misapplied and unnecessary. As far as I can tell Total is dropped into the middle of things with no particular consideration. You probably want something like this: ...

4

Just for fun, here's a recursive approach: accSum[{}] := {}; accSum[{x_}] := {x}; accSum[{r___, x1_}] := Join[{Total[{r, x1}]}, accSum[{r}]]; Usage: list = {11.5575, 11.397, 5.52734, 4.0878, 2.54815, 1.86652, 2.55028, 2.14952, 1.6242, 1.34117} accSum[list] // Reverse Gives: {11.5575, 22.9545, 28.4818, 32.5696, 35.1178, 36.9843, 39.5346, 41.6841, ...

Only top voted, non community-wiki answers of a minimum length are eligible