Tag Info

Hot answers tagged

23

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

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


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

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

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

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


11

This function will be rewritten in C for 10.0.2 and should come down to average-case complexity of $O(n)$ from its current $O(n \log(n))$. Note that the version most users will be bothered to write (and the way we advertized this before in the docpage for DeleteDuplicates) is $O(n^2)$, so most users are probably already winning. In the meantime, my advice ...


10

You could use Lagrange multipliers to maximize $f(x,y)=y$ subject to the constraint that $$g(x,y) = x^2 + y^2 - (2 x^2 + 2 y^2 - x)^2 = 0.$$ f[x_, y_] = y; g[x_, y_] = x^2 + y^2 - (2 x^2 + 2 y^2 - x)^2; eqs = {D[f[x, y], x] == lambda*D[g[x, y], x], D[f[x, y], y] == lambda*D[g[x, y], y], g[x, y] == 0}; Solve[eqs, {x, y, lambda}] // InputForm (* Out: { ...


9

I closed this question because rm -rf convinced me that what this answer was intended to do is ultimately impossible: that there is simply no way to give an approximate one-to-one mapping of functions between Octave/Matlab and Mathematica; apart from a few limited cases any recommendations are going to be localized and opinionated rather than truly ...


8

Some functions have new return types in MM10. For instance, I'm running into trouble with date functions (like DatePlus) since they return DateObjects in MM10 but returned date lists in MM9. Other functions (like DateDifference) now return Quantity objects instead of a number.


8

