Tag Info

Hot answers tagged

12

Original question As noted in the comments the use of Dispatch is the easiest way to make this replacement operation much faster. However taking this as an opportunity to explore other optimizations here are some examples for you to consider: (dsp = Dispatch[soln]) // RepeatedTiming // First 0.0030 Total[vars] /. dsp // RepeatedTiming {0.0038, ...


10

Update: I think I've got it. I found a token that does the replacement without bringing up a dialog. The values from the last use of the Find and Replace dialog will be used. The command is: FrontEndExecute @ FrontEndToken[nb, "ReplaceAll"] where nb is the target Notebook object. To preset the Find and Replace fields one can modify the FindSettings ...


5

Because sometimes I is not I... Hold[I] // FullForm I -> a // FullForm Results in: Hold[\[ImaginaryI]] Rule[Complex[0, 1], a] You can see that in the first expression I is held and remains the same. However, in the second expression I is evaluated (since Rule does not have any Attributes to hold its arguments). I gets evaluated into a Complex ...


4

From the first line in the details section in the documentation for ReplaceAll: ReplaceAll looks at each part of expr, tries all the rules on it, and then goes on to the next part of expr. The first rule that applies to a particular part is used; no further rules are tried on that part, or on any of its subparts." Basically what's happening is since ...


3

seems to work: expr /. Times[xx_?(! FreeQ[#, x] &) , p_plusd, rest___] :> Times[xx /. x -> 1, p, rest] and terms like: 2 Log[(sa1 (-2 + x))/(-mgl^2 + sa1 (-2 + x))] plusd[1/(1 - x)] and Log[2 + mgl^2/sa1 - x] plusd[-(2/(-1 + x))] were reduced to 2 Log[-(sa1/(-mgl^2 - sa1))] plusd[1/(1 - x)] and Log[1 + mgl^2/sa1] plusd[-(2/(-1 + x))] ...


3

Everything is as it should be. In the first instance, you want a separate rule for each element of the list. For that, you can use Thread: Subscript[Ε, si] = {Ex, Ey, Ez}; Subscript[B, si] = {Bx, By, Bz}; Subscript[S, si] = 1/Subscript[μ, 0] (Subscript[Ε, si]* Subscript[B, si]); Subscript[S, si] /. Thread[Subscript[B, si] -> Subscript[B, si]/c] ...


3

Look at LK4[{a, b, c, d}, I, 0, 0, 0, 0] What has happened is that the a in the argument {a, b, c, d} has been replaced by {1, 2, 3, 4} in the Sum[..., {a, 1, 4}] code in the definition of LK4. If you change the definition of LK4 to use a different iterator, you get consistent results: LK4[coeff_, tau_, xi1_, xi2_, x_, y_] := ...


3

Here is what might be considered a classic example of the difference. Suppose I have a list containing a number of xs and maybe some other elements, and I want to replace the xs with random integers in some range, but I want new random draw for each replacement. SeedRandom[42]; {x, y, x, x, y} /. x -> RandomInteger[9] {6, y, 6, 6, y} doesn't work ...


2

expandNCM[(h : NonCommutativeMultiply)[a___, b_Plus, c___]] := Distribute[h[a, b, c], Plus, h, Plus, ExpandNCM[h[##]] &] expandNCM[(h : NonCommutativeMultiply)[a___, b_Times, c___]] := Most[b] ExpandNCM[h[a, Last[b], c]] expandNCM[a_] := ExpandAll[a] To eliminate ** operators: compressNCM[expr_] := expr /. NonCommutativeMultiply[x__] :> ...


1

ReplaceAll works on "the structure", not the "pretty printed" form. FullForm[I] gives Complex[0, 1] and I /. Complex[0, 1] -> Complex[0, -1] gives -I


1

For Version 9.0.1.0 (Windows 8 64-bit), this seems to be a good use case for the function Internal`FromCoefficientList: bifclF = Block[{Power = f[#2] &, x = f[1]}, Internal`FromCoefficientList[#, x]] & Examples: Using @bobthechemist's examples test1 = 1 + x + x^2 + x^4; test2 = 2 + x - x^2 + x^4; test3 = -2 - x + x^2 - x^4; test4 = -2 - x - x^2 - ...


1

First, a fixed version of your code: ClearAll[eOF0, eOF1, eOF2, eOF3] eOF0[int_, dt_, nas_, nts_] := Module[{d = ConstantArray[2, {nas + 1, nts}]}, For[j = nts, j > 1, j--, d = ReplacePart[d, {1, j - 1} -> (1 - int*dt)*d[[1, j]]]]; Grid[d]] eOF0[0.01, 0.1, 4, 4] and a few alternatives eOF1[int_, dt_, nas_, nts_] := Module[{d = ...



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