# Tag Info

14

When using Set rather than SetDelayed you will need to hang the Condition on the left-hand-side: ClearAll[f] f[x_] /; x > 0 = Sqrt[x] f[2] f[-2] Sqrt[x] Sqrt[2] f[-2] There are other reasons to prefer this placement; see: Placement of Condition /; expressions However be aware that the use of Set results in "pre-evaluation" of the RHS which ...

10

{{4, 5}, {1, 2}, {1, 5}, {3, 5}} /. {a : ___, b : PatternSequence[{x_, j_}, {x_, k_}], c : ___} :> {a, Sequence @@ Table[{x, j + n (k - j)/3}, {n, 0, 3}], c} (* {{4, 5}, {1, 2}, {1, 3}, {1, 4}, {1, 5}, {3, 5}} *) Edit For supporting multiple adjacent values you may do the much more convoluted: g[x_] := Module[{s = 3 ...

9

Well, the following meets your formal requirements evenFunction[f_][args__] := f[Abs /@ Unevaluated[args]] evenFunction[even][a, b, c] even[Abs[a], Abs[b], Abs[c]] But is it really better than evenFunction[f_][args__] := f @@ Abs[{args}] I, myself, would choose the 2nd version over the 1st. Update It is not necessary to set the attribute ...

9

Without thinking about any consequences, one idea popped into my mind. First, your definitions for f with the DownValues. I made it a bit more interesting: ClearAll[f]; f // Attributes = {HoldAll}; f /: HoldPattern[f[x_] + f[y_]] := upvaluesSeen[f[x], f[y]]; f[x_] := downvalue[x] How about a small wrapper function that temporarily deletes all DownValues ...

8

I am betting that this is almost certainly an optimization short-cut. If Orderless had to try every ordering it would be extremely slow when there are a moderate number of arguments, but it is not. Consider for example: f @@@ Hold @@ {RandomSample[Range@12]} Hold @@ {f @@ Range@12 /. {7 -> _}} MatchQ[%%, %] Hold[f[2, 6, 11, 7, 12, 10, 4, 1, 3, 5, 8, ...

7

Here is a way to do what you want. However, this method does not retain the original ordering of the elements in your list. You did not mention whether that was important, so I hope it won't be: Reverse@SortBy[list, #[[3]] &]; DeleteDuplicatesBy[%, Drop[#, {3}] &] (* Out: {{"2", "1", "z", "6", "7"}, {"2", "3", "t", "7", "7"}, ...

7

We consider h = Exp[g]. The main idea is to calculate the Fourier transform of h, expand it into a series in powers of v, and then transform back Here we go. The normal distribution is explicitly f = 1/(v Sqrt[2 \[Pi]]) Exp[-(x - z)^2/(2 v^2)]; Now the Fourier transformation of h acts only on f, with the result ft = FourierTransform[f, x, t, ...

7

You might find useful this other approach which is not using ReplaceAll: foo[___, {x_, j_}, {x_, k_}] := Sequence @@ Rest@Table[{x, j + n (k - j)/3}, {n, 0, 3}] foo[__, s_] := s then for any number of adjacent items with matching x, for example testlist = {{4, 5}, {1, 2}, {1, 5}, {1, 7}, {3, 7}} just run FoldList[foo,testlist] (* FoldList[foo, ...

6

Modifying the approach by @bbgodfrey one might also use Tally to count all patterns: list = RandomInteger[{0, 9}, 1000]; (* some integers *) patternCount = Tally @ Split @ list (* returns a list of {{integer..},count} *) Now we just take the ones that interest us (e.g. more than one integer): patternCount2plus = Cases[ patternCount, { { ...

6

Just some other approaches: perm = Permutations[{a, e, q, r, t, u}]; pm = Permutations[{a, e, q, r, u}]; The following: pck = Pick[perm, #[[5]] & /@ perm, t]; sel = Select[perm, #[[5]] == t &]; con = #[[1 ;; 4]]~Join~{t}~Join~{#[[5]]} & /@ pm;

6

Adapting Leonid's method for How do you set attributes on SubValues? ClearAll[f, g]; SetAttributes[{f, g}, HoldAll]; g /: g[x_] + g[y_] := upvalue; f[x_] := downvalue f := With[{stack = Stack[_] /. HoldPattern[f] :> g}, With[{foo = Cases[stack, Alternatives @@ _ /@ First /@ UpValues @ g]}, g /; foo =!= {} ] /; stack =!= {} ] Now: f[1] ...

5

Because of the Orderless property of Times, the following Times[a, b, f[], c] /. Times[x__, f[], y__] :> {{x}, f[], {y}} gets turned into a b c f[]/. f[] x__ y__:>{{x},f[],{y}} through canonical ordering. Furthermore, the Orderless attribute of Times in the pattern means that "all possible orders of arguments are tried". Additionally, "If no ...

5

Using the findSubsequence function from the linked answer: http://mathematica.stackexchange.com/a/942/2079 findSubsequence[list_, {ss__}] := ReplaceList[list, {pre___, ss, ___} :> Length[{pre}] + 1] suppose we have b: b = {8, 4, 9, 1, 2} In principle you search the full sequence: findSubsequence[Flatten[IntegerDigits[Range@#] & /@ ...

5

Thanks to MarcoB, there is the final code: Grid[ Cases[Permutations[{a, e, q, r, t, u}], {Repeated[_, {4}], t, _}], Frame -> All]

5

If the List is named lst, then rept = Cases[Split[lst], z_ :> z /; Length[z] > 1] finds all runs of repeated integers, and Length[rept] finds the number of them. Applied to lst = {1, 2, 3, 3, 4, 5, 5, 5, 6, 4} they give (* {{3, 3}, {5, 5, 5}} *) (* 2 *) If only the number of repeated runs is desired, then Count[Split[lst], z_ /; ...

4

Here is my extension of the belisarius' first solution for the case of arbitrary number of elements in the sequence with matching $x$ coordinate (the sequence is still allowed to be only one): {{4, 5}, {1, 2}, {1, 8}, {1, 7}, {3, 5}} /. {a : ___, b : Longest@Repeated[{x_, _}, {2, Infinity}], c : ___} :> {a, Sequence @@ ...

4

We can see from this expression Reap[Sow[1, #] & /@ {1, 1, 2, 3}, _, f] (* Out: {{1, 1, 1, 1}, {f[1, {1, 1}], f[2, {1}], f[3, {1}]}} *) how Sow and Reap interact in general. In this example f is {#, Tr@#2} &. Tr in this context works just like Total but it can be faster. But the sum of the second part is just the sum of 1s, the number of times ...

4

To "official-ize" my comments above, use RuleDelayed: A[1, 2, 4, 5] /. A[a__] :> Position[{a}, 4] {{3}} The reason to use RuleDelayed here is the same as SetDelayed. They both keep the rhs unevaluated until the rule is used, meaning that once the pattern matches on the lhs, a__ is substituted in for a on the rhs. Otherwise with normal Rule, rhs is ...

3

I might use Fold to go down the nested rules. At some levels of the data, a list of rules creates an extra set of braces. The Flatten /@ First@... seemed a convenient fix. locations = Flatten /@ First@ Fold[ #2 /. #1 &, JsonResponse, {"response", "view", "result", "location", {"displayPosition", "address"}}]; {{"latitude", ...

3

You can hide your workaround in $Pre: SetAttributes[specialEvaluate, HoldAll] specialEvaluate[expr_] := ReleaseHold[ Hold[expr] /. UpValues[f] ]$Pre = specialEvaluate; And now: f[1] + f[2] (* Out: upvalue *)

3

My take: fn[list_, col_] := Module[{cr = Drop[Range@Length@list[[1]], {col}]}, Split[list[[Ordering[list[[All, Append[cr, col]]]]]], SameQ[#1[[cr]], #2[[cr]]] &][[All, -1]]]; Second argument is the column that is "special" (3 in the OP example case). This appears to be vastly more efficient than answers so far. I generated data with ...

3

Maybe someone can come up with a good explanation why this is a bug, but I believe it is more a feature. It is not specifically bound to Times, but this works for every function with the Orderless attribute. That being said, we can check whether or not the pattern matching and replacing leaks evaluation, because this would be the only reason for a ...

2

I'm posting what I have built as an answer, but feel there are inefficiencies so would like to see (and accept) other's solutions. Find all unique elements (after dropping Col C): Make an empty list, cleanedOutput. Get the Col C values for each unique value, Sort and assign the Last element to colC. Finds the Cases of each unique element and replace the ...

2

Add the option Heads -> False: Position[{{a, b}, {b}, {e, f}, {c, e}, {c}}, _?(subsetQ[#, {b, c, e}]&), 1, Heads->False] (* Out: {{2}, {4}, {5}} *)

2

Specifying that your pattern should only match expressions with a List head prevents the error that you observed. In addition, there already is a built-in SubsetQ function (here are its docs). I would typically try to use a built-in over a user function whenever possible, since the former might benefit from better algorithms and internal optimizations ...

1

The answer in the link you supplied indicates that the use of the Sow and Reap approach performs better than Tally when strings are involved. However, to count the number of occurrences in your list of 3D points, Tally is actually better. list = Partition[RandomInteger[15000, 300000], 3]; Tally@list // Timing // First 0.078 seconds on my machine while ...

1

If you want a pattern that matches only numbers that are not real (according to your title), use Except[_Real, _?NumberQ]: list = {I, 1, 2.2, 3 + 4 I, a, "hello", 3.14, E^(I Pi), Pi}; Cases[list, Except[_Real, _?NumberQ]] (* {I, 1, 3 + 4 I, -1} *) But keep in mind that this is matching on the heads of expressions, so something like 1 or Pi are not ...

1

mylist = {5, 5 + 7 I, 4.9, \[Pi], 8 - 7.4 I}; Select[mylist, # \[Element] Reals &] $\{5,4.9,\pi \}$ and.... Select[mylist, Im[#] != 0 &] $\{5+7 i,8.\, -7.4 i\}$

1

I am the OP. Below is the code I came up with that grinds out the result. Basically, because I couldn't figure out how to get MMa to do the substitutions and limits, I just did as much of the problem as I could on paper and found this shortcut: $$\frac{dg[x,v]}{dv}=\frac{1}{2}\frac{d^{2}g}{dx^{2}}+\frac{1}{2}\left(\frac{dg}{dx}\right)^{2}$$ So this gets ...

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