# Tag Info

19

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

18

Easy Solution v = Accumulate[list] Gives Output {11.5575, 22.9545, 28.4819, 32.5697, 35.1178, 36.9843, 39.5346, \ 41.6841, 43.3083, 44.6495}

18

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

17

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

16

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

15

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

12

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

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

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 ... 11 Update This runs in about half a second: Total[1.0 ~Divide~ Flatten@Outer[Plus, Range[0, 8], Range[0, 80, 10], Range[0, 800, 100], Range[0, 8000, 1000], Range[0, 80000, 10000], Range[0, 800000, 100000], Range[0, 8000000, 1000000], Range[10000000, 80000000, 10000000]]] (* 1.07145 *) Original This is far from ... 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 ... 10 Your sum can be computed more efficiently as follows: data = DeveloperToPackedArray[data]; n = Length@data; f[i_, j_] = Cos[(2 π*(3*10^8)/(1584 - 5 + j*0.1 - 0.05) - 2 π*(3*10^8)/(1584 - 5 + i*0.1 - 0.05)) t]; S3[t_] = (Total[data^2, -1]^2 - Total[(data.Transpose[data])^2 Array[f, {n, n}], -1])/2; ListPlot[Table[S3[i], {i, -0.01, 0.01, ... 10 From NSum: You should realize that with sufficiently pathological summands, the algorithms used by NSum can give wrong answers. In most cases, you can test the answer by looking at its sensitivity to changes in the setting of options for NSum. For instance: NSum[((-1)^n)/(n - (-1)^n), {n, 1, Infinity}, NSumTerms -> 100000] (* 0.693149 - ... 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 I did some computation of formal derivatives a while back which might be of interest in this context (though keep in mind that this is anything but bullet proof! it does work for the cases I bothered to check though). Clear[a]; Format[a[k_]] = Subscript[a, k] Let us say we have an objective function which is formally a function of the vector a[i] Q ... 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 ...

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

Here's another way: sum[n_, d_] = Sum[d k, {k, Floor[n/d]}]; f[n_] := Total[-(-1)^Length@# sum[n, Times @@ #] & /@ Subsets[{2, 3, 5, 7}, {1, 4}]] Example f[10^11] 3857142857207142857139

9

The default SumCompileLength is 250. You can increase this number for example to 500 using: SetSystemOptions["CompileOptions" -> {"SumCompileLength" -> 500}]

8

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}, ...

8

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] = ...

8

Let me elaborate my comment into an answer. To make the nested Sum compiled, let's first have a close look at the compiling result of code containing one Sum: sum = Compile[{{n, _Integer}}, 1/Sum[3.141 + j, {j, n}]]; Needs["CompiledFunctionTools"] p1 = CompilePrint@sum It's not hard to notice that Sum is actually translated into a loop by Compile. It's ...

8

Since your central question was about speed (or time complexity), you might wish to know an important result from elementary theory of algorithms and computational complexity, which is that the time and space complexity of matrix multiplication depends upon the order of such multiplication (see Introduction to Algorithms by Cormen, Leiserson and Rivest). As ...

7

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

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

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

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

7

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

7

Just for fun and a little ugly: f[x_, c_] := {x + (3 + 2 Mod[c, 2]) (-1)^(Mod[c, 2] + 1), c + 1} ans = NestWhileList[f @@ # &, {-30, 1}, First@# < 0 &]; The number of steps is Length@ans-1. Visualizing: This was animated gif made from: {pos, steps} = Transpose[ans]; anim = MapThread[ ListPlot[pos[[1 ;; #1]], Epilog -> ...

7

If you wish to compute the correct values using the method you have chosen you could specify Method -> "Procedural" for Sum: CumDigitSum[x_, b_] := Sum[DigitSum[n, b], {n, 1, x}, Method -> "Procedural"] CumDigitSum[1000001, 10] 27000003 However, the problem comes form the fact that Sum attempts to speed the calculation by finding a symbolic ...

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