Tag Info

Hot answers tagged

6

A computer is a finite machine and there is a limit to how well you can visually explore such functions. Perhaps it will give enough of a impression to sum only a few terms. Even 5 terms exceeds the monitor's ability to display all the turns. Plot[ Evaluate@Table[Sum[(1/2)^n*Cos[3^n*Pi*x], {n, 0, k}], {k, {1, 2, 5}}], {x, 0, 3}, PlotStyle -> ...


6

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 ...


5

I'm not sure how you were using FunctionDomain, but running the following example gets your error: f[x_] := If[x < 0, x, x^3] FunctionDomain[f[x], x, Reals] (* FunctionDomain::nmet: Unable to find the domain with the available methods. >> *) Changing If to Piecewise works though. f2[x_] := Piecewise[{{x, x < 0}}, x^3] FunctionDomain[f2[x], ...


5

The question of what you want to happen when the condition is not met will determine how you should proceed. If you wish the function to remain unevaluated try Condition: f[x_] := (1 + 2^n) /; 2^n < x n = 7; f[100] f[200] f[100] 129 See: Placement of Condition /; expressions Using a PatternTest versus a Condition for pattern matching For ...


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}]}] ...


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


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

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

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

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 = ...


2

Perhaps there is a more convenient way of doing this than resorting to esoteric boxes. The following uses the code you posted to define a function: summaryDisplay = DynamicModule[{open = True, sqrplus = RawBoxes@FrontEndResource["FEBitmaps", "SquarePlusIconMedium"], sqrminus = RawBoxes@FrontEndResource["FEBitmaps", "SquareMinusIconMedium"], ...


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

The default for all functions that have the option TimeZone is $TimeZone. Setting it to a different value should change it for all these functions. Unprotect[$TimeZone] $TimeZone = -0 Protect[$TimeZone] Sets the time zone to Greenwich Mean Time (GMT).


2

Lacking an example of your polynomial I did not attempt to test this extensively, but try: ClearAll[fn] fn[p_Plus] := fn /@ p fn[f_ g_] := fn[f] g + f fn[g]


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

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

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

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 *) ...


1

Edit, fixing some errors to show this "works" tend = AbsoluteTime[{2015, 4, 3, 18, 10}]; deltimed = (tend - AbsoluteTime[{2015, 4, 1, 3, 10}])/3600/24; {wholedays, fractionseconds} = {Floor[#], FractionalPart[#] 24 3600 } & @ deltimed; isnight[t_?NumericQ] := Boole[0 <= # < 6 || # >= 22] &@DateList[t][[4]]; nightfirstpartday = ...


1

If you know that x will always have 6 items then you can restrict f by f[a_List /; a \[Element] Vectors[6, Reals]] := a.x This will not evaluate the function when a has the wrong dimensions. Hope this helps.


1

Different version of Bob Hanlon's solution: f = (1 - # &)@*(1 - # &)@(# &) (*#1 &*) f@x (*x*)



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