mathematica

Struct data type in Mathematica?

After playing with Mathematica's symbolic and numerical capabilities, I find it to be a decent programming language, too. However, something making it less appealing as a general-purpose language is the lack of C-like struct data type (or the record type as known in Pascal). How can I get around this problem? ...

Mathematica's Tables and Modifications in Octave?

How can I do Mathematica-style a = Table[2^(2 k) + 1, {k, 1, 3}] Last[a], First[a] Append[a,3] in Octave? ...

Mixed Scaled and ordinary coordinates in Mathematica?

Is it possible to specify a position in terms of Scaled coordinates in one direction and use the ordinary coordinates from my data points in the other direction on the plot? In other words, I want to specify a position, where the x coordinate is an ordinary coordinate and will change position in the plot if the plot range is changed, bu...

Partial evaluation in Mathematica

I have a differential operator that acts on two functions. To simplify the problem let's say that my operator is A[F_,G_] := D[F,x] D[G,y] I want to be able, if I know F, to define a differential operator AF such that AF[G] is equal to A[F,G]. The obvious way is AF[G_] := A[F,G] which works without any problem. But what I would rea...

Optimization with Mathematica: Use BinCounts in Objective Function

Using Mathematica, I need to optimize a function that is defined in terms of BinCounts; the arguments that I want to maximize over define the bin cutpoints. I think the problem is that Mathematica expands the objective function in terms of the arguments before they have been given numerical values, so BinCounts complains that the bin...

In Mathematica, how can I find patterns that include rules and lists?

Example: test = {"a" -> {{1}, 12}, "b" -> {13}} I'd like to find all expressions in the list with this pattern: _ -> {_,_} The first element, "a" -> {{1}, 12}, is represented by this pattern. However, none of these expressions work: Cases[test,_->{_,_}], Cases[test,_->_], Cases[test,Rule[_,_]], etc. Any advice would be apprecia...

Sprintf equivalent in Mathematica?

I don't know why Wikipedia lists Mathematica as a programming language with printf. I just couldn't find the equivalent in Mathematica. My specific task is to process a list of data files with padded numbers, which I used to do it in bash with fn=$(printf "filename_%05d" $n) The closest function I found in Mathematica is PaddedForm....

Recurrence sequence in Java / Python / Mathematica

How can you write the following statement in the given languages? a(0) = 1 a_(n+1) = 1 - 1 / ( a_n + 3) I need to find the smallest value of n when a_n -> 0.732050.... My attempt in Mathematica a[(x+1)_] = 1 - 1/(a[x_] + 3) The problem is apparently in this a[(x+1)_]. However, I do not know how to do it iteratively in Mathematica....

Suppressing a trailing "." in numerical output from Mathematica

Is there some straightforward way to ensure that, when converted to strings, approximate numbers (i.e., numbers with the Real head) won't have a trailing "."? I would like it if they were to only have the decimal point in cases where there's actually a displayed fractional part. The solutions I've found are not robust, and depend on us...

What is better for running Monte Carlo simulations MATLAB or Mathematica?

I am doing a research project and need to use Monte Carlo simulation. I want to know, what is better to use: MATLAB or Mathematica? I have used MATLAB before but have heard good things about Mathematica. ...

How to implement MapThread with basic list mapping?

Mathematica has a function MapThread that behaves like this: MapThread[ f , { {a,b,c} , {d,e,f} } ] -> { f[a,d] , f[b,e] , f[c,f] } I'd like to implement this in TeX, which has very primitive programming facilities. I've basic facilities for iterating over lists, but no logical indexing into them. Given this restriction, is there an a...

creating distributions in mathematica

I have a function which I know to be a multivariate distribution in (x,y), and mathematica is having numerical stability issues when I form the marginal distributions. For example, marginalizing along y yields the following: 0.e^(154.88-0.5x^2) Since I know the result must be a distribution, I would like to extract just the e^(-.5x^2) ...

How to plot |z-1| = 2 in a specified domain in Mathematica?

How do I plot |z-1| = 2, from -10 to +10 in the real line and from -10i to +10i on the complex line? I've been trying for ages and seems like I can't get it right. Z stands for a complex number! Also, could I use as well the x+iy notation in mathematica? or a+ib? Thanks ...

How to use With inside a ContourPlot in mathematica?

I want to use something like: ContourPlot [Abs[z-1] == 2] and to define z as being = x + iy I saw somewhere an example like that with the With function, but I can't find it anymore and all my tries are not being successful. ...

Mathematica in batch mode from the command line on Mac OS X

I'd like to start writing some unit tests for my Mathematica programs and control everything from the command line with some Makefiles. It seems like Mathematica can be run from the command line but I can't see any basic instructions on getting started with doing this on Mac OS X — has anyone done this before? Update: Creating a te...

Parser for the Mathematica syntax?

Is there a built parser that I can use from C# that can parse mathematica expressions? I know that I can use the Kernel itself to parse an expression, and use .NET/Link to retrieve the tree structure... But I'm looking for something that doesnt rely on the Kernel. ...

Named arguments in Mathematica.

What's the best/canonical way to define a function with optional named arguments? To make it concrete, let's create a function foo with named arguments a, b, and c, which default to 1, 2, and 3, respectively. For comparison, here's a version of foo with positional arguments: foo[a_:1, b_:2, c_:3] := bar[a,b,c] Here is sample input a...

Mathematica: Getting rid of the "x ->" in FindInstance results

Suppose I have the following results: a=FindInstance[2*b^2 + b^3 == b^4 + t && t < 10 && t > -1, {b, t}, Integers, 20] {{b -> -1, t -> 0}, {b -> 0, t -> 0}, {b -> 1, t -> 2}, {b -> 2, t -> 0}} How can I get rid of the "b->" and just get the array of b answers? I can get halfway there with: a[[All,1]] {b -> -1, b -> 0, b -> 1, b...

Mathematica: Unevaluated vs Defer vs Hold vs HoldForm vs HoldAllComplete vs etc etc

I'm bewildered by all the built-in Mathematica functions that purport to prevent evaluation in some way: Unevaluated, Defer, Hold, and over half a dozen of the form Hold*. The Mathematica documentation just explains each function in isolation without explaining why you would choose one or the other. Can anyone offer a coherent explanat...

Mathematica Programming Graph Plot

I have a file with 40,000 data points. In Matlab I can use the plot command to draw the plot: aaa = Import('file Name'); plot(aaa,mesh) How do I do it in Mathematica? I tried: aaa = Import["File Name"] ListPlot3D[aaa] but it doesn't work. ...