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

1
vote
1answer
56 views

RandomVariate form a Specific Distribution with Restrictions

There are a lot of questions about generating random numbers form both uni-variate and bi-variate distributions. Mathematica has a lot of built in distributions for that purpose. However, I could not ...
3
votes
1answer
216 views

A bug fix to mathematica 10.0.1.0

There is a bug in the Wolfram Symbolic Transfer Protocol Development in C (Windows). I described it in the next answer by myself, from which I summarize: One cannot successfully compile the ...
20
votes
4answers
3k views

K-means clustering

In MATLAB, there is a command kmeans() that divides an array into $k$ clusters and calculates the centroid of each cluster. Is there any command in Mathematica to ...
4
votes
1answer
140 views

Association and rudimentary object-oriented programming

Doesn't Association provide us rudimentary object oriented programming via constructs like ...
314
votes
12answers
60k views

Where can I find examples of good Mathematica programming practice?

I consider myself a pretty good Mathematica programmer, but I'm always looking out for ways to either improve my way of doing things in Mathematica, or to see if there's something nifty that I haven't ...
1
vote
1answer
238 views

Millionaire quiz in mathematica

so here's the deal. I'm trying to write a millionare quiz in Mathematica. Now, this has been already done in other coding languages, specifically I've seen it done in C here: ...
0
votes
0answers
74 views

I cannot pass any script to `math`, in a command line

I want to run some lines of Mathematica code by a command-line call for math. As a test, I save just the following code in a file ...
13
votes
1answer
330 views

Evaluation indicator for a notebook

I have a GUI with a number of TabView and other Manipulate controls. Sometimes clicking from one tab view to the other can take ...
1
vote
3answers
65 views

GatherBy based on condition imposed on index variable

Excuse me. This might be user specific question but I need your help. I have an indexed data, where positive integers are used for indexing data. I am trying to group data putting condition on index ...
42
votes
0answers
635 views

Is this 30% slowdown in Mathematica 10 due to DownValues lookup time?

Running several numerical self-programmed packages I realized already some time ago that Mathematica 10 needed between 20 and 40% more time. I did already report this of course with a simple example ...
2
votes
1answer
837 views

Executing a .m file from the Linux command line

I have some code in a file named Demo-3.m, It can be evaluated in Mathematica 9. The final expression in the file is ...
4
votes
3answers
136 views

What is shorthand way of Reap list that may be empty because of zero Sow

Sometimes because of conditionals, you cannot guarantee that Sow will be called at least once. Reap[Sow[1]][[2,1]] results in ...
0
votes
1answer
48 views

How can I assign a vector unknown function to solve the ODE system?

I noted that using non-scalar variables is convenient when a system of differential equations is governed by a process that may be difficult to express symbolically. Taking a example from the tutorial ...
181
votes
19answers
19k views

What are the most common pitfalls awaiting new users?

As you may already know, Mathematica is a wonderful piece of software. However, it has a few characteristics that tend to confuse new (and sometimes not-so-new) users. That can be clearly seen from ...
1
vote
0answers
36 views

How to package a function for export?

I often post code for functions on Stack-Exchange, and then later find that they are unexecutable - that people can't use them because I mistakenly omitted some private functions or values that are ...
3
votes
0answers
62 views

How to assemble and disassemble a function into steps

I've watched many programmers (both novice and expert) write functions in Mathematica and have observed a common behavioral pattern. This pattern has many elements that would be automated and thus ...
8
votes
2answers
300 views

Get rational and irrational parts

Consider an expression of the form $a + b \sqrt{2}$, where $a,b \in \mathbb{Q}$. How can I extract $b$ (or equivalently $a$) from this expression?
18
votes
0answers
243 views

How to perform document classification (i.e. extracting topics from text)?

