Tag Info

New answers tagged

1

titanic[GroupBy[Key@"class" -> (Switch[#class, "1st", #age, "2nd", #sex, "3rd", #survived] &) ]] // Normal // Dataset


4

Note: the question has evolved, as has my understanding of it. My response has been changed completely, although I have kept my earlier responses below. If we can drop the requirement to use AssociationMap, pivotApply and foo, then we can generate the desired output directly using subqueries: titanicClass @ { "1st" -> Query[All, #age > 18&] ...


3

Using this helper function ClearAll[keyStrip] keyStrip[Key[k_]|{Key[k_]}] := k; keyStrip[expr_] := expr; you can achieve what you asked for as titanicClass[MapIndexed[foo[keyStrip[First@#2], #1] &]] The general issue about being able to reference some elements of the parent structures inside child structures during the traversal (in the queries) is ...


1

Mapping Trace[] Since List evaluates its arguments, the construct Trace /@ { stuff } will usually give you empty lists because the stuff has been evaluated before Trace sees it (and Mathematica is smart enough not to re-evaluate expressions which haven't changed). There is no more evaluation to be done, so Trace shows nothing. Solving the actual problem ...


3

There's a nice function in the GeneralUtilities package included in Version 10 called Where. You can use this as: Needs["GeneralUtilities`"] Where[n = 1, m = n + 1, {Slot[n], Slot[m]} & @@@ list1] Gives: {{a1, b1}, {a2, b2}, {a3, b3}}


6

You can use With to insert values into held expressions: With[{n=1,m=2}, {Slot[n], Slot[m]} & @@@ list1 ] {{a1, b1}, {a2, b2}, {a3, b3}} If you're so inclined, you can do a nested With: With[{n = 1}, With[{m = n + 1}, {Slot[n], Slot[m]} & @@@ list1 ] ] Or with Leonid's exceedingly nice LetL: LetL[{n = 1, m = n + 1}, {Slot[n], ...


13

Good News Everyone! Two-parameter syntax for Fold and FoldList has been (silently) implemented! Taliesin Beynon informs me that this was implemented in 2011, so check your older versions as well. Using version 10: Fold[f, a] FoldList[f, a] f[f[f[1, 2], 3], 4] {1, f[1, 2], f[f[1, 2], 3], f[f[f[1, 2], 3], 4]} And the held expression example: ...


7

That MapAt[Framed, Key[b]][data] doesn't work is a bug, which I'll fix (thanks!). On the other hand, data[All, Key[b] -> Framed] is not a supported syntax for doing applying a function to a specific part. You have to have the enclosing list. As for data[All, Key[b] -> Framed[#] &], the second query element there is a pure function. Pure functions ...


2

I like to use Sum for such things, when possible, as it conserves memory and is usually reasonably fast: Sum[Prime @ i, {i, PrimePi[2*^6]}] 142913828922 Performance (still in v7, for now...) compared to Artes's fully vectorized code, with a larger search space: Sum[Prime @ i, {i, PrimePi[2*^7]}] // Timing MaxMemoryUsed[] {1.857, 12272577818052} ...


3

When using advanced functionality to deal with primes it is highly recommended not using any of NestList or FoldList or whatever similar. PrimePi is especially designed to find how many primes are below of a given number, then Prime roughly inverse of PrimePi is Listable, therefore I'd suggest this approach yielding the result almost immediately: Total @ ...


0

First @ NestWhile[ With[{p = Prime[#[[3]]]}, {#[[1]] + #[[2]], p, #[[3]] + 1} ] &, {0, 2, 2}, #[[2]] <= 2 10^6 & ] EDIT: this is a faster version: Total[ NestWhileList[ {Prime@#[[2]], #[[2]] + 1} &, {0, 1}, #[[1]] <= 2 10^6 &][[;;-2, 1]] ]


4

I think your definition of d is not properly generalizable because the list dimensions don't match when doing higher derivatives. So I instead use a simpler definition of the Gateaux derivative from Wikipedia which does exactly the same thing as what you're trying to do. I call it gatD, and it takes the operator, the function u and a List of test functions. ...



Top 50 recent answers are included