Tag Info

Hot answers tagged

76

Preamble I spent some time and designed and implemented a tiny framework to deal with this problem, over the last two days. Here is what I've got. The main ideas will involve implementing a simple key-value store in Mathematica based on a file system, heavy use and automatic generation of UpValues, some OOP - inspired ideas, Compress, and a few other ...


43

This answer may be unacceptable right from the outset because it uses undocumented functions. However, it has advantages over some of the approaches suggested so far which might be redeeming enough in certain scenarios to recommend it in practice. In particular, it provides totally encapsulated state (unlike, e.g., DownValues or Temporary symbols) and O(1) ...


42

I will answer a couple of your questions only. Space efficiency Packed arrays are significantly more space efficient. Example: Let's create an unpacked array, check its size, then do the same after packing it: f = Developer`FromPackedArray[RandomReal[{-1, 1}, 10000]]; ByteCount[f] ByteCount[Developer`ToPackedArray[f]] (* 320040 80168 *) Time efficiency ...


41

Update: Mathematica 10 has introduced Association, which can be used as a close equivalent of structs. params = <| "par1" -> 1, "par2" -> 2 |> params["par1"] (* ==> 1 *) In version 10 pure functions can have named arguments, and can be effectively used as expression templates where the slots can be populated from an association. This is ...


38

Preview and comparative results The implementation below may be not the most "minimal" one, because I don't use any of the built-in functionality (DictionaryLookup with patterns, Graph-related functions, etc), except the core language functions. However, it uses efficient data structures, such as Trie, linked lists, and hash tables, and arguably maximally ...


38

My solution is a recursive tree traversal algorithm which seeks and searches neighbouring vertices only if it will lead to a word (e.g., Something starting with ZQ is immediately disqualified), but it's faster than yours because I construct the adjacent vertices list from the adjacency matrix rather than calling NeighborhoodGraph each time. On my machine, ...


37

The difference Packed arrays give you pretty much an access to a direct C memory layout, where the arrays are stored. Unpacked arrays reference arrays of pointers to their elements. This explains most of the other differences, in particular: Space efficiency: if you look at how much space is required for packed arrays, you see that it is exactly the ...


30

There were several attempts to emulate structs in Mathematica. Emphasis on emulate, since AFAIK there is no built - in support for it yet. One reason for that may be that structs are inherently mutable, while idiomatic Mathematica gravitates towards immutability. You may find these discussions interesting: Struct-data-type-in-mathematica ...


28

Using symbols to store data and object-like functions Here are interesting functions to use symbols like objects. (I originally posted these thoughts in What is in your Mathematica tool bag?). The post has grown quite big over time as I used it to record ideas. It's divided into three parts, one describing the function Keys, another one where properties ...


25

