Tag Info

New answers tagged

3

The introduction of Assocation brings a powerful new way to handle this problem. (* archive data *) ad = {"accept_rate" -> 75, "account_id" -> 395497, "age" -> 41, "badge_counts" -> {"bronze" -> 35, "gold" -> 0, "silver" -> 11}, "creation_date" -> 1326833982, "display_name" -> "Verbeia", "is_employee" -> False, ...


1

You are running afoul of the (beneficial) scoping that is applied inside Manipulate constructs by way of DynamicModule (or the low-level equivalent). If you "inject" the expression containing g[1] etc. into the Manipulate before it is evaluated it should work correctly I believe: With[{body = gplot[1] == 0}, Manipulate[ ContourPlot3D[body, {x, -1, 1}, ...


1

Standard method: create a univariate in a new variable t, by changing every variable x to t*x. Take a Series expansion in t. Then use Normal to make it explicitly polynomial, and substitute t->1. poly = a*x^2 + b*x^3 + c*x*y + d*x^2*y + e*x*y^2 + y^2; vars = {x, y}; Normal[Series[poly /. Thread[vars -> t*vars], {t, 0, 2}]] /. t -> 1 (* Out[6]= a ...


1

I've found a simple approach using the function Part (its shorthand is [[ ]]): 1. If you write your list lst1 rather that way: newlst1 = {{"A", "a"}, {"D", "dd"}, {"B"}} you can assign any value (for example here 999) to all these keys simply with: (assc[[##]] = 999) & @@@ newlst1; You can check the result: assc <|"A" -> <|"a" ...


4

Competely untested, but I believe this ought to work nicely: p = a*x^2 + b*x^3 + c*x*y + d*x^2*y + e*x*y^2 + y^2; coeffs = CoefficientArrays[p, {x, y}]; k = 2; (* highest degree wanted *) Fold[#1.{x, y} + #2 &, {0, 0}, Take[coeffs, {k + 1, 1, -1}]]


0

P = a*x^2 + b*x^3 + c*x*y + d*x^2*y + e*x*y^2 + y^2 + f*x^2*y^3; rule1 = {Times[a_, Power[x, n_], y] -> n + 1, Times[a_, x, Power[y, n_]] -> n + 1} rule2 = Times[a_, Power[x, n_], Power[y, m_]] -> m + n; rule3 = {Times[a_, Power[b_, n_]] -> n, Power[b_, n_] -> n, Times[a_, x, y] -> 2}; rule3 = {Times[a_, Power[b_, n_]] -> ...


3

Thanks to bbgodfrey's comment I think I understand what you want: p = a*x^2 + b*x^3 + c*x*y + d*x^2*y + e*x*y^2 + y^2; Total @ Exponent[#, Variables @ #] & /@ List @@ p Pick[P, # <= 3 & /@ %] {3, 4, 3, 4, 2, 4} a x^2 + c x y + y^2 Following your update here is a somewhat different formulation that may be useful: op[var_List, ...


3

g = Graphics[{PointSize[.02], Point[{9, 5}], Point[{2, -5}], Point[{5, 5}], Point[{3, 6}], Point[{1, 1}], Point[{5, -7}], Point[{-7, 4}], Point[{6, -10}], Point[{-6, -2}], Point[{0, 8}], Point[{1, 4}], Line[{{1, 4}, {2, 5}}], Point[{2, 5}]}, AspectRatio -> Automatic]; g /. Point -> (Circle[#, .5] &)


1

As already noted for Q2 the best solution is of course StringTrim. For Q1 here is an alternative using regular expressions: stringTrimAtEnd = StringReplace[#,RegularExpression["\\s*$"] :> ""] &


4

As commented, StringTrim for example will do the job. For example : StringTrim[s3] StringTrim[s3, StartOfString ~~ WhitespaceCharacter ..] StringTrim[s3, WhitespaceCharacter .. ~~ EndOfString] return "jbjkbasd nklnkln" "jbjkbasd nklnkln " " jbjkbasd nklnkln"


1

Q1 Column[StringReplace[#, " " .. ~~ EndOfString -> ""] & /@ {s1, s2, s3}] dasbdk asdnkal asn knkl nkn dvklsn jbjkbasd nklnkln Q2 Column[StringTrim[#] & /@ {s1, s2, s3}] dasbdk asdnkal asn knkl nkn dvklsn jbjkbasd nklnkln


0

Q1: keep deleting blank spaces from the end until they're gone (not very elegant but it works): s3 = " jbjkbasd nklnkln " While[Characters[s3][[-1]] == " ", s3 = StringJoin@Delete[Characters[s3], -1]]` Q2: if you use StringReplace[] you can also delete multiple blank spaces in the middle of the String: StringReplace[" asd nkal asn ...


3

MapIndexed can be used to map a function to the inner-most values in nested associations, for example: MapIndexed[f, <| "A" -> <|"a" -> 1|>|>, {-1}] (* <|"A" -> <|"a" -> f[1, {Key["A"], Key["a"]}]|>|> *) The level specification {-1} selects only the deepest level in the expression. Note how f receives not only the ...


3

This solution will traverse all nested associations regardless of depth and replace each value with the value that corresponds to its key in replacements. f[Rule[key_, assoc_Association]] := Rule[key, AssociationMap[f, assoc]] f[Rule[key_, val_]] := Rule[key, key /. replacements] replacements = { "a" -> 1, "dd" -> 1, "B" -> 1, "aa" -> 0, ...


4

Maybe with the help of Fold or FoldList : See what happens here: FoldList[#1 /. #2 &, a, {r1[a], r2[a], r3[a]}] {a, 3 a, -3 a, -3 (1 + a)} Replace FoldList with Fold to get directly the last result. In other words: rall[a_] := Fold[#1 /. #2 &, a, {r1[a], r2[a], r3[a]}] then rall[a] -3 (1 + a)


2

This works too and it's theoretically faster than ReplaceRepeated (//.): asc = Replace[list, r : {__Rule} :> Association[r], {0, Infinity}] The key observation is that Replace starts with the innermost levels first and works its way outwards. In contrast, ReplaceAll and ReplaceRepeated start with the outermost levels and work their way inwards.


1

I am somewhat confused about the aim. Here is an interpretation. Starting with string: string = "{{13,{17,4},{23,10},220/13,3},{17,{23,6},{29,12},4368/17,3},\ {19,{23,6},{29,12},4368/19,3},{23,{29,6},{41,18},26334/23,6},{29,{41,\ 12},{47,18},21474180/29,3},{29,{41,10},{53,22},493350,6},{31,{41,12},{\ ...


0

The replacement rules are like these for a variable with name "R1" [Ri]. Also added is a "s12" [sij] unique variable. It is important to set the rule immediately to the expression where a module is called with the localizeAll function build into it: [expression-to-replace] /. Flatten[{Table[ Symbol["R" <> ToString[ToExpression["i"]] <> "$" ...


5

You should replace __?MemberQ[{1, 2}, #] & with __?(MemberQ[{1, 2}, #] &). Because the & has the low priority. So, the pattern is interpreted as part of the function, and not the function as part of the pattern. This can be easily seen using FullForm: In[24]:= __?MemberQ[{1, 2}, #] & // FullForm Out[24]//FullForm= ...



Top 50 recent answers are included