I need to build a multi-label text classifier. There are a bunch of techniques for doing this, and it's called automatic document classification. I'm trying to use the EM technique (expectation ...
53
votes
11answers
9k views

How to check if a 2D point is in a polygon?

Background: I use code from An Efficient Test For A Point To Be In A Convex Polygon Wolfram Demonstration to check if a point ( mouse pointer ) is in a ( convex ) polygon. Clearly this code fails for ...
2
votes
1answer
94 views

What is the correct way to break a long expression across multiple lines?

This is probably a very simple question and I am probably not using the right keywords in my search; but I have a long expression that I would like to break across multiple lines to improve ...
0
votes
1answer
39 views

Elegant ways of solving this file batching task

There are a batch of data files in a folder (dir = "D:\\Documents\\datadir"), each of which consists of a list of data items, one per line. Each data item is a pair ...
0
votes
1answer
114 views

Is it possible to build/simulate/extract data from a System Modeler model completely inside a notebook using the MathModelica language syntax?

See examples in: http://www.mathematica-journal.com/issue/v10i1/contents/MathModelicaPaper/MathModelicaPaper_2.html It seems to be ruled out by the statement (Chapter 2 intro): "The subset of the ...
0
votes
0answers
44 views

Question about treillis structures

I have difficulties finding a command to plot multiple segments connected in nodes in Mathematica. Ultimately, my goal is to modelise something like this by entering nodes coordinates. Also, the ...
-1
votes
1answer
35 views

Finding the number that gives remainder equal to 0 [closed]

Hi i'm not english so I'll try to explain this as good as I can . If we have for example 250 : 5 = 50 , remainder 0 let's say I don't know the number i'm going to divide (because it is generated ...
2
votes
0answers
71 views

Mod[1.2, 0.2] is not equal to zero

I'm new to mathematica but this doesn't seem to make any sence In[1]:=Mod[1.2, 0.2] Out[1]=0.2 Why is it not zero? Edit: This is what I wanted to do: ...
0
votes
0answers
78 views

Solving for a Transfer Matrix

I am currently working on a program that will solve a transfer matrix and extract several elements. Particularly, the transfer matrix is characterized by four elements in a ...
2
votes
1answer
99 views

How to program a clickPanel for chemical elements (like a periodic table)?

The command ColorData["Atoms", "Panel"] indicates RGBColors of "Atoms" by clicking a panel. As I don't want to reinvent the wheel, I asked myself: How to ...
4
votes
0answers
77 views

Implement a piecewise function elegantly and efficiently

Given that $$ g(x)= \begin{cases} max(x_{min},x-m) & 0.5x \leq max(x_{min},x-m) \\ 0.5x & max(x_{min},x-m)<0.5x<min(1,x+m) \\ min(1,x+m) & min(1,x+m)\leq 0.5x \end{cases}$$ ...
4
votes
1answer
110 views

How to use RunProcess to import external function?

I have a functions written in an external language called GO. How can I quickly load them into the kernel for use as Mathematica functions? The two ways I've tried are with MathLink and LibraryLink ...
1
vote
1answer
180 views

How can I call the R-programming packages in my Mathematica?

I am trying to call the R-programming package called highfrequency in my Mathematica so that I can use its function rCov. I ...
1
vote
2answers
66 views

How do you apply an algorithm individually to an array with many graphs

I have and array with many graphs to which I want to apply an algorithm (below) to each graph in an array that contains all my graphs. ...
15
votes
1answer
291 views

Why does this line increase the consumed memory?

Probably this is a simple question, however I really don't understand this behavior in Mathematica. It particularly leads to a memory leak in a huge program I have to do processing of 3D images. I ...
5
votes
1answer
88 views

How to implement kvo in Mathematica 10? [duplicate]

Key value coding and observing is something that other languages do well, like objective-c for instance. What's the best way to achieve something similar in Mathematica (if at all possible)? That is, ...
1
vote
2answers
44 views

How to get the outermost command evaluated first?

I have an expression like ToString[Stuff[...]]. I plan on having the Stuff[...] be manipulated as a string according to the output of other parts of the program. The problem is that Mathematica tries ...
1
vote
1answer
109 views

For loops in Mathematica [duplicate]

I'm actually the first time using loops in Mathematica. For example, I have: For[i = 1, i <= n, i++, For[j = 1, j <= n, j++, ....] How it is possible now ...
5
votes
1answer
277 views

How to solve this probability symbolically or numerically?

I am trying to calculate the following probability $$\mathbb{P} \big(\sum_{i=1}^{m} (A_i + S_i) \le L < \sum_{i=1}^{m+1} (A_i + S_i) \big)$$ where, $$A_i \sim \exp(\lambda), \quad S_i \sim ...
22
votes
7answers
615 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 ...
6
votes
3answers
238 views

Calculating integral by Romberg Algorithm

In my "Numeric Anasyis" course, I learned the Romberg Alogrithm to calculate the numerical integral. The Romberg Alogrithm as shown below: $$ ...
0
votes
1answer
269 views

Implementing Rayleigh criterion method for finding the resolution of an optical system

I want to use the Rayleigh criterion to find the resolution of an optical system. I have two functions f1 = Sinc[x]^2 and ...
0
votes
0answers
44 views

Find the maximum/ minimum of the function f(x,y,z) = x+2y+3z on the curve of intersection of the plane x-y+z = 1 and the cylinder x^2+y^2 = 1 [duplicate]

How to write the program for: Find the maximum/ minimum of the function f(x,y,z) = x+2y+3z on the curve of intersection of the plane ...
0
votes
0answers
24 views

Calculate number of probabilities [migrated]

I want to calculate a number of probabilities of a+b+c=1 For a vary from 0 to 1 with an incrementing of 0.1 For ...
6
votes
1answer
307 views

Plotting a moving ball for projectile motion with animate

Here is my code for plotting an orbit. What I want to do is have the "planet" shown as it orbits in the simulation. Can anyone help with this? ...
0
votes
1answer
47 views

Symbol appears in multiple contexts when load two packages with unique function names

I have two packages "packageA" and "packageB". The function names across packageA and packageB are unique. No two functions have the same name. When I issue the ...
0
votes
1answer
45 views

Programming a series involving alternative log expressions

This is likely my error but I cannot see it. I define a vector: x1 = {{{2, 2}}, {{2, 1}, {3, 1}}, {{3, 2}}, {{2, 1}, {5, 1}}} If x1[[ i, 1,2]] is 2, I would like to find $(\log$ x1[[ i,1,1]]$^2$). ...
8
votes
2answers
293 views

Recursive partitioning: Is there a better way to do this?

Consider the following function, which recursively splits a list into equal length sublists (the length of the list is assumed to be a power of the second argument): ...
5
votes
0answers
143 views

How to download and combine S&P 500 stock prices

I want to download historical S&P 500 stock prices and combined them into one big file, having only one column of date referencing all stocks. Here is what I have do so far. ...
4
votes
2answers
238 views

Approximate value for the area between the curve

I've got this task: Use Mathematica to obtain an approximate value for the area between the curve $y=1/4$ and the x-axis over the interval $[1,2]$ with $50$ subintervals using the left ...
44
votes
2answers
2k views

Once more on object orientation in Mathematica: does it have to be so hard?

Recently I came across a set of problems which would be solved most easily within an object-oriented approach. I first attempted to solve them by other means, but found the complexity of the code ...
0
votes
1answer
94 views

Optimizing Chi-square distance between two distribution

I want to program the chi-square between two distribution Xu and Vu with length N. This function is defined as below: Xu and Vu are real and positive. ...
4
votes
1answer
82 views

Slowdown of Flat attributed function

I was trying to write an associative function f using the attribute Flat. ...