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

55
votes
2answers
2k views

Flatten command: matrix as second argument

One thing I could never wrap my head around is how Flatten works when provided with a matrix as the second argument, and the Mathematica help isn't particularly ...
4
votes
2answers
302 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 ...
0
votes
0answers
58 views

Selling Code to Mathematica [on hold]

Lets pretend that evaluating & graphing elliptic functions was not part of mathematica (I have no idea if these are possible in mathematica, just a hypothetical here). If I made up some code ...
91
votes
3answers
3k views

Performance tuning in Mathematica?

What performance tuning tricks do you use to make a Mathematica application faster? MATLAB has an amazing profiler, but from what I can tell, Mathematica has no similar functionality.
0
votes
0answers
67 views

How do I perform the following numerical integration

I have the following integral to evaluate numerically: $$x(t) = \frac{1}{f(t)}\int_0^{t_b} t^m (t + n)^o \sin(pt) \mathrm{d}t \quad m,n,o,p \in \mathbb{R}$$ ...
4
votes
2answers
116 views

How to understand the process of ListCorrelate when it in two-dimensional condition?

Toady,I learn a function ListCorrelate,and I can understand process of execution his result in one-dimensional. However,the ...
2
votes
1answer
59 views

Using HoldFirst but not wishing to hold everything

I have a DynamicModule in which I go through some data analysis and keep or reject the results. Here is a toy example of what I am doing. ...
5
votes
1answer
165 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? ...
89
votes
3answers
12k views

Alternatives to procedural loops and iterating over lists in Mathematica

While there are some cases where a For loop might be reasonable, it's a general mantra – one I subscribe to myself – that "if you are using a ...
1
vote
1answer
47 views

How to understand the extensional usage of Flatten? [duplicate]

Recently, I have been reading Mathematica program that written by many advanced Mathematica user, like Mr.Wizard, J.M., Kuba,etc to improve my programming capacity In the On generalizing Partition[] ...
8
votes
7answers
557 views

Populate an upper triangular matrix from a vector of elements

What is the best way to populate an upper triangular (or alternatively: lower triangular or symmetric) matrix from a vector of elements? This example input: ...
2
votes
1answer
64 views

Probability evaluation fails with equals condition

