wolfram-mathematica

Time efficient Partial Inverted Index building

我怕爱的太早我们不能终老 提交于 2019-12-03 12:13:34
I need to build a partial Inverted Index . Something like: l = {{x, {h, a, b, c}}, {y, {c, d, e}}} iI[l] (* -> {{a, {x}}, {b, {x}}, {c, {x, y}}, {d, {y}}, {e, {y}}, {h, {x}}} *) I think it is pretty clear what it does. In the input list, the {x, y ...} are unique, while the {a, b, c, ..} are not. The output ought to be ordered by #[[1]] . Right now, I am doing this: iI[list_List] := {#, list[[Position[list, #][[All, 1]]]][[All, 1]]} & /@ (Union@Flatten@Last@Transpose@list) But it looks too convoluted for such an easy task, seems too slow, and I should be able to cope with Legion. A test drive

Using MySQL databases in Mathematica

倾然丶 夕夏残阳落幕 提交于 2019-12-03 12:00:28
问题 I've seen it's possible to make a connection between Mathematica and MySQL databases using Input Needs["DatabaseLink "] and conn = OpenSQLConnection[JDBC["MySQL(Connector/J)", "yourserver/yourdatabase"], "Username" -> "yourusername", "Password" -> "yourpassword"] (in case anyone wants to give it a try). Documentation of DatabaseLink here, by the way. Does anyone have experience using Mathematica in this way, probably to analyze data contained in the database? Are there obvious drawbacks

Reducing memory usage in an extended Mathematica session

独自空忆成欢 提交于 2019-12-03 11:47:08
问题 I'm doing some rather long computations, which can easily span a few days. In the course of these computations, sometimes Mathematica will run out of memory. To this end, I've ended up resorting to something along the lines of: ParallelEvaluate[$KernelID]; (* Force the kernels to launch *) kernels = Kernels[]; Do[ If[Mod[iteration, n] == 0, CloseKernels[kernels]; LaunchKernels[kernels]; ClearSystemCache[]]; (* Complicated stuff here *) Export[...], (* If a computation ends early I don't want

Optimally picking one element from each list

余生长醉 提交于 2019-12-03 11:43:10
问题 I came across an old problem that you Mathematica/StackOverflow folks will probably like and that seems valuable to have on StackOverflow for posterity. Suppose you have a list of lists and you want to pick one element from each and put them in a new list so that the number of elements that are identical to their next neighbor is maximized. In other words, for the resulting list l, minimize Length@Split[l]. In yet other words, we want the list with the fewest interruptions of identical

Can I check with the Stackoverflow API which SO answerers are sleep-deprived?

。_饼干妹妹 提交于 2019-12-03 11:35:50
问题 In how-do-i-access-the-stackoverflow-api-from-mathematica I outlined how one could use the SO API to get Mathematica to make some interesting reputation graphs of top answerers. Could this API also be used to provide some privacy-invading insights in the answerers' nocturnal habits? 回答1: Certainly, for instance using this MMA8 code: getActionDates[userID_Integer] := Module[{total}, total = "total" /. Import["http://api.stackoverflow.com/1.1/users/" <> ToString[userID] <> "/timeline?pagesize=1

Import data from URL

一世执手 提交于 2019-12-03 11:32:56
问题 The St. Louis Federal Reserve Bank has a great set of data available on a variety of their web pages, such as: http://research.stlouisfed.org/fred2/series/OILPRICE/downloaddata?cid=32217 http://www.federalreserve.gov/releases/h10/summary/default.htm http://research.stlouisfed.org/fred2/series/DGS20 The data sets get updated, some as often as daily. I tend to have an interest in the daily data (see the above settings on the URLS) I'd like to import these kinds of price or rate data streams

how to generate such an image in Mathematica

╄→гoц情女王★ 提交于 2019-12-03 11:29:07
问题 I am thinking of process an image to generate in Mathematica given its powerful image processing capabilities. Could anyone give some idea as to how to do this? Thanks a lot. 回答1: Here's one version, using a textures. It of course doesn't act as a real lens, just repeats portions of the image in an overlapping fashion. t = CurrentImage[]; (* square off the image to avoid distortion *) t = ImageCrop[t, {240,240}]; n = 20; Graphics[{Texture[t], Table[ Polygon[ Table[h*{Sqrt[3]/2, 0} + (g - h)*

Mathematica's pattern matching poorly optimized?

女生的网名这么多〃 提交于 2019-12-03 11:02:58
I recently inquired about why PatternTest was causing a multitude of needless evaluations: PatternTest not optimized? Leonid replied that it is necessary for what seems to me as a rather questionable method. I can accept that, though I would prefer a more efficient alternative. I now realize, which I believe Leonid has been saying for some time, that this problem runs much deeper in Mathematica , and I am troubled. I cannot understand why this is not or cannot be better optimized. Consider this example: list = RandomReal[9, 20000]; Head /@ list; // Timing MatchQ[list, {x__Integer, y__}] //

The semantics of Mathematica's Thread function, someone needs to finally put this to rest

僤鯓⒐⒋嵵緔 提交于 2019-12-03 10:38:54
Wolfram Research has had the same documentation for this function for the last 8 years at least: Thread[f[args]] "threads" f over any lists that appear in args. A lovely circular definition if I've ever seen one. Does anyone know what the actual semantics are and can provide a proper explanation that is non-circular? Thread is a bit like a generalization zip from other functional languages. For simple cases, where all the elements of args from your example are lists, Thread[f[args]] is equivalent to f @@@ Transpose[{args}] as shown in the first couple examples in the documentation. The major

Question on Condition (/;)

。_饼干妹妹 提交于 2019-12-03 10:29:05
问题 Condition has attribute HoldAll which prevents evaluation of its first argument before applying the Condition . But for some reason Condition evaluates its first argument even if the test gives False : In[1]:= Condition[Print[x],False] During evaluation of In[1]:= x Out[1]= Null/;False Why is this? For what purposes Condition evaluates its first argument if the test gives False ? In which cases this behavior can be useful? P.S. Its behavior differs when the Condition is used as the second