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.

learn more… | top users | synonyms

7
votes
1answer
765 views

Handling failed FindRoot calls

I want to handle FindRoot calls which did not converge (e.g "thrown" error message FindRoot::cvmit) ...
17
votes
2answers
777 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 ...
5
votes
3answers
1k 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
385 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: ...
77
votes
1answer
2k 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 ...
12
votes
2answers
329 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 ...
16
votes
1answer
327 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 ...
21
votes
2answers
2k 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
337 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: ...
9
votes
7answers
379 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: ...
51
votes
5answers
3k 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?
14
votes
1answer
298 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 ...
8
votes
5answers
358 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. ...
9
votes
5answers
1k 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 ...
15
votes
3answers
1k 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 ...
15
votes
2answers
601 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
2k 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
465 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 ...
37
votes
3answers
3k 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
263 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 ...
11
votes
3answers
2k 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
1k 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. ...
28
votes
5answers
2k 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 ...
13
votes
6answers
929 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
161 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 ...
14
votes
1answer
240 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? ...
10
votes
2answers
328 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
403 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? ...
19
votes
3answers
1k 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, ...
8
votes
2answers
1k views

ParallelTable and Table do not give same result

I have a question regarding the use of Parallelize. In the help one can read that “Parallelize[Table[expr,iter, …]] (which is ...
11
votes
3answers
374 views

Does AbsoluteTiming slow the evaluation time?

I've started using AbsoluteTiming to discriminate between similar function constructs, but I sometimes feel like it takes substantially more time to evaluate a ...
15
votes
5answers
455 views

Alternative ways to implement a triangular recursion

Triangular recursions are a class of algorithms that frequently turn up in computational mathematics. These recursions are expressible in the general form ...
9
votes
1answer
174 views

How can I make CurrentValue for font characteristics pick up the font of the output not the input cell?

The command CurrentValue (documentation) allows you to access various properties of fonts, such as x-height, line height, and various measures of width such as ...
12
votes
9answers
445 views

Generating a matrix using sublists A and B n times

I want to write a function that generates a square matrix from sublists. My sublists are a = Range[0, x, 0.5]; b = Range[0.25, x + 0.25, 0.5]; Suppose x=2, then I ...
3
votes
1answer
1k views

Embed CDF into an Apple iBook?

Edit From the comments I received and verifying the information about CDF at wolfram.com, it appears that it is presently impossible to embed CDF into iBooks. WRI does not appear to be happy with this ...
7
votes
3answers
481 views

Threading a compiled function over multiple arguments of different lengths

I am interested in threading a compiled function over multiple argument lists of different lengths. I will explain this in two steps: Suppose f is some function. Then one can define another ...
30
votes
4answers
2k views

Can Mathematica be regarded as a software prototyping environment?

I am an undergraduate student, starting to use Mathematica to model simple physical chemistry problems, as a prelude to a summer internship in a computational/theoretical chemistry group. The solution ...
28
votes
3answers
5k views

ListPlot with each point a different color and a legend bar

I would like to generate a ListPlot with the color for each point in the plot corresponding to a particular value (not associated with the position in the plot). I'd then like to add a legend ...
20
votes
3answers
1k views

Downloading files without using Import

A quick question, but which I don't believe has been asked here or at SO. Does Mathematica have a simple way to just download a file from the web? i.e. if I have a list of PDF links (~ 2,000), can I ...
5
votes
1answer
261 views

Alternative to overloading Set

In the ideas shared in my answer to this post Struct equivalent in Mathematica? at the end I propose a solution that changes the way Set works in a particular case that can be expressed in plain words ...
3
votes
2answers
184 views

Cleaning up a List of HTML Data to Render Usable Information

I am using Mathematica to scrape information from webpages. To get file information, I am gathering plain text that is enclosed by <tr> and ...
0
votes
1answer
137 views

FE`symbols that remain in notebook internals even after removing them and SaveDefinitions->True issue

How does one clear the notebook from symbols that remain in it after an edit session?
13
votes
2answers
838 views

Suppressing negative roots in Mathematica

Problem Using Mathematica's Solve operator can sometimes lead to an output involving a positive and negative root (say when solving for a variable such as ...
4
votes
2answers
224 views

How to Text justify string that includes SubScriptBox in it?

I am using Grid to document a demo I have. i.e. I am writing the documentation of the demo as a help option, which will display information when clicked on. Everything is working well. I am using the ...
77
votes
7answers
7k views

Struct equivalent in Mathematica?

I really miss having something like a struct in Mathematica. I know of (and regularly use) a couple of programming techniques which feel like a ...
9
votes
2answers
145 views

How to make TraditionalForm display the same in the Manipulate output as in a notebook?

I noticed that TraditionalForm does not display as nice in the Manipulate output as in normal notebook. Is there a way to make it look the same? Here is an example, ...
3
votes
1answer
531 views

How to make GraphicsGrid with content tight inside it?

I am trying to use GraphicsGrid to typeset some labels for my plots. I found the labels look little better when I use GrapicsGrid vs Grid. The labels have equations in them and not just plain labels. ...
9
votes
3answers
493 views

How can I speed up SQL queries in Mathematica?

I have an application that queries a MySQL database many times for data. At the moment, Mathematica and MySQL are running on the same box and yet it takes over 1.6 seconds for the query to run and ...
45
votes
6answers
2k views

Functions vs. patterns

Every time I define a new function, I wonder which construct I should use: true functions obtained by using Function, or rule-based syntax. For example, these are ...
6
votes
3answers
302 views

How to eliminate the need to double evaluate a Manipulate so that a Module in its Initialization section works?

Using V 8.04 on windows. I think this issue might be related to the fact that Mathematica reads main body the Manipulate before evaluating or running the Initialization option. Or, might be ...