Leonid Shifrin
  • Member for 10 years
  • Last seen this week
Can Mathematica be regarded as a software prototyping environment?
Accepted answer
51 votes

I think that Mathematica is a great prototyping environment, and has a bright future as a system for both prototyping and implementation of complete components of other systems, from back-ends to ...

View answer
Can one identify the design patterns of Mathematica?
50 votes

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

View answer
Are there guidelines for avoiding the unpacking of a packed array?
Accepted answer
49 votes

I will try to list some cases I can recall. The unpacking will happen when: The result, or any intermediate step, is a ragged (irregular) array. For example Range /@ Range[4] To avoid this, you ...

View answer
Looking for "Longest Common Substring" solution
48 votes

Preamble and motivation While I am much late to the party here, I hope this answer will not be totally useless. This is a first in a series of posts where I will advocate a wider use of Java in our ...

View answer
Is there a name for #1@#2&?
Accepted answer
46 votes

Compose and Composition There is, but it is deprecated (in favor of Composition): Compose: MapThread[Compose, {{a, b, c}, {1, 2, 3}}] (* {a[1], b[2], c[3]} *) I still use Compose myself, but I would ...

View answer
What are some advanced uses for Block?
Accepted answer
46 votes

Safety Module is safer than Block because: It is a lexical scoping construct, which means that variable bindings are only tied to a specific piece of code. Variables outside that piece of code are ...

View answer
Are there any cases when For[] loops are reasonable?
Accepted answer
46 votes

To my mind, there are at least two cases when For loops are ok: Inside Compile, or in code which is being written with Compile in mind When your inner loops are vectorized or made efficient by some ...

View answer
How well does Mathematica code exported to C compare to code directly written for C?
Accepted answer
46 votes

A lot depends on how you write your code in Mathematica. In my experience, the rule of thumb is that the generated code will be efficient if the code inside Compile more or less resembles the code I ...

View answer
How do I get my equation to have the form $(x-a)^2 + (y-b)^2 + (z-c)^2-d = 0$?
Accepted answer
45 votes

You can use custom transformation rules, for example: -11 - 2 x + x^2 - 4 y + y^2 - 6 z + z^2 //. (a : _ : 1)*s_Symbol^2 + (b : _ : 1)*s_ + rest__ :> a (s + b/(2 a))^2 - b^2/(4 a) + ...

View answer
Fastest way to calculate matrix of pairwise distances
Accepted answer
44 votes

Using Outer is here one of the worst methods, and not just because it computes the distance twice, but because you can't leverage vectorization in this approach. This is actually a common issue and an ...

View answer
How can I implement dynamic programming for a function with more than one argument?
Accepted answer
43 votes

Yes, there is, although the speed-up is not as dramatic as for 1D memoization: ClearAll[CharlierC]; CharlierC[0, a_, x_] := 1; CharlierC[1, a_, x_] := x - a; CharlierC[n_Integer, a_, x_] := Module[{...

View answer
Is it possible to reference "self" from a pure function?
Accepted answer
42 votes

Per suggestion of rcollyer, I have moved this answer here, with a few modifications / additions. Recursive pure functions (#0) seem to be one of the darker corners of the language. They are mentioned ...

View answer
How to load a package without naming conflicts?
41 votes

The answer of @R.M. already explains the essence of the problem. You can streamline the process of removing the Combinatorica from the $ContextPath by loading it via Block[{$ContextPath}, Needs["...

View answer
Functions vs. patterns
41 votes

These two forms may be similar on the surface, but they are very different in terms of the underlying mechanisms invloved. In a sense, Function represents the only true (but leaky) functional ...

View answer
Customizing syntax highlighting for private cell styles
40 votes

As a part of a larger sets of development tools which I am working on currently, I have developed a general syntax highlighter generator which does just that (not yet with styles though, this is ...

View answer
Two ways to map a function on a list: Which one is faster?
Accepted answer
38 votes

The role and meaning of Listable The Listable attribute serves to impose automatic threading over lists for symbols for which it is set (or present from the start, for some built-in functions). ...

View answer
Monitor doesn't work with ParallelTable
38 votes

One way is to set a shared variable that would be assigned to an iterator variable, and monitor that: SetSharedVariable[j] Monitor[ ParallelTable[j = n;Length[FactorInteger[2^n - 1]], {n, 50, 300}]...

View answer
General strategies to write big code in Mathematica?
37 votes

Managing the complexity III: using powerful abstractions In this section I will list a few techniques which allow one to write more modular code and better separate the concerns, by using certain ...

View answer
Data Table Manipulation in Mathematica
Accepted answer
37 votes

I think, your question has 3 levels: convenient syntax, data representation, and efficiency. I can offer a very lightweight solution which addresses all of these in the simplest way: syntax is ...

View answer
What is the story with Removed symbols?
Accepted answer
37 votes

Some observations on Removed Removed is not a normal head, but rather a print form. Consider a definition x := y Once we Remove the y, we invalidate x in a subtle but permanent way - reintroducing ...

View answer
How to create strong types?
Accepted answer
37 votes

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

View answer
Struct equivalent in Mathematica?
37 votes

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

View answer
Debugging memory leaks
Accepted answer
36 votes

Preamble It is hard to say what exactly is causing this without seeing the code, but, assuming that there are no memory leaks in the built-in functions you are using, I am only aware of a very few ...

View answer
Can a Trie be implemented efficiently?
Accepted answer
36 votes

UPDATE Since version 10, we have Associations. Here is the modified code for trie building and querying, based on Associations. It is almost the same as the old code (which is below): ClearAll[...

View answer
Mathematica Debuggability
36 votes

While I agree that the debugging tools could have been better developed by now, let me just throw in a few notes and links. Function chaining (f[g[h[...]]]): I'd argue that this is a good thing. Why: ...

View answer
What is the most convenient way to read definitions of in-memory symbols when we don't have the source files? (Spelunking tools)
35 votes

I can now offer a solution which leverages the full power of the code formatter, in its new, more robust form. Load the formatter: Import["https://raw.github.com/lshifr/CodeFormatter/master/...

View answer
What are the requirements for a well behaved indexed variable? Subscript, ToExpression, Downvalue?
Accepted answer
34 votes

General usage Here is what I think Using strings and subsequently ToString - ToExpression just to generate variable names is pretty much unacceptable, or at the very least should be the last thing ...

View answer
Declaring Package with dependencies in multiples files?
33 votes

Preamble I will interpret your question as a general question on how one can organize multi-file projects in Mathematica / WL. There are two main axes in this problem, and they are Loading procedure ...

View answer
Is the renaming mechanism of `With` flawed?
Accepted answer
33 votes

Short answer The local variables of the form varname$... are used by the system, and it is unwise to use symbols with such names as local variables. With, like many other lexical scoping constructs, ...

View answer
The paradigm behind Dynamic and related functionality
Accepted answer
32 votes

General With a disclaimer that I am by far not an expert in this field, I will still try to answer. Perhaps this answer could then be improved by others more knowledgable than me. Model From the ...

View answer
1
2
3 4 5
23