Questions on writing non-trivial programs in Mathematica. Do not use this tag for questions on plotting/graphics or for questions on doing mathematics with Mathematica, where the focus is more on the math than the program.
3
votes
2answers
84 views
How do Forms affect the interpretation of Expressions?
Below are three formats for representing an undirected edge between vertices a and b:
...
4
votes
1answer
245 views
Can a Table iterator “leak” into a Module?
I want to repeatedly call a module by using a Table statement. When I use an i iterator in the Table command calling the module, I receive the error message ...
8
votes
1answer
283 views
Why does Mathematica choose the second function definition?
While working on a solution to this question I've come across a case where I simply don't understand Mathematica's behaviour.
I've got the following definitions:
...
16
votes
1answer
360 views
What is the proper method to load a Mathematica package inside a DynamicModule
I have a DynamicModule that requires loading of Mathematica packages. The usual methods do not seem to apply (...
13
votes
4answers
690 views
Is there a Mathematica/Lisp link?
I'd like to start learning Lisp as it sometimes leads to interesting answers on this site with concepts borrowed from this language, so I'm curious. As I already spend most of my programming time in ...
14
votes
2answers
211 views
When should I, and when should I not, set the HoldAll attribute on a function I define?
I have seen a number of examples on this site
(such as in syntax highlighting and checking evaluation status) in which functions definitions are preceded with
...
18
votes
3answers
440 views
Finding a “not-shortest” path between two vertices
In designing a routine for making a simple three dimensional (5x5x5) labyrinth, I realized that my solutions (a solution is a labyrinth includes a single path from {1, 1, 1} to {5, 5, 5} in a 5 x5x5 ...
8
votes
1answer
375 views
GeoDirection and GeoDistance Memory Leaks: How to Recover the Memory?
The functions GeoDirection and GeoDistance have memory leaks. To see this, run the example functions below and watch as the Physical Memory Available in the Windows Task Manager, Performance tab, ...
6
votes
0answers
222 views
MMA implementation of Earth Movers Distance?
Has anyone implemented in MMA the Earth Mover's Distance (EMD)? A concept dating back to Monge 1871, used to compare histograms and images, e.g., the CIEDE2000 Color Difference algorithm. Yossi ...
8
votes
2answers
176 views
Cases[data,Colon[key,_]] vs. Cases[data,key:_] toward XPath, XQuery
Exploring methods to index and search tree-structured key:value pairs via named-entity index (key paths) as opposed to Position-based indexing.
Using Rule as ...
3
votes
2answers
219 views
Randomly redrawing part of a random sample that matches a criterion
I would need some (a lot of) help to improve/rewrite some code.
Since my real problem is a bit too complicated to explain, I try to show with a simplified scheme what I want to achieve.
I create ...
7
votes
1answer
238 views
Abort not aborting
I have a relatively simple bit of code that looks like the following:
---some code here---
If[statement, Abort[]];
---more code following---
However, while ...
9
votes
1answer
275 views
Modifying a List in a function in place
An example will be most specific:
func[list_, column_] := list[[All, column]] = Map[#*2 &, list[[All, column]]];
This throws errors.
I want to avoid doing ...
29
votes
2answers
862 views
What is a Mathematica packed array?
A simple sounding question with a few sub questions:
What is the difference between unpacked vs packed array?
Are packed arrays more space efficent, how much so?
Are packed arrays more time ...
16
votes
2answers
569 views
Does every Symbol in Mathematica induce a monad?
From my question about types in Mathematica, I assume that every Symbol in Mathematica is a type if it appears as a Head of some ...
18
votes
3answers
822 views
What is a type in Wolfram Mathematica programming language?
"Everything is an expression" is a popular citation from many Mathematica guidebooks. So, what is type in Mathematica? How does it relate to common types from Haskell, for example?
I did some ...
15
votes
4answers
433 views
How do I generate the upper triangular indices from a list?
I have some list
{1,2,3}.
How do I generate nested pairs such that I get
{{1,2},{1,3},{2,3}}?
That is I'd like a way to ...
12
votes
3answers
405 views
Reading from STDIN, or: how to pipe data into Mathematica
Today I tried using Mathematica's plotting capabilities to display the output of a C++ program. This made me wonder whether it is possible to somehow tell a Mathematica script to read from STDIN and ...
25
votes
3answers
451 views
Is there a convenient way to copy/paste text-interspersed SE code snippets into Mathematica?
Is there a way to copy and paste code snippets from SE to Mathematica if these snippets are interspersed with text?
Like e.g. in Morphing Graphics, color and location in both the question and answer, ...
4
votes
1answer
250 views
Path queries for tree-structured data
Can anyone suggest documentation or tutorials for developing path queries and indices for (XML-like) tree-structured data?
Suppose data is organized hierarchically in key->value pairs, eg:
...
6
votes
1answer
457 views
Handling failed FindRoot calls
I want to handle FindRoot calls which did not converge (e.g "thrown" error message FindRoot::cvmit)
...
11
votes
2answers
401 views
Using a list of tuples in a pure function
I want to use a list of tuples within a function to make assignments.
Say I want to make assignments of the form
value[i,j] = val
What I have is a list of ...
8
votes
2answers
375 views
shortcut key for traverse command history in a Mathematica session in a terminal?
I wonder what is the shortcut key for traverse command history (i.e. retrieve last, last last, ..., commands) in a Mathematica session in a terminal?
If I hit the up/down arrow or pageup/pagedown, it ...
4
votes
3answers
773 views
Basic questions about running Mathematica
Is there a command window and an editor in Mathematica 8 similar to
those in Matlab? In Mathematica GUI mode, all I saw is write code and run it in the same
notebook.
In my Ubuntu terminal, I ...
15
votes
1answer
282 views
Importing Zip files
Mathematica can import zipped files automatically, but when the content of the zip file it's another zipped file, I just get the list of files in the second zip:
...
48
votes
1answer
1k views
Programmatic formatting for Mathematica code - possible?
It seems to be both an interesting programming challenge and a very useful practical application to have a Mathematica program which would allow one to pretty-print Mathematica code, so that it is ...
11
votes
2answers
195 views
Listing subgraphs of G isomorphic to SubG
If I have an undirected graph G, how could I write a function in Mathematica to obtain a list of subgraphs of G that are isomorphic to some other undirected graph SubG?
I'd like to learn how to ...
15
votes
1answer
222 views
Are there advantages to using additional arguments of common functions rather than alternative ways of calculating with lists?
(Apologies for the long question title.)
One of the interesting, if sometimes confusing, things about Mathematica is that there is always more than one way to do things. Even intermediate users can ...
18
votes
2answers
943 views
Cycles of length N in a graph
If I have an undirected graph represented with an adjacency matrix, how can I find all the subgraphs which are a cycle of length N?
I don't really know the math nor the programming language well, so ...
4
votes
1answer
208 views
How to match two sets of data over 1 or more identifier, of unequal length?
Brief Question about how to best match lists with Criteria from Identifiers (thus for me: translate SQL to Mathematica ;) ).
If I have two sets:
...
8
votes
7answers
281 views
How to efficiently Append a result of an operation on each element of a list to itself
I'm looking for the best function to apply the product of the last two elements of sublist elements to each element:
Example:
...
32
votes
5answers
2k views
What does the construct f[x_] := f[x] = … mean?
This question mentions "x := x = trickery".
What does defining a function as f[x_] := f[x] = ... do and what is it good for?
13
votes
1answer
207 views
Count number of sublists with a total not greater than a given max
Suppose I have a list of positive integers:
data={1, 1, 2, 3, 3, 3, 5, 5, 5, 7, 7, 8, 8, 9, 10, 10, 12, 16, 23}
I want to count the number of subsets up to ...
7
votes
5answers
310 views
How to distinguish between lists and values?
I have a (hopefully small) problem with some numerical integration algorithm, more specifically I want to integrate the imaginary part of a complex valued function, e.g. ...
6
votes
5answers
407 views
Select/Delete with Sublist elements?
Probably easy and short question, I still didn't fully figure out how to easily select/delete sublists from a list. Example:
tt = {{2, 4}, {4, 8}}
I want to delete/select all the elements where ...
14
votes
3answers
663 views
Effective matrix power like algorithm
First example
Suppose you want to calculate the 6th power of some matrix $A$. The brute force attempt of doing this is considering
$$(((AA)A)A)A)A$$
which requires a total of 5 matrix ...
13
votes
2answers
312 views
Lexicographic ordering of strings in Mathematica
I recently realized that Mathematica seems incapable of comparing strings in the "normal" expected lexicographic order. Indeed, for some simulations, I need to process text directly, without fiddling ...
13
votes
5answers
962 views
Finding all elements within a certain range in a sorted list
Suppose we have a sorted list of values. Let's use list = Sort@RandomReal[1, 1000000]; for this example.
I need a fast function ...
6
votes
2answers
334 views
Very long Refine/Solve batch run - is my code broken, or just complicated?
So I'm trying to run some Mathematica code in batch mode from my university cluster. Specifically, I'm trying to find the equilibria of a system of ordinary differential equations. Inspired by the ...
30
votes
3answers
2k views
Metaprogramming in Mathematica
Being functional, and having no "quotation", i.e. essentially treating data and programs semantically same, Mathematica seems naturally capable of doing metaprogramming. But could anyone construct a ...
4
votes
3answers
160 views
Brackets in output make unable to use output to identify matrix element
I'm trying to write a code where each matrix element swaps with a randomly selected neighbor (cardinal directions only) to redistribute the matrix. So far I've been able to replace an element with its ...
10
votes
3answers
787 views
Programmatically generate packages from notebook files?
Currently, there are two ways I know of to generate a package (.m) file from a notebook file; one is to set the AutoGeneratedPackage option in the Option Inspector, ...
21
votes
6answers
870 views
Can this be written well, without loops?
Inspired by this question I would like to know if the following code can be written without explicit loops (For, While, etc.) in a clean, efficient and non-contrived way. I have been unable to do so.
...
24
votes
5answers
1k views
Are there any cases when For[] loops are reasonable?
To my mind the only reason for the existence of For[] loops in Mathematica is to allow new users with some experience in procedural programming languages to write ...
12
votes
6answers
558 views
Splitting up delimited data in lists
One task that I frequently find myself doing in Mathematica is splitting lists into lists of sublists, using specific elements to define the break-points. This is particularly useful with imported ...
3
votes
3answers
138 views
Simplifying already defined symbols
Consider the following code:
ClearAll[x, y]
x = y;
y = 2;
?x
?y
This will store $x$ being equal to the variable $y$, and $y$ having the value of $2$.
Now switch ...
12
votes
1answer
196 views
How to make a function like Set, but with a Block construct for the pattern names
How can we define a function that works like f[x_]=ComputeSomething[x] and treats x as a variable that does not have a value? ...
9
votes
2answers
267 views
Visualizing several long lists of numerical information to see relative frequency
Mathematica has very powerful visualization techniques. However, I'm at a loss at how to best make the following chart readable.
...
7
votes
3answers
267 views
RecurrenceTable not evaluated: mathematica just echoes input
Forgive the newbie question... I am trying to create a table of the following system, following the Stirling example in the documentation, but Mathematica just echoes my input. What am I doing wrong?
...
17
votes
3answers
742 views
Deploying Mathematica Content Online
I have the following program at home, which draws on a batch of pre-processed files (that are word frequency text files, compiled using ToLowerCase, ...
