Related
I have a list and an arbitrary function taking 4 parameters, let's say {1, 11, 3, 13, 9, 0, 12, 7} and f[{x,y,z,w}]={x+y, z+w}, what I want to do is to form a new list such that 4 consecutive elements in the original list are evaluated to get a new value as the new list's component, and the evaluation has to be done in every 2 positions in the original list, in this case, the resulting list is:
{{12, 16}, {16, 9}, {9, 19}}
Note here 4 and 2 can change. How to do this conveniently in Mathematica? I imagine this as something like Map, but not sure how to relate.
There's an alternative to Map[f, Partition[...]]: Developer`PartitionMap. Which works exactly like Map[f, Partition[list, n, ...]]. So, your code would be
Needs["Developer`"]
f[{x_, y_, z_, w_}] = {x + y, z + w};
list = {1, 11, 3, 13, 9, 0, 12, 7};
PartitionMap[f,list, 4, 2]
giving the same result as Mark's answer.
f[{x_, y_, z_, w_}] = {x + y, z + w};
list = {1, 11, 3, 13, 9, 0, 12, 7};
f /# Partition[list, 4, 2]
I am exporting data from mathematica in this manner to a file with "dat" extension.
numbercount=0;
exporttable =
TableForm[
Flatten[
Table[
Table[
Table[{++numbercount, xcord, ycord, zcord}, {xcord, 0, 100, 5}],
{ycord, 0, 100, 5}],
{zcord,10, 100, 10}],
2]];
Export["mydata.dat", exporttable]
Now what happens is the "mydata.dat" file the output appears like this
1 0 0 10
2 5 0 10
3 10 0 10 and so on
But I want the data to appear like this in the "mydata.dat" file.
1, 0, 0, 10
2, 5, 0, 10
3, 10, 0, 10 and so on
If you observer I want a comma after every first,second and third number but not after the fourth number in each line.
I have tried this code it inserts the commas between the number But it takes a long time to run as I have huge amounts of data to be exported.I also feel that someone can perhaps come up with a better solution.
numbercount=0;
exporttable =Flatten[
Table[
Table[
Table[{++numbercount, xcord, ycord, zcord}, {xcord, 0, 100, 5}],
{ycord, 0, 100, 5}],
{zcord,10, 100, 10}],
2];
x = TableForm[Insert[
exporttable[[i]], ",", {{2}, {3}, {4}}], {i, 1, Length[exporttable]}];
Export["mydata.dat", x]
Have you tried exporting it as a CSV file? The third parameter of Export is file type, so you'd type
Export["mydata.dat", x, "CSV"]
To add to this, here is a categorical list and an alphabetical list of the available formats in Mathematica.
As an aside note, please note that you can build your list with only one Table command, and without explicit index variables:
exporttable1 = MapIndexed[Join[#2, #1] &,
Flatten[Table[{xcord, ycord, zcord},
{zcord, 10, 100, 10},
{ycord, 0, 100, 5},
{xcord, 0, 100, 5}], 2]]
exporttable1 == exporttable
(*
-> True
*)
daList={62.8347, 88.5806, 74.8825, 61.1739, 66.1062, 42.4912, 62.7023,
39.0254, 48.3332, 48.5521, 51.5432, 69.4951, 60.0677, 48.4408,
59.273, 30.0093, 94.6293, 43.904, 59.6066, 58.7394, 68.6183, 83.0942,
73.1526, 47.7382, 75.6227, 58.7549, 59.2727, 26.7627, 89.493,
49.3775, 79.9154, 73.2187, 49.5929, 84.4546, 28.3952, 75.7541,
72.5095, 60.5712, 53.2651, 33.5062, 80.4114, 63.7094, 90.2438,
55.2248, 44.437, 28.1884, 4.77477, 36.8398, 70.3579, 28.1913,
43.9001, 23.8907, 12.7823, 22.3473, 57.6724, 49.0148}
The above are a sample of actual data I am dealing with.
I use BinCounts, but this is just to illustrate visually histogram should do it : I would like to fit the shape of that histogram
Histogram#data
I know how to fit datapoints themselves like :
model = 0.2659615202676218` E^(-0.2222222222222222` (x - \[Mu])^2)
FindFit[data, model, \[Mu], x]
Which is far from what I wan to do : How can I fit bin-counts/histograms in Mathematica ?
If you have MMA V8 you could use the new DistributionFitTest
disFitObj = DistributionFitTest[daList, NormalDistribution[a, b],"HypothesisTestData"];
Show[
SmoothHistogram[daList],
Plot[PDF[disFitObj["FittedDistribution"], x], {x, 0, 120},
PlotStyle -> Red
],
PlotRange -> All
]
disFitObj["FittedDistributionParameters"]
(* ==> {a -> 55.8115, b -> 20.3259} *)
disFitObj["FittedDistribution"]
(* ==> NormalDistribution[55.8115, 20.3259] *)
It can fit other distributions too.
Another useful V8 function is HistogramList, which provides you with Histogram's binning data. It takes about all of Histogram's options too.
{bins, counts} = HistogramList[daList]
(* ==> {{0, 20, 40, 60, 80, 100}, {2, 10, 20, 17, 7}} *)
centers = MovingAverage[bins, 2]
(* ==> {10, 30, 50, 70, 90} *)
model = s E^(-((x - \[Mu])^2/\[Sigma]^2));
pars = FindFit[{centers, counts}\[Transpose],
model, {{\[Mu], 50}, {s, 20}, {\[Sigma], 10}}, x]
(* ==> {\[Mu] -> 56.7075, s -> 20.7153, \[Sigma] -> 31.3521} *)
Show[Histogram[daList],Plot[model /. pars // Evaluate, {x, 0, 120}]]
You could also try NonlinearModeFit for fitting. In both cases it is good to come with your own initial parameter values to have the best chances that you end up with a globally optimal fit.
In V7 there is no HistogramList but you could get the same list using this:
The function fh in Histogram[data,bspec,fh] is applied to two
arguments: a list of bins {{Subscript[b, 1],Subscript[b,
2]},{Subscript[b, 2],Subscript[b, 3]},[Ellipsis]}, and corresponding
list of counts {Subscript[c, 1],Subscript[c, 2],[Ellipsis]}. The
function should return a list of heights to be used for each of the
Subscript[c, i].
This can be used as follows (from my earlier answer):
Reap[Histogram[daList, Automatic, (Sow[{#1, #2}]; #2) &]][[2]]
(* ==> {{{{{0, 20}, {20, 40}, {40, 60}, {60, 80}, {80, 100}}, {2,
10, 20, 17, 7}}}} *)
Of course, you can still use BinCounts but the you miss MMA's automatic binning algorithms. You have to provide a binning of your own:
counts = BinCounts[daList, {0, Ceiling[Max[daList], 10], 10}]
(* ==> {1, 1, 6, 4, 11, 9, 9, 8, 5, 2} *)
centers = Table[c + 5, {c, 0, Ceiling[Max[daList] - 10, 10], 10}]
(* ==> {5, 15, 25, 35, 45, 55, 65, 75, 85, 95} *)
pars = FindFit[{centers, counts}\[Transpose],
model, {{\[Mu], 50}, {s, 20}, {\[Sigma], 10}}, x]
(* ==> \[Mu] -> 56.6575, s -> 10.0184, \[Sigma] -> 32.8779} *)
Show[
Histogram[daList, {0, Ceiling[Max[daList], 10], 10}],
Plot[model /. pars // Evaluate, {x, 0, 120}]
]
As you can see the fit parameters may depend quite a bit on your binning choice. Particularly the parameter I called s depends critically on the amount of bins. The more bins, the lower the individual bin counts and the lower the value of s will be.
I have the following problem.
I need to build a very large number of definitions(*) such as
f[{1,0,0,0}] = 1
f[{0,1,0,0}] = 2
f[{0,0,1,0}] = 3
f[{0,0,0,1}] = 2
...
f[{2,3,1,2}] = 4
...
f[{n1,n2,n3,n4}] = some integer
...
This is just an example. The length of the argument list does not need to be 4 but can be anything.
I realized that the lookup for each value slows down with exponential complexity when the length of the argument list increases. Perhaps this is not so strange, since it is clear that in principle there is a combinatorial explosion in how many definitions Mathematica needs to store.
Though, I have expected Mathematica to be smart and that value extract should be constant time complexity. Apparently it is not.
Is there any way to speed up lookup time? This probably has to do with how Mathematica internally handles symbol definition lookups. Does it phrases the list until it finds the match? It seems that it does so.
All suggestions highly appreciated.
With best regards
Zoran
(*) I am working on a stochastic simulation software that generates all configurations of a system and needs to store how many times each configuration occurred. In that sense a list {n1, n2, ..., nT} describes a particular configuration of the system saying that there are n1 particles of type 1, n2 particles of type 2, ..., nT particles of type T. There can be exponentially many such configurations.
Could you give some detail on how you worked out that lookup time is exponential?
If it is indeed exponential, perhaps you could speed things up by using Hash on your keys (configurations), then storing key-value pairs in a list like {{key1,value1},{key2,value2}}, kept sorted by key and then using binary search (which should be log time). This should be very quick to code up but not optimum in terms of speed.
If that's not fast enough, one could think about setting up a proper hashtable implementation (which I thought was what the f[{0,1,0,1}]=3 approach did, without having checked).
But some toy example of the slowdown would be useful to proceed further...
EDIT: I just tried
test[length_] := Block[{f},
Do[
f[RandomInteger[{0, 10}, 100]] = RandomInteger[0, 10];,
{i, 1, length}
];
f[{0, 0, 0, 0, 1, 7, 0, 3, 7, 8, 0, 4, 5, 8, 0, 8, 6, 7, 7, 0, 1, 6,
3, 9, 6, 9, 2, 7, 2, 8, 1, 1, 8, 4, 0, 5, 2, 9, 9, 10, 6, 3, 6,
8, 10, 0, 7, 1, 2, 8, 4, 4, 9, 5, 1, 10, 4, 1, 1, 3, 0, 3, 6, 5,
4, 0, 9, 5, 4, 6, 9, 6, 10, 6, 2, 4, 9, 2, 9, 8, 10, 0, 8, 4, 9,
5, 5, 9, 7, 2, 7, 4, 0, 2, 0, 10, 2, 4, 10, 1}] // timeIt
]
with timeIt defined to accurately time even short runs as follows:
timeIt::usage = "timeIt[expr] gives the time taken to execute expr,
repeating as many times as necessary to achieve a total time of \
1s";
SetAttributes[timeIt, HoldAll]
timeIt[expr_] := Module[{t = Timing[expr;][[1]], tries = 1},
While[t < 1.,
tries *= 2;
t = Timing[Do[expr, {tries}];][[1]];
];
Return[t/tries]]
and then
out = {#, test[#]} & /# {10, 100, 1000, 10000, 100000, 100000};
ListLogLogPlot#out
(also for larger runs). So it seems constant time here.
Suppose you enter your information not like
f[{1,0,0,0}] = 1
f[{0,1,0,0}] = 2
but into a n1 x n2 x n3 x n4 matrix m like
m[[2,1,1,1]] = 1
m[[1,2,1,1]] = 2
etc.
(you could even enter values not as f[{1,0,0,0}]=1, but as f[{1,0,0,0},1] with
f[li_List, i_Integer] := Part[m, Apply[Sequence, li + 1]] = i;
f[li_List] := Part[m, Apply[Sequence, li + 1]];
where you have to initialize m e.g. by m = ConstantArray[0, {4, 4, 4, 4}];)
Let's compare timings:
testf[z_] :=
(
Do[ f[{n1, n2, n3, n4}] = RandomInteger[{1,100}], {n1,z}, {n2,z}, {n3,z},{n4,z}];
First[ Timing[ Do[ f[{n2, n4, n1, n3}], {n1, z}, {n2, z}, {n3, z}, {n4, z} ] ] ]
);
Framed[
ListLinePlot[
Table[{z, testf[z]}, {z, 22, 36, 2}],
PlotLabel -> Row[{"DownValue approach: ",
Round[MemoryInUse[]/1024.^2],
" MB needed"
}],
AxesLabel -> {"n1,n2,n3,n4", "time/s"},ImageSize -> 500
]
]
Clear[f];
testf2[z_] :=
(
m = RandomInteger[{1, 100}, {z, z, z, z}];
f2[ni__Integer] := m[[Sequence ## ({ni} + 1)]];
First[ Timing[ Do[ f2[{n2, n4, n1, n3}], {n1, z}, {n2, z}, {n3, z}, {n4, z}] ] ]
)
Framed[
ListLinePlot[
Table[{z, testf2[z]}, {z, 22, 36, 2}],
PlotLabel -> Row[{"Matrix approach: ",
Round[MemoryInUse[]/1024.^2],
" MB needed"
}],
AxesLabel -> {"n1,n2,n3,n4", "time/s"}, ImageSize -> 500
]
]
gives
So for larger sets up information a matrix approach seems clearly preferrable.
Of course, if you have truly large data, say more GB than you have RAM, then you just
have to use a database and DatabaseLink.
If I type
DateString[{2011, 2, 29, 0, 0, 0}, {"DayName"}]
It gives "Tuesday".
And also,
DateString[{2011, 2, 29, 0, 0, 0}, {"DayName"}]
DateString[{2011, 3, 1, 0, 0, 0}, {"DayName"}]
Needs["Calendar`"];
myDay[x_List] := DateString[x, {"DayName"}] /; DateQ[x]
myDay[{2000, 1, 1}]
->"Saturday"
myDay[{2000, 13, 13}]
->myDay[{2000, 13, 13}]
Of course you may throw a message (or Abort[], or whatever) if you want to :
Needs["Calendar`"];
Clear#myDay;
myDay[x_] /; If[DateQ[x], True, Message[myDay::nodate, x]; False] :=
DateString[x, {"DayName"}]
myDay::nodate = "The argument `1` is not a valid date.";
This looks to me like correct behaviour. The documentation for DateString says: "Values of m, d, h, m, s outside their normal ranges are appropriately reduced." which is just what's happened: there isn't really a 29th of February this year, but if there were it would be the same day that's actually the 1st of March, which is indeed a Tuesday.