Tag Info

Hot answers tagged

7

The issue is lack of HoldFirst in attributes of your function, Sow is being called too early otherwise before Reap can capture ClearAll[reap2] SetAttributes[reap2,HoldFirst] reap2[x_]:=Join @@ Reap[x][[2]] idea to use Join@@ taken from @Mr.Wizard from here What is shorthand way of Reap list that may be empty because of zero Sow


6

The convolution approach is quite flexible. For example, here a Gaussian function is used to round the edges of the rectangle: f[y_] = Convolve[Exp[-100 x^2], UnitStep[x - 1] - UnitStep[x - 2], x, y]; Plot[f[y], {y, 0, 3}] One nice thing about the Gaussian is that it gives an analytic form, as you can see by querying f[y] 1/20 Sqrt[π] Erfc[10 - 10 y] - ...


6

Generate and export Unique variables from LowLevelFunction. LowLevelFunction[ba_] := Module[{res}, res = Unique["res"]; Table[res[elem] = If[ToString[ba] == "yes", X, 2 X], {elem, {one, two}}]; res] These variables are not temporary, and you should be able to save them


5

You should use Block to keep external definition from being substituted while the RHS of the definition is being evaluated. rem = 1; Block[{u, rem, reengy, fun}, refun[u_, rem_, reengy_] = DSolveValue[{D[D[fun[u], u], u] + (-rem^2 + 1/4)/u^2*fun[u] - u^2*fun[u] + reengy*fun[u] == 0}, fun[u], u] /. C[1] -> 0 /. C[2] -> 1]; ...


5

You can use Fold instead: f[n_Integer] := Fold[#2/(1 + #) &, n, Reverse@Range[n - 1]] f[3] $\frac{1}{1+\frac{2}{1+3}}$ It not very useful analytically, but it allows you to invoke the CPU gods: f /@ Range[50] // ListLinePlot[#, PlotRange -> All] &


4

Yes, you can use Round. The second argument of Round is the quantization step. Examples: Plot[ Round[Sin[x], 0.2], {x, 0, 2 Pi} ] Plot[ Round[Sin[x], 0.2], {x, 0, 2 Pi}, ExclusionsStyle -> Automatic ]


3

Could it be that you're looking for the function Fold rather than Nest? Fold[f[n - #2, #1] &, a, Range[5]] f[-5 + n, f[-4 + n, f[-3 + n, f[-2 + n, f[-1 + n, a]]]]]


2

Please read the documentation about Piecewise f[x_, i_, t_] := Piecewise[{ {i, 1/(1 + i) < Abs[x - t] <= 1/i}, {0, x == t} }] Plot Manipulate[Plot[f[x, i, t], {x, -2, 2}], {{i, 1}, -2, 2}, {{t, 1}, -2, 2}] Plot3D[ f[x, i, 0], {x, -2, 2}, {i, -1/2, 1} , MaxRecursion -> 5 , PlotPoints -> 50 , AxesLabel -> {"x", "i", "f"} ...


2

First thing to note is that your HighLevelFunction is not relevant here. When it's evaluated, res variable, inside Module, evaluates to result of LowLevelFunction call. res$... symbols you're seeing are coming from Module in your LowLevelFunction. In this module you're saving your calculation results as DownValues of temporary module variable res. If you ...


2

Perhaps something like this: allNumeric[vars_] := VectorQ[{vars}, NumericQ] (* define once, use many times *) f[x_, y_, z_, t_] /; allNumeric[x,y,z,t] := ...


1

I'm afraid I don't know what realized covariance means. Perhaps the easiest solution is to use RLink and directly use the R implementation. Here are some links to the documentation to get you started. http://reference.wolfram.com/language/RLink/guide/RLink.html http://reference.wolfram.com/language/RLink/tutorial/UsingRLink.html


1

That's because you set Psi to accept only real x. Meanwhile symbol x is not generally considered to be real by Mathematica, so just delete _Reals or use it with assumptions: Assuming[x \[Element] Reals , \[Psi][x, 1]]


1

initialvls = {}; m = 0; f[x_] := Module[{}, If[x[[2]] > m, m = x[[2]]; initialvls = x];] Thread[ff[{{a, 1}, {b, 2}}]] /. ff -> f; initialvls (* {b, 2} *) m (* 2 *) or initialvls = {}; m = 0; f[x_] := Module[{}, If[x[[2]] > m, m = x[[2]]; initialvls = x];] Thread[Hold[f][{{a, 1}, {b, 2}}]] // ReleaseHold; (* {b, 2} *) m (* 2 *)


1

We can express f[n] as a continued fraction: f[n] == ContinuedFractionK[k, 1, {k, 1, n}] Now unfortunately, Mathematica says the following diverges, when it really doesn't: ContinuedFractionK[k, 1, {k, 1, Infinity}] ContinuedFractionK::div: The continued fraction does not converge. >> ContinuedFractionK[k,1,{k,1,Infinity}] We can easily ...


1

You can use Select if you are willing to modify your function slightly: Select[lis, (Function[{x, y}, #][2, 2] == 4) &] (*{x + y, x y}*)


1

list = {x - y, x + y, x*y}; Using a different test function, that tests for the equality and accepts the equality value as a parameter: testFun[{a_, b_, c_}] := (# /. {x -> a, y -> b}) == c &; testList = {{2, 2, 4}, {2, 0, 2}}; (* {2, 2, 4} means f[2,2] == 4, and {2, 0, 2} means f[2,0] == 2 *) You can either Map over list, returning the ...


1

All you have to do is to be consistent about using only expressions as arguments to the operator, not function heads. The reason is that the operator as you define it returns an expression and not a function. That's why you then cannot apply the operator twice. But by working only with expressions, this problem is avoided: xleft = Function[f, D[f, x] - y ...


1

If you don't like the ReplaceAll approach, you can do it with Block and Set: Clear[f] f[n_Integer] := {x[0], Array[x, n]} Clear[fp] fp[var__] := Block[{x}, MapIndexed[Set[x[#2[[1]] - 1], #1] &, {var}]; f[Length@{var} - 1] ] f[3] fp[a, c, 3, w] {x[0], {x[1], x[2], x[3]}} {a, {c, 3, w}} In case you even don't like ...


1

Not sure if this would this would achieve what you described (I haven't been using MMA as much as I would like so my answer might be clumsy): Clear[countSameLetters] countSameLetters[list_List, distance_Integer] := Position[ Flatten@Differences@Position[list, #] & /@ CharacterRange["a", "z"], distance] // Length To display each distance with ...


1

comp[a_, b___, c_] := a == c k[list_, dist_] := Count[ListConvolve[SparseArray[{1 -> 1, # -> 1}, #]&[dist+1], list, {-1, 1}, 0, Times, comp], True] list = {d, a, e, a, e, b, e, b, c, d, d}; k[list, #] & /@ Range[Length[list] - 1] (* {1, 4, 0, 1, 0, 0, 0, 0, 1, 1} *)



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