Hot answers tagged

29

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


21

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}


21

A story of incremental improvement Let's look at the OP's original expression again, for reference: $$\sum_{m=1}^{c}\frac{1}{m}\sum_{d \mid m}\mu(d)n^{m/d}$$ Most people here are familiar with Sum[], and would not have much trouble translating the outer summation into Mathematica syntax. The inner part, $$\sum_{d \mid m}\mu(d)n^{m/d}$$ is not terribly ...


19

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

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


16

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


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


14

The default SumCompileLength is 250. You can increase this number for example to 500 using SetSystemOptions["CompileOptions" -> {"SumCompileLength" -> 500}] or to infinity using SetSystemOptions["CompileOptions" -> {"SumCompileLength" -> ∞}] What is "SumCompileLength" for? For sums with a finite number of at least "SumCompileLength" ...


12

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 $w_{deb}...


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

The Simplify idea is on the right track, but you have to tell Mathematica about the relationship between f and Plus in a slightly different way: ClearAll[f]; Plus[f[x_], f[y_]] /; x + y == 1 ^:= 1 The Upset symbol ^:= means this is a definition for f rather than Plus. This approach is inefficient--there is going to be a lot of blind checking to find ...


12

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


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


12

Your sum can be computed more efficiently as follows: data = Developer`ToPackedArray[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, ...


11

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


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

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


10

As a general rule, one should usually set WorkingPrecision to be higher than either of AccuracyGoal and PrecisionGoal (if not set to ∞) to mitigate any roundoff error incurred during the evaluation. For the specific methods implemented within NSum[]: it is documented, but not apparently well-known, that you can change the way the integral term in the Euler-...


10

To elaborate a bit on Michael's comment, let's first consider this statement from the docs: Sum and Product use over 100 pages of Wolfram Language code. Executing ?Sum`* will in fact show you some of the internal routines used behind the scenes by the function Sum[]. You, the regular user, are not usually intended to access these by yourself; however, ...


10

For large enough bounds, NSum is used. Compare timings: NSum[(-1)^(n + 1)/n, {n, 1, 100000}] // AbsoluteTiming {0.004744, 0.693142 - 1.3494*10^-16 I} N[Sum[(-1)^(n + 1)/n, {n, 1, 100000}]] // AbsoluteTiming {1.84727, 0.693142}


9

I like FindSequenceFunction it can be pretty awesome. Here I give it the sequence (your case is when k=2012): $$ \left\{ \sum_{n=1}^{k-1} f \left( \frac{i}{n} \right) \right\}_{k=1}^{25} $$ partial=Rationalize@NSum[f[i/#],{i,1,#-1}]&/@Range[1,25] g=FindSequenceFunction[partial] (* 1/2 (-1+#1)& *) g[10000] == Rationalize@NSum[f[i/10000],{i,1,9999}]...


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

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


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

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

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


9

It takes less work to use the definition of a generating function directly than by typing FindGeneratingFunction: In[1]:= Sum[Mod[n, 2] x^n, {n, 0, Infinity}] Out[1]= -(x/((-1 + x)*(1 + x)))


9

I have had the same troubles. When Sum returns an expression containing Pochhammer, it's very often wrong. However, I never experienced this problem with FindSequenceFunction: Table[gg4[q, 1], {q, 2, 6}] (*{2/15, 2/315, 2/5005, 4/153153, 5/2909907}*) gg4k1[q_] = FullSimplify[FindSequenceFunction[Table[gg4[q, 1], {q, 2, 11}], q - 1]] (*(2^(5 - 4 q) Sqrt[\[...


9

The problem is that it reevaluates the sum every single time you call it, recomputing every 20^4 term again and again. You just need to compile the function CC2 so that it performs the summation only once. Using the code you have, it takes my machine about 6 seconds to compute a single data point: CC2[0.003] // AbsoluteTiming (* {6.069311, 1.49893} *) ...



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