Tag Info

Hot answers tagged

5

You should replace __?MemberQ[{1, 2}, #] & with __?(MemberQ[{1, 2}, #] &). Because the & has the low priority. So, the pattern is interpreted as part of the function, and not the function as part of the pattern. This can be easily seen using FullForm: In[24]:= __?MemberQ[{1, 2}, #] & // FullForm Out[24]//FullForm= ...


4

As commented, StringTrim for example will do the job. For example : StringTrim[s3] StringTrim[s3, StartOfString ~~ WhitespaceCharacter ..] StringTrim[s3, WhitespaceCharacter .. ~~ EndOfString] return "jbjkbasd nklnkln" "jbjkbasd nklnkln " " jbjkbasd nklnkln"


4

@Kuba is actually pretty specific. In[1]:= Log[PDF[NormalDistribution[m, s], x]] /. HoldPattern[Log[Times[x__]]] :> Plus @@ Log[List[x]] Out[1]= Log[E^(-((-m + x)^2/(2 s^2)))] - 1/2 Log[2 \[Pi]] + Log[1/s] BTW, you may also interested in PowerExpand.


4

Maybe with the help of Fold or FoldList : See what happens here: FoldList[#1 /. #2 &, a, {r1[a], r2[a], r3[a]}] {a, 3 a, -3 a, -3 (1 + a)} Replace FoldList with Fold to get directly the last result. In other words: rall[a_] := Fold[#1 /. #2 &, a, {r1[a], r2[a], r3[a]}] then rall[a] -3 (1 + a)


4

Competely untested, but I believe this ought to work nicely: p = a*x^2 + b*x^3 + c*x*y + d*x^2*y + e*x*y^2 + y^2; coeffs = CoefficientArrays[p, {x, y}]; k = 2; (* highest degree wanted *) Fold[#1.{x, y} + #2 &, {0, 0}, Take[coeffs, {k + 1, 1, -1}]]


3

Thanks to bbgodfrey's comment I think I understand what you want: p = a*x^2 + b*x^3 + c*x*y + d*x^2*y + e*x*y^2 + y^2; Total @ Exponent[#, Variables @ #] & /@ List @@ p Pick[P, # <= 3 & /@ %] {3, 4, 3, 4, 2, 4} a x^2 + c x y + y^2 Following your update here is a somewhat different formulation that may be useful: op[var_List, ...


3

g = Graphics[{PointSize[.02], Point[{9, 5}], Point[{2, -5}], Point[{5, 5}], Point[{3, 6}], Point[{1, 1}], Point[{5, -7}], Point[{-7, 4}], Point[{6, -10}], Point[{-6, -2}], Point[{0, 8}], Point[{1, 4}], Line[{{1, 4}, {2, 5}}], Point[{2, 5}]}, AspectRatio -> Automatic]; g /. Point -> (Circle[#, .5] &)


3

MapIndexed can be used to map a function to the inner-most values in nested associations, for example: MapIndexed[f, <| "A" -> <|"a" -> 1|>|>, {-1}] (* <|"A" -> <|"a" -> f[1, {Key["A"], Key["a"]}]|>|> *) The level specification {-1} selects only the deepest level in the expression. Note how f receives not only the ...


3

This solution will traverse all nested associations regardless of depth and replace each value with the value that corresponds to its key in replacements. f[Rule[key_, assoc_Association]] := Rule[key, AssociationMap[f, assoc]] f[Rule[key_, val_]] := Rule[key, key /. replacements] replacements = { "a" -> 1, "dd" -> 1, "B" -> 1, "aa" -> 0, ...


3

Some parentheses and RuleDelayed instead of a Rule: dot[add[a, b, c], d, e, f] /. dot[add[y__], z__] :> (dot[#, z] & /@ add[y]) (* add[dot[a, d, e, f], dot[b, d, e, f], dot[c, d, e, f]] *) The :> prevents dot[#, z] & /@ add[y] from evaluating until after y and z have been replaced by the expressions they matched. DownValues shows the ...


3

The function you are looking for is Chop[]. Wrap it around anything and it will act at all levels to replace everything under $10^{-10}$ with zero. There is an optional second argument to adjust the tolerance. Then you can just do your /.{0->.1}


3

One way to use HoldForm is to do something involving Insert like this: F = (A[1] + A[2]) (B[1] + B[2]); G = HoldForm[Evaluate@Expand@F]; Do[G = Insert[G, Bra[2, i - 1], {1, i, 3}], {i, 1, Length@Expand@F}]; G = ReleaseHold@G A[1] B[1] Bra[2, 0] + A[2] B[1] Bra[2, 1] + A[1] B[2] Bra[2, 2] + A[2] B[2] Bra[2, 3] This process is a little simpler with ...


2

In the depth-first preorder traversal of ReplaceAll complete expressions are matched before heads therefore one can use a skip rule(1)(2). Since you also want to replace x with x[a], the pattern to be skipped, you can use a single rule with Alternatives: {x[a], x} /. x[a] | x -> x[a] {x[a], x[a]} ( Since Plus[x[a], x[a]] evaluates to 2 x[a] I used ...


2

This works too and it's theoretically faster than ReplaceRepeated (//.): asc = Replace[list, r : {__Rule} :> Association[r], {0, Infinity}] The key observation is that Replace starts with the innermost levels first and works its way outwards. In contrast, ReplaceAll and ReplaceRepeated start with the outermost levels and work their way inwards.


1

You are running afoul of the (beneficial) scoping that is applied inside Manipulate constructs by way of DynamicModule (or the low-level equivalent). If you "inject" the expression containing g[1] etc. into the Manipulate before it is evaluated it should work correctly I believe: With[{body = gplot[1] == 0}, Manipulate[ ContourPlot3D[body, {x, -1, 1}, ...


1

Standard method: create a univariate in a new variable t, by changing every variable x to t*x. Take a Series expansion in t. Then use Normal to make it explicitly polynomial, and substitute t->1. poly = a*x^2 + b*x^3 + c*x*y + d*x^2*y + e*x*y^2 + y^2; vars = {x, y}; Normal[Series[poly /. Thread[vars -> t*vars], {t, 0, 2}]] /. t -> 1 (* Out[6]= a ...


1

I've found a simple approach using the function Part (its shorthand is [[ ]]): 1. If you write your list lst1 rather that way: newlst1 = {{"A", "a"}, {"D", "dd"}, {"B"}} you can assign any value (for example here 999) to all these keys simply with: (assc[[##]] = 999) & @@@ newlst1; You can check the result: assc <|"A" -> <|"a" ...


1

Q1 Column[StringReplace[#, " " .. ~~ EndOfString -> ""] & /@ {s1, s2, s3}] dasbdk asdnkal asn knkl nkn dvklsn jbjkbasd nklnkln Q2 Column[StringTrim[#] & /@ {s1, s2, s3}] dasbdk asdnkal asn knkl nkn dvklsn jbjkbasd nklnkln


1

As already noted for Q2 the best solution is of course StringTrim. For Q1 here is an alternative using regular expressions: stringTrimAtEnd = StringReplace[#,RegularExpression["\\s*$"] :> ""] &


1

I am somewhat confused about the aim. Here is an interpretation. Starting with string: string = "{{13,{17,4},{23,10},220/13,3},{17,{23,6},{29,12},4368/17,3},\ {19,{23,6},{29,12},4368/19,3},{23,{29,6},{41,18},26334/23,6},{29,{41,\ 12},{47,18},21474180/29,3},{29,{41,10},{53,22},493350,6},{31,{41,12},{\ ...


1

This question has been asked for times, Evaluation of Derivative in a Module and Replace rule with function? Derivatives don't evaluate. Here are several ways to solve it: If you do not mind pollute Global namespace, assign the function first. y[x_] = Cos[x] x y''[x] + 2 y'[x] + l^2 x y[x] == 0 If you mind, assign it in a Block Block[{y}, y[x_] ...


1

I think this can be done if we stick to numeric functions. This is the solution that worked for me: Missing /: f_Symbol[___, m_Missing, ___] /; MemberQ[Attributes[f], NumericFunction] := m; This way, expressions like Missing[...] - Missing[...] or a Exp[b + Missing[...]] evaluate to Missing[...] (even for symbolic a, b). Expressions like {1, 2, 3, ...



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