Tag Info

New answers tagged

0

another version Clear@"`*" variables = {}; expr = f[x] + g[x] + 3 f[y] + f[x + y] f[x]; var[x_] := CompoundExpression[variables = variables~Join~{x}, x] (* usage *) expr /. f[x_] :> fNew@var@x variables (* out *) fNew[x] + 3 fNew[y] + fNew[x] fNew[x + y] + g[x] {x, y, x, x + y}


3

Sow and Reap: {result, variables} = Reap[ expr /. f[x_] :> fNew[Sow@x] ] variables (* Out: {{x, y}} *) In your original solution you do not need Block: variables = {}; expr /. f[x_] :> (variables = Union[variables, {x}]; fNew[x]) And AppendTo can make your code shorter: variables = {}; expr /. f[x_] :> (AppendTo[variables, x]; fNew[x]) ...


0

variables = Union@Cases[expr, f[x__] :> x, {0, Infinity}] (* {x, y} *)


3

This is because Mathematica looks for the largest piece of the expression that matches the pattern. Your pattern is x_, which means every conceivable expression there is will match that pattern. As a result, the entire expression {x,y,z} will be matched as it is the largest matching piece (x, y, and z won't be matched because they are smaller pieces compared ...


2

What goes wrong The help page of ReplaceAll explains why your code is not doing what you want. ReplaceAll (/.) applies a rule or list of rules in an attempt to transform each subpart of an expression expr In the end it applies the rule to the largest subpart it can match. The expression {x,y,z} has two levels (depth of two). {x, y, z} // FullForm ...


2

Replace[{x, y, z}, x_ :> ("^_^" <> ToString[x]), Infinity]


0

Probably you will have to adapt my basic idea to your special needs. mem = 1/Sqrt[2] + 1/54 (34 - 3 π^2 + (-8 + Log[8]) Log[64]) // ExpandAll // Apply[List, #] & {mem[[1]], mem[[2]]/Sqrt[2], mem[[3]]/Pi^2, mem[[4]]/Log[2], mem[[5]]/Log[2]^2} // FullSimplify a possible adaption The key is: after ExpandAll the summands are sorted in the same ...


4

I believe you're looking for RepeatedNull Count[IdentityMatrix[10], {0 ..., 1, 0 ...}] (* 10 *)


1

mxOrdr = Max[0,Max@Cases[Replace[#, HoldPattern[Dt[x_, t:Except[_List]]] :> HoldForm[Dt[x, {t, 1}]], {0, Infinity}], x_Dt :> Unevaluated[x][[2, -1]], {0, Infinity}]] &; mxOrdr[f g + Dt[f, t] g + Dt[g, {t, 2}] f] (* 2 *)


2

maxOrder[expr_] := Max[{0, (# /. _Symbol :> 1) & /@ Last /@ (Level[#, {-1}] & /@ Cases[expr, _Dt, Infinity])}]; maxOrder[f g + Dt[f, t] g + Dt[g, {t, 2}] f] 2 maxOrder[f g + Dt[f, t] g] 1 maxOrder[f g] 0


3

This should work for any function (f, g or whatever) and any independent variable (t or whatever): Cases[f g + Dt[f, t] g + Dt[g, {t, 2}] f, Dt[_, {_, order_}] :> order, Infinity] If you want to inlcude the first order derivative, there is a little bit more to write. Indeed, the pattern Dt[_, _] is evaluated to 1 before it can be used by Cases to find ...


10

The problem does not lie with WordBoundary, it is due to the use of __. The string pattern WordBoundary ~~ __ ~~ "man" will find a word boundary properly, but then will match a following sequence of characters up to "man" without restriction -- including characters that lie on word boundaries themselves. To exclude that possibility, we should restrict the ...


1

In your example StringCases works as expected because by default it searches the Longest substring which matches the pattern __ (by default all repeated string patterns are greedy). Apparently the whole string without ending "." matches your pattern: In[21]:= mystring = "I am a big fan of Superman, Spiderman and Batman."; substr = StringCases[mystring, ...


5

There is a reason to use WordBoundary, but your example sentence doesn't bring it out. Consider mystring = "I am a demanding fan of Superman, Spiderman and Batman."; StringCases[mystring, LetterCharacter ... ~~ "man"] {"deman", "Superman", "Spiderman", "Batman"} so you might prefer StringCases[mystring, LetterCharacter ... ~~ "man" ~~ WordBoundary] ...


1

Keep the LHS of the replacement rule as simple as possible, e.g., use b -> 1/a {a b, a b c, a a b} /. b -> 1/a {1, c, a}


0

Use HoldForm for this: HoldForm[a a b] /. a b -> 1 (* a 1 *)


2

I use l1, l2, and S2P as defined by @Coolwater (you don't really have to Flatten and then Partition again when the original output of IntegerPartitions is already partitioned). I define my function twice to make it take 2 lists as arguments as well as any n as defined by l1[n], and l2[n]. seismaticaQ[lis1_List, lis2_List] := Pick[lis2, Complement[S2P[#], ...


1

With l1[n] and l2[n] defined as above, Table[Cases[l2[n], q_Integer /; And @@ (MemberQ[l1[n], #] & /@ S2P[q])], {n, 64}] takes 58 seconds and produces for n=64: {5,7,9,10,11,13,15,16,17,19,21,23,25,27,28,29,31,33,35,36,37,39,40,41,43,45,47,49,51,52,53, 55,56,57,59,61,63,64,65,67}


2

Simplified you could use: (this simpler l1 is sorted unlike the original) l1[n_] := DeleteCases[Union @@ Table[i*j, {i, 1, n}, {j, i + 1, n}], _?PrimeQ] l2[n_] := Range[2, 2 n] S2P[a_] := DeleteDuplicates[Times @@@ IntegerPartitions[a, {2}]] V10 introduces SubsetQ func[n_] := With[{u = l1[n]}, Select[l2[n], SubsetQ[u, S2P[#]] &]]


1

I await more detail regarding your working set, but if l1 is not too long you might use this: dsp = Dispatch @ Thread[Rest @ Subsets @ l1 -> True]; Pick[l2, S2P /@ l2 /. dsp] {5, 7} The same thing using Associations (v10): asc = <|Thread[Rest @ Subsets @ l1 -> True]|>; Pick[l2, Lookup[asc, S2P /@ l2, False]]


4

This question has essentially been asked many times before, perhaps most recently here: Converting hierarchies of rules to associations I addressed it myself when I demonstrated the use of Replace and FixedPoint in place of ReplaceRepeated to get the standard traversal in: ReplaceRepeated seemingly omits some rules As already explained by RunnyKine ...


6

Use Replace instead of ReplaceAll Replace[a[b[c, d]], head_[arg__] :> newHead[arg], {0, Infinity}] newHead[newHead[c, d]] ReplaceAll fails to do what you want because it acts from the outside in. Once it found a match (the entire expression) on the outside, its job was done. Replace on the other hand, acts from inside out.


9

The following functions will load the expressions and erroneous cells from a notebook: notebookExpressions[path_, pattern_:_] := Cases[Import[path, "Notebook"] // First , c:Cell[_, "Input"|"Output"|"Print", ___] :> Module[{v = eval[c]}, v /; MatchQ[v, _$Failed | Hold[pattern]]] , Infinity ] eval[cell_] := Quiet @ Check[ ...


1

You could use the replacement rule that Öskå already provided: Integrate[f[x], {x, lb, ub}] /. Integrate[x__] :> NIntegrate[x, Method -> "GaussKronrodRule"] However this will throw one unnecessary error message: Integrate::argmu: Integrate called with 1 argument; 2 or more arguments are expected. >> You could wrap the left-hand-side of the ...


0

I think Bob Hanlon had the right idea in using Orderless but his suggestion is overly naive. Instead we must treat only the triplets as orderless so we will need an additional head. SetAttributes[o, Orderless]; f[a_, b_, c_, x_, y_, z_] := f[o[a, b, c], o[x, y, z]] f[o[0 ..], o[0, y_, z_]] := -(1/(y - z)) Log[y/z] Now all of these match that one rule: ...



Top 50 recent answers are included