Tag Info

Hot answers tagged

8

There is an example of what I think you are trying to do at the end of WeatherData help page, but its a little hard to read. However - here's what I think you want. (* Function to Get a list consisting of {CityName, Temp, Co-ords} given a Countries weather station, note we get the 1st nearest weather station to the city *) weatherdata[cityname_] := ...


7

Mathematica does it internally by using BoxForm`ArrangeSummaryBox, which is quite straightforward to figure out: MakeBoxes[obj_MyObject, fmt_] ^:= Module[{o = List @@ obj, shown, hidden, icon = Graphics[{Blue, Circle[]}, ImageSize -> 70]}, shown = {{ BoxForm`MakeSummaryItem[{"Name: ", "Name" /. o /. "Name" -> Missing[]}, fmt], ...


6

SphericalBesselJ[210, (1/1.5)*2*Pi*1.5*1000/40] // InputForm 0. Use higher precision input SphericalBesselJ[210, (1/1.5`20)*2*Pi*1.5`20*1000/40] // InputForm 9.4770515229477837927439`0.13632911832271324*^-16 SphericalBesselJ[210, (2/3)*2*Pi*(3/2)*1000/40] // N[#, 20] & // InputForm 9.47705152294778379274395028349340334928589`20.*^-16 ...


6

Couple of ways: f[x_, y_] := y*x^2 SparseArray[{i_, j_} :> f[i, j], {3, 3}] // Normal Array[f, {3, 3}] Table[f[x, y], {x, 3}, {y, 3}] (* {{1, 2, 3}, {4, 8, 12}, {9, 18, 27}} {{1, 2, 3}, {4, 8, 12}, {9, 18, 27}} {{1, 2, 3}, {4, 8, 12}, {9, 18, 27}} *) If you already have the array (say in this example a 3x3 zeroes) and you want to go over it and ...


5

myfunc[x_] := Piecewise[{{c x^2 + 2 x, x <= 2}, {x^3 - c x, True}}]; lim1 = Limit[myfunc[x], x -> 2, Direction -> -1] lim2 = Limit[myfunc[x], x -> 2, Direction -> 1] sol = c /. First@Solve[{lim1 == lim2}, c] (*2/3*) Plot[myfunc[x] /. c -> sol, {x, 1, 3}, Epilog -> {Red, PointSize[.015], Point[{2, myfunc[2] /. c -> sol}]}] ...


5

