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

13
votes
5answers
694 views

How to correct my code for solving the Josephus problem?

Problem Decription Recently, I have been reading the book Schaum's Outline of Mathematica (2nd Edition), where I encounted the problem: Flavius Josephus was a Jewish historian of the first ...
5
votes
7answers
665 views

Better code to find Narcissistic number

My code finding Narcissistic numbers is not that slow, but it's not in functional style and lacks flexibility: if $n \neq 7$, I have to rewrite my code. Could you give some good advice? ...
0
votes
1answer
220 views

How to combine two usages of # into one and speed up the code?

I have a function num with two variables, matf and matg. How can I combine the last two lines of code below into one? The code is aimed to find out the maxiaml count bel when we give each $i3 = ...
4
votes
3answers
379 views

Improve the performance of solutions to Project Euler (#14)

Problem I have following code, when n<=10^5 it's not slow, but n>2*10^5 it's became very slow. I think maybe some temp value greater than 2^31-1, so make compile invalid. Could you give any idea ...
5
votes
2answers
182 views

ParallelMap and Sow/Reap - not behaving as expected? [duplicate]

Is ParallelMap and Parallelize@Map expected to work with Sow and Reap construct. This is a rather brief toy code to find all Primes just to force the use of Sow/Reap inside Map ...
9
votes
2answers
849 views

Implementation of smoothing splines function

I have some problems by writing a module for spline smoothing. Actually i am trying for about two weeks. My listing is here: ...
2
votes
1answer
268 views

How does FindInstance work?

I was asked a question one day: find an integer n such that the first five digits of 5*7^n is 12345. It's very nice that I can quickly write code like this: ...
2
votes
1answer
86 views

Problem with Function and Postfix [closed]

My Methods: Style["Mathematica", Pink] "Mathematica" // Style[#, Pink] Style@("Mathematica", Pink) How to revise it?Thanks!
3
votes
0answers
238 views

How to compute the Lovász number for the given graph in Mathematica?

Here is a graph whose adjacency matrix is ...
7
votes
1answer
171 views

Raise systemwide keypress event

Is there a way to raise system-wide "keypress" events programmatically from within Mathematica? Say "Ctrl+C" or "Ctrl+V", which all other programs can recognize? This is an attempt to gain simple ...
37
votes
2answers
985 views

Reconstruct text pages cut by shredder

Q1: I have a page of printed text file, but it was cut by a paper shredder (from top to bottom). You can download all the paper fragment here. There are some examples of them. How can I reconstruct ...
2
votes
2answers
151 views

How to unprotect GeneratingFunction

I'm trying to add custom downvalues for GeneratingFunction, and to do so I thought I just had to Unprotect it. However, that ...
4
votes
4answers
357 views

How to find the maximum of a function on a set of discrete points?

How to find the maximum of a function on a set of discrete points? For example, what is the best way to find the maximum of ...
7
votes
1answer
296 views

Find a word's linguistic pronunciation

Is there a way to get a word's linguistic pronunciation given the word as a string? I would like a function LingusticPronunciation such that, for example, ...
10
votes
4answers
282 views

Searching linked lists that contain lists?

Following the advice I've read here and other sites, I've been trying to use the Mathematica equivalent of a linked lists... ...
15
votes
2answers
468 views

Efficient priority queues?

I'm trying to figure out how to transfer the concept of a priority queue to the functional world. Searches have turned up some implementations that use Append and other expense list copying ...
4
votes
4answers
1k views

Numerical differentiation methods

Is it possible to write a code in Mathematica that implements various differentiation methods (like forward, central, extrapolated, etc.)?
19
votes
1answer
193 views

How to get Cases to wrap results in an arbitrary head?

A number of functions provide for results to be wrapped in an arbitrary head. This is very important in cases where the results should not be evaluated. Take ...
10
votes
5answers
503 views

How to find first list element that differs from average of N previous elements by more than a given amount?

Given a list of real numbers, I need to write a function that returns the value of the first element in the list that differs from the mean of the previous $N$ elements by greater than $P$ standard ...
6
votes
2answers
260 views

Clustering of peaks in 2D spectra

I'm having some problems clustering data in a 2D spectra and will really appreciate some help. I've been playing around with the FindClusters function but wasn't able to tune it appropriately. Maybe ...
17
votes
12answers
1k views

Pick elements of largest absolute value

For example, given list = {{1, -3, -5}, {2, 1, 6}, {0, 2, 4}, {-9, 2, 6}} should return: {-5, 6, 4, -9} Updated I found ...
3
votes
0answers
55 views

How can I append Mathematica variables to an output file preserving both names and values? [duplicate]

I have a Mathematica program that calculates a series of large vectors that I need to save for future reference (in addition to some scalar values). I want to save all these to a file, tagged by their ...
1
vote
1answer
209 views

What's wrong with my code for finding primes?

From here I found a fast method to make prime list, the python version works well, but my Mathematica version does not. Obviously ,169 is not a prime number. What's ...
3
votes
3answers
333 views

Need help with exception handling

I have read the docs for Catch, Throw, and friends multiple times, and I confess that I find them utterly incomprehensible... ...
7
votes
3answers
753 views

Programming Mathematica in plaintext, i.e. no formatting, non-notebook

I'm new to Mathematica and I've just begun reading Paul Wellin's Programming in Mathematica. The book, like many other books and online tutorials, revolves around using the notebook, with all its ...
0
votes
0answers
173 views
1
vote
2answers
132 views

How to automatically put the part of a variable name after the first letter in symbolized subscripts?

So here is what I have thought: Use ToString and StringLength in order to get the number of characters in the variable name. If it is bigger than one, change variable name by putting everything but ...
6
votes
2answers
131 views

Consecutive Print to target same output Cell

Is it possible to conveniently direct the output of consecutive Print statements to the same output without actually accumulating the output in a temporary string/variable. This would be useful in ...
0
votes
0answers
87 views

Implementing a back button (in CDF)

I have a rather large notebook, which contains a lot of hyperlinks to different parts of the notebook. Is it possible to make a back button (maybe as a docked cell button?) so that when a reader ...
28
votes
2answers
1k views

when is f@g not the same as f[g]?

I have always thought that f@g will give the same result as f[g] in all cases, and it is just a matter of style which one to use ...
0
votes
1answer
212 views

A problem on generating convex hull

For example, I typed the following: ...
18
votes
1answer
365 views

Underlying Algorithms for List Manipulation Functions

Does anyone know, or know of a link for the underlying algorithms used for list manipulation functions? Specifically: Union with and without the ...
5
votes
1answer
172 views

Are there functions or packages in Mathematica to sort subsets in lex order, colex, revlex, etc.?

I am looking for non-canonical sorts of lists. A function that would give: Sort[X, "order type"] With "order type" being colex, revlex, etc.
42
votes
4answers
1k views

Automatic e-mail/text message when a calculation has finished?

Sometimes Mathematica takes quite a while to finish calculations and it would be convenient to be able to move away from the computer while it is working. I was wondering if there is any "clever" hack ...
3
votes
0answers
120 views

Force Dispatch to generate dispatch tables

Dispatch creates hash tables only for lists of length larger than 3. Is there any way to force MMA to create hash tables even for lists of length smaller than 4? ...
2
votes
1answer
186 views
4
votes
1answer
74 views

Constructing an Array from typed expression

I want to produce an array from expressions given by test[one][1] = 3; . . test[one][5] = 7; etc. Normally would use ...
2
votes
1answer
264 views

Call Functions From File Without Modifying Context (Sandbox)

Let's say I have file named test.m containing test[arg1_] := ( Print[arg1];) What is the best technique for calling test like ...
1
vote
1answer
95 views
0
votes
3answers
119 views

Why is my funtion is wrong? [duplicate]

My aim is to know whether the data has same element. ...
4
votes
3answers
221 views

Better code to solve number puzzle [duplicate]

Question: (34 O 5 O 6 O 8 O 9 O 1) O 2=2008 Replace the Symbol of "O" to "+" or "-" or "*",and make the expression is true. My solution: ...
1
vote
0answers
34 views

How can I convert a list to an expression? [duplicate]

For example, I have the following list: {x1,op1,x2,op2,x3,op3,...} And I would like to convert the list to the following expression: x1 op1 x2 op2 x3 op3 ...
7
votes
1answer
162 views

The Flat Attribute, Unevaluated and the Evaluation Process

To make this question easy to follow let's give some symbols a couple attributes. Attributes[f] = {Flat}; Attributes[s] = {Orderless}; Now consider the ...
28
votes
3answers
1k views

Getting serious about Mathematica programming

I'm at a point in my Mathematica journey where I need to be a little bit more strategic about my coding. Presently, I have a bunch of notebook files organized using the filesystem of the OS and ...
14
votes
0answers
294 views

What Mathematica features and idioms have n^2 complexity or worse

Here are a few: AppendTo[data, elem], Sort[data, test], in some cases use of ___ (as ...
2
votes
2answers
404 views

Matrices and polynomials: MMA 8 vs. 9?

I want a function to take a polynomial from a list, plug a certain matrix T into that polynomial, and return the answer as another matrix. My program uses the applyPoly function suggested by a user in ...
2
votes
1answer
188 views

Differential operator involving permutations

I would like to construct an operator in Mathematica of the following form, $$D_i=\partial_i +\sum_{i \neq j}^n\frac{1}{x_i-x_j}p_{ij}, \quad i=1,...,n$$ where $p_{ij}$ is a (symmetric) permutation ...
1
vote
1answer
215 views

Pseudo-code for rules of probability?

I am having a hard time in Stats understanding some basic rules of probability and set-theory, namely $P(A|B)$, $P(A\ \textrm{and}\ B)$ and $P(A\ \textrm{or}\ B)$ where $A$ and $B$ depend on each ...
5
votes
2answers
204 views

How to group lists that own one common element?

I have produced a list of sub-lists by the following: ...
0
votes
1answer
110 views

Conditioning on group of functions

I have two sets of functions which I want to implement them in a single function with an option indicating which set of functions should be used. Suppose that I have two lists like:(updated example) ...