A combination of rules and recursion is able to produce rather powerful solutions. Here is my take on it: ClearAll[makeTree]; makeTree[wrds : {__String}] := makeTree[Characters[wrds]]; makeTree[wrds_ /; MemberQ[wrds, {}]] := Prepend[makeTree[DeleteCases[wrds, {}]], {} -> {}]; makeTree[wrds_] := Reap[If[# =!= {}, Sow[Rest[#], First@#]] & /@ ...


23

In practice, enforcing strong types in Mathematica seldom pays off, just because, as mentioned by @belisarius, Mathematica is untyped (and perhaps more so than most other langauges, since it is really a term-rewriting system). So, most of the time, the suggestion of @Mr.Wizard describes what I'd also do. The way to define ADT-s (strong types) was described ...


21

Here are a few ways, each of which operates upon the individual component associations. We can explicitly construct a new association that includes all of the old columns and adds a new one: ds[All, <| "col1"->"col1", "col2"->"col2", "col3"->(#col1 + #col2&) |>] (* col1 col2 col3 1 2 3 3 4 7 5 6 11 *) This ...


20

You can implement an imperative-style circular buffer. big = Range@1*^7; size = Length@big; pointer = size; updateElement[new_Integer] := (pointer = 1 + Mod[pointer, size]; big[[pointer]] = new) Do[updateElement[RandomInteger@99], {100}] // AbsoluteTiming {0.000374, Null} To bring the buffer back to the normal form use big = RotateLeft[big, ...


19

The answers already posted show that built-in Mathematica functionality can be used to get the meaningful functionality provided by a C struct. If you want your code to be readable by other Mathematica users, I suggest using a list of rules as already advised above. However, if you really want struct-style syntax I'll offer an implementation that I've ...


19

I'm the developer of Dataset. Yes, this is a gross documentation oversight. We planned this functionality but had to push it back to a point release. Somehow no-one caught this piece of legacy documentation. I'm filed a bug on the documentation problem right now, it's easy to fix. As for when L-value assignment will be available, I'm hoping 10.0.1 or ...


16

So the naive way to set up a data structure like struct is, as the OP suggested, to simply used DownValues and/or SubValues. In the below, I use SubValues. Copying the Wikipedia C language struct example struct account { int account_number; char *first_name; char *last_name; float balance; }; struct account s; // Create new account labelled s ...


16

As you mentioned in your question and belisarius illustrates above, you can check arguments with arbitrary pattern matching. When I need to do checks of this kind I often use a couple of methods; I will define the pattern once and then reference it by name: p1 = {{_Integer, {_Integer ...}} ...}; dat = {{100, {1, 2, 3, 4, 5}}, {105, {2, 4, 6, 8}}, {42, ...


15

I arrived very late to this party and I'm very much afraid that nobody comes here anymore. Still I'm posting this in hope that an occasional visitor may find it a practical approach to implementing data structures with named fields within Mathematica. The concept The idea is to use protected symbols to name a structure and its fields. The symbol that names ...


15

I will answer the technical part of the question - namely, how to get the entire graph. How one would go about analyzing and visualizing it, is another story. This will open and parse a given guide notebook, and get the links to other notebooks: ClearAll[getLinks]; getLinks[file_] := With[{nb = NotebookOpen[file]}, With[{result = ...


14

No ADT in Mma (natively at least) ... but in your case you could use pattern matching: yours = {{100, {1, 2, 3, 4, 5}}, {105, {2, 4, 6, 8}}, {42, {42, 39, 56}}}; f[x_] := 1 /; MatchQ[x, List[List[_Integer, List[_Integer ...]] ...]] f[yours] f["mySymbol"] (*-> 1 f["mySymbol"] *)


14

Mathematica has no obvious hash-table structure but what most people forget is, that the DownValues of symbols, which means the simple, always-used function definitions, are implemented using hashing. Therefore, the most straightforward way to create a dictionary from string to integer is by making definitions: dict["hello"] = 1; dict["blub"] = 2; By ...


13

As Michael Pilat explained here it is more robust to use MakeBoxes, rather than Format. Using MakeBoxes: MakeBoxes[diag[m_?MatrixQ], _] ^:= InterpretationBox[RowBox[{"diag", "[", #, ",", #2, "]"}], diag[m]] & @@ ToBoxes /@ {Dimensions[m], Diagonal[m]} Here is a definition for handling Part extraction: diag[m_?MatrixQ][[part___]] ^:= m[[part]] ...


13

Using this webcrawler code from Wolfram site, and Guides page in online docs as the starting url: webcrawler[rooturl_, depth_] := Flatten[Rest[NestList[Union[Flatten[Thread[# -> Import[#, "Hyperlinks"]] & /@ Last /@ #]] &, {"" -> rooturl}, depth]]]; style = {VertexStyle -> White, VertexShapeFunction -> "Point", EdgeStyle -> ...


13

Preamble I will discuss here two methods for doing computations on very large data sets which don't fit into memory. The first method is based on sequential reading of chunks of data from a file. The second method is based on converting a data set to a file-backed list representation. The unifying idea for both methods is the use of iterators as a useful ...


12

Preamble I think this is a very good question. Trying to address it in a reasonably general way, I ended up with a tiny framework which implements a limited form of pointer-like semantics, which I'd like to describe and illustarate. Code This allows one to mark some portion of the code (some expression) as a reference. ClearAll[Ptr, new, llp]; ...


12

If your Excel file test.xls is very simple: Then the code is a one-liner (if I understand correctly what is needed): Set@@@Transpose[{ToExpression[First[#]], Transpose[Rest[#]]}&@Import["test.xls"][[1]]] To check: {Paris, Moscow} {{1., 2., 3., 4., 5., 6., 7., 8.}, {12., 23., 34., 45., 56., 67., 78., 89.}} The rest is more complex cases. ...


12

You can effectively create your own types by using the feature that Mathematica expressions have a Head, the head can be used to define a type. Functions can then use the Head value to apply only to arguments matching the defined type. A version with loose format checking, format checked only upon creation,can be implemented as simply as this: (* Define ...


12

The notebook DocumentationNavigator.nb has all the inter-dependencies built-in (they're arguments supplied to TreeBrowse`LoadVirtualCells and other undocumented functions that build up the documentation center. We can then parse the raw text contents of this notebook to pull out this list. I do that in the following, but I haven't restricted it solely to ...


12

This is going to be transcript of Roman E. Maeder's priority queue code with any updates I can find to make to take advantage of functions added since he wrote it. I believe I am within right to copy it here for noncommercial purposes. Refactor v0.2 -- any bugs are almost certainly my own. BeginPackage["PriorityQueue`"] MakeQueue::usage = ...



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