Tag Info

New answers tagged

2

The method described in File-name completion for custom functions can be used to complete the "description" argument in your examples, but it won't work for the curried parameters and variables. Note that the method does not appear to support dynamic computation of completion choices, so it cannot be used to generate the list directly from a symbol's ...


2

You did not explain (at least to my satisfaction) what this code it supposed to do therefore is hard to give a rigorous answer, but I will attempt to cover multiple issues. First a note: Module[{}, . . . ] is arguably a purposeless construct, one I have no use for and which I remove with every opportunity I get. However at least one very experienced user ...


3

Why not just this? f[l_] := Block[{a,b,c}, a = Length[l]; b = First[l]; c = b/a; c*10] Then f[{2, 3, 4, 5}] 5


6

If you have Version 10 use the function Where in the GeneralUtilities package like this: Needs["GeneralUtilities`"] f[l_] := Where[a = Length[l], b = First[l], c = b/a, c*10] Then: f[{2, 3, 4, 5}] 5


2

For N[ _, 40] all inputs must have at least that precision. Your definition of Z[1], Z[2], and z[3] use weights with just machine precision. Rationalize the definitions. Z[1] = EmpiricalDistribution[ {0.5, 0.5} -> {0, 1}] // Rationalize; Z[2] = EmpiricalDistribution[ {0.6, 0.4} -> {0, 1}] // Rationalize; Z[3] = EmpiricalDistribution[ ...


0

Posting this as an answer because it's too long for a comment and it's a significant correction and amplification of @Hagus's answer. First, we need for options to be a BlankNullSequence to handle natural call forms where options need not be packaged in lists: ClearAll[constructorWithOptions]; constructorWithOptions[symbol_, options___Rule] := ...


5

I believe this results from Association being atomic (or "not NormalQ" as Taliesin puts it) without being fully overloaded to behave as a normal expression of equivalent structure. Observe: asc = <|"a" -> q, "b" -> r, "c" -> s|>; Block[{q = 1, r = 2, s = 3}, asc] <|"a" -> q, "b" -> r, "c" -> s|> Also: With[{q = 1, r = ...


6

I think your problem is probably related to the way OptionValue[name] is evaluated. When you use a regular List instead of an Association, you'll find the whole thing works. I don't have the mathematica-fu to understand why in detail, but here is an alternative solution that makes your function definitions pretty concise. Define a utility function that ...


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


6

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


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


6

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


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


0

You can use Mod with a non-integer second argument: f[a_] := Ceiling[Mod[a, 2 \[Pi]]/\[Pi]] To generalize you can make a Periodic operator as follows: Periodic[T_, offset_: 0][f_] := f@*((Mod[#, T] - offset) &) f2pi = Periodic[2 \[Pi]][Ceiling[#/\[Pi]] &] f3pi = Periodic[3 \[Pi], 0.1][Ceiling[#/\[Pi]] &] quadratic[x_] := (x/\[Pi])^2; parabola ...


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


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 π]/π]


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


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


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


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


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


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


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


1

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


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


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


0

One way to do this, most likely not the most elegant, is to rename variables of the form a[n] temporarily. Suppose v = (a[1] + a[2]) b[1]. Then define f[a1_, a2_, b1_] := Evaluate[v /. {a_[n_] :> ToExpression[ToString[a] <> ToString[n]]}] With this definition you get the desired result if you evaluate f[a[1],a[2],b[1]].


0

Are you trying to nest functions? i.e. are 'a' and 'b' two functions that you apply the parameter values of '1' or '2' to them and then apply the results to f? Or in the case that a[1] is simply a name for a variable (something like 'x') Then maybe you're simply looking for f[x_,y_,z_]:=(x+y)z


1

Yes! You have passed the same function f into a pure function so it is evaluated every time you calculate g. Specially you define f by = so Mathematica calculates it once and then re-use it in Do loop. For the sake of demonstration if I define f by :=: f1 := Interpolation[Table[{i, i^2}, {i, -5, 5}]]; f2 = Interpolation[Table[{i, i^2}, {i, -5, 5}]]; ...


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


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


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


0

FindSequenceFunction[{1/2, 1/3, 1/4, 1/5, 1/6}, n] 1/(1 + n)


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



Top 50 recent answers are included