Skip to main content
26 events
when toggle format what by license comment
Mar 13, 2020 at 14:18 comment added Mr.Wizard @jsxs I meant to write so long as they appear in separate expressions. So as you observed you can use mem : f1[x_] := mem . . . and mem : f2[x_] := mem . . . in the same Notebook or Cell without conflict.
Mar 13, 2020 at 6:51 comment added lxy @Mr.Wizard thank you for your reply. Well, what did you mean by so long as they appear separate expressions? By experimentation, I found whether using mem : f1[x_] := mem = . . . and mem : f2[x_] := mem = . . . or using memf1 : f1[x_] := memf1 = . . . and memf2 : f2[x_] := memf2 = . . ., I obtained the same output when evaluating an expression, in which f1[x] and f2[x] appear simultaneously, e.g., a*f1[x]+b*f2[x].
Mar 11, 2020 at 14:29 comment added Mr.Wizard @jsxs Yes, you can use the same pattern name for different applications within the same Notebook so long as they appear separate expressions. Crucially this is not the same as assigning a pattern to a global Symbol as in e.g. (30591). By "consistent pattern names" I merely mean that it makes code easier to read when you follow a naming convention; using mem : consistently lets me know at a glance why I am labeling the left-hand-side as a pattern, since there are other reasons why I might need to do that beside memoization.
Mar 11, 2020 at 13:37 comment added lxy @Mr.Wizard Can I use the same pattern name for several different patterns? For example, using mem : f1[x_] := mem = . . . and mem : f2[x_] := mem = . . . in a notebook. Could you explain a little more about by using consistent pattern names for each application Thank you!
Feb 16, 2020 at 15:20 comment added lxy @Mr.Wizard now, I see this point. Btw, for the example: g[x_] := (g[y_] := y - 1; x + 1) when 1st called, g defines itself as g[y_] := y - 1 and evaluates x+1 and returns the results, when g is called later, it evaluate g[y_] := y - 1, right?
Feb 16, 2020 at 11:55 comment added Mr.Wizard @jsxs If I understand your question, no. This is a general property: when specificity is easily ranked more specific definitions are tried first. So if we have both f[x_] := 1 and f[5] := 2 in the system, f[5] evaluates to 2 no matter the order those definitions are made. When specificity ranking is not apparent to Mathematica the first definition given has priority. For example with definitions g[x_ /; x > 0] := 1 and g[y_ /; y == 5] := 2 the output of g[5] will depend on the order in which they are given.
Feb 16, 2020 at 5:13 comment added lxy @Mr.Wizard very impressive! Is the more specific definition has priority realized by placing f[y] = Sequence[] before y? thanks!
Apr 13, 2017 at 12:55 history edited CommunityBot
replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/
Mar 15, 2017 at 17:04 comment added Carl Woll @Mr.Wizard Go here and download StackExchange.m. Then, Get["StackExchange`"], run StackExchange`InstallStylesheet[], save the stylesheet, then use the style sheet.
Mar 15, 2017 at 6:23 comment added Mr.Wizard @CarlWoll I am fairly busy these days but I am interested nevertheless. If you don't have expedations of my helpfulness I would like to try it.
Mar 7, 2017 at 20:53 comment added Mr.Wizard @CarlWoll For what it's worth I now see Leonid already noted as much in a comment: mathematica.stackexchange.com/questions/2419/…
Mar 7, 2017 at 20:49 comment added Carl Woll Don't worry, that's not necessary.
Mar 7, 2017 at 20:40 comment added Mr.Wizard @CarlWoll I learned UnsortedUnion from the documentation. I am not surprised that came from you. Would you like me to add a note of credit to this answer?
Mar 7, 2017 at 20:29 comment added Carl Woll This is basically the function I came up with back in 1999
Jul 19, 2016 at 8:33 history edited Mr.Wizard CC BY-SA 3.0
added 334 characters in body
Dec 31, 2013 at 17:49 history edited Mr.Wizard CC BY-SA 3.0
added 157 characters in body
Mar 9, 2013 at 0:15 comment added Mr.Wizard @Michael Ah well, I did say it was a contrived example. :^)
Mar 9, 2013 at 0:13 comment added Michael E2 Thanks for the update! (Although I would have chosen NonNegative instead of Positive).
Mar 9, 2013 at 0:01 history edited Mr.Wizard CC BY-SA 3.0
added 694 characters in body
Mar 9, 2012 at 18:27 comment added celtschk I don't think it has an advantage in this case (I didn't measure, though), hovever it would have an advantage if the internal function itself is complicated/expensive to define (e.g. if defining it as result of some calculation), because this way the original definition is only executed once instead of every time UnsortedUnion is called). Another difference is that the internal symbol is still accessible by looking at the definition of UnsortedUnion; this might be useful for debugging.
Mar 8, 2012 at 7:42 comment added Mr.Wizard @celtschk is there an advantage to that over UnsortedUnion = Module[{f}, f[y_] := (f[y] = Sequence[]; y); f /@ Join@##] & or do you share it as a curiosity?
Mar 7, 2012 at 15:16 comment added celtschk Well, then encapsulate it, e.g. Module[{dedup},dedup[x_]:=(dedup[x]=Sequence[];x);UnsortedUnion[l_List]:=Internal`InheritedBlock[{dedup},dedup/@l]]
Mar 7, 2012 at 13:58 comment added Mr.Wizard @celtschk interesting method, although if f were ever used outside InheritedBlock the result could be very frustrating.
Mar 7, 2012 at 11:34 comment added celtschk You can avoid reinitialization if you wrap the application in an Internal`InheritedBlock like this: Internal`InheritedBlock[{f}, f/@ {3,5,2,3,2,4,3}]
Mar 7, 2012 at 6:49 comment added Leonid Shifrin Very good points. +1
Mar 7, 2012 at 0:56 history answered Mr.Wizard CC BY-SA 3.0