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

3
votes
2answers
184 views

My code for solving n queens problem Compile Failed

I want to make my code for solving n-queens problem faster, but it seems that MapIndexed can't be compiled. Mathematica return CompiledFunction::cfse: Compiled ...
1
vote
1answer
198 views

Gram Schmidt inner and outer products

I know the Gram-Schmidt orthogonalization generates an orthonormal basis from an arbitrary basis. I need help with: Write a program that inputs a list $\{b_1,\dotsc,b_n\}$ of linearly independent ...
9
votes
1answer
121 views

How to get control over DepthFirstScan?

Is there a way to conditionally take control the flow during a depth-first scan against a TreeGraph? I have a tree graph whose nodes are constructed as object, and there's a method ...
1
vote
0answers
72 views

Benefits of using Intellij IDEA as Mathematica IDE? [duplicate]

I use Intellij IDEA for Java programming, but have not really considered it for Mathematica. I saw a video with a Wolfram programmer who was describing it as "essential" for him, but from what I could ...
34
votes
1answer
588 views

Export Code to Pseudocode?

I'm writing a paper and all my code is in Mathematica. The snippets are mostly simple, and consist of loops, maps, variables, and conditionals. I'd like add it to my paper, however, not many academic ...
2
votes
1answer
366 views

Is there anything like a C pointer (or returning a reference) in $Mathematica$?

I want to do in Mathematica what the following does in C. ...
2
votes
2answers
159 views

How to replace data in a list (tree) in a functional Mathematica way?

I have list containing 2D points in various groupings, i.e. points, lists of points (figures), lists of lists of points (patterns) and finally a list of patterns. The list also contains other data, ...
4
votes
2answers
260 views

How and when to use Evaluate?

A month ago, I used Mathematica to solve my experiment of the Test Technology of Mechanics Engineering. The exeriment is mainly about the Fourier Transform and verify the validity of the theory of ...
0
votes
0answers
84 views

Programming a Meijer G related result

I am not that new in Mathematica, but I do not remember I have ever used it for programming (like we may do in Java). I have an important result given here by this integral: $$ \int_0^{+\infty} ...
11
votes
3answers
526 views

How can I type-check the arguments of a Mathematica function?

(For educational purposes) I defined the following functions: Translation (in $\mathbb{R}^2$): trM[vec_] := {{1, 0, vec[[1]]}, {0, 1, vec[[2]]}, {0, 0, 1}} ...
9
votes
4answers
392 views

When should I use Apply (or Function) and when @@ (or &)?

This is a rather general question, which I fail to answer myself. I guess it is mainly due to my insufficient knowledge of the precise terms. If I understand correctly, the following are equivalent: ...
12
votes
5answers
638 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
565 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
215 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 = ...
3
votes
2answers
278 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
134 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
621 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
231 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
79 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
224 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
154 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 ...
36
votes
2answers
951 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
124 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
324 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
273 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, ...
8
votes
4answers
271 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... ...
14
votes
2answers
397 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
861 views

Numerical differentiation methods

Is it possible to write a code in Mathematica that implements various differentiation methods (like forward, central, extrapolated, etc.)?
17
votes
1answer
182 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
486 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
249 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 ...
16
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
53 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
195 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
2answers
234 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
573 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
149 views
1
vote
2answers
124 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
103 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
83 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 ...
26
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
197 views

A problem on generating convex hull

For example, I typed the following: ...
17
votes
1answer
346 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
164 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.
41
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 ...
2
votes
0answers
104 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
183 views
4
votes
1answer
70 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
245 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
93 views