# Tag Info

73

Leaving the general discussion on design patterns for some later time, I will try to collect a comprehensive information on some specific design patterns / idioms, one pattern per post. This time it will be memoization Basic memoization What is memoization Memoization is a technique which enables a program to reuse parts of computations it has done ...

71

It is a simple way to implement Memoization. The trick is that if you define a function as f[x_]:=f[x]=ExpensiveFunctionOf[x] then when you for the first time call e.g. f[3], it will evaluate as f[3]=ExpensiveFunctionOf[3] which will evalulate the expensive function, and assign the result to f[3] (in addition to giving it back, of course). So if e.g. ...

63

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

59

Memoization is perhaps the most common application, but it is not the meaning of that construct. More generally it is a construct for a function that redefines itself. This has many uses beyond memoization. Consider this function: f[y_] := (f[y] = Sequence[]; y) It is used to remove duplicates in a list. When the function is first called with a ...

50

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

37

Linked lists: general discussion I would like to describe here a pattern which is IMO quite important yet seems to be generally under-appreciated and under-used in our community (and probably in Mathematica programming in general) - namely, linked lists. Linked lists are important for several reasons. Here is a (partial) list of benefits they can bring to ...

33

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

31

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

31

This is my first reply in this group. So please bear with me if I make any mistake, it would not be intentional, just lack of familiarity with the rules. Although the replies above mention important aspects, I generally like to view things from alternative perspectives. I'd like to offer a few of those on this question. Understanding is enhanced by viewing ...

29

It's ... oh, why not let the docs speak: tutorial/FunctionsThatRememberValuesTheyHaveFound (in Doc center) Edit You may also find additional information by searching for "memoization" on this site. This has always been a great trick to avoid having to re-evaluate the result of a computationally intensive function call. In the above link, it is also used ...

23

"Design patterns" are a standard approach to a class of problems. They may become idioms if the community adopts them. They may be simple, but non-trivial to figure out and identify (for oneself) as a pattern worthy of re-use. Here's one particular pattern I personally like to use: Example: the "TransformBy" approach There are functions which transform ...

20

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

This is just a beginning of some notions of what the patterns of Mathematica programming might be. Others should feel free to add or correct anything here. It might well be that I've simply misunderstood what patterns in software development really are. Pattern-based overloading to create polymorphous functions It's pretty common to use Mathematica's ...

18

It is probably debatable to what extent it has built-in object oriented features. In any case, this answer is not intended to lead you to try to emulate object oriented programming, which is in general a bad idea. (see @Leonid 's answer) However, it is not debatable that Mathematica is tremendously flexible (as to style and notation at least, the evaluation ...

17

At the risk of repeating myself, I would like to stress that one has to be critical towards the superficial flexibility offered by Mathematica, when (particularly mutable) data structures are concerned. Using mutable data structures assumes a programming style for which Mathematica is not optimized. It can emulate it, yes, and we have seen a number of such ...

16

I think the simple answer is, there isn't one, but you could always just use UML itself, particularly for behavioral diagrams, even if the code isn't object oriented. You wouldn't use class or object diagrams, but there is nothing to stop you from using, say, a component diagram. You may find the tutorial and white paper on building large software systems ...

16

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

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

It also has another practical side. If you have a random function, which you only want to evaluate once, but you don't know where exactly it will be evaluated or you want to declare it before the parameters it depends on are defined, and you want it to be the same after the first evaluation, for any subsequent call, you can use the memoization trick: ...

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

image = Import["http://i.stack.imgur.com/6YRfK.jpg"]; If you want to use your f, uRange and vRange as the arguments to ParamatricPlot3D, you need to wrap each with Evaluate: f = {u, Sin[v]*(u^3 + 2 u^2 - 2 u + 2)/5, Cos[v]*(u^3 + 2 u^2 - 2 u + 2)/5}; uRange = {u, -2.3, 1.3}; vRange = {v, 0, 2 Pi}; ParametricPlot3D[Evaluate@f, Evaluate@uRange, Evaluate@...

11

The "unnecessary" complication is needed for those cases where you specify deeper levels than the first: MapIndexed[f, {{a}, {b}}, {2}] (* {{f[a, {1, 1}]}, {f[b, {2, 1}]}} *) The following code produces what you want: myMapIndexed[f_, l_] := Inner[f, l, Range[Length[l]], List]; myMapIndexed[f, {a, b, c, d}] (* {f[a, 1], f[b, 2], f[c, 3], f[d, 4]} *)

9

Although some of the techniques below use the core language, they could nonetheless be new to a beginner or intermediate Mathematica user. Many illustrations can be found in my answer here: Where can I find examples of good Mathematica programming practice? that collects interesting questions and answers, I'm just trying to summarize here some techniques I ...

9

The answer to the more general question of how necessary "software architecturizationing" is in Mathematica is, in short: Not that necessary. The reason is basically 1) lists 2) dynamic typing and 3) lists + dynamic typing. For example, Mathematica doesn't need classes/OO because lists allow you to represent a huge swath of data structures. You would gain ...

9

To directly get the opposite of Alternatives, you could negate each pattern with Except and then negate the Alternatives: also[patts__] := Except[Alternatives @@ Except /@ {patts}] Cases[Range[1, 15, 1/2], _Integer ~also~ _?(# > 10 &)] (* {11, 12, 13, 14, 15} *) Generally some other approach will be simpler, though, as discussed in the comments.

7

One of my favorite Mathematica paradigms is what we used to call "self modifying code." Back in assembly language days (before the dinosaurs) this was considered a VERY BAD THING and was discouraged -- many modern languages forbid it. But it's a normal and common part of the Mathematica syntax. For example, say you need to implement a filter on an image, but ...

6

From a first look, late at night. In the third panel of code, aren't all of the terms really parts of a list p, and should therefore be written as p[[1]], p[[2]], etc., rather than p[1], p[2], etc. The paired single square brackets would surround the argument(s) of a function, e.g. Sin[x] or Sqrt[x]; paired double square brackets designate an element of a ...

5

I don't know a way with subvalues, but you can use Module to create objects without explicit identifier in makeObj: makeObj[] := Module[{field = 0}, Switch[#, "increase", field++; #0, "field", field]&] (Note that I slightly changed your "increase" function to return #0, that is, "self", so I can chain ...

5

Well, one obvious idea would be to build on the struct implementation by Bob Beretta. You would have to add information about methods and modify the implementation of --> to consider those as well, and for polymorphism, you'd also have to store the base class (or base classes, if multiple inheritance should be supported), and have --> look there if the ...

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