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.
-1
votes
0answers
53 views
7
votes
4answers
286 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
512 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
6answers
295 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
187 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
199 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 ...
4
votes
2answers
66 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
...
5
votes
1answer
126 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:
...
0
votes
0answers
67 views
2
votes
1answer
97 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
53 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
152 views
How to compute the Lovász number for the given graph in Mathematica?
Here is a graph whose adjacency matrix is
...
6
votes
1answer
71 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 ...
4
votes
0answers
133 views
When should I begin a new line for more readable code [closed]
After a quick coding, I often try a lot of line feeds patterns on it.
And no matter how much I do, there's seems to be no end in sight.
Front end automatically indents the code, but I cannot fully ...
33
votes
2answers
694 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 ...
0
votes
0answers
37 views
Packingof Spheres in 3D [migrated]
I am looking to find out the size of the largest sphere , that can fit in the voids created by packing spheres ( hcp) of radius R.
2
votes
2answers
86 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
172 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
...
1
vote
0answers
41 views
How to Control Flow on Error/Warning Messages [closed]
I frequently experience trouble with FindRoot in returning a valid solution to systems of equations in part due to the difficulty in specifying good starting points for the parameters and so am using ...
4
votes
1answer
151 views
Using Mathematica to 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
226 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...
...
12
votes
2answers
208 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
179 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
153 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
356 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
187 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 ...
15
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
47 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
0answers
116 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
91 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
293 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
56 views
Inversion algorithm for Laplace Domain
I'm trying to invert an equation in the Laplace domain using Stehfest algorithm. It works very fine, but since I'm trying to represent a sinusoidal behavior I need a different algorithm that makes use ...
1
vote
2answers
85 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
60 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
58 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 ...
21
votes
2answers
588 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
114 views
12
votes
1answer
200 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 ...
2
votes
1answer
84 views
Are there functions or packages in Mathematica to generate 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 = Lex, Colex, Revlex, etc.
36
votes
4answers
791 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 ...
0
votes
0answers
82 views
Pearl's Message Passing Algorithm
Has anyone tried to implement the Message Passing algorithm using Mathematica's Graph/Combinatorica features?
2
votes
0answers
82 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
171 views
4
votes
1answer
62 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
178 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
83 views
0
votes
3answers
89 views
4
votes
3answers
172 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
32 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 ...
0
votes
3answers
110 views
How to create a distributive function in Mathematica
More specifically, I would like to define an algebra such that:
f[x1 op1 x2 op2 x3,...]=f[x1] op1 f[x2] op2 f[x3]...
I know of the Distribute function but the problem I get into is that the function ...