Tag Info

Hot answers tagged

15

Ramblings Arguments of the left-hand-side head are evaluated in the course of function definition, therefore you can use a utility function that constructs the patterns that you want. For example: SetAttributes[nq, HoldFirst] Quiet[ nq[s_Symbol] := s_?NumericQ ] Now: ClearAll[f] f[nq @ a, nq @ b, nq @ c] := a + b + c Definition[f] f[a_?NumericQ, ...


11

This has nothing to do with PatternSequence rather the problem is with how you use Repeated (..). Take for example the following function definition: f[x : {{_, _} ..}] := Norm[N[x]] Now if we feed it the following input: f[{{1, 1}, {1, 2}, {1, 3}}] The function works as expected and yields: 4.07914333 Now let's redefine the function as follows (we ...


9

Association is atomic: <|x -> 1|> // AtomQ True Therefore standard pattern matching inside the structure will not work. You can still match on the implicit head using: MatchQ[<|x -> 1|>, _Association] True There is also AssociationQ: <|x -> 1|> // AssociationQ True MatchQ[<|x -> 1|>, _?AssociationQ] ...


8

This is a common problem when using /. with numeric LHS for the replacement. Since you didn't include a space after /., Mathematica parses it as a division by 0.2. Check the full form to convince yourself: FullForm@Hold[{1,2,3,4}/.2->5] (* Hold[Rule[Times[List[1,2,3,4],Power[0.2`,-1]],5]] *) It is generally helpful to leave a space on either side of ...


8

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


7

If your aim is to remove the first and last argument: Cases[g[h, r, t, b, m], _[_, y__, _] :> y, {0}] Don't forget to localize y with :> g[h, r, t, b, m][[2 ;; -2]] /. g -> List List @@ (g[h, r, t, b, m][[2 ;; -2]]) Delete[List @@ fun, {{1}, {-1}}]


5

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.


5

ImportString is all you need, as demonstrated below:


5

Interesting problem given the need to preserve structure. The first idea that comes to mind: pull[data_, s_, f_] := data /. {___, {___, s, a___}, b___, f, ___} :> {{s, a}, b, f} Test: pull[data, "(111)", {}] {{"(111)", -0.0547764, -0.236856}, {"(112)", -0.0420584, -0.20054, "(113)", -0.0297211, -0.191328, "(114)", -0.0190941, -0.186208}, ...


5

To paraphrase Oleksandr's comment to this answer: Not all numbers with a finite number of digits in base 10 can be also expressed with a finite number of digits in base 2. Look at this: r // FullForm (* Out: List[39.7026`,39.7027`,39.702799999999996`,39.7029`,39.702999999999996`] *) and compare it with {39.7026, 39.7027, 39.7028, 39.7029, 39.703} // ...


5

Good question; the notion of a tensorial (covariant) derivative is something that is missing in Mathematica AFAIK. I can think of two ways to proceed: Option 1 One way is to overload the TensorRank, TensorDimensions, and TensorSymmetry functions for patterns that have head CD: CD /: TensorRank[CD[tensor_]] := TensorRank[tensor] + 1 CD /: ...


4

Try using Longest: Cases[g[h, r, t, b, m], x_[__, Longest@y__, __] -> y, {0}] Your single underscore after y only matches 1 element.


3

I can't yet explain the behavior but I can greatly reduce your minimum example: MakeBoxes[a_ /; FreeQ["foo", a], fmt_] := "bar" /; False ODE = F'[z] == g F[z]; Fguess = {F -> (Exp[a #] &)} ODE /. {F -> (Exp[a #] &)} ODE /. Fguess {F -> (Exp[a #1] &)} a E^(a z) == E^(a z) g Derivative[1][F][z] == g F[z] Critically: If I ...


3

Position[r, n_ /; n == #] & /@ {39.7026, 39.7027, 39.7028, 39.7029, 39.703} (* {{{1}}, {{2}}, {{3}}, {{4}}, {{5}}} *)


3

I would use StringFreeQ: (* files = {"10.txt", "11.txt", . . ., "inelasticov3-8.txt", "inelasticov3-9.txt"}; *) Select[files, StringFreeQ[#, "inelastic"] &] {"10.txt", "11.txt", "12.txt", "13.txt", "14.txt", "15.txt", "16.txt", "17.txt", "18.txt", "19.txt", "1.txt", "20.txt", "2.txt", "3.txt", "4.txt", "5.txt", "6.txt", "7.txt", "8.txt", ...


3

My apologies to those who closed this question for my unilaterally reopening it, but there is a nontrivial aspect to this question that I wish to address, and it would not nicely fit in comments. (I am not making an exception for myself; when someone has such an answer he wishs to give to a closed question I nearly always reopen it for him to do so.) ...


2

You can define f to operate on f however you would like: ClearAll[f] f@f := f@f@# &; f[x_] := x /. a -> I a {f[a], f@f[a], (f@f)[a]} {I a, -a, -a} However if you expect this to extend to e.g. (f@f@f)[a] you may want something like: ClearAll[f] f[f] = Superscript[f, 2]; f[Superscript[f, n_]] := Superscript[f, n + 1] Superscript[f, n_][x_] := ...


2

It is not defined in the question what you wish to consider "real" for the purpose of this pattern. Is Pi real for your purposes for example? If you want to match only explicit decimal values you should use _Real, e.g. {{_Real, _Real} ..}. If you want something more general I propose: realQ = Re[#] == # &; Because Re is Listable this can be applied ...


2

I dont know how to solve your question,but I know that just compile it will save some times. Clear["Global`*"] string = {5, 4, 0, 5, 3, 3, 1, 4, 0, 2, 4, 0, 2, 3, 5, 0, 0, 0, 5, 4, 2, 3, 3, 5, 5, 4, 1, 5, 5, 4, 4, 5, 3, 2, 1, 3, 1, 2, 2, 4}; kOmni = Block[{f = Total@BitSet[0, DeleteDuplicates@#1], z, cnt = 0}, Fold[If[(z = BitAnd[f, BitSet[#, #2]]) ...


2

To get the file list without the files containing "inelastic", you can use: Cases[{your file list here}, x_String /; StringMatchQ[x, "*inelastic*"] == False] Then, to import them: Import[#] & /@ % One-step solution: Import[#] & /@ Cases[{your file list here}, x_String /; StringMatchQ[x, "*inelastic*"] == False] Edit: Out of curiosity, ...


2

You need to use the pattern "*inelastic*.txt" in FileNames. This will only return the list of files you need. Then use Map with pure functions (Function) to import all of them in one go: Import[#, "Table"]& /@ fileNames or similar. To use everything except file names with "inelastic", you can use Select,Cases,DeleteCases, etc. withStringMatchQ`. ...


2

Cases is looking for matches within the input expression, given the default levelspec of {1}, or a single match to the entire expression if you use levelspec {0}. Instead you need multiple matches for the entire expression for which you can use ReplaceList: ReplaceList[list, {__, {__, first, x__}, y : Except[last] .., last, __} :> {{first, x}, y, ...


2

... because you should write {1, 2, 3, 4} /. (2) -> 5


1

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


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


1

On my system this is a little bit faster than Mr. Wizard's code, though it's a slightly odd way to approach the problem. I use a symbol which makes a list vanish when it appears as an element of that list... killme /: {___, killme, ___} = Unevaluated[]; rls = Thread[list2 -> killme] // Dispatch; result = Complement[list1, Replace[list1, rls, {2}]];


1

This seems to work pretty well. Edit: now a bit faster. rls = Thread[list2 -> True] // Dispatch; Pick[list1, TrueQ /@ Or @@@ Replace[list1, rls, {2}]] // Length // Timing {0.546003, 75230} On my system this takes just over half a second to find 75,230 boundary sets. (I am running version 10.0.0 under Windows.) Looking at Simon's answer I find ...


1

In addition to Pickett's answer, you can use Round to get closer to the expected behaviour (see also this answer): r = Round[Range[39.7026, 39.703, 0.0001], 0.0001]; r // InputForm {39.702600000000004, 39.7027, 39.7028, 39.7029, 39.703} MemberQ[r, #] & /@ {39.7026, 39.7027, 39.7028, 39.7029, 39.703} {True, True, True, True, True} I don't ...


1

PullAll[data_, s_] := DeleteDuplicates@Cases[data, {___, s, a__} :> {s, a}]~Join~ Most@DeleteCases[data, {a_ /; ToExpression@a < ToExpression@s, __}]; PullAll[data, "(116)"] {{"(116)", -1.40166*10^-15, -0.182966, "(117)", 0.00938815, -0.183744}, {"(118)", 0.0190941, -0.186208, "(119)", 0.0297211, -0.191328, "(120)", 0.0420584, ...


1

It seems to me that for the basic case described in the question it would be best to simply check if a System function considers it valid, as I did for Pattern that matches colors. Therefore: variableQ = Quiet @ ListQ @ Solve[{}, #] &;



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