mathematica

Mathematica: get number of arguments passed to a function?

How do I get the number of arguments passed to a function, such as Plus[2,3,4,5] has 4 arguments passed to it. I was thinking it may involve the use of the function Length and getting the arguments into a list. The intention is to iterate an operation based on the number of arguments for a function. There is probably a simple solution or...

Initial conditions with a non-linear ODE in Mathematica

Hi, I'm trying to use Mathematica's NDSolve[] to compute a geodesic along a sphere using the coupled ODE: x" - (x" . x) x = 0 The problem is that I can only enter initial conditions for x(0) and x'(0) and the solver is happy with the solution where x" = 0. The problem is that my geodesic on the sphere has the initial condition that x...

String quotation marks when exporting a Mathematica Grid as vector graphics

Running the following agrid = Grid[{{Style["hello", Bold]}}] ImportString[ExportString[agrid, "PNG"], "PNG"] ImportString[ExportString[agrid, "EPS"], "EPS"] spits out hello hello "hello" That is, the EPS exporter included the quotation marks in the output. Same for PDF. Without the Grid, all exporters leave out the quotat...

What types of projects is Mathematica good for?

I once saw a dismal comparison of Matlab vs Mathematica. As you can see Matlab achieves the same with very little code. It looks highly efficient to an untrained eye, so I ask, what types of projects is Mathematica good for? Are there any scientific research problems that are easier solved with it? or is Matlab better for almost every co...

Simple? Message Passing in Mathematica 7

Uu[z_,x_,t_] := A1[z]*F[t*a*x] Wu[z_,x_,t_] := B1[z]*F[t*a*x] Pu[z_,x_,t_] := C1[z]*F[t*a*x] eq1 = D[Uu[z,x,t],t]==-R*D[Pu[z,x,t],x]; C1z = DSolve[eq1,C1[z],z]; eq2 = D[Wu[z,x,t],t]==-R*D[Pu[z,x,t],z]/.C1z[[1]] The assignment /.C1z[[1]] does not behave the way I expect it to. I am unsure of even what this pheonomena is called, (which ...

How to subtract specific elements in a list using functional programming in Mathematica?

I have a list of dates and values in the format: {{{dateInfo1},value1},{{dateInfo2},value2},...,{{dateInfoN},valueN}} With some actual dates and values: {{{1971, 1, 31, 0, 0, 0.}, 1.0118}, {{1971, 2, 28, 0, 0, 0}, 1.0075}, ..., {{2010, 5, 31, 0, 0, 0.}, 1.0403}} For those curious, it's a list of US versus CAD $ values pulled from...

automatize a mathematic procedure: I need industrial amouts of results, and I get one.

I've the following simple code in mathematica that is what I want to a single output. But I want to get hundreds or thousands of it. How can I do? Clear["Global`*"] k = 2; Put["phiout"]; Put["omegadiffout"]; Random[NormalDistribution[0, 0.1]]; For[i = 1, i < 31, rnd[i] = Random[NormalDistribution[0, 0.1]]; i++] Table[rnd[i], {i, 1...

Evaluate beyond one level within Hold in Mathematica

The mathematica documentation on Evaluate at possible issues says: Evaluate works only on the first level, directly inside a held function Why does Mathematica have this limitation? So if I have an expression with more than one level take this simplified example: Hold[Plus[Plus[2, 2], 2]]] Now suppose I want to see what the an...

Caching of data in Mathematica

Hello, there is a very time-consuming operation which generates a dataset in my package. I would like to save this dataset and let the package rebuild it only when I manually delete the cached file. Here is my approach as part of the package: myDataset = Module[{fname, data}, fname = "cached-data.mx"; If[FileExistsQ[fname], ...

How do I URL-escape a string in Mathematica?

For example, urlesc["foo.cgi?abc=123"] should return foo.cgi%3Fabc%3D123 This is also known as percent-encoding. Also, for better readability, spaces should encode to pluses. I believe that's always acceptable for URL escaping. ...

Scatter plot with indication of the density of points.

R's qplot function has a nifty alpha parameter for shading coincident points in a scatter plot darker. Here it is in action: http://www.decisionsciencenews.com/2010/07/01/maps-without-map-packages I'm wondering how to do the same in Mathematica. Here's code to grab the data from the above article and plot it, without the nifty shading...

Resize a list of images in Mathematica for use with ListAnimate.

Suppose we have a list of images, like the following: imageList = Table[Plot[Sin[k*x], {x, 0, 2 Pi}], {k, 1, 4, .05}]; We can animate those images like so: ListAnimate[imageList] But now suppose we want the animation bigger. We could stick an ImageSize option into the Plot command used to generate the images, but suppose we're not ...

Calculating percent difference between elements in a list with functional programming in Mathematica?

This stems from a related discussion, How to subtract specific elements in a list using functional programming in Mathematica? How does one go about easily calculating percent differences between values in a list? The linked question uses Differences to easily calculate absolute differences between successive elements in a list. Howev...

Help me identify the plot in this image

Hi - I'm not very math savvy, but this Mathematica plot caught my eye and I was hoping you could help me identify it. Have a look at the image (sorry about the quality, it's a screen capture of a video) I've searched the various functions and keywords found in the pictured code, but none of the results suggested anything specific to me...

Force Mathematica to interpolate on non-structured tensor grid

This list is a simple function that maps a 2D point to a number, if you regard each {{x,y},z} as f[x,y]=z { {{1,3},9}, {{1,4},16}, {{2,4},8}, {{2,5},10} } I now want a function that interpolates/extrapolates f[x,y] for any {x,y}. Mathematica refuses to do this: Interpolation[{{{1,3},9}, {{1,4},16},{{2,4},8}, {{2,5},10}}, I...

Optimizing an inner loop calculation in Mathematica

Hi all, I'm currently doing some computation in Mathematica related to Quantum Mechanics. As we've moved from a 1D to 2D lattice model, the problem size is becoming problematic Currently, we have a summation that looks something like this: corr[r1_, r2_, i_, j_] = Sum[Cos[f[x1, x2] Angle[i] r1 + f[y1, y2] Angle[j] r2], {x1, HL}, {x2, H...

Stable Sorting, ie, Minimally-Disruptive Sorting.

Suppose I have a list of things (numbers, to keep things simple here) and I have a function I want to use to sort them by, using SortBy. For example, the following sorts a list of numbers by last digit: SortBy[{301, 201}, Mod[#,10]&] And notice how two of (ie, all of) those numbers have the same last digit. So it doesn't matter which ...

Monotonify (list-munging) without a For loop.

For or While loops in Mathematica code always make me feel a little dirty but I was confusing myself trying to do some list munging all functional-like, and resorted to this: (* # Given a list of {x,y} pairs, transform the data as follows: every time # there's a decrease in y-value from one datapoint to the next, say {x1,Y} # fo...

Reliable clean-up in Mathematica

For better or worse, Mathematica provides a wealth of constructs that allow you to do non-local transfers of control, including Return, Catch/Throw, Abort and Goto. However, these kinds of non-local transfers of control often conflict with writing robust programs that need to ensure that clean-up code (like closing streams) gets run. Man...

Methods to convert mathematical formulas into code in Matlab, C++, etc?

Hi, I have a basic question for all of the math experts out there. "If I have an academic paper, whats the easiest way to convert a simple mathematical equation into working Matlab (or C++) code?" Ideally, there would be a Latex >> Matlab (or C++) conversion tool. However, failing this, is there a "cheat sheet" which contains all of ...