Tag Info

Hot answers tagged

24

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

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


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


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

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


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

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


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

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

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

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


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

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} *) ...


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

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


8

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


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

This appears to be a bug in version 8 that was fixed in version 9. Sum[a[n] x^n, {n, 0, Infinity}] gives (-x - Log[1 - x])/x^2 in both. In version 8.0.4, GeneratingFunction[a[n], n, x] gives PolyLog[2, x]/x, which is incorrect. In version 9.0.1, GeneratingFunction[a[n], n, x] gives (-1 - Log[1 - x]/x)/x which is equivalent to the result from Sum.


8

There is a way without hypergeometric functions. Binomial asymptotics is bin = Normal@Series[Binomial[n - i - 1, 2*n/3 - 1], {n, ∞, 0}, {i, ∞, 0}] // FullSimplify $\displaystyle\frac{2^{-2 n/3} \sqrt{\frac{1}{n}} 3^{n-i}}{\sqrt{\pi }}$ Approximately sum is integral sum = Integrate[bin, {i, 0, n/3}] ...


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

Sum uses more than one Method. By default it selects automatically. If you specify one of them you should see more consistent behavior: SetOptions[Sum, Method -> "Procedural"]; Table[ Sum[x, {x, 1, 10^n}] // AbsoluteTiming, {n, 4, 9} ] // MatrixForm $\left( \begin{array}{cc} 0.001000 & 50005000 \\ 0.003000 & 5000050000 \\ 0.011001 ...



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