Tag Info

Hot answers tagged

6

We can see that unlike {x_, x_} two occurrences of OptionsPattern[] do not need to have the same content: pattern = {foo[OptionsPattern[]], foo[OptionsPattern[]]}; MatchQ[{foo[a -> b], foo[a -> c]}, pattern] True We can also see that Repeated can in some cases work with OptionsPattern[]: MatchQ[ {foo[a -> b], foo[a -> c]}, ...


6

You can use the syntax for matching a head _Head, along with the alternative | symbol, and repeated .. to do pattern = {(_foo|_bar)..}. Which should match any list containing only expressions with the heads foo, and bar in any order.


6

I'll provide the following as a simplified example of what I think you're trying to accomplish - comment if it's not and I'll gladly delete. Let's make two simple functions that do two differing things on their arguments (standing in place for your coord2D and coeff2D): ClearAll["Global`*"] b[a1_, a2_] := a1*a2 c[a1_, a2_] := a1 + a2 Now, let's define a ...


4

In version 10.1 timings are pretty much as I expected: f1[x_Integer] := x; f2[x_] := x /; x ∈ Integers; f3[x_?IntegerQ] := x; First @ Timing @ Do[#[i], {i, 1*^6}] & /@ {f1, f2, f3} {0.374402, 1.06081, 0.499203} As m_goldberg commented _Integer should be fastest as unlike the others it does not require evaluation; it directly matches the ...


3

seems to work: expr /. Times[xx_?(! FreeQ[#, x] &) , p_plusd, rest___] :> Times[xx /. x -> 1, p, rest] and terms like: 2 Log[(sa1 (-2 + x))/(-mgl^2 + sa1 (-2 + x))] plusd[1/(1 - x)] and Log[2 + mgl^2/sa1 - x] plusd[-(2/(-1 + x))] were reduced to 2 Log[-(sa1/(-mgl^2 - sa1))] plusd[1/(1 - x)] and Log[1 + mgl^2/sa1] plusd[-(2/(-1 + x))] ...


3

I think it would be better in the long run if you simply replaced TableForm by Grid before converting to TeXForm. Unlike TableForm, Grid dividers are translated correctly into $\LaTeX$: TeXForm[ Grid[ Prepend[Transpose[ Prepend[Transpose[ {{1, 2}, {3, 4}} ], {"a", "b"}]], {"", "c", "d"}], Dividers -> {{None, Automatic, ...


3

Cases[list, x : {_, _} :> Plus @@ x] (* {3, 9, 6, e + f, Cos[b] + Sin[a]} *) Cases[list, x : {_Integer, _Integer} :> Plus @@ x] (* {3, 9, 6} *) Cases[list, x : {_, __} :> Plus @@ x] (* {3, 8, 9, 6, a + b + c, e + f, Cos[b] + Sin[a]}*) Cases[list, x : {___} :> Plus @@ x] (* {3, 2, 8, 9, 6, a + b + c, e + f, g, 0, Cos[b] + Sin[a]} *) Or ...


3

{a^2, a^b, x^4, (x - 1)^2} /. x_^y_ :> y*x^(y - 1) {2 a, a^(-1 + b) b, 4 x^3, 2 (-1 + x)}


2

First: sols = {{{0, -1, -x[1][2][1], -x[1][1][1]}, {-1, 0, -1, 0}, {-1, 0, 0, -1}}, {{0, 1, -2, 0}, {-1, 0, -1, 0}, {-1, 1, -1, -1}}, {{0, -1, x[1][1][1], -x[1][1][1]}, {-1, 0, -1, 0}, {-1, 0, 0, -1}}, {{0, -1, 0, -x[1][1][1]}, {-1, 0, -1, 0}, {-1, 0, 0, -1}}, {{0, -1, 0, 0}, {-1, -C[1], -1, 0}, {-1, -C[2], 0, -1}}, {{0, -1, 0, 0}, {-1, 0, ...


2

Suppose you have four solution matrices: mysol = {{{1, 0, a}, {0, b, 1 - a}, {0, 0, 0}}, {{1, 0, a}, {0, a, 1 - a}, {0, 0, 0}}, {{1, 0, 2 b}, {0, b, 2 - a}, {0, 0, 0}}, {{1, 0, a}, {0, b, 1 - b}, {0, 0, 0}}}; You can find the matrices that are equivalent (or degenerate) this way: degens = Table[ If[Solve[mysol[[i]] == mysol[[j]], {a, b}] ...


2

I think there is another way to do that: f[s : (PatternSequence[_?NumericQ] ..)] /; Length[{s}] == 3 := Function[{a, b, c}, a^2 Sin[b] Log[c]][s] It's suitable for the cases that conditions are the same.


2

Inexact zeros preThreeJSymbols[l1,m1,l2,m2,k] seems to return both 0 and 0. You introduced inexact arithmatic in the expression: ThreeJSymbol[{l1, 0.}, {k, 0.}, {l2, 0.}] You can either add Chop to your result or make these zeros exact: ThreeJSymbol[{l1, 0}, {k, 0}, {l2, 0}] However if you use expr != 0 as Pickett recommended you do not need ...


1

I think you are bumping up against the limitation described here: Position of a pattern-matched part of an expression I say this because I think you want a combination of Replace and Position in one step, but this is not directly possible. We can still do it in multiple steps however. For simplicity I shall assume any Condition expressions will appear ...


1

I think this matches what you want it to: str11 = "<strong style=\"font-size:20px\" style=\"color:#dfa57c\" style=\"font-size:20px\" style=\"color:#dfa57c\" class=\"new\">"; StringCases[str11, ("style=\"" ~~ Except["\""] .. ~~ "\"" ~~ Whitespace | "") ..] {"style=\"font-size:20px\" style=\"color:#dfa57c\" style=\"font-size:20px\" ...


1

f1[x_Integer] = x; f2[x_] = x /; x \[Element] Integers; f3[x_?IntegerQ] = x; Timing[f1 /@ Range[10^6];] {0.424561, Null} Timing[f2 /@ Range[10^6];] {0.69525, Null} Timing[f3 /@ Range[10^6];] {0.630597, Null}


1

The easiest way to do this is via pattern matching and DeleteCases: set = {x, y, z, w, u, t}; DeleteCases[Permutations[set, {3}], {___, t, ___, u, ___}] I've omitted the output because it's 108 terms. Also, note that Permutations[set, {3}] === Permutations[set, {3, 3}] by definition.


1

ReplaceAll works on "the structure", not the "pretty printed" form. FullForm[I] gives Complex[0, 1] and I /. Complex[0, 1] -> Complex[0, -1] gives -I


1

For Version 9.0.1.0 (Windows 8 64-bit), this seems to be a good use case for the function Internal`FromCoefficientList: bifclF = Block[{Power = f[#2] &, x = f[1]}, Internal`FromCoefficientList[#, x]] & Examples: Using @bobthechemist's examples test1 = 1 + x + x^2 + x^4; test2 = 2 + x - x^2 + x^4; test3 = -2 - x + x^2 - x^4; test4 = -2 - x - x^2 - ...


1

Select[list, Plus @@ # == 7 &]; Cases[list, _?(Plus @@ # == 7 &)];



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