Tag Info

Hot answers tagged

18

One way would be to redirect all messages issued by ToExpression to a string-stream. Here is an example of that approach, with minimal error-checking: Needs["Developer`"] interpret[str_String] := Module[{s = StreamToString[], r, m} , Block[{$Messages = {s}}, r = ToExpression[str, InputForm, HoldComplete]] ; m = StringFromStream[s] ; Close[s] ; ...


12

UPDATE: quite interesting parallel discussion and solutions (see Emerson Willard answer) can be found HERE. Maybe this is not exactly what you are looking for, but at least this gives you a very close guess and it is easy to figure out the rest. dis = ProbabilityDistribution[ 1/(2*E^((-m + Log[5])^2/8)*Sqrt[2*Pi]), {m, -Infinity, Infinity}]; PDF[dis, ...


9

f[m_] = 1/(2*E^((-m + Log[5])^2/8)*Sqrt[2*Pi]); Integrate[f[m], {m, -Infinity, Infinity}] 1 dist = ProbabilityDistribution[f[m], {m, -Infinity, Infinity}]; Since the integral of f[m] is unity, f[m] does not have to be scaled to be a distribution. A candidate distribution will probably have two parameters and must be defined on the interval ...


8

I think you've found a bug in pattern matcher. This problem can be reduced to matching sequence of length one with named BlankSequence patterns in Orderless functions, it stopped working in v10.1. In previous versions your replacement rule works (as noted by belisarius). Minimal example of this behavior is: ClearAll[f, a] SetAttributes[f, {Orderless}] ...


6

The following appears to be what you want. repl[expr_, max_] := With[{largest = Max[Cases[expr, C[n_] :> n, Infinity]]}, If[largest <= max, expr /. (R[C[largest], anything__] :> R[C[largest], anything].(II + R[C[largest + 1], C[largest]])) // repl, expr] ] repl[myExpr, 7] Alternative formulation if recursion doesn't float ...


6

Update In the interest of simplifying the code somewhat, I've modified one of the replacements. For instance, we can do expr2 = Thread[expr1, Plus] /. Plus -> Times or epxr2 = expr1 /. expT[Plus[a__]] :> Times @@ expT /@ a rather than expr2 = expr1 //. {expT[a_ + b_] :> expT[a] expT[b]} So: f[expr_] := Thread[expr /. Power[E, a_] :> ...


4

Not that I would recommend this, but anyway: positionPayload = 2. x + 3. t^2; variableList = Variables[positionPayload]; positionPayload = Function[Evaluate@variableList, Evaluate@positionPayload]; positionPayload[q, r] (* 3. q^2 + 2. r *) Edit positionPayload = 2. x[1] + 3. t[3]^2; variableList = Variables[positionPayload]; ul = ...


3

I'm certain that this will not be the solution to whatever real problem there is behind your question, but incidentally, you could use this function from a former answer fultzTokenize[t_String] := Cases[MathLink`CallFrontEnd[ FrontEnd`UndocumentedTestFEParserPacket[t, False]], _String, Infinity] fultzTokenize["a+b+c-c"] it gives you but be ...


3

You might try something like this, which makes pure functions, which means the variables used int the expression to converted only have to be clear at time expToF is called. expToF[exp_, vars : {_Symbol ..}] := With[{body = exp /. Thread[Rule[vars, Slot /@ Range @ Length[vars]]]}, Function[body]] Clear[x,t] f = expToF[2. x + 3. t^2, {x, t}]; f[x,t] ...


3

This expression, designated exp for convenience, can be simplified substantially as follows. num = Map[FullSimplify[#] &, Numerator[exp]]; Map[FullSimplify[#, θ[_] ∈ Reals && ϕ[_] ∈ Reals] &, Denominator[exp] /. Abs[z_]^2 :> FullSimplify[Abs[z]^2, θ[_] ∈ Reals && ϕ[_] ∈ Reals]; /. Abs[z_]^2 :> z^2] den = ...


3

If I understand you correctly: {a, b, c} = Extract[{{1}, {2, 1, 2}, {2, 2, 2}}]@ {-1, {x -> 0, y -> 1}} After this, a == -1 && b == 0 && c == 1.


2

My one shot at answering this question: Attributes[convert] = {HoldFirst}; convert[def_Symbol?ValueQ] := With[{old = def, pats = Quiet[Sequence @@ Cases[Variables @ def, s_Symbol :> s_]]}, ClearAll[def]; def[pats] := old; ] Test: positionPayload = 2. x + 3. t^2; convert[positionPayload] ?? positionPayload Global`positionPayload ...


2

positionPayload = 2. x + 3. t^2 (* 3. t^2 + 2. x *) variableList = DeleteDuplicates[Variables[positionPayload]] (* {t, x} *) temp = positionPayload; positionPayload =. Evaluate[positionPayload @@ (Pattern[#, Blank[]] & /@ variableList)] := Evaluate@temp Definition@positionPayload (* positionPayload[t_, x_] := 3. t^2 + 2. x *) positionPayload[q, r] (* ...


2

In V10, here are a few ways to use Values: {a = #, {b, c} = Values[#2]} & @@ Minimize[{x - y, -3 x^2 + 2 x y - y^2 >= -1}, {x, y}] (* {-1, {0, 1}} *) {a, {b, c}} = Minimize[{x - y, -3 x^2 + 2 x y - y^2 >= -1}, {x, y}] /. sol : {__Rule} :> Values[sol] (* {-1, {0, 1}} *) With[{minsol = Minimize[{x - y, -3 x^2 + 2 x y - y^2 >= -1}, ...


2

Here's how I handle roots of integer power: myroot[x_, n_, k_] := Root[#^n - x &, k] allroots[multivalexpr_] := FixedPoint[ Function[{expr}, Flatten[MapAt[ Function[{ex}, Replace[ex, myroot[sym_, i_] :> (myroot[sym, i, #] & /@ Range[i])]], expr, FirstPosition[expr, myroot[_, _], {0}]]]], multivalexpr] Then ...


2

To fix this problem on Mma 10.1 on OS X 10.10.4 I took off one of the blanks on term, i.e. ReplaceAll[a.b.c.d + a.ss.e.g.r + Transpose[a.b.c.d], {Plus[front___, term__, middle___, Transpose[term__], end___] :> Plus[front, middle, end, 2*term]}] a.b.c.d + a.ss.e.g.r + Transpose[a.b.c.d] ReplaceAll[a.b.c.d + a.ss.e.g.r + Transpose[a.b.c.d], ...


2

I have uploaded your table.txt to a code sharing server. This is exactly like the data should look on your disk. As already mentioned in several comments, you just have to import it as "Table" and everything is fine: data = Import["http://hastebin.com/raw/cudesisezu", "Table"]; data[[2]] (* {0, 0.997046, -4.00611*10^-6, -0.00442103, 0.299956, 0, ...


1

Cases[expr, m[a___] :> a, Infinity] (* {{d,f,g},p}*) Be careful with expr = m@m@p; Cases[expr, m[a___] :> a, Infinity] (* {p} *) versus expr = m@m@p; Cases[expr, m[a___] :> a, {0, Infinity}] (* {p, m[p]} *)


1

I think you are making a mountain of a mole hill. Just because you can see a difference in the two orderings doesn't mean Mathematica can. Consider the following expressions. MatchQ[(2 Conjugate[B] μ[1] + 2 B μ[2]), (2 b_ h_[2] + 2 Conjugate[b_] h_[1] )] True Position[μ[2]] /@ {(2 Conjugate[B] μ[1] + 2 B μ[2]), (2 B μ[2] + 2 Conjugate[B] μ[1] )} ...


1

I believe the most clear way is something like {a, {b, c}} = {#[[1]], {x, y} /. #[[2]]} &@ Minimize[{x - y, -3 x^2 + 2 x y - y^2 >= -1}, {x, y}]


1

I think I'll go with this one. {a, b, c} = With[{r = Minimize[{x - y, -3 x^2 + 2 x y - y^2 >= -1}, {x, y}]}, Extract[r, Position[r, _?NumericQ]]]


1

I think that the following three methods are clear and sufficiently verbose (and work in all MMa versions at least starting from version 5): res = Minimize[{x - y, -3 x^2 + 2 x y - y^2 >= -1}, {x, y}]; Clear[a, b, c] {a, {b, c}} = res /. Rule[_, v_] :> v {-1, {0, 1}} Clear[a, b, c] {a, {b, c}} = res /. r_Rule :> Last[r] {-1, {0, 1}} ...



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