Tag Info

6

A slightly faster method (30x for large lists) is to use Ordering and Drop: Drop[a, Ordering[a, -1]] This will always remove 1 element, even in the extreme rare case that the maximum value appears twice…

4

Sort @ Select[Exponent[# /. y -> x, x] < 5 &] @ MonomialList[ser] TeXForm @ % $\left\{1,-\frac{x}{2},-\frac{x^2}{8},-\frac{x^3}{16},\frac{y}{2},\frac{3 x y}{4},\frac{23 x^2 y}{16},\frac{27 x^3 y}{32},-\frac{y^2}{8},-\frac{31 x y^2}{16},-\frac{127}{64} x^2 y^2,\frac{y^3}{16},\frac{35 x y^3}{32}\right\}$

2

You could also use CoefficientArrays v = {x, y}; c = CoefficientArrays[ser, v] result = {c[[1]], c[[2]].v, c[[3]].v.v, c[[4]].v.v.v, c[[5]].v.v.v.v} // Expand $$\left\{1,\frac{y}{2}-\frac{x}{2},-\frac{x^2}{8}+\frac{3 x y}{4}-\frac{y^2}{8},\frac{23 x^2 y}{16}-\frac{x^3}{16}-\frac{31 x y^2}{16}+\frac{y^3}{16},-\frac{127 x^2 y^2}{64}+\frac{27 x^3 y}{32}+\... 2 You can adapt Yode's method from his answer to: Removing terms of certain degree in multivariable polynomial var={x,y}; Sort@Select[MonomialList[ser], Tr[Exponent[#, var]] < 5 &]$$\left\{1,-\frac{x}{2},-\frac{x^2}{8},-\frac{x^3}{16},\frac{y}{2},\frac{3 x y}{4},\frac{23 x^2 y}{16},\frac{27 x^3 y}{32},-\frac{y^2}{8},-\frac{31 x y^2}{16},-\frac{...

2

You can use CoefficientList and select necessary items in the matrix: ser = 1 - x/2 - x^2/8 - x^3/16 + y/2 + (3 x y)/4 + (23 x^2 y)/16 + (27 x^3 y)/32 - y^2/8 - (31 x y^2)/16 - (127 x^2 y^2)/64 - (351 x^3 y^2)/128 + y^3/16 + (35 x y^3)/32 + (407 x^2 y^3)/128 + (1915 x^3 y^3)/256; PolynomialQ[ser, x] (* True *) coef = CoefficientList[ser, {x,...

5

You can use Jens's method from this answer: expr[x_, y_] := 1 - x/2 - x^2/8 - x^3/16 + y/2 + (3 x y)/4 + (23 x^2 y)/16 + (27 x^3 y)/32 - y^2/8 - (31 x y^2)/16 - (127 x^2 y^2)/64 - (351 x^3 y^2)/128 + y^3/16 + (35 x y^3)/32 + (407 x^2 y^3)/128 + (1915 x^3 y^3)/256 expr2[x_, y_] = Normal[Series[expr[x t, y t], {t, 0, 4}]] /. t ...

4

The first thing to note is that a_ is a short hand notation. It is almost always helpful to look at the FullForm when you are trying to write patterns. When you do that with a_ you will notice that it is interpreted as Pattern[a, Blank[]] From the docs, this can be written as a: _ This provides a way to name repeated sequences, e.g. a : Repeated[_, {k}] ...

8

I suspect, much to my chagrin, that the FE is right and the InputForm parser is wrong. For one thing, the kernel itself thinks that parens are needed for Repeated: Repeated[Pattern[x,Blank[]]]//InputForm (*(x_)..*) The reason it breaks in the FE is that the FE treats the x_. as a single token: RowBox[{"x_.", "."}]. And that's absolutely correct: see ...

11

The notebook interface and the kernel have different parsers, because the notebook needs to manipulate the input before the kernel sees it for a variety of reasons. There are many differences between the two parsers. It has not been a priority to Wolfram to fix the differences. I have been trying to catalog the differences here (rendered as a webpage here)....

1

This method doesn't work in recent versions. In/Out from version 10.1 below. For the short example given you could specify it like this: f[y : g[x_: 10] : g[10]] := {y, x}; f[] {g[10], 10} I just realized this introduces a behavior you probably don't want: f[g[]] {g[], 10} The same (flawed) method applied to the longer example: squareLattice = ...

4

Well, here's one way to do it: f[y : g[x_] : g[10]] := {y, Replace[Unevaluated[x], Sequence[] :> First[y]] } f[] (*{g[10], 10}*) f[g[20]] (*{g[20], 20}*) Note that x_ can't bind to the default, because there's no requierment that the default match the pattern: In[83]:= f[y:g[x_]:{1, 2, 3}] := {y, Replace[Unevaluated[x], Sequence[] :> First[y]]} f[...

3

Except confuses me a bit in StringExpression, it can only be used on a Character. Maybe someone can chime in with a better answer about Except in StringExpression, but in 12.1 I can confirm this workaround works as a pattern directly to at least skip the Import: ___ ~~ "." ~~ ext___ /; (! StringMatchQ[ext, "png", IgnoreCase -> True]) Unfortunately this ...

6

It looks like there's a hack that I've found to work until someone comes up with a something better. Looking at the code for ExtractArchive[], this function has a pattern that takes a list of destfiles. This wasn't obvious to me since the docs just specify that the third argument can be a 'pattern'. But given this, you can get the names of the files that ...

6

Cases[sol, {_?PrimeQ, _?PrimeQ}] (* {{2063, 853}, {2069, 857}} *)

9

Cases[{__?PrimeQ}] @ sol {{2063, 853}, {2069, 857}}

3

Here is a way that uses version 12.1's shiny new SubsetReplace: SubsetReplace[x:{{_, d_} ..} :> {Total@x[[All, 1]], d}] /@ assoc

4

Another way, without using regular expressions StringSplit[#, "="] & /* First /@ {"A1=(345.2345,3423.1)", "B=(2123,97.123)", "KX=(2144.546,-4455)"} {* {"A1", "B", "KX"} *)

4

Use positive lookahead: ClearAll[f] f = StringCases[RegularExpression["^[^=]+(?==)"]]; f /@ {"A1=(345.2345,3423.1)", "B=(2123,97.123)", "KX=(2144.546,-4455)"} {{"A1"}, {"B"}, {"KX"}}

5

ClearAll[f] f = StringCases[RegularExpression["(^.+)="] :> "$1"] f /@ {"A1=(345.2345,3423.1)", "B=(2123,97.123)", "KX=(2144.546,-4455)"} {{"A1"}, {"B"}, {"KX"}} 2 You may use Query. Query[All, GroupBy[Last -> First] /* KeyValueMap[Reverse[{##}] &], Total]@assoc Hope this helps. 3 Update: "Adding values (...) through pattern matching": You can use ReplaceRepeatedas follows: rule = {a___, {b_, c_DateObject}, d___, {e_, c_}, f___} :> {a, {b + e, c}, d, f}; ReplaceRepeated[rule] /@ assoc Original answer: f1 = Map[Values @ GroupBy[#, Last, {Total[First /@ #], #[[1, 2]]} &] &]; f1 @ assoc Also f2 = Map[{Total[#[[All, 1]]... 5 m = { {3, 1, 2}, {0, 5, 1}, {1, 4, 2}, {0, 6, 9}, {0, 4, 7}, {2, 6, 8} }; Flatten[ConstantArray[#, #[[1, 1]]] & /@ Split[m, #2[[1]] == 0 &], 2]$\left( \begin{array}{ccc} 3 & 1 & 2 \\ 0 & 5 & 1 \\ 3 & 1 & 2 \\ 0 & 5 & 1 \\ 3 & 1 & 2 \\ 0 & 5 & 1 \\ 1 & 4 & 2 \\ 0 &...

1

ClearAll[f] f = Module[{m = #, p = SequencePosition[#, {{Except[0], __}, {0, __} ...}]}, Join @@ (Join @@ ConstantArray[m[[Span @@ #]], m[[#[[1]], 1]]] & /@ p)] &; Examples: a = {{3, 1, 2}, {0, 5, 1}, {1, 4, 2}, {0, 6, 9}, {0, 4, 7}, {2, 6, 8}}; MatrixForm /@ {a, f @ a} SeedRandom[1] b = RandomInteger[3, {7, 3}]; MatrixForm /@ { b, f @ b}

3

m = {{3, 1, 2}, {0, 5, 1}, {1, 4, 2}, {0, 6, 9}, {0, 4, 7}, {2, 6, 8}} SequenceReplace[m, seq : {{x_ /; x > 0, __}, Repeated[{0, __}, {0, Infinity}]} :> Table[seq, x]] Flatten[%, 2]

3

This could be a starter for discussions: A = RandomInteger[{0, 3}, {6, 3}]; idx = Pick[Range[Length[A]], Unitize[A[[All, 1]]], 1]; B = Join @@ MapThread[ Join @@ ConstantArray[A[[#1 ;; #2]], A[[#1, 1]]] &, { idx, Append[Rest@idx - 1, Length[A]] } ]; Hm. Building the list of rows first and to read from A only once seems to be ...

6

One way to is to use direct replacement: exponents = {2, 2}; _^# & /@ exponents /. {e__} :> HoldPattern[Times[e]] (* HoldPattern[_^2 * _^2] *) For more elaborate mechanisms that partially evaluate held expressions, see Replacement inside held expression.

1

StringReplace[string, { StartOfString -> "\"", "," -> "\",\"", patt : ("\n" ~~ WhitespaceCharacter ..) :> StringJoin["\"", patt, "\""], EndOfString -> "\""}] "a","b","c","d" "e","f","g","h" or without the brackets StringReplace[string, { StartOfString -> "\"", "," -> "\",\"", patt : "\n" ~~ WhitespaceCharacter .. :...

3

When you need a pattern to match what is normally used as a pattern, you should use Verbatim: foo[Verbatim[Pattern][_, Verbatim[Blank[Integer]]]] := 1 foo[Verbatim[Pattern][_, Verbatim[Blank[Real]]]] := 1.5 foo[Verbatim[PatternTest][_, StringQ]] := "foo" foo /@ {a_Integer, b_Real, c_?StringQ} (* {1, 1.5, "foo"} *) When writing patterns like this, it's ...

7

Mathematica is generally more reliable if your replacement is for a single variable rather than an expression, such as: -Cos[π/6 - 6*wt] + 7/5 Cos[π/6 + 6*wt] /. {wt -> wt/6} (*7/5 Cos[wt + π/6] - Cos[π/6 - wt]*)

8

You can use -Cos[π/6 - 6*wt] + 7/5 Cos[π/6 + 6*wt] /. {a_ wt :> Sign[a] wt} or, if you want to replace only coefficients 6 or -6,: -Cos[π/6 - 6*wt] + 7/5 Cos[π/6 + 6*wt] /. {(a : 6 | -6) wt :> Sign[a] wt} to get -Cos[[Pi]/6 - wt] + 7/5 Cos[[Pi]/6 + wt]

3

you may try something like: -Cos[\[Pi]/6 - 6*wt] + 7/5 Cos[\[Pi]/6 + 6*wt] /. Cos[x_ + y_] :> Cos[x + y/6]

4

In addition: DeleteCases[featureList, str_/;StringContainsQ[str, "HVAC"]] But perhaps combined with a regular expression is more powerful? For example (delete cases where str begins with 'HVAC' only): DeleteCases[featureList, str_/;StringContainsQ[str, RegularExpression["^HVAC"]]] {ML #, Property Type, SubSpcSqFt, Land Sz SF, Building Type, List ...

10

featureList = {"ML #", "Property Type", "SubSpcSqFt", "Land Sz SF", "Building Type", "List Date", "Yr Blt", "Address", "Area", "Class", "Clear Ceiling Ht (Feet)", "Lot Frontage (ft)", "Lot Depth (ft)", "Zone", "Price", "Sale Type", "Transaction Type", "Zoning/Land Use", "Amenities-HVAC System", "Building Type-Freestanding", "HVAC-See ...

Top 50 recent answers are included