Tag Info

Hot answers tagged

8

As others have indicated this result is not surprising at all. What you actually need is not StringReplace but ReplaceAll (/.): {"90", "", "20"} /. "" -> "0" {"90", "0", "20"}


7

The illustrated behavior seems reasonable to me, even if it may not seem "natural." For position or replacement a given pattern should match when it appears between any two sequences of characters, or it is at the beginning and end of a string. A zero-length string effectively is between every pair of characters. Why should it not match? To keep it from ...


6

I would use something like this: Clear[f]; f[key_ -> val_] := key -> If[ StringMatchQ[ToString@key, ___ ~~ "A" | "a" ~~ ___], q@val, val] then f /@ {Aa->1/2, Av->2/5, Bx->3/7, Ce->4/9} (* {Aa -> q[1/2], Av -> q[2/5], Bx -> 3/7, Ce -> 4/9} *) If you have v10, and your data is in an Association, you use AssociationMap, ...


5

That is a rather slippery replacement, "Find nothing and replace it with something" Try telling the replacement that there nothing between the beginning and end StringReplace[{"90", "", "20"}, {StartOfString ~~ "" ~~ EndOfString -> "0"}]


5

Brute force: toylist /. Rule[lhs_, rhs_] :> With[{ coeff = Coefficient[rhs, Variables[rhs]] }, Solve[lhs == rhs, #][[1, 1]] &[ Last[Variables[rhs][[Ordering[coeff]]]] ] /; Max[coeff] > 10^5 ] {b->(a-2 c)/1000000000000,d->-0.5 b+e/100000000000,f->2 g} You can reverse condition: /; Min[coeff] < 10^-5 ...


5

You could define it with Array[Subscript[a, Min[##], Max[##]] &, {4, 4}]


5

Here's the problem in a nutshell: Module[{c1, c2 = foo[c1]}, c2 /. c1 -> value] (* ==> foo[c1] *) The point is, Module replaces c1 by a local variable in its body, but not in later assignments in the variable list. You can see what happens by just looking at the variables: Module[{c1, c2 = foo[c1]}, {c1,c2}] (* ==> {c1$184, foo[c1]} *) You ...


4

Using the built-in matrix manipulation commmands mat = Array[Subscript[a, ##] &, {4, 4}]; LowerTriangularize[mat] + Transpose[LowerTriangularize[mat]] - DiagonalMatrix[Diagonal[mat]] gives the same answer. This takes the lower triangular part and adds it to the transpose of itself, giving a symmetric matrix in which the diagonal entries have been ...


4

You can apply a rule using Condition: mat2 /. Subscript[a, i_, j_] :> Subscript[a, j, i] /; j > i giving $$\left( \begin{array}{cccc} a_{1,1} & a_{2,1} & a_{3,1} & a_{4,1} \\ a_{2,1} & a_{2,2} & a_{3,2} & a_{4,2} \\ a_{3,1} & a_{3,2} & a_{3,3} & a_{4,3} \\ a_{4,1} & a_{4,2} & a_{4,3} & a_{4,4} \\ ...


3

A variation on the theme: test = {Aa -> "1/2", Av -> "2/5", Bx -> "3/7", Ce -> "4/9"}; test /. (key_ /; StringMatchQ[ToString[key], ___ ~~ "a" | "A" ~~ ___] -> val_) :> (key -> ToExpression[val]) (*{Aa -> 1/2, Av -> 2/5, Bx -> "3/7", Ce -> "4/9"}*)


3

When you define a function f, you are (a) applying Set or SetDelayed to a pattern, (b) creating a rule, (c) associating the rule with a symbol (f), and (d) storing that in DownValues (usually). FullForm[Hold[f[x_] := x^2]] (* Hold[SetDelayed[f[Pattern[x, Blank[]]], Power[x, 2]]] *) f[x_] := x^2 DownValues@f (* {HoldPattern[f[x_]] :> x^2} *) First ...


2

It seems that a direct replacement (using ReplaceAll and RuleDelayed) may be adequate: Arg[1 + I a] /. Arg[1 + I*x_] :> ArcTan[x] ArcTan[a]


2

rewriteF[e_, v_, cf_] := If[cf[Coefficient[#[[2]], v]], Solve[Equal @@ #, v][[1, 1]], #] & /@ e rewriteF[toylist, b, # > 10^5 &] (* {b -> (a - 2 c)/1000000000000, d -> -0.5` b + e/100000000000, f -> 2 g} *) rewriteF[toylist, g, Positive] (* {a -> 1000000000000 b + 2 c, d -> -0.5` b + e/100000000000, g ...


1

cr = CoefficientRules[myExpr, {Delta, phi}] cr /. HoldPattern@(a_ -> b_) :> f @@ a -> b FromCoefficientRules[cr /. HoldPattern@(a_ -> b_) :> a -> f @@ a, {Delta, phi}] (* f[0, 0] + Delta f[1, 0] + Delta phi f[1, 1] *)


1

As a place to start I suggest TransformationFunctions: FullSimplify[expr1, TransformationFunctions -> {Automatic, schId, sortarg}] 0 This works even if both sortarg and schId are defined with /. rather than //.. For more manual application consider MapAt. Also familiarize yourself with levelspec.


1

a = TrigToExp[3 + 4 Cos[u] + Cos[2 u]] 3 + 2/E^(I*u) + 2*E^(I*u) + 1/2/E^(2*I*u) + (1/2)*E^(2*I*u) b = a /. u -> Log[z]/I // Simplify (1 + z)^4/(2*z^2) (b /. z -> E^(I*u)) == a // Simplify True


1

StringReplace just searched from left to right and replaced all the zero-length string for once. Here is a way to visualize the process: i = 0; StringReplace[{"90", "", "20"}, "" :> ToString[Style["0", {Orange, 15 + 5 i++}], StandardForm]] The inserted zero is dyed into orange and becomes bigger every time the replacing rule is applied so you can ...


1

Assuming that the problem only lies with c1, c2 and the Table, and ignoring any error message, if I use a self-constructed minimal working example c1 = "I'm GLOBAL"; vesselness[im_, Sigma_] := Module[{k1 = BAR1, k2 = BAR2, c1, c2 = c1}, {im, k1, k2, c2} /. c1 -> "I'm local" ]; vesselness[subIm, ref] the result is {subIm, BAR1, BAR2, "I'm ...


1

Without the 1. +, your expression evaluates to: 1/2 I (PolyGamma[0, 1 - I/2] - PolyGamma[0, 1 + I/2] - PolyGamma[0, 51 - I/2] + PolyGamma[0, 51 + I/2]) When you add 1., although the imaginary parts cancel, the answer remains a complex number. The reason you get a real number when you evaluate g[50] is that the sum doesn't use the general form, but ...



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