Tag Info

Hot answers tagged

32

Let you have a function and an initial point f[x_] := Cos[x] x0 = 0.2; Then you can calculate a sequence seq = NestList[f, x0, 10] (* {0.2, 0.980067, 0.556967, 0.848862, 0.660838, 0.789478, \ 0.704216, 0.76212, 0.723374, 0.749577, 0.731977} *) and vizualize it with a so-called Cobweb plot p = Join @@ ({{#, #}, {##}} & @@@ Partition[seq, 2, 1]); ...


30

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


26

The behavior described here is the same from 10.0.0 up to at least 10.1.0, with the minor exception of IndexBy. 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. ...


25

The following is based on the fact that the determinant of a matrix is equal to zero when two rows are the same. Thus, if you plug any of the points in, you get a true statement. SeedRandom[3]; pts = RandomReal[{-1, 1}, {5, 2}]; row[{x_, y_}] := {1, x, y, x*y, x^2, y^2}; eq = Det[Prepend[row /@ pts, row[{x, y}]]] == 0 (* Out: ...


22

Diagnosis Spelunking the definition of Commonest, which is written in top-level Mathematica code, I see that the two parameter form is handled by this internal function: Commonest; (* preload *) ? Statistics`DescriptiveDump`oCommonestSetLength oCommonestSetLength[list_, n_] := Catch[Block[{res, reslen, ord}, res = Tally[list]; reslen = Length[res]; ...


20

We can use Replace to express an almost literal translation of the problem statement: Replace[input, n : Except[Max[input]] :> n/10., {1}] (* {0.2, 0.3, 0.1, -0.3, -0.5, 9, 0.2, 0.6, -0.1, 0.06} *) Why Replace instead of /.? Replace is used instead of ReplaceAll (/.) to ensure that the replacement rule is only applied to the level one list elements ...


19

One way is to use an extra argument that acts as a switch. Clear[f]; f[0] = 1; f[1] = 1; f[n_, True] := f[n - 1] + f[n - 2] Example: f7 = f[7, True] (* Out[329]= f[5] + f[6] *) To proceed another step, can do a replacement. f7 /. f[aa_] :> f[aa, True] (* Out[330]= f[3] + 2 f[4] + f[5] *) Can use Nest to repeat this n times. Nest[# /. f[aa_] ...


19

Max[StringLength@Names["System`*"]] 38 Select[ Names["System`*"], 38 == StringLength[#] &] {"MultivariateHypergeometricDistribution"} As far as I can say there is no limit for lengths of symbol names, besides that of the memory limitation.


18

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


18

Timing charts updated for version 10.0.2. The behavior remains unchanged. 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 ...


17

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


17

Let me first answer your second question, since I can only guess about the main question: I also observed that the syntax colouring (version 10, windows 7) suggests that Trace can be used with only two arguments. It's really just the coloring that goes wrong and has nothing to do with functionality. You can see that it is not even related to ...


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


16

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} *) ...


16

It seems that there is a significant overhead every time a color scheme is switched. Once a scheme is loaded each use is fast, but changing color schemes apparently unloads and reloads the mechanism. The result is that the speed of application is directly related to the frequency of switching. With sorted values there is only one switch and application is ...


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


15

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


15

Normally I like to use On and Off for this kind of tracing as it is easy to set up without modifying any symbols. However, it does not immediately work in this case: On[Roots] Solve[x^3 - 2 x + 12 == 0, x]; Off[] This does not produce any trace messages. Something must be using Quiet to suppress them. We can check this hypothesis: On[Quiet] Solve[x^3 ...


15

Let us try to produce the solution without applying brute force, similar to mgamer's answer (that did not actually use Mathematica). Reduce[Mod[10^r - 1, 37] == 0, r, Integers] (* -> C[1] \[Element] Integers && C[1] >= 0 && r == 3 C[1] *) We see that the value of r can in fact be any nonnegative multiple of 3. The result sought is ...


14

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


14

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


14

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.


14

The term pure function used in Mathematica is not being used in the same sense as the cited Wikipedia article. In Mathematica it refers to an anonymous function. In the Wikipedia article it is a term extracted by analogy from the increasingly popular term "purely functional" which refers (mainly) to deterministic programming free of side-effects. The ...


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

For full ranges There is the function DayRange that can be used for this purpose, but not in the same simple way like CharacterRange. For the days: DayName /@ DayRange[Today, Today ~DatePlus~ {{1, "Week"}, {-1, "Day"}}] {Wednesday, Thursday, Friday, Saturday, Sunday, Monday, Tuesday} For the months: DateValue[#, "MonthName"] & /@ DayRange[Today, ...


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


12

It seems me that the answers of mathe and Yves Klett do not meet expectations of the author. The latter is as much as I have got it, to have a short analytical expression for the solution. Probably the author has an intention to use the result further in some analytical calculations, or to do something comparable. Am I right? If yes, one should first of ...


12

Here is a different approach based on the awesome new Region functions: f[x_] := x^3 g[x_] := x^5 - 2 x^3 - 3 x We solve for the intersections: sol = x /. NSolve[f[x] == g[x], x, Reals] {-1.94712297, 0, 1.94712297} Edit Here are the regions of interest: r1 = ImplicitRegion[g[x] > y && f[x] < y, {{x, -1.94712297, 0}, y}]; r2 = ...


12

Here is my solution: input = {2, 3, 1, -3, -5, 9, 2, 6, -1, 0.6}; factor=10.; pos = First@Ordering[input, -1]; output = ReplacePart[input/factor, pos -> input[[pos]]] {0.2, 0.3, 0.1, -0.3, -0.5, 9, 0.2, 0.6, -0.1, 0.06}



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