Tag Info

Hot answers tagged

7

We can use this function to see that the conversion is not made during parsing: parseString[s_String, prep : (True | False) : True] := FrontEndExecute[FrontEnd`UndocumentedTestFEParserPacket[s, prep]] parseString["x x"] {BoxData[RowBox[{"x", "x"}]], StandardForm} We can use this to see that the conversion does not take place while converting boxes ...


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


5

Taking the question at face value, I think the answer is yes. A While loop While[test, body] evaluates test then body until test fails to give True. This can be implemented in NestWhile by putting test and body into Functions: NestWhile[body &, Null, test &] For example the following both do the same thing: i = 0; While[i < 10, i++]; i = 0; ...


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


4

In Mathematica version 10, you can also use Inactive to allow the Symbol to be created before doing the assignment. Here is an example: Clear["x"]; Activate[Inactive[Set][Symbol["x"], 3]] (* ==> 3 *) x (* ==> 3 *)


4

Mathematica embraces the notion of canonical form. Internally it wants all expressions to be reduced to their canonical forms. Times[x, x] is not a canonical form, so it gets reduced to Power[x, 2], which is. I share Mr.Wizard's occasional frustration over the choices of canon made by Wolfram Research, but I think it is far too late to expect any change. ...


4

There may be a better way to do this, but perhaps 4 Fm Derivative[1][Fm] /. Function[b_] :> b // Evaluate // Function 4 Cos[#1] Sin[#1]& 1/4 Fp (2 Fp - Fp^3/Fm^2 + 4 Fm Derivative[1][Fm] Derivative[1][Fp]) /. Function[b_] :> b // Evaluate // Function 1/4 Cos[#1] Sin[#1] (2 Cos[#1] Sin[#1]-Cos[#1]^3 Sin[#1]+4 Cos[#1] Sin[#1] ...


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


4

Maybe I don't understand what you're trying to do, but... q = <|elems -> ConstantArray[Null, 4]|> q[[1, 2]] = 42; q <|elems -> {Null, 42, Null, Null}|> q[[Key[elems], 4]] = 99; <|elems -> {Null, 42, Null, 99}|> So whether you set by position or Key it works.


4

To compute $\nabla^nr$ for arbitrary integer $n$, you can use the built-in tensor derivative syntax. For example, you can compute the second-derivative $\nabla^2r$ using r = Sqrt[x^2 + y^2 + (z - a)^2]; X = {x, y, z}; D[r, {X, 2}] To get an answer in terms of $r$, you can sort of cheat your way to the correct answer via the following modification: r = ...


4

As others have noted, it's not entirely clear what you want. A possibility is you want to "simplify" the rules so that, on application, all four variables would give numbers. I'll show a way to obtain that and possibly you can modify to suit the actual need. We'll start with the lists of rules and create defining polynomials and also extract the variables ...


3

Update Following the new examples you gave I believe this does what you want: Thread[lists[[All, 1]] -> (lists[[All, 2]] //. lists)] Output: {v[4] -> 2000000000000, v[6] -> 4.92958*10^12, v[15] -> 100000000, v[31] -> 6.*10^11, v[32] -> 0.05, v[33] -> 2000000000000, v[35] -> 400000000000, v[41] -> 1.6*10^12, v[45] -> 1, ...


3

You can "inject" that variable by grabbing it with With. It then gets put into the body of the With verbatim, hence it gets inside the Function in the desired form. f2[expr_] := With[{var = First@Variables[expr]}, expr /. var -> # &] f2[x^2] (* Out[79]= x^2 /. x -> #1 & *) This can also be done in the way you had tried, with a bit of work ...


3

I had a flash of insight and did the following. Notice particularly the necessary RuleDelayed on the elems tag in the association: q = Module[{storage = ConstantArray[Null, 4]}, <|elems :> storage, set -> ((storage[[#1]] = #2) &)|>]; q[set][2, 42]; q[elems] {Null, 42, Null, Null}


3

Plot has attribute HoldAll, therefore the expression Fit is hold in an unevaluated form. Using Attributes@Plot (* out *) {HoldAll, Protected} reveals the attributes


2

Merely my own variation of the existing answer by mfvonh: expr = 1/4 Fp (2 Fp - Fp^3/Fm^2 + 4 Fm Derivative[1][Fm] Derivative[1][Fp]); Fm = Sin[#] &; Fp = Sin[#] Cos[#] &; FullSimplify[expr /. (x_ &) :> x] Function @@ {%} 1/32 (3 + 7 Cos[2 #1]) Sin[2 #1]^2 1/32 (3 + 7 Cos[2 #1]) Sin[2 #1]^2 &


1

Another replacement version: expr /. (x_ &) :> x /. x_ :> (x &)


1

Postfix-Definition Clear@"`*" Sin*Cos // f[r_] := Sin[r]*Cos[r] Sin*Cos // f[Pi/7] (* out *) Cos[Pi/7] Sin[Pi/7] Prefix-Definition Clear@"`*" g[r_][Sin*Cos] := Sin[r]*Cos[r] g[Pi/11][Sin*Cos] (*out*) Cos[Pi/11] Sin[Pi/11] Just 1 letter more to type


1

Expanding on Mr.Wizard's answer, the side effects can be eliminated simply be doing the work inside a function rather than at top-level. list1 = {a -> b + c, d -> b + c}; list2 = {b -> 1, c -> 1}; f[list1_, list2_] := {list1, list2} /. list2 // Values f[list1, list2] {{2, 2}, {1, 1}} Column@{{a, b, c, d}, list1, list2}


1

The random crashing of the kernel seems to arise due to your definition of evolve and fct. I'll keep h[x_, p_] := -x^2 + p^2 + x^4; and change the definition of evolve to evolve[xini_, pini_, time_] := Module[{x, p}, Hold[{x[time], p[time]} /. First@NDSolve[{x'[t] == p[t], p'[t] == -x[t], x[0] == xini, p[0] == pini}, {x, p}, ...


1

Manipulate is based on Dynamic, so if output of Manipulate is already created, then it will dynamically update every time when related variables have changes. If you want to stop such behavior of Mathematica, you need to stop dynamic update temporary. And Start it again when it is needed. You can do it through GUI or by usual Mathematica command ...


1

r[x_, y_, z_] = Sqrt[x^2 + y^2 + (z - a)^2]; D[r[x, y, z], #] & /@ {x, y, z} {x/Sqrt[x^2 + y^2 + (-a + z)^2], y/Sqrt[ x^2 + y^2 + (-a + z)^2], (-a + z)/Sqrt[x^2 + y^2 + (-a + z)^2]} or more simply, % == D[r[x, y, z], {{x, y, z}}] True %% == {x, y, z - a}/r[x, y, z] True EDITED to add higher order partial derivatives Second ...



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