Tag Info

Hot answers tagged

13

There are a number of options and their attractiveness will depend on the scenario for their use, therefore it is difficult to make any broad recommendations of best practice. I will say that generally it is not recommended to rely on global assignments as in your first example, because this method scales poorly and because it is easy to make mistakes and ...


10

In V10, another option is to use Association. par=<|"mu"->1,"sigma"->1,"lb"->0,"ub"->10|>; f[x_, p_Association:par] := PDF[LogNormalDistribution[p["mu"], p["sigma"]], x] Plot[f[x, ##], {x, #lb, #ub}] &@par Another form for Plot is: Plot[f[x, par], {x, par@"lb", par@"ub"}] And as @Mr.Wizard commented, you can use the default ...


7

"Is this a better way?" Yes, but I think there are alternatives: Nest newton1[fun_, xi_, n_] := With[{f = fun/D[fun, x]}, Nest[# - f /. x -> # &, 2., n]] newton1[x^3 - 2, 2., 10] 1.25992 NestList newton2[fun_, xi_, n_] := With[{f = fun/D[fun, x]}, NestList[# - f /. x -> # &, 2., n]] ListLinePlot[newton2[x^3 - 2, 2., 10], Mesh ...


7

It's on our list of things to do, but there are many other areas we want to cover, such as custom feature functions, customizable feature selection, boosting, NLP, deep learning of neural networks, convolutional nets, GPU acceleration, and so on. Until then, your only real solution is to deploy your trained classifier as an API function. Some simpler ...


7

You can Partition the data into pairs of successive values. Reverse the data to make the previous day the dependent variable. Examples: Partition[{1, 2, 3, 4, 5}, 2, 1] (* {{1, 2}, {2, 3}, {3, 4}, {4, 5}} *) Partition[Reverse@{1, 2, 3, 4, 5}, 2, 1] (* {{5, 4}, {4, 3}, {3, 2}, {2, 1}} *) Reverse /@ Partition[{1, 2, 3, 4, 5}, 2, 1] (* {{2, 1}, {3, 2}, {4, ...


6

The answer is 0, if the question is what is the value of Mean[fit["FitResiduals"]] and fit is a linear, least-squares fitted model. data = WeatherData["London", "Temperature", {{2004, 1, 1}, {2013, 12, 31}, "Day"}]; normaldata = Partition[Reverse[data["Values"][[All, 1]]], 2, 1]; fit = LinearModelFit[SetPrecision[normaldata, Infinity], x, x, ...


5

Using @b.gatessucks' hint, I solved it with the following transformation: max = Max[data]; Show[ ListPlot[{Sqrt[max #[[2]]] Sin[Sqrt[max #[[1]]]], Sqrt[max #[[2]]] Cos[Sqrt[max #[[1]]]]} & /@ data, AspectRatio -> 1, PlotRange -> {{-20, 20}, {-20, 20}}], ParametricPlot[{t Sin[t], t Cos[t]}, {t, 0, 20}]]


5

Some silliness with Optional i.e. : (thanks @Mr.Wizard for the -. tip). I thought I could ride the Optional gravy train all the way but just could not find a way to deal with arguments that equal to 1 i.e. x^0 p^0, so I defined another instance of the function to convert 1 to x^0 p^0 (and then pass it back to the original definition) (oh yes I can!). This ...


5

For me your command doesn't run. I run: t = QuantityMagnitude[ WeatherData[ "California", "MeanTemperature", {{2013, 1, 1}, {2014, 1, 1}, "Day"} ]["Values"]]; This contains all temperature quantities. A list of dates in your period is easily constructed by DateRange[DateObject[{2013, 1, 1}], DateObject[{2014, 1, 1}]] so ...


4

list = Range[20]^2 (*{1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, \ 289, 324, 361, 400}*) f[n_] := list[[n]] f[10] (*100*)


4

The generalized definition of partial fails because the generated function looks like this: partial[accumulate, $myAccumulator] (* accumulate[Sequence@@Quiet[Join[$myAccumulator,##1]]]& *) Note how accumulate will be called with only a single argument. Since accumulate is HoldFirst, that argument will not be expanded into a sequence of arguments and ...


3

You can also use the built-in function SquareWave: ClearAll[f]; f[x_] := SquareWave[{2, 1}, x/(2 Pi)]; Plot[f[x], {x, 0, 10 Pi}, Ticks -> {Pi Range[10], Automatic}, ExclusionsStyle -> Automatic, PlotRange -> {0, 3}, ImageSize -> 400] Update: another alternative: Use ListInterpolation with options InterpolationOrder->0 and ...


3

Perhaps myfcn2[arg1_, arg2_] := basefcn @@ Flatten[Exponent[#, {x, p}, List] & /@ {arg1, arg2}]; myfcn2[x p^2, p] (* basefcn[1, 2, 0, 1] *) Update: To restrict the arguments to monomials (Thanks: @wxffles ) ClearAll[myfnc3]; myfcn3[arg1_, arg2_] /; FreeQ[{arg1, arg2}, Plus] := basefcn @@ Flatten[Exponent[#, {x, p}, List] & /@ {arg1, ...


3

Borrowing from Szcabolcs' answer here: Off[FunctionInterpolation::ncvb] PointsOnCurve[fun_, lim_, points_] := Module[{arclength, curvepoints}, arclength = Derivative[-1][FunctionInterpolation[Evaluate @ Norm @ D[fun, t], {t, 0, lim}]]; curvepoints = fun /. t -> # & /@ Table[InverseFunction[arclength][x], {x, 0, #, # / points}] ...


3

Fundamental problem Pardon me if I miss some points of your question as I didn't attempt to understand what your code is intended to do, because I think I understand what the problem is from the title alone. Please consider: SetAttributes[f, {HoldFirst, Listable}]; f[x_] := foo[x] f[{1, 2, 3}] {foo[1], foo[2], foo[3]} bar = {1, 2, 3}; f[bar] ...


3

This should solve your problem: ClearAll@Updater SetAttributes[Updater, HoldAll] Updater[a_, b_, c_, Sudoku_] := Sudoku[[a, b]] = c Sudoku = {{Null, Null, 8, 1, 7, 6, Null, 2, Null}, {Null, 4, Null, Null, Null, 9, 7, Null, Null}}; Updater[1, 1, 99, Sudoku]; Sudoku {{99, Null, 8, 1, 7, 6, Null, 2, Null}, {Null, 4, Null, Null, Null, 9, 7, Null, ...


2

First, how you would you find the critical points of a function, say $f(x)=x^3-x$, from scratch? I guess you might write D[x^3 - x, x] (* Out: 3x^2 - 1 *) Then, you want to know when that's equal to zero. So you might type Solve[% == 0, x] (* Out: {{x -> -(1/Sqrt[3])}, {x -> 1/Sqrt[3]}} *) where the % sign refers to the previous output. Now ...


2

I prefer to do it this way: Clear@updater SetAttributes[updater, HoldFirst]; updater[sudoku_, a_, b_, c_] := (sudoku[[a, b]] = c); HoldFirst keeps the variable sudoku in an unevaluated form, which means it stays a variable symbol and does not become a variable value, in your specific case a list of values. a, b, c are evaluated, because only the first ...


2

ClearAll[base]; base = If[Head[#] === BaseForm, BaseForm[First @ #, #2], BaseForm[##]] &; FoldList[base, 63696, {16, 8, 2}]


2

You can define your own function that works with input the BaseForm of a number, sure: myBase[a_?NumericQ, base_Integer] := BaseForm[a, base]; myBase[a_BaseForm, base_Integer] := BaseForm[ FromDigits[ IntegerString @@ a, Last@a ], base]; so that 63969 // myBase[#, 16] & // myBase[#, 8] & gives you BaseForm[63969,8] but ...


2

Here is one approach to using a more reasonable stopping criterion. For clarity we separately define a function that effects one step of the Newton-Raphson procedure. The first argument will be a function rather than a function expression. newtonStep[{f_, x_}] := {f, x - f[x]/f'[x]} newton[f_, start_, \[Delta]_] := Last /@ NestWhileList[newtonStep, ...


2

You can use a combination of Floor, Floor[x, a] gives the greatest multiple of a less than or equal to x and Mod, Mod[m, n] gives the remainder on division of m by n to get f[x_] := 1 + Floor[Mod[x, 2 π]/π]


1

Here is a method that uses UnitStep and Sin to generate the square wave. Sin produces the periodicity and UnitStep maps the sinusoid into a square-wave. This method will be faster than Which. f[x_] := 1 + UnitStep[Sin[x + Pi]] Plot[f[x], {x, -10, 10}, Exclusions -> None, PlotStyle -> Thick]


1

Here's one way to make your function periodic: f[t_] := Which[0 <= t < Pi, 1, Pi <= t < 2 Pi, 2, t < 0, f[t + 2 Pi], t > 2 Pi, f[t - 2 Pi]] For example: Plot[f[t], {t, -10, 10}] This method works well for any function you care to use (not just square waves). For instance, f[t] can be linear in one half and quadratic in the second ...


1

A slightly different way: list = Range @ 49; MapIndexed[(f[First @ #2] = #1)&, list]


1

You don't want to pass the full data set into the Manipulate. You just want to pass its name and have it evaluated inside the Manipulate. Try the following. Is it fast enough? SeedRandom[42]; data = RandomReal[{0, 1}, {500, 500, 500}]; SetAttributes[vizData, HoldFirst]; vizData[dataVar_Symbol] := Manipulate[Image[dataVar[[All, All, i]]], {i, 1, 500, 1}] ...


1

f[a_, b_, c_] = a + b + c; list = {2, 3}; f[a, Sequence @@ list] (* 5 + a *)


1

Because the latter is equivalent to {1,2,3,4,5}[[2]] = 0 which is invalid. The former stays as listCopy[[2]] = 0 You can't change (i.e. assign to) parts of a literal like this. You can change (i.e. assign to) a variable. Generally, f[x_] := x[[2]] will substitute the value of x directly. So will With[{x=...}, x[[2]] ]. In contrast, ...


1

Another alternative: Clear[f, V] V = (a[1] + a[2]) b[1]; f[x_, y_, z_] := V /. Thread[Variables[V] :> {x, y, z}]; f[1, 2, 3] (* 9 *)


1

ClearAll[f,g]; f[a_[1], a_[2], b_[1]] := (a[1] + a[2]) b[1] f[a[1], a[2], b[1]] (* (a[1] + a[2]) b[1] *) f[z[1], z[2], w[1]] (* w[1] (z[1] + z[2]) *) f[z[1], z[2], w[2]] (* f[z[1], z[2], w[2]] --- f undefined for this input pattern *) Or, more generally, g[a_[x___], a_[y___], b_[z___]] := (a[x] + a[y]) b[z] g[a[1], a[3], b[5]] (* (a[1] + a[3]) b[5] *) ...



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