Hot answers tagged

8

I think an acceptable solution is to Thread over Alternatives: Basic solution: SetAttributes[f, Flat]; f[a, b, c] /. Thread[f[a, f[b, c] | other], Alternatives] -> post post Though, it won't be very helpful in more complex situations: f[a | b, f[b, c | h]]. General solution (experimental) tupplesOver[ f[a | g, f[b, c | h] | other], ...


7

The two examples in this question relate to two different aspects of pattern matching. I will start with the simpler to understand and intentional aspect, which is the second example. g[2] /. g[ 1 + (1|other) ] -> post (* g[2] *) In the above, the pattern doesn't match, and it can never match. g[2] has one argument. Since Plus is OneIdentity, 2 ...


7

This is related to the Orderless attribute of Times and Plus. These attributes could be removed permanently with some hacks, but that would break Mathematica. If you only want to display the result in a certain way, but not do calculations with it, it may be safe to remove those attributes temporarily using Block. Block[{Plus, Times}, With[{result = ...


5

We have to use Is there a GraphicsPrimitiveQ (or a complete list of Heads of graphics primitives)? primitivesQ = MatchQ[#, Alternatives @@ {Point, PointBox, Line, LineBox, Arrow, ArrowBox, Rectangle, RectangleBox, Parallelogram, Triangle, JoinedCurve, JoinedCurveBox, FilledCurve, FilledCurveBox, StadiumShape, DiskSegment, ...


5

Instead of using BlankSequence, use PatternSequence: expr /. Times[rest___, subExpr : PatternSequence[___a, ___d]] :> myfunc[Times[subExpr]] myfunc[a[x, r] a[y, 1] d[w, m]] The orderlessness of the pattern sequence is ensured by the Orderless attribute of Times.


4

check[expr_, vars_]:= Not[Or @@ Map[FreeQ[expr, #] &, vars]]; check[expr1, {a, b, c}] check[expr2, {a, b, c}] check[expr3, {a, b, c}] (* False, True, False *)


4

It's not entirely well defined due to handling of numbers, but the following should be at least close to what's wanted. poly1 = 1/64 (3 c^2 e - f^2 - x) (c^2 (4 e + b^2 g) - 4 (f^2 + x))^2 (c^2 (4 e + (a^2 + b^2) g) - 4 (f^2 + x)); poly2 = -(1/64) (b^2 c^2 - 4 x)^2 (a^2 c^2 + b^2 c^2 - 4 x) x; poly3 = x^42 (-(1/2) c^2 d^2 + x)^6; ...


4

Personally, I find this behavior somewhat surprising -- in particular, I would intuitively expect the following patterns to be completely equivalent: somePattern[..., a|b, ...] somePattern[..., a, ...] | somePattern[..., b, ...] While that may seem a natural expectation I do not believe the documentation ever states that they are. Nowhere can I ...


3

I generalize Jason B's answer to group by like terms in any number of coefficients, as well as in cases where you have polynomials multiplying one another: It sounds like you mostly want Plus to keep from evaluating. You can do this by replacing Plus with plus, where plus is a Flat function (since we might as well keep associativity of addition) ...


3

You have your expression expression = x^2 (6 (2 c1 x^6 + c2)/x^4) - x (4 c1 x^3 - 2 c2/x^3) - 8 (c1 x^4 + c2/x^2) (* -x (-((2 c2)/x^3) + 4 c1 x^3) - 8 (c2/x^2 + c1 x^4) + ( 6 (c2 + 2 c1 x^6))/x^2 *) for now it hasn't been evaluated to zero yet, but if you try most anything it will do so: Expand@expression Series[expression, {c1, 0, 2}] ...


3

After discussing with Kuba, I conjecture the following: Patterns involving alternatives are not evaluated further when attempting to match each alternative That is, somePattern[..., a|b, ...] originally evaluates as if a|b is a black box. Then, during pattern matching, the pattern does not evaluate any further when a|b is replaced by a and b in turn. To ...


3

Instead of pattern-matching the expressions you want to keep, you could also remove the ones you do not want to keep: myfunc[Replace[expr,x_ /; Not@MemberQ[{a, d}, Head@x] -> Sequence[], {1}]] This approach should be faster than using pattern matching using multiple blanks. It also works with other expression heads than Times.


3

f[x_] := Not[Or @@ (PossibleZeroQ /@ Last@CoefficientArrays[x, {a, b, c}])] f /@ {expr1, expr2, expr3} (* {False, True, False} *)


3

f[expr_] := Count[D[expr, {{a, b, c}}], 0] == 0 f /@ {expr1, expr2, expr3} {False, True, False}


2

f[expr_, var_List] := SubsetQ[Level[expr, {-1}], var]


2

Table[Union@Flatten@Map[Cases[ex, #, Infinity] &, {a, b, c}] == {a, b, c}, {ex, {expr1, expr2, expr3}}] {False, True, False} Or f[ex_] := Union@Flatten@Map[Cases[ex, #, Infinity] &, {a, b, c}] == {a, b, c} f /@ {expr1, expr2, expr3} {False, True, False} Or f[ex_] := And @@ Map[MemberQ[ex, #, Infinity] &, {a, b, c}] f /@ ...


1

Try fulleq=Append[eqnsMain, eqnsAdd]; And then Eliminate[fulleq, c] Using the {} parenthesis to fuse them would require Flatten on the result, as Mathematica will combine the vectors, not their components.


1

eqns = {AA == 2 a + 4 b + 45 c + 5, BB == 4 a + 45 b + 31 c + 78, CC == 0.23 a + 0.4 b + 4.35 c + 0.12, DD == 0.73 a + 0.2 b + 43.455 c + 3.12}; Substituting for c eqns2 = eqns /. c -> 43.5 AA + 34 b + 32 (* {AA == 5 + 2 a + 4 b + 45 (32 + 43.5 AA + 34 b), BB == 78 + 4 a + 45 b + 31 (32 + 43.5 AA + 34 b), CC == 0.12 + 0.23 a + 0.4 b + ...


1

This is as close as I could get for this expression, but I don't know what other types of expressions you would need to work with. The code is also ugly, so there has got to be something prettier, but here goes: if we set exp = (23991 x^3)/(250000 a^5) + (87271/(5000000 a^6) + 31/(600 a^2) - b/2816) x^4 then exp /. {Rational[x_, y_]*rest_ :> ...



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