The following evaluation fails (the result is just the copy of the input) $$\text{Probability}[0<x\leq a | x-y=t,\{x\ {ExponentialDistribution}[\lambda ],y\ {ExponentialDistribution}[\lambda ...
0
votes
0answers
83 views

Is Object Oriented Programming paradigm still necessary in Mathematica? [closed]

OK, this problem is more close to philosophy. People program in C++, Java and C#, etc, always think OOP to be a divine and inevitable programming paradigm to make a large project easy to build, to ...
0
votes
0answers
123 views

Converting a matlab program into Mathematica 10

I sought help from this entry at Calling MATLAB from Mathematica to see if I could CONVERT a mathlab program which simulates polymer dynamics that is provided at the download section at ...
1
vote
2answers
131 views

Plotting an orbit using first principles [duplicate]

I am fairly new to Mathematica, but I am trying to plot an orbit of the Earth around the sun using the equation: $\quad r(t+dt) = (2 - G*M*dt^2/r^3)*r(t) - r(t-dt)$ I need to break this into ...
7
votes
5answers
375 views

Computing the average over the values of the boxes surrounding a box in a grid

Recenty,I encounter a problem described as below: I have a table that owns many boxes,and each box has a sequence like 1,2,3,4,etc.In addition,every box has a value. Now I want to make the ...
160
votes
19answers
15k 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
2answers
65 views

How to define a function or procedure that includes multiple steps?

I would like to define a procedure or function that takes as input an integer, s, and gives as output two nested lists. I have written code (shown below) that does this for one particular value of s, ...
3
votes
3answers
268 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... ...
1
vote
2answers
59 views

Changing a tag when one function is passed into another using UpValues

I have two complicated functions f[x,y] and g[t]. Before the main evaluation code in ...
0
votes
0answers
29 views

Path must include /TypeSystemLoader.m?

I have encountered a number of odd issues using the Wolfram Workbench 3.0 Debugger. Whenever I try to step into a Switch[] statement, I get the following error message: "Problem Occurred - Debug ...
2
votes
0answers
25 views

How to syntax check that all instance of symbol must be contained in Subscript[]

I have a function f[expr] that is only allowed to take an expression expr that is a polynomial in ...
17
votes
5answers
1k views

How to speed up my Project Euler code

The evaluation speed of Mathematica often depresses me. It did it again when I wrote a code to solve a Project Euler problem. https://projecteuler.net/problem=206 Here's my code: ...
300
votes
12answers
54k 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 ...
3
votes
2answers
104 views

Apply Collatz Funtion to solve and print the answer

Good day to all! The objective of my task is apply the collatz function for a number x1 and print the exact number of iterations until ...
7
votes
6answers
335 views

How to build a list of functions

Having said that I'm no expert, I have some problems in generating a list of functions inside a loop. The functions in the list should depend on the elements of a parameters list, that are read in ...
3
votes
1answer
131 views

How to replace While construct to NestWhile(functional-style) correctly?

Recently,I find the program of @J.M. about Get a "step by step" evaluation in Mathematica is a very useful example for learning Mathematica programming. In the function ...
0
votes
0answers
46 views

Expectation Maximization Algorithm

I would like to use the expectation maximization algorithm within Mathematica. The issue is that I am very basic user and progress slowly with understanding functional programming.. I have found this ...
0
votes
2answers
97 views

Are there ways to create local variables analogous to a local variable in C++ and other languages? [closed]

There has been a great deal of discussion on local variables in Modules and Manipulate. If I am reading this correctly, local variables in a language such as C++ or C# are not exactly analogous to ...
5
votes
4answers
537 views

Use Mathematica to calculate the area enclosed between two curves

use mathematica Hence, calculate the exact area enclosed by these curves using Mathematica. $f(x)=x^3$ and $g(x)=x^5 - 2 x^3 - 3 x$ i have got the graph...now I'm stuck with finding the area
3
votes
2answers
197 views

approximate value for the area between the curve

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 endpoint, midpoint and right ...
1
vote
1answer
106 views

How determine The most possibility probable with programming

i have a scatter chart of probable of happen some thing like below chart link of google chart i what determine the probable The most point in below chart , can use a mathematic method to determine ...
16
votes
2answers
757 views

Using a list of tuples in a pure function

I want to use a list of tuples within a function to make assignments. Say I want to make assignments of the form value[i,j] = val What I have is a list of ...
0
votes
1answer
168 views

Defining a new matrix product of matrix function [closed]

Suppose I have two matrices A, B, whose entries are functions of x. Suppose they are 2*2. In the product, each entry is of the form $\sum_k A_ik B_kj$. There are two summands corresponding to k=1,k=2. ...
0
votes
1answer
57 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 ...
26
votes
1answer
463 views

What can I use as the second argument to Return in my own functions?

Having found out about the second argument to Return I played a bit with it, and the following tests all resulted in ...
5
votes
3answers
142 views

Prevent iterator name from being confused with symbol passed into function body

I have a massive amount of code with lots of Table and Sum inside a Module. Each with their ...
9
votes
0answers
135 views

Can code optimize code?

Are there functions, programs or packages that deal with optimizing mathematica code i.e. re-writing ineffecive or convoluted code. -This question has haunted me since I joined SE. The background for ...
1
vote
0answers
140 views

What are the best tips in Michael Trott's beer/whiskey Wolfram Blog post? [closed]

Michael Trott posted this incredible tour-de-force on the wolfram blog: http://blog.wolfram.com/2014/08/19/which-is-closer-local-beer-or-local-whiskey/ The length of the post is overwhelming, so I'm ...
10
votes
2answers
210 views

Why isn't Lookup treated like other symbols?

With Mathematica 10, I wanted to overload Lookup using TagSetDelayed, but it doesn't work. For instance, if I define two objects ...
3
votes
2answers
70 views

How to define a nestable Baseform function, BaseForm output becomes BaseForm input

Mathematica won't change a number's BaseForm recursively: 63969 // BaseForm[#, 16] & // BaseForm[#, 8] & // BaseForm[#, 2] & Well, I inspected former ...
15
votes
6answers
735 views

How to understand the usage of Inner and Outer figuratively?

Description: In Mathematica the functions like Thread, Inner, Outer etc. are very important ...
36
votes
4answers
3k views

How to change the default ColorData used in Mathematica's Plot?

This question leads on from the recent question What are the standard colors for plots in Mathematica? There it was determined that the default color palette used by ...
0
votes
1answer
143 views
0
votes
3answers
142 views

Avoid creation of OwnValues when inserting transformation rules into Experimental`OptimizeExpression

I have a large list of transformation rules that I want to simplify. I find Experimental`OptimizeExpression works as it gets me the answers. My question is: **I can only get this to work when I ...
5
votes
1answer
178 views

How can I make a “self-referential” replacement operation self-contained?

Fairly often I find use for replacement rules that call themselves on the right-hand side of the rule, e.g.: ...
34
votes
0answers
437 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 ...
4
votes
3answers
199 views

Skipping over iterator values in a Table

I have been using the built-in function Table for some computations. (The following is not an actual representation of the equations I am using). ...
3
votes
2answers
155 views

The value of an option inside a native function

I'm looking for something similar to AbsoluteOptions but that can be evaluated inside the function. That is, I want to read the absolute settings of options ...
10
votes
0answers
279 views

Create a new infix operator [duplicate]

It is well known that the infix operator @@ is used as a short form of Apply. For example, both ...