Tag Info

Hot answers tagged

27

First let me note that I didn't write PositionIndex, so I can't speak to its internals without doing a bit of digging (which at the moment I do not have time to do). I agree performance could be improved in the case where there are many collisions. Let's quantify how bad the situation is, especially since complexity was mentioned! We'll use the ...


25

f = {1, 5, 9, 14}; v = {-1, 1, 3, 4, 6, 9, 10, 13, 14, 15}; BinLists[v, {Join[{-Infinity}, f, {Infinity}]}] {{-1}, {1, 3, 4}, {6}, {9, 10, 13}, {14, 15}}


24

Summary We can look at the code of DeleteDuplicatesBy and it turns out it uses GroupBy. The test cases proposed by Mr.Wizard are all handled by some part of the code of DeleteDuplicatesBy. Other parts of this code also seem to have some issues. Most of the members of the *By family of functions seem to have side effects. How DeleteDuplicatesBy works It ...


22

test = {5, 6, 9, 3, 2, 6, 7, 8, 1, 1, 4, 7} MaxFilter[test, 1] (* {6, 9, 9, 9, 6, 7, 8, 8, 8, 4, 7, 7} *) You can also use Max /@ Transpose[{Rest[Append[#, 0]], #, Most[Prepend[#, 0]]}] &[yourList] which is competitive with the MM MaxFilter, but will allow you to change the 'slide' (e.g.pad with zeroes, or other arbitrary 'start').


19

There was an update for Array, not done to the end. The method below does not work for earlier versions even though that Array is New in 1 | Last modified in 4 Moreover WRI forgot to update docs for error messages: Array::plen - the first example gives no error in V9. V9 Array[# &, n, {start, stop}] Array[# &, 10, {-1, 1}] {-1, ...


17

Not sure if this will work for you, but... There is a cool blog by Roman Osipov in Russian (use Google Translate to translate): Study of arbitrary functions by methods of mathematical analysis in the system Mathematica I will give 2 functions from there (see the blog for more tricks). The domain of the function DefinitionDomain[expr_, variable_: x] := ...


16

Looking at the Trace of one which does work: x = Sin[Pi/5] (* Sqrt[5/8 - Sqrt[5]/8] *) Trace[ArcSin[x], TraceInternal -> True] It appears that Mathematica computes the ArcSin numerically and then recognises the result, 0.628319 as possibly equal to Pi/5. To check it computes Sin[Pi/5], and subtracts it from the original argument to see if it gets ...


16

Maximize[{y, x^2 + y^2 == (2 x^2 + 2 y^2 - x)^2}, {x, y}] {(3 Sqrt[3])/8, {x -> 3/8, y -> (3 Sqrt[3])/8}}


16

I see no mention of the new-in-10 PositionIndex in the other answers, which takes a list (or association) of values and returns a 'reverse lookup' that maps from values in the list to the positions where they occur: In[1]:= index = PositionIndex[{a, b, c, a, c, a}] Out[1]= <|a -> {1, 4, 6}, b -> {2}, c -> {3, 5}|> It doesn't take a level ...


16

Attempting to analyze the performance of this function in the manner that Taliesin Beynon did for PositionIndex I shall use the same tools. The old method that will be compared in all cases below: myDeDupeBy[x_, f_] := GatherBy[x, f][[All, 1]] Speed A BenchmarkPlot of DeleteDuplicatesBy versus myDeDupeBy: Needs["GeneralUtilities`"] BenchmarkPlot[ ...


15

May be the most compact approach: Hold[i1++, i1--, i2++, i2--][[RandomInteger[{1, 4}]]]


15

The best I have is manual RHS holding and Join, after which an arbitrary head could be Applied: Join @@ Cases[expr, x : _Times :> Hold[x], 3] Hold[2/2, 8/4, 1/0] This could be done automatically as follows: makeHeld[(L_ -> R_) | (L_ :> R_)] := L :> HoldComplete[R]; makeHeld[pat_] := x : pat :> HoldComplete[x]; heldCases[expr_, rule_, ...


15

There are nice trigonometric formulas δ = 0.01; trg[x_] := 1 - 2 ArcCos[(1 - δ) Sin[2 π x]]/π; sqr[x_] := 2 ArcTan[Sin[2 π x]/δ]/π; swt[x_] := (1 + trg[(2 x - 1)/4] sqr[x/2])/2; Plot[{TriangleWave[x], trg[x]}, {x, -2, 2}, PlotRange -> All] Plot[{SquareWave[x], sqr[x]}, {x, -2, 2}, PlotRange -> All, Exclusions -> None] Plot[{SawtoothWave[x], ...


15

I'm guessing you're coming from a programming language where every expression must evaluate to a value, and if it didn't evaluate to something (like 5[Cos+Sin]), it's a syntax error. To me, Mathematica started to make a lot more sense, once I stopped thinking about functions and values, and started to think of every expression as evaluating to an "expression ...


14

You can give the function one of the Hold Attributes. SetAttributes[fun, HoldFirst] Then as Leonid suggested fun[var_] := SymbolName[Unevaluated@var] Without the hold attribute, this will not work.


14

Short story $$ \vartheta(x) = \arg \left[(\operatorname{Bi}x+i \operatorname{Ai}x)e^{-\frac{2}{3} i (-x)^{3/2}}\right]+\frac{2}{3} \operatorname{Re}\left[(-x)^{3/2}\right] $$ Update: I see that you want use only real functions, so you can expand this as $$ \vartheta(x) = \begin{cases} \arctan\frac{\cos \left(\frac{2}{3} (-x)^{3/2}\right) ...


13

Your question is not well specified for several reasons: Pure functions accept a flexible number of arguments #1 + #2 &[a, b, c, d] a + b It is common for some arguments to not be used: #1 + #3 & @@ {a, b, c, d} a + c SlotSequence includes all arguments after the given position: +##3 & @@ {a, b, c, d} c + d Without ...


13

The separation-of-variables solution you quoted has two indices appearing in it: n and j (the subscripts of the coefficient $A_{nj}$). Here, n is azimuthal mode order, i.e. it counts the number of nodes along the direction in which the polar-angle $\theta$ varies (divided by 2). The index j is needed because the wave is supposed to satisfy the boundary ...


13

Let's start by taking a look at the compiled form of one of our queries: Dataset`CompileQuery[Query @ First @ spans] (* Dataset`WithOverrides@*Checked[Slice[205 ;; 313], Identity] *) We can see that the operation is not implemented directly in terms of part. Indeed, there are three components: Dataset`WithOverrides, GeneralUtilities`Checked and ...


13

I don't know why no one mentioned this, but all you have to do is to use a special form of OptionsPattern: pfunc[x0_, plotopts : OptionsPattern[{Plot, pfunc}]] := your-code where inside OptionsPattern go all sub-functions you need, in a list. Now everything is fine and dandy. This has been explained already in this answer of Mr. Wizard, so this answer ...


13

I believe this is correct, and very fast: fn[x_Integer, n_Integer] := Complement[Range @ x, Join @@ Range[#, x, #]] & @ Prime @ Range @ n Test: fn[10000, 1223] {1, 9929, 9931, 9941, 9949, 9967, 9973} It seems I am a bit late to return to this problem and Simon Woods already provided a memory optimized approach. His sieve is comparatively ...


13

This is competitive with Mr Wizards code and seems faster in some cases: fn2[x_Integer, n_Integer] := Module[{y = Range @ x}, (y[[# ;; x ;; #]] = 0) & /@ Prime[Range @ Min[n, PrimePi @ x]]; SparseArray[y]["NonzeroValues"]] AbsoluteTiming[fn[10000, 1223];] (* {0.004000, Null} *) AbsoluteTiming[fn2[10000, 1223];] (* {0.010001, Null} *) ...


12

It depends on what you want to do with result, but you could try like this: Function[{x}, {x, #}] /@ {1, 2} {{1, #1}, {2, #1}}


12

Maybe : Reduce[Exists[{x}, x^2 + y^2 == 1 ], Reals] -1<=y<=1


12

To avoid those scoping constructs being recognized as such and having their variables renamed, I like wrapping their heads with Identity. In your case, func[opt_] := Identity[With][{a = True}, "x" /. opt]


12

r[n_] := Symbol["r" <> ToString[n]] Then: r[1] gives r1


12

Yes, there is. Group your extra arguments in a list, and address them by their positions in the function under Fold. For your particular example: FoldList[#1 (1 + First@#2) - Last@#2 &, 1000, Transpose@{{.01, .02, .03}, {100, 200, 300}}] (* {1000, 910., 728.2, 450.046} *)


12

Multiple versions of Mathematica can co-exist on a computer without problems. The best approach is to have both version 10 and version 9 installed simultaneously until you are confident that all your critical code work with 10. When using multiple versions it is useful to go to Preferences -> System and check "Create and maintain version specific front end ...


11

The "unnecessary" complication is needed for those cases where you specify deeper levels than the first: MapIndexed[f, {{a}, {b}}, {2}] (* {{f[a, {1, 1}]}, {f[b, {2, 1}]}} *) The following code produces what you want: myMapIndexed[f_, l_] := Inner[f, l, Range[Length[l]], List]; myMapIndexed[f, {a, b, c, d}] (* {f[a, 1], f[b, 2], f[c, 3], f[d, 4]} *)


11

Using Function with a named parameter, as halmir showed, is the standard way to do this, however anything that prevents a literal Slot[1] from appearing in the body of the Function will work. Inactive as chuy showed is one possibility, but I find this cleaner: {#, Slot @@ {1}} & /@ {1, 2} {{1, #1}, {2, #1}} If the body will not be evaluated you ...



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