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

8
votes
3answers
241 views

Pairwise distances of points and classification using GraphDistanceMatrix

The challenge Given $n$ points in $\mathbf{R}^3$ and a cutoff distance, $d$, I would like to determine all pairs of points that are separated by less than the given cutoff distance, $d$, and all ...
10
votes
2answers
507 views

Finding all possible solutions to a math mind game

In the example below the letters shall be replaced by numbers. The numbers are from 1 to 9, and each letter has the same number ...
15
votes
2answers
425 views

Schedule programing problem with integer linear programming

I know how to model this question on paper, but I don't know how doing it in Mathematica. Imagine , there is a university student who want to choose courses for this semester and he got the ...
1
vote
2answers
167 views

Plotting in the “Manipulate” function - simple coding

Hello Mathematica community, my name is Preston! I have a real quick question that should be easy to answer. Alright, so for a research project that I'm involved it at my university, I need to ...
4
votes
2answers
263 views

Can Mathematica functions have static variables?

In C, one can have variables that are declared static. These are not re-initialized every single time a function is called. ...
4
votes
2answers
146 views

How to test whether an expression is a valid variable?

I would like to test whether an argument is a valid variable for functions like Solve and DSolve. For instance several ...
7
votes
1answer
296 views

How to visualize the structure of this dynamic programming code

I am studying Mr. Wizard's answer to this question which tries to identify elementary circles in a graph. That answer uses dynamic programming technique to simplify the code. The underlying guideline ...
1
vote
2answers
176 views

Making nice code: choosing one of several procedures

Happy new year mathematica gurus of stack exchange! As I see it one of the major obstacles in getting decent at programming mathematica is that, not only do you need to learn how certain commands ...
9
votes
2answers
254 views

Create a new infix operator

It is well known that the infix operator @@ is used as a short form of Apply. For example, both ...
1
vote
0answers
83 views

Compiled version of user-defined function

I have a function for parameter estimation with these codes: ...
1
vote
4answers
159 views

Efficient replacement of conditional nested Do over elements of a matrix

I have two matrices like these: m1=Table[Random[],{i,10},{j,10}] m2=Table[Random[],{i,10},{j,10}] I want to create a new matrix in which its main diagonal ...
1
vote
2answers
153 views

Changing Default Color for Titles, Sections, Subsections, etc., and Items (bullet color) [duplicate]

I'm new to Mathematica 9, and I assume there must be an easy fix for my problem, but I have not been able to find any yet. Essentially, I want the default color for the text of all of my titles, ...
9
votes
2answers
479 views

Memory leak issues

I am working with noncommuting objects and basically I am using Mathematica to sort large expressions by normal ordering and simplifying them. To this end, I first create an object called ...
2
votes
1answer
105 views

Piecewise function with user-input interval

I would like define a function g[data,bins,x] which is a piecewise constant function taking value data[[i]] in the range ...
2
votes
1answer
115 views

Need help looping a notebook evaluation

I'm a new user and would like to find the minimum value of soln1[30] obtained after repeating the following evaluation (below) for 1000 cycles (for instance). I can obtain sequential values of ...
9
votes
1answer
467 views

Mathematica Application in Real Life

As an 'user'-level in Mathematica, I mainly use built-in Mathematica function and benefit Mathematica as a mathematical tool-box for my specific scientific problem. My level is not yet ...
1
vote
0answers
97 views

Sum slowing down computations

I am trying to compute higher order Casimir invariants for some representations of certain groups. I can get the quadratic, cubic and quartic but beyond that things are taking too long to the point I ...
2
votes
1answer
351 views

Calculate mean square displacements for different particles from a excel file

I have the particle movement data in the following format: ...
0
votes
3answers
174 views

Determinant of a random matrix consisting of integers

I am trying to implement a function that finds the determinant of a random matrix consisting of integers. This is the code I have written so far, but I am stuck. Suggestions ? I want to find a ...
0
votes
0answers
113 views

Improving Function with Loop Structure