To illustrate whats going on, your function is 0 at x=0, rises to a max and becomes essentially zero very quickly. With[{a = .9}, Plot[x Exp[-(a^2+.001^2) x^2], {x, 0, 3}]] Now look at the values NIntegrate computes: res = Reap[With[{a = .9}, NIntegrate[y = x Exp[-(a^2 + 0.001^2) x^2], {x, 0, 8000}, EvaluationMonitor :> Sow[{x, ...


4

This isn't a problem you should try to solve automatically. Use good code hygiene and make sure you don't call private functions. You should (aim to) understand your code well enough that you don't get surprises like this --- you wrote it, and you know it better than anyone else. If there are surprises even to you, how will anyone else understand it?! ...


4

You can include an iterator along with the function in the mapping: k = 0; (++k; f[#]) & /@ Range[100];


4

This is a good question, but unfortunately there doesn't seem to be a perfect solution. You can use ToExpression, e.g. ToExpression["1.23"]. But: (1) this gives no error checking (2) it's a serious security risk if you obtain the string from users (and it can go things go haywire in general if the string comes from an unknown source) ...


4

Since, Attributes[Function] {HoldAll, Protected} Use Evaluate f = Function[{x}, Evaluate[(1 - # &)@*(1 - # &)@x]] Function[{x}, x] f@x x


4

What the OP is trying to code is already in Mathematica in the form of ParametricNDSolveValue. myodessystem = ParametricNDSolveValue[{y'[x] == k1 y[x] Cos[k2 x + y[x]], y[0] == 1}, y, {x, 0, time}, {k1, k2, time}] (* ParametricFunction[<>] *) mysolve = myodessystem[1, 1, 30]; mysolve[1] (* 0.991387 *)


3

d1 = {2015, 4, 1, 3, 10}; d2 = {2015, 4, 3, 18, 10}; j[d_, s_, h_] := Join[DatePlus[d, s][[1 ;; 3]], {h, 00}] abst = AbsoluteTime; l1 = DateRange[j[d1, -2, 22], j[d2, 2, 22]]; l2 = DateRange[j[d1, -1, 6], j[d2, 3, 6]]; ints = Interval /@ Map[abst, Transpose@{l1, l2}, {2}]; iu = IntervalUnion@@(IntervalIntersection[Interval[abst/@{d1,d2}], #] & /@ ints); ...


3

Given a sounds wave, you can read the samples, and plot part of the sounds wave fname = "ExampleData/rule30.wav"; ele = Import["ExampleData/rule30.wav", "Elements"] So the sound file contains the above elements. You can import each on its own. fs = Import[fname, "SampleRate"] You can look at first few milliseconds of the sound data = ...


3

It seems to be a bug, that manifests because of the fact that the answer could be positive or negative. If you look at the Trace of this operation, with TraceInternal -> True, you can see that it gets as far as finding the answer as a rounded version of $\pm720000\sqrt{6}$. It has candidate solutions of either Ceiling[-720000 Sqrt[6]] or Floor[720000 ...


3

this would be a definition which does what you want for a list of rules: f[r : {__Rule}] := someFunction @@@ r and this would be one which handles the Association case: f[a_Association] := someFunction @@@ Normal[a] As mentioned by Gerli in a comment in version 10.1 one can also use KeyValueMap for the second case, for which that new function was ...


3

Before I begin, a quick note. In the problem as stated, the optimal value of c is $-\infty$, since that makes the value of $f-(-\infty)g=f-\infty=-\infty$ (since $g<0$ for all $x\in(0,1)$). I'll instead work the problem of minimizing $|f-cg|$. First we can evaluate the Maximize statement, just to see what we get: Maximize[{Abs[f[x] - c g[x]], 0 < x ...


3

first = {1 + a, 2 + a, 3 + a, 4 + a}; t = {0, 1, 2, 3}; MapThread[#1 /. a -> #2 &, {first, t}] (* {1, 3, 5, 7}*) or #[[1]] /. a -> #[[2]] & /@ Transpose@{first, t} (* {1, 3, 5, 7}*)


3

Note change at end of your function... you might also want to add checks to ensure that there was a solution so as not to return a nonsense function. myodessystem[k1_, k2_, time_] := Module[{odes, y, x, sol, myfun}, odes = {y'[x] == k1 y[x] Cos[k2 x + y[x]], y[0] == 1}; sol = NDSolve[odes, y, {x, 0, time}]; myfun = First[y /. sol]]; mysolve = ...


3

Dilation produces the same output as MaxFilter and has comparable speed. test = {5, 6, 9, 3, 2, 6, 7, 8, 1, 1, 4, 7}; Dilation[test, 1] (* {6, 9, 9, 9, 6, 7, 8, 8, 8, 4, 7, 7} *) It also has a Padding option which may be convenient: Dilation[test, 1, Padding -> 10] (* {10, 9, 9, 9, 6, 7, 8, 8, 8, 4, 7, 10} *) Dilation[test, 1, Padding -> ...


3

WeatherData can be told to give you all the weather stations it knows about, but there's more than one type of weather station (and duplicates!): allWeatherStations = WeatherData[]; fullformStations = FullForm /@ allWeatherStations; Tally[StringLength /@ fullformStations[[All, 1, 2]]] (*{{4, 6919}, {5, 10306}, {8, 4697}}*) Looking at the documentation, I ...


3

Treat the maximum machine number as a singularity: ListPlot[Table[{a, NIntegrate[x Exp[-(a^2 + 0.001^2) x^2], {x, 0, 0.5 Log[$MaxMachineNumber/(a^2 + 0.001^2)], 8000}]}, {a, 0.001, 1, 1/100}], Joined -> True Update [Forgive me, I actually have a job, and, while I could solve the problem quickly over breakfast, I could not compose a complete ...


2

Using NotebookWrite in this manner is really no different from manually modifying the content of an Output cell. The FrontEnd converts the cell to Input, since it anticipates the user would be interested in evaluating it afterwards. What style is used is determined by DefaultDuplicateCellStyle.


2

f[a_List] := a.{3, 4, 5, 6, 7, 12}; f[{1, 2, 3, 4, 5, 6}] (* 157 *) Incidentally, do not use upper-case variables, as it is likely to conflict with internal functions (such as N). I presume you know the number of components of a (i.e., you're not asking about inputting a vector of arbitrary length), since you apparently have a fixed x (of known length). ...


2

I take it you are interested in the minimum in the absolute value of the difference Abs[f[x]-c g[x]]. Otherwise, the answer is trivial: c=-Infinity. Let us define the functions: f[x_] := (-1 + x) Log[1 - x] - x Log[x]; g[x_] := Log[1 + 2 (-1 + x) x]; w[x_, c_] := f[x] - c*g[x]; and plot them: Manipulate[Plot[w[x, c], {x, 0, 1}], {c, -3, -0.1}] ...


2

r = {"a" -> "1", "b" -> "2", "c" -> 3, d -> 231, "e" -> 1.25}; someFunction[key_, value_] := {key, value}; (* say *) f = someFunction @@@ # & f@r {{"a", "1"}, {"b", "2"}, {"c", 3}, {d, 231}, {"e", 1.25`}}


2

Using the generator in your post: RandomPolynomial[degree_Integer?Positive, distribution_: NormalDistribution[0, 1]] := With[{functionbody = Sum[RandomVariate@distribution #^(i), {i, 0, degree}]}, functionbody &]; Generate some polys, and their derivatives: degree = 3; var = x; number = 5; polys = ...


2

In Mathematica v10.1 there's undocumented GeneralUtilities`ReapList function. It accepts two arguments: expression and a tag. Using it on adapted test suite from OP: Needs["GeneralUtilities`"] ClearAll[tag] ReapList[Sow[15, tag], tag] (* {15} *) ReapList[Sow[{}, tag], tag] (* {{}} *) ReapList[ Sow[{16, 17}, tag]; Sow[18, tag]; Print["hello"]; Sow[{19, ...


2

I evaluated the computational complexity in the two main aspects: Time (Runtime/CPU-Time) Space (Memory) with regard to increasing i for artificial parameters: cpl=Module[{a, b, z}, Table[ a = Range[i]; b = Range[i]; z = RandomReal[{-10., 10.}]; AbsoluteTiming[MaxMemoryUsed[HypergeometricPFQ[a, b, z]]], {i, 1, 500} ]] Time complexity: ...


2

Here's something very similar to the Neat example found in the Documentation for FindShortestTour. Is this what you're asking for, or are you asking how FindShortestTour was implemented? (* grab random cities and their GeoPositions *) cities = SemanticInterpretation["US state capitals"]; locs = EntityValue[cities, "Position"]; (* find the shortest tour *) ...


2

With N replaced by n and exact numbers, the function in the Question can be written as f[n_] = Sum[Binomial[n/2 - 1, a]*Binomial[n/2 - 1, a - 1]*(7/20)*(3/10)^(n - a - 1), {a, 2, n, 2}] Although Mathematica can perform the Sum, the result in terms of HypergeometricPFQ is not particularly enlightening. Instead, plot f[n]. ListLogPlot[Table[f[n], {n, ...



Only top voted, non community-wiki answers of a minimum length are eligible