Tag Info

New answers tagged

1

Try this: set = {a, b, c, d}; maps = Thread[set -> #] & /@ Tuples[set, {Length[set]}] If you want all permutations (one-to-one mappings from set onto set) replace Tuples with Permutations[set].


2

A simple workaround is to use Part and SlotSequence like this: ColorData[col][{##}[[n]]] Another workaround is to generate the function that is applied (@@@) with another function: pointslf[1] = RandomReal[1, {12, 7}]; Manipulate[DynamicModule[{min, max, col, fn}, {min, max} = Through@{Min, Max}@pointslf[1][[All, n]]; col = {"TemperatureMap", {min, ...


3

You can do this: Manipulate[Evaluate@Sin[Slot[n]] &[0, Pi/2], {{n, 1}, Range[2]}] but I don't think it is as handy as: Manipulate[Sin[{0, Pi/2}[[n]]], {{n, 1}, Range[2]}]


2

Solution to Question Part 1 Option 1: "Which one is the largest function within the range?" ClearAll["question*"]; questions = {{question1, 9.9-10\[Beta]}, {question2, 4.9-4.9\[Beta]}, {question3, -9.9+10\[Beta]}, {question4, -4.9+4.9\[Beta]}}; questions[[pos=Extract[ Position[#, Max@#]& @ ( Maximize[{#, ...


1

Think this answers my question plotofSystem[system_, init_, runtime_] := Module[{sys = system, in = init, tmax = runtime}, par = {sys, in}; lorenz = NDSolve[par, {x, y, z}, {t, 0, tmax}, MaxSteps -> Infinity]; ParametricPlot3D[Evaluate[{x[t], y[t], z[t]} /. lorenz], {t, 0,tmax}, PlotPoints -> 10000, PlotStyle -> Thin] ] and I define ...


3

we need to force Integrate function to evaluate w.r.t x ClearAll[f, g, x] g[x_] := 3 x^2 + 2 x f[x_] := Evaluate[20 x + Integrate[g[x], x]] f[2] (* 52 *)


5

A pattern matching approach: list = {a, b, c, d}; MapAtSequence1[f_, list_, span_] := list /. {Sequence @@ list[[;; Last@span]], e___} :> {Sequence @@ list[[;; First@span - 1]], f @@ list[[span]], e} MapAtSequence1[g, list, 2 ;; 4] {a, g[b, c, d]} An approach using Join MapAtSequence2[f_, list_, span_] := list[[;; First@span - ...


0

Courant[X_, Y_] := Module[{dim, x, y, \[Lambda], \[Omega]}, dim = Length[X]/2; x = X[[1 ;; dim]]; \[Lambda] = X[[dim + 1 ;; Length[X]]]; dim = Length[Y]/2; y = Y[[1 ;; dim]]; \[Omega] = Y[[dim + 1 ;; Length[Y]]]; Return[Flatten[{LieD[x, y], LieD[x, \[Omega]] - LieD[y,[Lambda]]- (1/2) d[interiorProduct[x, ...


5

After studying the documentation of Stack I was able to come up with a minimal working example. Thanks, enzotib! Needs["CustomTicks`"] GetGeometry[g_Graphics] := Module[{q = Rasterize[ Show[g, FrameTicks -> None, Ticks -> None, Epilog -> {Annotation[Rectangle[Scaled[{0, 0}], Scaled[{1, ...


1

It is not clear to me what you are really asking, but perhaps this will be of interest to you. {x, y} = {{a, b, c, d}, {e, f, g, h}}; p[x_List, y_List] := Inner[Times, x, y, List] p[x, y] {a e, b f, c g, d h} or q[x_List, y_List] := Thread[Times[x, y]] q[x, y] {a e, b f, c g, d h} If these simple ways of producing a list of products sparks ...


2

ClearAll["Global`*"] (* create four random lists for example *) {a, b, c, d} = RandomInteger[100, {4, 20}]; (* some rules... *) rules = {1 -> 0, 2 -> 3, 3 -> 5}; (* some function *) f[a_, b_] := a*b; (* do it *) result=Fold[f /. rules, {a, b, c, d}] Notes: Don't use uppercase initials for user-defined symbols. You may clash with built-ins. ...


4

listed = {{3, 2, 1, 5, 3, 2, 1}, {2, 3, 1, 4, 2, 1, 1}, {5, 1, 7, 5, 2, 1, 1}}; Graphics3D[Cylinder[{#[[1 ;; 3]], #[[4 ;; 6]]}, #[[7]]] & /@ listed]


5

Graphics3D[Cylinder[{{#1, #2, #3}, {#4, #5, #6}}, #7] & @@@ listed] Or Graphics3D[Cylinder[ArrayReshape[{##}, {2, 3}], #7] & @@@ listed] Or MapThread[Cylinder[ArrayReshape[{##}, {2, 3}], #7] &, Transpose@listed] // Graphics3D Or Graphics3D[Cylinder[ArrayReshape[#, {2, 3}], #[[-1]]] & /@ listed]


2

As you surely know, the likelihood of a dataset (or log-likelihood) is the (log of) probability you select that dataset randomly from a candidate distribution. As such, the proper way is to use (for a Gaussian, for instance): LogLikelihood[NormalDistribution[0, 1], data] (* -73126.5 *) If you want it as a parameterized function (e.g., by the mean of a ...


5

r[i_, dt_] := r[i, dt] = r[i - 1, dt] + v[i, dt]*dt; r[0, dt_] = {0, 0}; v[i_, dt_] := {1/100*(Min[#, 100 - #] &@Last@r[i - 1, dt]), 0.4}; ListPlot[Table[r[n, 2.5], {n, 1, 100}]] An even better implementation (similar to the answer of kguler to your previous question): f[Δt_] := NestWhileList[# + {1/100*(Min[Last@#, 100 - Last@#]), 0.4}*Δt ...


4

You can also use NestList: tbl[v_, delta_, n_, init_: {0, 0}] := NestList[# + v delta &, init, n] tbl[v, 1, 10] (* {{0, 0}, {0.01, 0.4}, {0.02, 0.8}, {0.03, 1.2}, {0.04, 1.6}, {0.05, 2.}, {0.06, 2.4}, {0.07, 2.8}, {0.08, 3.2}, {0.09, 3.6}, {0.1, 4.}} *)


3

With v = {0.01, 0.4}; you can define r[n_, dt_] := r[n, dt] = r[n - 1, dt] + v dt r[0, dt_] = {0, 0} Now Table[r[n, 1], {n, 1, 10}] {{0.01, 0.4}, {0.02, 0.8}, {0.03, 1.2}, {0.04, 1.6}, {0.05, 2.}, {0.06, 2.4}, {0.07, 2.8}, {0.08, 3.2}, {0.09, 3.6}, {0.1, 4.}} and Manipulate[ ListPlot[Table[r[n, deltat], {n, 1, 10}], PlotRange -> 20], ...


1

Common Mathematica idiom. Say, e.g., Fib numbers: ClearAll[f] ?f (* Global`f *) f[1] = 1; f[0] = 0; f[x_] := f[x] = f[x - 1] + f[x - 2] ?f (* Global`f f[0]=0 f[1]=1 f[x_]:=f[x]=f[x-1]+f[x-2] *) f[4] (* 3 *) ?f (* Global`f f[0]=0 f[1]=1 f[2]=1 f[3]=2 f[4]=3 f[x_]:=f[x]=f[x-1]+f[x-2] *) Notice how intermediate results have a pattern ...


1

pwToIf = With[{fpw = Reverse@Transpose@Internal`FromPiecewise@#}, Fold[If[## & @@ #2, #] &, fpw[[1, 2]], fpw[[2 ;;]]]] &; Examples: pw = Piecewise[{{x^3, x < -1}, {-x^2, -1 <= x < 0}, {x, 0 <= x < 1}, {Sqrt[x], 1 <= x}}]; pwToIf@pw (* If[x < -1, x^3, If[-1 <= x < 0, -x^2, If[0 <= x < 1, x, If[1 ...


0

not an answer, just a simpler demonstration of the issue ( version 9.01 ) Integrate[r (1/(1 + 4 Pi^2 r^2)) Exp[-I 2 Pi r Cos[t - u]], {r, 0, Infinity}, {t, 0, 2 Pi}] In the second case I've just copied the expression and changed t to z .. puzzling. It seems in the first case u has been assumed real and the second not.


0

(Fold[{Sequence @@ (Reverse@#2), #1} &, f[[2]], Reverse@f[[1]]]) /.List -> If (*If[x < -1, x^3,If[-1 <= x < 0, -x^2, If[0 <= x < 1, x, If[1 <= x, Sqrt[x], 0]]]]*)


1

My quick-n-dirty (meaning pretty untested on anything but simple cases): toif = With[{if = If @@@ (Reverse /@ #[[1]]), l = #[[2]]}, Nest[{Insert[#[[1]], if[[Length@#[[2]] + 1]], #[[2]]], Append[#[[2]], -1]} &, {if[[1]], {-1}}, Length@if - 1] // Insert[#[[1]], l, #[[2]]] &] &; Seems a bit more robust than your solution: ...


1

ifs = Reverse /@ (f /. Piecewise -> List /. {{x__, v_}, e_} :> {x, Join[{e}, v]}); Hold @@ Quiet@(ifs //. {x___, {a__, b__}, {c__, d__}, e___} :> {x, {a, b, {c, d}}, e} /. List -> If) (* Hold[If[x < -1, x^3, If[-1 <= x < 0, -x^2, If[0 <= x < 1, x, If[1 <= x, Sqrt[x], 0]]]]]*)


2

Note the structure of Piecewise. The first element is a list of {value, condition} pairs and the second element is the "default" value for when no other condition applies. If you view it as a list, here's how f looks: f /. Piecewise -> List {{{x^3, x < -1}, {-x^2, -1 <= x < 0}, {x, 0 <= x < 1}, {Sqrt[x], 1 <= x}}, 0} First, here is a ...


4

ClearAll[f, g] f = ConditionalExpression[(1/27)*(#^4 - 6 #^3 + 12 #^2 + 19 #), 0 <= # <= 5] &; g = InverseFunction[f]; Grid[Partition[#, 2] &[Plot[#, {x, 0, #2}, PlotLabel -> #3, ImageSize -> 300] & @@@ {{f[x], 5, "f[x]"}, {g[x], 10, "g[x]"}, {{f[x], g[x]}, 5, "{f[x], g[x]}"}, {f[x] - g[x], 5, "f[x]- g[x]"}}]] Update: ...


1

Illustrating the problem with a simple example: f = #^2 &; g = InverseFunction[f]; InverseFunction::ifun: Inverse functions are being used. Values may be lost for multivalued inverses. g[f[3]] -3 As the message says, there may be multiple solutions. Only one is returned.


1

You need a minor modification of the answer in the linked Q/A: ClearAll[eF2] eF2[z_: y] := Module[{j = 1, p = Partition[#, 2]}, First[(z[Length@p] - p[[-1, 1]]) Exp[Total[(Subtract[##] 2 z[j++] + {# - #2}^2) & @@@ p]]]] & Examples: ClearAll[a, b, c, d, e, f] eF2[][{a, b, c, d}] (* E^((a-b)^2+(c-d)^2+2 (a-b) y[1]+2 (c-d) y[2]) (-c+y[2]) *) ...


0

In the question you compare the patterns f[s__] and f[s_Sequence]. Note that these are different patterns: FullForm[f_Sequence] (* Pattern[f, Blank[Sequence]] *) FullForm[f[s__]] (* Pattern[s, BlankSequence[]] *) As the comments point out, Blank[Sequence] would be matched by an expression with head Sequence, but Sequence automatically disappears during ...


1

You can't do this. If correct behaviour is f[{{1,2},{3,4}}] -> {f[{1,2}],f[{3,4}]} then what is correct behaviour for f[{1,2}] ??? Clearly the second expression has no idea that it came from a previous application of f unless you find some way to tell it. Map is the simple and correct way of achieving what you want; I don't believe that it is ...


1

The reason is that you have an inefficient inner loop when you use myBBBlik. Every time you call myBBBlik, it takes a certain amount of time to calculate the result (on my computer, about 1/2 second of mucking around because your likelhood function is a bit complex). But myBBBlik2 creates an algebraic expression once (?or twice?) and can substitute the ...


4

In the Standard Evaluation Sequence the heads of expressions are evaluated first: If the expression is a raw object (e.g., Integer, String, etc.), leave it unchanged. Evaluate the head h of the expression. Evaluate each element of the expression in turn ... Therefore since f[1] is the head of f[1][2] it will evaluate if it has a definition that matches. ...


3

SeedRandom[42]; m1 = .5; m2 = 0; m3 = 0; m4 = .5; n1 = .5; n2 = 0; n3 = 0; n4 = .5; k1 = .5; k2 = 0; k3 = 0; k4 = .5; F = {({{m1, m2}, {m3, m4}}.# + {0, 0}) &, {{n1, n2}, {n3, n4}}.# + {1/2, Sqrt[3]/2} &, {{k1, k2}, {k3, k4}}.# + {1, 0} &}; ListPlot@NestList[RandomChoice[F][#] &, {1, 1}, 1000]


1

Maybe obvious but if you want to test if $z$ represents an integer you can use: Assuming[z \[Element] Integers, Simplify[z \[Element] Integers]] This gives True


5

Basically IntegerQ is for determining whether the object inside it is an integer, not whether it represents an integer. Since z is a Symbol, we get False. As for your function, I'm assuming you're using some variant of If[EvenQ[z], ___]. As you said, this won't work because EvenQ will always return False on a symbol since z does not have head Integer. ...


3

According to the documentation (ref/IntegerQ): IntegerQ[expr] returns False unless expr is manifestly an integer (i.e. has head Integer). [emph added] Evidently the assumption does not make z an actual integer, that is, an expression whose head is Integer.



Top 50 recent answers are included