# Tag Info

11

idtz = InternalDeleteTrailingZeros; trim0a = With[{t = Reverse/@ idtz/@DeleteCases[#, ConstantArray[0, Length@#[[1]]]]}, Reverse /@ idtz /@ t] &; trim0b = With[{t = DeleteCases[#, ConstantArray[0, Length@#[[1]]]]}, Fold[idtz@#2@# &, #, {Reverse, Reverse}] & /@ t] &; Equal @@ (#@test & /@ {trim0, trim0a, ...

10

It is because they use different algorithms. MeanFilter is one of a family of filters which use DeveloperPartitionMap internally. This means that Mathematica is separately computing the mean of each run of 5 elements in your data set. MovingAverage is based on ListCorrelate which uses a fast FFT method. The documentation states: ...

8

It's pretty clear that the complexity of this function is $\text{O}(n^3)$, since we're iterating over each element of a matrix (a factor of $n^2$) and taking a sum at each one (an additional factor of $n$). As a former USACO competitor, I don't like $\text{O}(n^3)$ algorithms. However, we can't really do anything to reduce the complexity; LU decomposition ...

7

It seems, that a pure function calling a Listable one breaks internal optimization in Mathematica's ImageApply. Compare: t = Abs[Sqrt[#]] &; (* pure function *) q[x_] := Abs[Sqrt[x]]; (* "standard" function, implicitly listable *) SetAttributes[h, Listable]; h[x_] := Abs[Sqrt[x]]; (* explicitly listable function *) First /@ { ...

5

It turns out that Reduce finds candidate solutions relatively quickly and spends the vast majority of time proving correctness and completeness of the result. NSolve didn't have its own code for handling such problems, and was ending up using the same code as Reduce, finding symbolic solutions, and then numericizing them. I have implemented an NSolve version ...

5

This function prints out the solutions to all polynomials of degree d with coefficients of unit magnitude. printAllSolutions[d_] := Do[ With[{p = FromDigits[c~Prepend~1, x]}, Print[Expand[p] -> (x /. NSolve[p == 0, x])] ], {c, Tuples[{-1, +1}, d]} ] I made one optimization, which is that the leading coefficient is always 1. If it is ...

3

I'm not too sure why one is so much slower than the other, but your second (slower) method can be improved by compilation (inspired by this answer). q3Compile = Compile[{{x, _Real}}, Piecewise[{{0., x <= 1/3.}, {.5, x <= 2/3.}, {1., x <= 1.}}, 0], RuntimeAttributes -> {Listable} ]; img = ExampleData[{"TestImage", "Apples"}] ...

3

MeanFilter uses different algorithms (as noted by Simon), and is optimized for images (ability to use on "plain" data is a nice feature here, more so with some of the other image processing functions). This can be seen by doing : ImageData[MeanFilter[Image@{dat}, {0, 2}]][[1, 3 ;; -3]] Resulting in roughly comparable timings and the same result as your ...

3

Updated with cleaner and easier to adapt method: The checking of the patterns gets expensive, more so when conditions are attached. Looking at your generators, it's clear that the fulfillment of the patterns and/or conditions is quite sparse. Better to generate directly the hits, and create the array from those. Using your 'c' generator with just the ...

2

The function q3[#] & is not Listable and because the Interleaving option value is True by default, there is not much optimization that can be figured out automatically. Setting the Listable attribute whenever possible will help. Working with "Bit", "Byte", or "Bit16" data types will be faster, too. In this very case, ImageApply[q3, img] and ...

1

It appears that among other optimizations ImageApply uses a kind of memoization but that it is inactive in the second example. With a small change to the definition we can see how many times the function is actually applied: qX[x_] := (Sow @ x; Piecewise[{{0., x <= 1/3.}, {.5, x <= 2/3.}, {1., x <= 1.}}, 0]) SetAttributes[qX, Listable] The ...

1

The function doolittleDecomposite2 refactored by @2012rcampion can use Span(;;) to avoid the inner Do loop doolittle[mat_?MatrixQ] := Module[ {temp = ConstantArray[1, Dimensions@mat], row = Length@mat}, Do[ temp[[k, k ;; row]] = mat[[k, k ;; row]] - temp[[k, ;; k - 1]].temp[[;; k - 1, k ;; row]]; temp[[k + 1 ;; row, k]] = (mat[[k + 1 ;; ...

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