I'm the one inside the company who suggested RightComposition (and pushed for syntax for Composition and RightComposition). I'm sympathetic to your need, and have wanted the same thing once or twice myself. Given that not much /* and @* code has been written yet, I think it is certainly possible we could have /* parse to LeftComposition. I'm not sure what ...


8

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


7

In version 10, RegionBounds@ImplicitRegion[x^2 + y^2 == (2 x^2 + 2 y^2 - x)^2, {x, y}] (* {{-(1/8), 1}, {-((3 Sqrt[3])/8), (3 Sqrt[3])/8}} *)


6

I feel that the basics of this topic are well covered in my answer to: Can a function be made to accept a variable amount of inputs? Once you have read that and understand BlankSequence, Repeated, and Pattern you will understand that you could use either of these: f1[samples : {__Real}] := makeRealDist[samples] f2[samples : {_Real ..}] := ...


6

This implicit equation is simple enough to be converted to explicit equations eqn = x^2 + y^2 == (2 x^2 + 2 y^2 - x)^2; yExpr = (y /. Solve[eqn, y]); yMax = SortBy[Maximize[#, x] & /@ yExpr, N[First[#]] &][[-1, 1]]; yMin = SortBy[Minimize[#, x] & /@ yExpr, N[First[#]] &][[1, 1]]; xExpr = (x /. Solve[{eqn, yMin < y < yMax}, x, ...


6

Using this site as my rubber duck and attempting to answer my own questions: (1) Reason for existing behavior One may want to be able to do this: heldRow = HoldForm @* Row @* List; (* version 10 syntax *) x = 7; Block[{x}, heldRow[x + x + x, x^2*x^3] ] 3 xx^5 (* proposed behavior would yield: x+x+xx^2 x^3 *) My counterargument: this ...


5

One difference I just run into is Dispatch. To get the original rules, use First@Dispatch[•••] in v9, but Normal@Dispatch[•••] in v10.


4

Since all of the component functions are Listable f[{a_, m_, s_}, x_] := Total[a*Exp[-(x - m)^2/(2*s^2)]] n = 5; amp = Array[a, n]; mean = Array[m, n]; sigma = Array[s, n]; f[{amp, mean, sigma}, x] a[1]/E^((x - m[1])^2/ (2*s[1]^2)) + a[2]/E^((x - m[2])^2/ (2*s[2]^2)) + a[3]/E^((x - m[3])^2/ ...


4

f[data_] := Total[#1*Exp[-((x - #2)^2/(2 #3^2))] & @@@ data]; f[{{A, mx, sigma}}] f[{{A, mx, sigma}, {A2, mx2, sigma2}}] Use Function, Apply and Total.


4

Also just for fun (in case you don't like to solve equations): cp = ContourPlot[x^2 + y^2 == (2 x^2 + 2 y^2 - x)^2, {x, -1, 2}, {y, -1, 1}, AspectRatio -> Automatic]; x = Cases[cp, {_Real, _Real}, Infinity]; points = Point /@ {Take[SortBy[x, First], 2], First@SortBy[x, Last], Last@SortBy[x, First], Last@SortBy[x, Last]}; Show[cp, ...


4

Mathematica does not automatically calculate the quantile (or InverseCDF) for arbitrary distributions. You need to do it. xd = ExponentialDistribution[1]; (* use exact argument *) yd = ExponentialDistribution[5]; (* use exact argument *) td = TransformedDistribution[ x/(x + y), {x \[Distributed] xd, y \[Distributed] yd}]; quantile[q_] = z /. ...


3

When you use the syntax f[x_]:= ... in Mathematica, you are not defining a function f. You are defining a pattern-replacement rule. The documentation and the users tend to be vague or misleading or just incorrect about this point. Of course you can define "function" as something quite to your liking and ignore the protestations of mathematicians, ...


3

I post this just for fun. It does not address the general question of maximizing implicit function but Kuba has shown how to maximize y subject to constraint f(x,y). The problem can (with a small amount of manipulation) converted to explicit polar form: $r=0.5(cos\theta+1)$. Using this: rho[t_] := (Cos[t] + 1)/2; ycrit = Solve[D[Cos[u] Sin[u] + Sin[u], u] ...


3

In order to apply a function to every element we can use Map with the level specification: Map[If[MemberQ[keep, #], #, 100] &, matrix, {2}] Another option using the Listable attribute: Function[{x}, If[MemberQ[keep, x], x, 100], Listable]@matrix; This turned out to be a lot slower though. I ran these on Michael E2's test case and the Listable ...


3

I guess I'll answer my question. As Leonid Shifrin alluded to in the comment, AllTrue is actually a more general function than VectorQ and this has various consequences: (1) VectorQ is overloaded to work efficiently with packed arrays which is evident from my benchmark above. (2) If we append a non-integer to the example in the question, we no longer have a ...


2

You have to write the parameters like this (see documentation): Graphics[Ellipsoid[{0, 0}, {100, 200}, {0, 1}], Axes -> True] Parameter 1: Center Parameter 2: Radii Parameter 3: Direction EDIT If you work with vectors, please consider: Show[Graphics[Ellipsoid[{0, 0}, {100, 200}, {1, 0.5}], Axes -> True], Graphics@Arrow[{{-100, 200}, {100, ...


2

Using the Laplacian operator of v9 you can extract the list of variables and insert it into the second slot as follows: ql = Laplacian[#, List @@ #] & so eg ql[f[x, y, z, p, q]] or even ql[f @@ (x[#] & /@ Range[10])] works. This assumes Cartesian coordinates. No doubt there are better methods.


2

Reduce[x^2+y^2==(2 x^2+2 y^2-x)^2,{y},{x},Reals] -((3 Sqrt[3])/8) <= y <= (3 Sqrt[3])/8


2

You can define f to operate on f however you would like: ClearAll[f] f@f := f@f@# &; f[x_] := x /. a -> I a {f[a], f@f[a], (f@f)[a]} {I a, -a, -a} However if you expect this to extend to e.g. (f@f@f)[a] you may want something like: ClearAll[f] f[f] = Superscript[f, 2]; f[Superscript[f, n_]] := Superscript[f, n + 1] Superscript[f, n_][x_] := ...


2

iter = Reap[FindRoot[Sin@x == Cos[x], {x, 0}, EvaluationMonitor :> Sow[x]]] {{x -> 0.785398}, {{0., 1., 0.782042, 0.785398, 0.785398}}} Length@Last@Last@iter - 1 4



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