mathematica

How to extract a Date from an SQLDateTime object in Mathematica

I am trying to do a plot of a time series with DateListPlot. I want to feed it a time series I obtain from an SQL database. When I retrieve the time series the list is composed of SQLDateTime entries that DateListPlot doesn't understand. In[24]:= t=SQLExecute[conn, "select timestamp,value from timeseries order by timestamp asc"] Out[...

Weird behavior of substitution in Mathematica.

My question is: why doesn't the following work, and how do I fix it? Plot[f[t], {t, 0, 2*Pi}] /. {{f -> Sin}, {f -> Cos}} The result is two blank graphs. By comparison, DummyFunction[f[t], {t, 0, 2*Pi}] /. {{f -> Sin}, {f -> Cos}} gives {DummyFunction[Sin[t], {t, 0, 2 *Pi}], DummyFunction[Cos[t], {t, 0, 2 * Pi}]} as desired. ...

Mathematica ListcontourPlot3D

I have data in the form { {x,y,z,f}...} I am using ListContourPlot3D but all I get is an empty box with dimensions -1,1 in each direction. Here is my code: ListContourPlot3D[data5, PlotRange -> All, AxesLabel -> {"[Beta]", "[Omega]", "Vo"}, Contours -> {1500}]. These are the first 5 points of my data:( the whole set has 55 points) {{200,...

How can I check if OpenSQLConnection was successful (in Mathematica)?

How can I check if DatabaseLink`OpenSQLConnection was successful? My code is as follows conn = OpenSQLConnection[JDBC["hsqldb", "file"], "Name"-> "test"]; Can I use something like Head[conn]? ...

Mathematica regular expressions on unicode strings.

This was a fascinating debugging experience. Can you spot the difference between the following two lines? StringReplace["–", RegularExpression@"[\\s\\S]" -> "abc"] StringReplace["-", RegularExpression@"[\\s\\S]" -> "abc"] They do very different things when you evaluate them. It turns out it's because the string being replaced in the ...

Mathematica - Import CSV and process columns?

I have a CSV file that is formatted like: 0.0023709,8.5752e-007,4.847e-008 and I would like to import it into Mathematica and then have each column separated into a list so I can do some math on the selected column. I know I can import the data with: Import["data.csv"] then I can separate the columns with this: StringSplit[data[...

How do I get Mathematica to thread a 2-variable function over two lists, using functional programming techniques?

Lets say I have a function f[x_, y_], and two lists l1, l2. I'd like to evaluate f[x,y] for each pair x,y with x in l1 and y in l2, and I'd like to do it without having to make all pairs of the form {l1[[i]],l2[[j]]}. Essentially, what I want is something like Map[Map[f[#1, #2]&, l1],l2] where #1 takes values from l1 and #2 takes value...

Mathematica quickie: About making cells Input cells by default.

I'm going through some lecture notes, Fundamentals of Mathematica Programming (see the .nb file there). I'd like to be able to do the exercises right there in the notebook; but for some reason I can't figure out how to make the default cell an Input cell. In other words, when I'm clicking in that notebook to create a new cell--in the exe...

ListPlot With Two Data Sets in Mathematica

Is there a cleaner way to do the following, assuming that I have a reason to keep the data sets independent?: x = {1, 2, 3}; y = {1, 4, 9}; ListPlot[Partition[Riffle[x, y], 2]] Thanks! ...

Parsing and generating JSON

Mathematica's list of built-in formats is pretty extensive; however, JSON is not on that list. Is there an existing solution for generating and parsing JSON in Mathematica, or are we going to have to roll our own solution? ...

posmax: like argmax but gives the position(s) of the element x for which f[x] is maximal

Mathematica has a built-in function ArgMax for functions over infinite domains, based on the standard mathematical definition. The analog for finite domains is a handy utility function. Given a function and a list (call it the domain of the function), return the element(s) of the list that maximize the function. Here's an example of fin...

Compute average distance from point to line segment and line segment to line segment

Hi everyone, I'm searching for an algorithm to calculate the average distance between a point and a line segment in 3D. So given two points A(x1, y1, z1) and B(x2, y2, z2) that represent line segment AB, and a third point C(x3, y3, z3), what is the average distance between each point on AB to point C? I'm also interested in the averag...

Debugging a working program on Mathematica 5 with Mathematica 7

Hi everybody, I'm currently reading the Mathematica Guidebooks for Programming and I was trying to work out one of the very first program of the book. Basically, when I run the following program: Plot3D[{Re[Exp[1/(x + I y)]]}, {x, -0.02, 0.022}, {y, -0.04, 0.042}, PlotRange -> {-1, 8}, PlotPoints -> 120, Mesh -> False, ColorFunction...

Splitting a list based on another list values in Mathematica

In Mathematica I have a list of point coordinates size = 50; points = Table[{RandomInteger[{0, size}], RandomInteger[{0, size}]}, {i, 1, n}]; and a list of cluster indices these points belong to clusterIndices = {1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1}; what is the easiest way to split the points into two separa...

Curve fitting: Find the smoothest function that satisfies a list of constraints.

Consider the set of non-decreasing surjective (onto) functions from (-inf,inf) to [0,1]. (Typical CDFs satisfy this property.) In other words, for any real number x, 0 <= f(x) <= 1. The logistic function is perhaps the most well-known example. We are now given some constraints in the form of a list of x-values and for each x-value, a pa...

Octave: Multiple submatrices from a matrix

I have a large matrix from which I would like to gather a collection of submatrices. If my matrix is NxN and the submatrix size is MxM, I want to collect I=(N - M + 1)^2 submatrices. In other words I want one MxM submatrix for each element in the original matrix that can be in the top-left corner of such a matrix. Here's the code I have...

Lexical and dynamic scoping in Mathematica: Local variables with Module, With, and Block

The following code returns 14 as you'd expect: Block[{expr}, expr = 2 z; f[z_] = expr; f[7]] But if you change that Block to a Module then it returns 2*z. It seems to not matter what other variables besides expr you localize. I thought I understood Module, Block, and With in Mathematica but I can't explain the difference in beha...

Why would Mathematica break normal scoping rules in Module?

As was pointed out in a recent post scoping does not work as expected inside of Module. An example from that thread is: Module[{expr}, expr = 2 z; f[z_] = expr; f[7]] (*2 z*) But the following works almost as expected. Module[{expr}, expr = 2 z; Set@@{f[z_], expr}; f[7]] (*14*) What language design consideration made wol...

output with "Private`" Content in Mathematica Package

Hello everyone, I am trying to solve the following implementation problem in Mathematica 7.0 for some days now and I do not understand exactly what is happening so I hope someone can give me some hints. I have 3 functions that I implemented in Mathematica in a source file with extension *.nb. They are working okay to all the examples. N...

Two strange efficiency problems in Mathematica

FIRST PROBLEM I have timed how long it takes to compute the following statements (where V[x] is a time-intensive function call): Alice = Table[V[i],{i,1,300},{1000}]; Bob = Table[Table[V[i],{i,1,300}],{1000}]^tr; Chris_pre = Table[V[i],{i,1,300}]; Chris = Table[Chris_pre,{1000}]^tr; Alice, Bob, and Chris are identical m...