Tag Info

13

edit: this doesn't really answer the question but merely provides some other alternatives, you should probably up-vote other more useful answers. There are also faster ways to do this using Pick or by compiling Select. Timing comparison done on a Macbook Air OS X 10.8.3 w/ 1.7 GHz Intel Core i5 with Mathematica 9.0.0.0. t = RandomInteger[100, 10^7]; ...

12

Here is a code that is about 2 orders of magnitude faster. We will use a finite element method to solve the issue at hand. Before we start, note however, that the transition between the Dirichlet values should be smooth. We use the finite element method because that works for general domains and some meshing utilities exist here and in the links there in. ...

9

Mathematica does not do well with code that relies on mutable state (i.e. an explicit variable whose value is changing during the run of the program). Let's look at your For code: For[i=0, i < Length[t], i++, If[ t[[i]] > 50, r=Append[r, t[[i]]]] ] Notice that for every iteration, it needs to evaluate the following by interpreting high level ...

9

In the first example, the list r1 is getting shorter each iteration, resulting in much fewer iterations overall: max = 10^5; r1 = Range[max]; c1 = 0; Timing[ For[i = 2, i <= Length[r1], i++, c1++; r1 = del[r1, i]]; c1] (* ==> {0.012426, 356} *) r2 = Range[max]; c2 = 0; Timing[Do[c2++; r2 = del[r2, i], {i, 2, Length@r2}]; c2] (* ==> ...

9

You can get about 100 times faster by using Java, without any particular tuning, but you will have to provide the date format explicitly. Here is the solution based on Java reloader. Implementation Load the Java reloader Compile the following class: JCompileLoad@ " import java.text.ParseException; import java.text.SimpleDateFormat; import ...

8

This is similar to my own question: How are MemberQ and FreeQ so fast? Simply, outside of specific cases of auto-compilation or explicit uses of Compile Mathematica code is not compiled and operations are performed in a very literal manner at a high level without a lot of apparent optimizations. For this reason internal functions, which are typically ...

7

The documented (!) TetGenConvexHull can compute the convex hull. Then using a GraphicsComplex will be efficient: << TetGenLink {coords, incidences} = TetGenConvexHull[pts]; Graphics3D[{EdgeForm[], GraphicsComplex[coords, Polygon[incidences]]}]

6

The (undocumented!) function ComputationalGeometryMethodsConvexHull3D[] is up to the task for this particular case: ComputationalGeometryMethodsConvexHull3D[pts, Axes -> None, GraphicsMeshFlatFaces -> False]

6

First, the old Random function uses an inferior PRN generator and should not be used other than for legacy compatibility, if I recall correctly. On my machine (version 7, Windows 7) the second method is faster than the first, though not by a great amount: 4.*Mean@Table[Boole[RandomReal[]^2 + RandomReal[]^2 < 1], {10^6}] // AbsoluteTiming With[{n = ...

6

If all you want is to remove a linear trend from the data you don't need all the fancy statistics done by LinearModelFit and a faster alternative is to just use LeastSquares and then use the resulting parameters to remove the trend from the data. (*Generate 150k datapoints with a linear trend*) data = RandomVariate[NormalDistribution[0, 50], {150000, 2}] + ...

5

Unfortunately, my Tarot-Cards are in repair so here is a guess: data = yourSecertData; Precisionp[data] pd = DeveloperToPackedArray[N[data, $MachinePrecision]]; (* must be True *) Developer`PackedArrayQ[pd] AbsoluteTiming[pd]; cExp = Compile[{{in, _Real, 2}}, Exp[in], RuntimeAttributes -> Listable, CompilationTarget -> "C"]; ... 5 One option is to use MapThread, which gets you there: List /@ MapThread[Plus, u v f] But the infix notation for the Dot product is more elegant (as J.M. proposed in the comment above): List /@ (f.(u v)) Note that if you're doing a lot of these computations, that f.(u v) has a very slight computational edge, which you can test with the Timing command: ... 5 As always there are several ways to improve the speed Options Use MaxRecursions and MaxPoints Method Try using different methods to obtain quickest solution. Precalculate Use Block or Module to have some intermediate results only calculated once when required. Analyze Very general advice: use debug features as AbsoluteTiming at several places to see, ... 4 AbsoluteTime is faster on DateLists as compared to arbitrary DateStrings. Since the date format is known in this case, converting the strings to DateLists first gives a speed improvement of a factor of 10: AbsoluteTime /@ (ToExpression /@ (StringSplit[#, {" ", "-", ":"}] & /@ dateStrings)); Not as fast as the java programming variant, but good enough ... 4 An alternative way of doing things is using wavelets. Wavelets are quite good at denoising. (*Define data and noise*) temptimelist = Range[200]/10; data = Sinc[temptimelist]; noise = RandomReal[{-0.02, 0.02}, 200]; (* Define Wavelet for denoising *) dwd = DiscreteWaveletTransform[data + noise, SymletWavelet[7], 6]; (* Use universal threshold *) dwd = ... 4 For the example problem I get about a factor of 4 speedup over PowerMod by memoizing Mont. This of course means that Mont should not contain any global variables so I rewrote the code slightly: MontExp[b_, e_, n_] := Module[ {RLength, R, RM1, RInverse, NPrime, M, Result}, RLength = BitLength[n]; R = 2^RLength; RM1 = R - 1; RInverse = PowerMod[R, -1, ... 4 If the problem is that the transformation function is slow to compute, a simple way to create and use a look-up table is to memoize the function: (* create an example image *) image = RandomImage[1, {30, 20}, ColorSpace -> "RGB"] ~ ImageResize ~ Scaled[10] (* define the transformation function with memoization *) mem : func[{x_, y_}] := mem = {x + 0.01 ... 3 ImageTransformation works with functions, not tables. It should be straightforward to define a function that carries out the same transformation as the table, but you will need to be aware that the #[[1]] and #[[2]] arguments go from 0 to 1 (across the image) so you will need to design the function to handle this input range. For example, you might want a ... 3 The syntax you are using is incorrect. Try model[{1,2,3}] and notice that it can't be applied to a list. Just change model[data[[All,1]]] to model /@ data[[All,1]]. This will finish in time, but it won't be fast at all (I do not know why). This will be much faster (in place of model /@ data[[All,1]]): model["BestFit"] /. x -> data[[All, 1]] 3 You can wrap your Skip and Readlist in a Module and make it a function: importfile[name_] := Module[{strm, mydata}, strm = OpenRead[name]; Skip[strm, Record, 38, NullRecords -> True]; mydata = ReadList[strm, Number, 7500*3, RecordLists -> True]; Close[strm]; mydata ] importfile@"f1-Oxs_TimeDriver-Magnetization-000000-0000028.omf" This ... 3 Ok, so, thanks to Spawn1701D's comment here's the simplest way to proceed : A={{{0, 1}, {1, 1}},{{-1, 0}, {0, 0}}}; res={q1,q2}.#.{q1,q2}&/@A; final={q1p,q2p}+res; To state it simple, Spawn1701D prescribes the usage of pure functions to make it extremely terse : # function creates a slot between the two versions of the$\mathbf q\$ vector /@ is a ...

3

Here is an alternative based on LinearModelFit and polynomial regression. The idea is to fit your data to a polynomial of some degree, to find approximate extrema points of the smoothed data, and then locate nearest extremal points. Here is the code: ClearAll[findExtrema]; findExtrema[points_List, fitOrder_Integer, around_Integer: 5] := Module[{fit, fn, ...

3

The problem you face is very common in signal- and imageprocessing. I'm trying to find local minima / maxima in noisy data Since all the noise introduces small local extrema, the very question is which extrema are still noise and which are signal. What you want to do is to smooth out the noise without loosing signal information. This task is one of ...

2

A simplistic,if slightly inefficient, solution might be: data = Import[#,"Table"][[39;;-3]]&/@Filenames[]; Other options might involve, 'Cases', 'Choices', Drop, Take. For example: data = Drop[Drop[Import[#,"Table"],38],-2]&/@Filenames[]; If you are using a Linux derivative or something like cygwin on Windows, then this is an efficient ...

2

Your original function can be written much more concisely using ArrayPad: f2[list_, p : {_, _}] := Partition[list ~ArrayPad~ p, Tr@p + 1, 1] f2[Range@5, {1, 3}] Also, since this question is tagged performance-tuning I would like to point out that using the zero-padding might be more efficient. A raw demonstration, using MinHsuan Peng's function to ...

2

OK, I am stupid. ListCorrelate is what I want, indeed. I just have to use the correct parameters. acf = norm2 ListCorrelate[int, int, {1, 1}, 0]*norm1 -1; where norm1 and norm2 give me the normation I need and are defined as norm1 = Table[1/(Deltat + 1 - m), {m, 0, Deltat}]; norm2 = 1/Mean[int]^2; Thanks to Bill S, RunnyKine and Daniel Lichtblau ...

2

I think you have to make sure that your transformation function always handles input cleanly. Here's a test you can do to see what goes into your function. (And I think you can use real coordinates if you use the DataRange option.) i = ImageResize[ExampleData[{"TestImage", "Mandrill"}], {20, 20}]; The function: f[pt_] := (Print[pt]; {pt[[1]], pt[[2]]}); ...

1

Since you only want to remove the linear trend. Just use Fit, and this is also easy to understand.(And a little faster than Mr Alpha's on my computer.) data = RandomVariate[NormalDistribution[0, 50], {150000, 2}] + Range[150000]; AbsoluteTiming[ f[x_] = Fit[data, {1, x}, x]; ans=Transpose@{data[[;; , 1]], data[[;; , 2]] - f@data[[;; , 1]]};] ...

1

Maybe you can undersample your data according to a mean as suggested in the comments, and then try and use one of the methods from the links you provide. The following is semi-manual but it would work reasonably well and quick for a dataset like the one you provide. Starting with the data: temptimelist = Range[200]/10; tempvaluelist = Sinc[temptimelist] + ...

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