New answers tagged

3

fun:=(1-Unitize[#])*Range[Length@#]+#& Usage fun/@CombiningCyclesCase2 {{1,2,3,4,5},{1,2,4,3,5},{1,2,4,5,3},{3,2,1,4,5},{3,2,1,5,4},{3,2,4,1,5},{3,4,1,2,5},{3,2,4,5,1},{3,5,4,1,2}}


6

If your lists are large (and rectangular as your example), this s/b considerably faster than existing answers: fix=With[{d=Dimensions@#},#+BitXor[1,Unitize@#]*ConstantArray[Range@d[[2]],d[[1]]]]&; or simpler and similar speed: fix2=With[{rx = Range@Length@#[[1]]}, (# + BitXor[1, Unitize[#]]*rx) & /@ #] &;


5

ClearAll[f] f = ReplacePart[#, # -> Last @# & /@ Position[#, 0]]&; f @ CombiningCyclesCase2


6

One solution: MapIndexed[If[# == 0, Last[#2], #] &, CombiningCyclesCase2, {2}]


1

May be, like this: A = {H2*H3*Ax[x, y, z], H1*H3*Ay[x, y, z], H1*H2*Az[x, y, z]}; Then expr = 1/(H1*H2*H3)*Inactive[Div][A, {x, y, z}] (* Inactive[Div][{H2 H3 Ax[x, y, z], H1 H3 Ay[x, y, z], H1 H2 Az[x, y, z]}, {x, y, z}]/(H1 H2 H3) *) And you may substitute: expr1=expr /. Thread[{H1, H2, H3} -> {1, 1, 1}] (* Inactive[Div][{Ax[x, y, ...


3

I would argue that the most idiomatic solution is A /. a | b | c -> 0


6

Or you can use Map A = {{a, b, c, d, e}, {f, g, h, j}}; A /. (# -> 0 & /@ {a, b, c}) Update: or to have fun. Fold[#1 /. #2 -> 0 &, A, {a, b, c}]


6

In terms of generating the replacement rule efficiently, you can do the following: A = {{a, b, c, d, e}, {f, g, h, j}}; A /. Thread[{a, b, c} -> 0] (*{{0, 0, 0, d, e}, {f, g, h, j}}*)


0

I like this question and I know I have played with the concept before, either for a question here or for my own interest. (I cannot recall.) I also like your method but I am immediately struck by the fact that it will not work with held expressions, for example: Hold[1 + 2] /. AndRuleDelayed[a_ + b_, c_?NumericQ, {a, b, c}] Hold[1 + 2] /. (a_ + b_) :> {...


2

As pointed out by Leonid Shifrin in comments, the behaviour you observe is not specific to associations, but is true of any function with the attribute HoldAll (or HoldAllComplete, or the relevant HoldFirst or HoldRest). As an example I use Hold. The simple rule {a_, b_}:>a+b does not force evaluation. To force it, you can use With in the right-hand ...


3

Since space is implicit Times similar methods to Convert head Times to List can be applied: Block[{Times = List}, ToExpression @ "{1 2 3 4 5 6 7 8}"] // First {1, 2, 3, 4, 5, 6, 7, 8} Though in this case a much shorter method works too: ToHeldExpression[expression] ~Level~ {3} {1, 2, 3, 4, 5, 6, 7, 8}


5

You can achieve either result depending on the order of evaluation. As Marius pointed out, once a rule out of a set of rules has been applied to a specific part of an expression, no further rules will be applied to that expression. Notice the difference between applying both rules at the same time, and applying them one after the other: (* At the same time,...



Top 50 recent answers are included