New answers tagged

1

If interest is in a Partition of an interval most probable an equidistant subinterval sequence will be selected where the interval are direct additive. Direct additive means the sum is the interval and the interval do not overlap, have just the upper and lower in common. There is usually an exhaustion parameter n. This can be used to making the intervals ...


3

I can't understand what your code does but I think this might fix your BinCounts problem: Don't use the letter I - it's reserved. I replaced it with ii Use a Module because m isn't accessible in that inner With scope. Move the & just outside the BinCounts and before /@ Subscript[ii, 1][r_] := Module[{m = 10^16}, With[{k = Ceiling@Log2@N@m + 2}, ...


10

A packed arrays (or a MTensor on the C++ side of Mathematica) consists of a linear array containing all entries plus the information needed for storing of the Dimensions of the packed array. That is 1 mreal=double/mint=long long int = 64 bit = 8 byte per entry + a few bytes for storing the array of the Dimension of the packed array (again probably a mint/...


2

BinCounts is a bit of overkill but still efficient. Beware the bins in the partition are disjoint. I assumed the number 1 was meant to be in the last bin of the partition. Block[{m = 1000}, With[{seq = 2^Range[-Ceiling@Log2@N@m - 2., -1.]}, Total[#] + 1 - Last[#] &@Unitize@BinCounts[ Flatten@Outer[Plus, seq, seq, seq], {0., 1., 1./m}] ...


1

for v5.2, s Conjugate[s] is fast too, ref the pic:


6

This is a slightly faster rewrite of OP's CompiledFunction. It exploits faster read access through Compile`GetElement. It is about twice as fast as OP's original function (which took about 1.51672 seconds on my machine). But this speed-up is due mostly to changing the argument pattern from {{vec, Complex, 1}} to {{vec, Real, 1}} (because the former enforces ...


5

This is not a bug. It's an improvement. The integral is divergent. The V5 Oscillatory method is defunct. NIntegrate chooses the "ExtrapolatingOscillatory" method (which is the method it chooses for this integral if Method -> Automatic). This method checks convergence, and the amplitude of the oscillations goes to infinity. Therefore you ...


17

Based on the experience obtained here: friction = Compile[{{v, _Real}, {vt, _Real}}, If[v > vt, -v*3.0, -vt*3.0*Sign[v]]]; simulateSpring = Compile[{{x0, _Real}, {t, _Real}, {dt, _Real}, {vt, _Real}}, Module[{τ, times, positions, v = 0.0, a = 0.0, x = x0}, τ = t; times = Range[0.0, t, dt]; positions = Table[0.0, {Length@times}]; ...


0

Yes, but your problem is underspecified. Here is an example. NestWhile[Join[#, Range[RandomInteger[10]]] &, {}, Length[#] < 100 &]


1

Note: this solution here works for your second case: "Or alternatively the program starts a new computation (in my case very long lists) and the program ends after obtaining the first 1000 (or another number) of results". For your first case (interrupting a computation) you can follow this instructions (depending if you use Windows/Mac and if you'...


7

The integral diverges, please see Michael's answer for more detal. This is just an answer reproducing result of v5. Unlike higher versions, v5 is using very few points for the generation of graphic: plot = Plot[T, {τ, 0, 6*10^3}]; // AbsoluteTiming (* {32.1406250 Second, Null} *) plot[[1, 1, 1, 1]] % // Length To obtain the result in a reasonable time in ...


6

This depends on the specific compiler driver that is being used. The easiest way to get not only the compiler options, but the entire compilation command is to add "ShellCommandFunction" -> Print to CreateLibrary (or whichever function you used to compile).


0

We want to reproduce the result of following code with a FFT Abs[FourierTransform[f[p], p, x, FourierParameters -> {a, b}]]^2 This code does it correctly for all positive values of the x-space: Clear[fft2] fft2[fp_(*f(p) in Matrix form*), dp_(*grid resolution*), a_, b_] := Module[{pgrid, samples, n, factor, transform, xgrid}, pgrid = Range[fp[[All, ...


2

Drop the series expansion altogether and define the qmultinomial directly: ClearAll[qmultinomial] qmultinomial[q_, m1_, m2_, m3_] := QFactorial[m1 + m2 + m3, q]/(QFactorial[m1, q] QFactorial[m2, q] QFactorial[m3, q]) Then retain the rest of the code as written and you will obtain ostensibly the same result, but in 1.5 s (your original code took ca. vs. 20 ...


4

Instead of 30 hours, do the job in 2.6 seconds. Edit Used NIntegrate, as @flinty recommended. ClearAll["Global`*"] (Ef[a_] = Pi^2*(a + 2)^2; Eb[a_] = Pi^2*(a + 1)^2; f[n_, x_] = Sqrt[2/((n + 2)^2 - 1)]*((n + 2)*Cos[Pi*(n + 2)*x] - Cot[Pi*x]*Sin[(n + 2)*Pi*x]); b[n_, x_] = Sqrt[2]*Sin[(n + 1)*Pi*x]; (* Table[xf[m, n] = If[Mod[m - n,...


3

In order to determine where to concentrate your efforts, you need to know where your bottlenecks are. To do this work through a single Y1 calculation step by step. I would target your Integrate & Sum. I'd be writing all results at once instead of using OpenAppend too. As a guide, on my Linux 18.04 XUbuntu 12.0 combination (Xeon E5-2690 v4 @ 2.60GHz), ...


3

I read Paul Nylander's code too and I based my code on Simon Woods and JM's work. The plot part was tough to understand for me as a beginner. Besides, I wanted to change the number of magnets, color,... I thought the modifications would interest some people. Here is my code: SetAttributes[ShowProgress, HoldAll]; ShowProgress[a_, {i_, min_, max_}] := With[{...


7

You may want to look at Bob Sandheinrich's post on the Wolfram Community Website US Counties COVID-19 confirmed cases by population density timelines for ideas on how to deal with population density. It is not super fast, but the timings are not on the order of multiple hours. Alternatively, you could try to build your own dataset from data tables from ...


2

The simplest answer may just be that the calls to retrieve the case data are taking a long time. Here's what I see from my machine: Timing[f[1500]] Timing[g[1500]] {0.136274, 7.71462} {5.78885, 2.} Response times vary, but they seem to be in the neighborhood of 4–6 seconds per data point retrieved. Since there are 3108 counties in your data set, a bit of ...


4

Use With. It will inject the pre-calculated value of the larger Subsets expression in your definition, but without “messing up” the readability of your code: ClearAll[inversions] With[ {SUBSETS = Subsets[{1, 2, 3, 8, 4, 7, 6, 5}, {2}]}, inversions[list_] := Complement[SUBSETS,Subsets[list, {2}]] ] You can check that downvalues (i.e. the definition) of ...


2

But this variable SUBSETS is used for only the function inversions, and thus should've been bound to this function, instead of dangling in the global scope like this You could put the stuff inside some new context? From clean Kernel Begin["myStuff`"] SUBSETS = Subsets[{1, 2, 3, 8, 4, 7, 6, 5}, {2}]; inversions[list_] := Complement[SUBSETS, Subsets[...


0

The question (130984) pointed out to me by Mr.Wizard in the comments was really helpful. The answer there mentions that matching against an explicit Head like f[_] can enable optimizations in the pattern matcher that would not apply when for example using _f. This essentially solved the problem for me, as performing replacements with the pattern HoldPattern@...


4

LeastSquare seems to use MKL lib internally and is already parallelized. Run the following code and observe cpu load: SeedRandom[1] ; size = 3000 ; m = RandomReal[{-1,1},{size,size}] ; b = RandomReal[{-1,1},size] ; LeastSquares[m,b] ; // AbsoluteTiming Check your parallel options and compare with observed cpu load: SystemOptions["ParallelOptions"] ...


8

If you have v12.1, there's no need to ever call Sort if you can incrementally add your values to a "PriorityQueue" data structure. It always stays sorted as you add/remove elements. SeedRandom[1234]; ds = CreateDataStructure["PriorityQueue"]; (* push a million random values in - use Scan for pushing many values. The slowest part here is ...


5

Use the rule: HoldPattern @ Plus[__NumSymb] :> NumSymb[Unique[]] instead. For example: Table[ testSum[[Range@n]] /. HoldPattern @ Plus[__NumSymb] :> NumSymb[Unique[]], {n, 16} ] //AbsoluteTiming {0.000197, {NumSymb[$11], NumSymb[$275], NumSymb[$276], NumSymb[$277], NumSymb[$278], NumSymb[$279], NumSymb[$280], NumSymb[$281], NumSymb[$282], ...


5

For the binary search idea, you could use Leonid Shifrin's fast, compiled binary search function here. It would look like this: sortedInsert[list_, el_] := Insert[ list, el, bsearchMax[list, el] ] sortedInsert[2 Range[10], 13] {2, 4, 6, 8, 10, 12, 13, 14, 16, 18, 20} list = Sort@RandomInteger[100000, 10000]; values = RandomInteger[100000, 1000]; ...


Top 50 recent answers are included