I'd like to write a function that would perform a loop, I guess Do or While should be helpful here. I've simplified the problem, ...
8
votes
3answers
288 views

Find the best parallel lines to a given data set

I have a set of data in an uptrend. I would like to find two BEST parallel lines to contain all the data but I don't know how to approach it, anyone can help ? Thanks.
1
vote
2answers
190 views

What's the best way to generate all the upper triangular matrix whose singular values are given?

For example, given $\lambda_1 = 1, \lambda_2 = 2, \lambda_3 = 3$, what's the best way to generate all the upper triangular matrix ($3\times 3$) whose singular values are $\lambda_i$? Note:Given a ...
4
votes
0answers
169 views

Call Haskell from Mathematica and return the results inside Mathematica

I would like to learn Haskell using Mathematica's environment. I know there is the RLink protocol. Is there a similar protocol for Haskell?
2
votes
1answer
69 views

Evaluate only one from a set of procedures

Let's say I've got a list of procedures: {Print[1];, Print[2];, Print[3];} And I want to only evaluate 3rd one: ...
4
votes
1answer
134 views

Selecting expressions from a sequence that are not subexpressions of earlier elements

I have a While loop that generates and prints an expression on each iteration (there can be thousands of iterations). UPDATE: Some expressions are generated in ...
3
votes
1answer
81 views

Assign clipboard object in mathematica when print screen is pressed (windows)

Mathematica runs behind, while another window is selected. When I press print screen, I want mathematica too detect this and call a function with the picture as argument. ...
3
votes
0answers
41 views

Is it possible to enclose code into collapsible/expandable regions? [duplicate]

In some programming languages (e.g. C#, Visual Basic) there are language constructs that have no effect on the execution of code whatsoever, but exist solely to allow grouping fragments of code into ...
8
votes
3answers
182 views
1
vote
2answers
105 views

How to return matching sublists or empty list

How to return matching sublists or empty list m1 = {{1, 0.5}, {2, 0.4}, {3, 0.1}}; m2 = {{1, 0.5}, {0, 0.4}, {0, 0.1}}; Commonest[Join[m1, m2]] out: ...
4
votes
1answer
342 views

How to implement Kemeny-Young method? (rank aggregation problem)

Prehistory I am trying to make some statistical analysis of some experimental data, arises from measurements made ​​on an ordinal scale. I faced with the problem of rank aggregation: to get from ...
4
votes
2answers
168 views

How to insert / merge a list into another list?

Background: I am working on a 2D geometry algorithm where I need to insert a list of 2D points into another list of 2D points. Somewhat to my surprise I found no Mathematica function capable of doing ...
5
votes
2answers
148 views

Remove versus ClearAll

What is the difference between Remove and ClearAll? I thought that "not being recognized by Mathematica", which is the effect ...
-4
votes
1answer
110 views

Programming Multipolar Expansions in Spherical Tensors

We have that a density matrix can be written in a basis of spherical tensors: $\rho =\sum _{K=0}^S\sum_{q=-K}^K \rho _{\text{Kq}}^{(S)} T_{\text{Kq}}^{(S)}$ for example for a 3x3 matrix, $S=1$, ...
0
votes
0answers
143 views

How minimize a function involving abs $(|\ldots|)$?

I am trying to decide the best way to solve this: Minimise $Y = | K + ax^2 + bxy + cy^2 |$, with decision variables $x$ and $y$ and $a > 0$. I also have a number of convex and affine ...
9
votes
1answer
395 views

Workflow in the Mathematica and Workbench IDE

After visiting one of the Wolfram presentations, I started writing code for my number theory research project in Mathematica because I found it to be faster than Sage. I downloaded Workbench and ...
5
votes
7answers
375 views

How to find the longest matching sublists in a list

Given a list of 2D points, i.e. {p1, p2, ..., pn} the list may contain equal points, i.e. pi = pj. In this case it may also be true that a sublist of ...
3
votes
2answers
183 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
194 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
119 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
69 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
572 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
354 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
155 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
255 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
80 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
499 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
387 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
635 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
558 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 = ...