Tag Info

Hot answers tagged

20

What you need is something like this: patt = Null | (x_ /; MatchQ[x, {_Integer, patt}] ) The trick is to delay the evaluation for the recursive part, until run-time (match attempt), and Condition is one way to do it. So: MatchQ[#, patt] & /@ {Null, {4, Null}, {3, {4, Null}}, {2, {3, {4, Null}}}, {1, {2, {3, {4, Null}}}}} (* {True, True, True, ...


13

Not sure if this is what you are looking for, but let´s see (wash, rinse, repeat): test = {Null, {4,Null}, {3, {4,Null}}, {2, {3, {4,Null}}}, {1, {2, {3, {4, Null}}}}, {}, {Null}, {Null, Null}, {3,4, Null}}; MatchQ[Null, # //. {_Integer, Null} -> Null] & /@ test (*{True, True, True, True, True, False, False, False, False}*)


6

Another way that seems efficient: pat = Module[{check}, check[_Integer, Null] := Null; check[___] := Throw[False]; Catch[# /. List -> check /. {Null -> True, _ -> False}] ] &; test = { Null, {4, Null}, {3, {4, Null}}, {2, {3, {4, Null}}}, {1, {2, {3, {4, Null}}}}, (*True*) {}, 5, {Null, {5, Null}}, {{5, Null}, 4}, {Null}, ...


5

the following is not bullet proof but works for your example and I think the trick might also be of use when working out more robust variants: expr /. Power[n_, d_?Negative] :> Power[n /. f -> ff, d] /. f -> Plus /. ff -> f I have often used similar tricks in matching operations: when the stuff you want to not match is so much easier to ...


5

When using replacements, always remember that the actual expression you are looking at could have a different internal representation. As Kuba already suggested, by looking at the FullForm you can reveal very easily what is really going on. In your case, I would suggest to use Numerator and Denominator to first extract the parts of the rational. After this, ...


4

The issue in your rule is that a_ and b_ don't necessarily need to be symbols. What if your a matches a larger portion of the expression? OrderedQ[{(Com[p2, p1] + p1 ** p2), p1}] (* False *) Then the rule would be applied and I'm not sure this your intention. Please try this set of rules. I have added some print commands so that you see which rule was ...


4

As I've pointed out before in, Mathematica complaints that convergent integral diverges and How to help MMA to simplify integrands?, Expectation is much faster on integrands that are a sums of terms of the form polynomial * Gaussian, provided you convert the Gaussian exponential to normal distributions. It basically is a built-in version of approach 2 that ...


4

We can apply the method I used for How to match expressions with a repeating pattern: test[Null | {_Integer, _?test}] = True; _test = False; Confirmation: good = { Null, {4, Null}, {3, {4, Null}}, {2, {3, {4, Null}}}, {1, {2, {3, {4, Null}}}} }; bad = {{}, {Null}, {Null, Null}, {3, 4, Null}}; test /@ good test /@ bad {True, True, True, ...


3

This is a minor variation on the last example in the Examples > Generalizations & Extensions section of RegularExpression, StringCases["big bad wolf", RegularExpression["(.*) .* (.*)"] -> {"$1", "$2"}] {{"big", "wolf"}} Does this work for you?


2

The definition of aa can be simplified from 52 to 12 terms by aa = Collect[D[f[x]*Log[f[x]], {m, 20}] /. m -> 0 // PowerExpand // Expand, x, Simplify] but direct integration by Integrate[aa, {x, -Infinity, Infinity}, Assumptions -> B > 0] remains painfully slow. The solution offered by Histograms FullSimplify[Integrate[#, {x, -Infinity, ...


2

Cases[Sin[x] Cos[x], _@_, {0, Infinity}] (* {Cos[x], Sin[x]} *) Cases[Sin[x] Cos[x], h_@_ :> h, {0, Infinity}] (* {Cos, Sin} *) Note: watch out for expressions that "look like" functions with a single argument: Cases[Sqrt[x ] Times[Sin[x], w], h_@_ :> h, {0, Infinity}] (* {Sin} *) because Sqrt[x] // FullForm (* Power[x,Rational[1,2]] *)


2

This seems to work, at least for the example you provide. expr = ((a^2 (a^2 + b^2 + 4 b c + c^2 - 2 a (b + c)) + 2 a d f[a, b, c]) Log[c/d])/f[a, b, c]^2 + ((2 a (-2 a^2 - 2 b^2 + b c + c^2 + a (4 b + c)) + (a + b - c) f[a, b, c]) Log[a/b])/(2 f[a, b, c]^2) Apart@With[{exp = Together@expr}, ReplaceAll[Numerator[exp], f[a, b, c] :> a + b + ...


2

Actually since version 9 there is ParametericNDSolve and NDSolveValue which both make the mentioned idiom even more attractive and doesn't even need the pattern matching you are struggling with: model = Module[{x, y, a, t}, ParametricNDSolveValue[ {a*(y'[x] t - y[x]) == 7, y[0] == 0}, y, {x, 0, 1}, {a, t} ] ] data = {#, model[0.5, 0.6][#] + ...


2

The function checks if the input is a number (NumberQ), otherwise it prints out an error. The function is only defined if the input is a number. Please check this alternate example: f[x_?OddQ] := "Here the function is defined for an odd number"; f[3] But the function it is not defined for an even number: f[2]


1

Also this way: expr = ((a^2 (a^2 + b^2 + 4 b c + c^2 - 2 a (b + c)) + 2 a d f[a, b, c]) Log[c/d])/ f[a, b, c]^2 + ((2 a (-2 a^2 - 2 b^2 + b c + c^2 + a (4 b + c)) + (a + b - c) f[a, b, c]) Log[ a/b])/(2 f[a, b, c]^2); g[expr_] := Simplify[Numerator[expr] /. f[___] -> a + b + c]/ Denominator[expr] Now Map[g, expr] (* ...


1

I think you might be looking for a pattern structure like this: str = "big bad wolf"; StringCases[str, "big " ~~ x__ ~~ " wolf" -> x] which returns bad or whatever happens to lie between big and wolf. In the event that there is no match, you get a null {}.


1

Unevaluated[y/x^2] /. x^2 -> k returns y/k



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