Tag Info

New answers tagged

2

This is a question of finding a suitable algorithm rather than use of Mathematica. The challenge is to generate only the permutations that will be used, rather than generate all permutations and filter those that satisfy a criterion. Fun problem. Here's my solution: cond = {0, 0, 1}; possibleElements = Range[cond, Length@cond-1]; (* {{0, 1, 2}, {0, 1, ...


2

This is pretty functional: f = Module[{comps, r = Reverse@Range[#2, #1 - 1]}, comps[l1_, l2_] := Join @@ Map[Thread[{Sequence @@ #, Complement[l2, #]}] &, l1]; Reverse /@ Fold[comps, Transpose@{First@r}, Rest@r]] &; This is about 10-15% faster, very slightly higher memory use (but still far below your current solution): fz = With[{r = ...


2

pdata[[Flatten[ Values[Map[{#[[1]], #[[-1]]} &, PositionIndex[pdata[[;; , 1]]]]]]]] (*{{AA, 1, 10}, {AA, 2, 20}, {CC, 3, 30}, {CC, 7, 70}, {DD, 8, 80}, {DD, 10, 100}, {EE, 11, 110}, {EE, 13, 130}, {HH, 14, 140}, {HH, 20, 200}}*)


3

You can also use the Version 10 functions GroupBy and Merge: Join@@Values@GroupBy[pdata, First, Through@{First,Last}@#&] and Join@@Values@Merge[#->{##}&@@@pdata,Through@{First,Last}@#&] to get (* {{AA, 1, 10}, {AA, 2, 20}, {CC, 3, 30}, {CC, 7, 70}, {DD, 8, 80}, {DD, 10, 100}, {EE, 11, 110}, {EE, 13, 130}, {HH, 14, 140}, {HH, 20, ...


3

If you data is sorted: Flatten[SplitBy[pdata, #[[1]] &][[All, {1, -1}]], 1] if not: Flatten[GatherBy[pdata, #[[1]] &][[All, {1, -1}]], 1]


4

In the Standard Evaluation Sequence the heads of expressions are evaluated first: If the expression is a raw object (e.g., Integer, String, etc.), leave it unchanged. Evaluate the head h of the expression. Evaluate each element of the expression in turn ... Therefore since f[1] is the head of f[1][2] it will evaluate if it has a definition that matches. ...


0

I managed to update the workbook for Mathematica-10-compatibility, but stopped just before the section Duffing Equation. I also did not copy the original explanatory texts (yet), since this will need some refurbishing also. Here is what I managed so far (just copy, paste and execute): ...


7

I believe the first part of your question is answered by Stack. Observe: g := Stack[] something[f1[g], f3[g]] something[f1[{something, f1}], f3[{something, f3}]] So you can find that g was evaluated in f1 or f3 and further that these were evaluated in something. However this should not be necessary for your Ticks application. The value of Ticks ...


1

The error itself may be isolated to this operation: zeroordereqn = ω^2 x[0][T[0],T[1]]+(x[0]^(2,0))[T[0],T[1]]; DSolve[zeroordereqn == 0, x[0], {T[0], T[1]}] DSolve gives a result in terms of a Function using the parameters T[0] and T[1] but this is not valid. Perhaps at some time DSolve gave different output but I cannot test that. However if one ...


0

I can't follow your code. I suggest your question is not about "Set" but how to index data by strings. In the past, I have found code like this to be useful: people = { {"First name", "Last name", "Birth"}, {"Johannes", "Bach", 1685}, {"Peter", "Sculthorpe", 1929}}; (peopleFn[First@#] = Rest@#) & /@ Transpose@people; peopleFn["Last name"] ...


1

Nothing intrinsically wrong with what you've done, but perhaps a bit messy. For example, here's the epsilon>1 case done a bit more Mathematica style. Easier to read and faster: (* epsilon>1 case as function *) genResults = Module[{inc = 1, init = RandomVariate /@ #1, rvs = RandomReal[{-1, 1}, {#2, Length@#1}]}, ...


0

I found the problem— your comments all helped immensely. (x[[i]].w) y[[i]] yields a single-element list like {0.1}. Because of this, the if condition was defaulting to undefined. I changed it to ((x[[i]].w) y[[i]])[[1]] and this fixed it. Thanks again for all your help!


4

Leaving the Suggestions Bar enabled The predictive interface (Suggestions Bar) is the source of many bugs reported on this site and surely many more that have yet to be reported. I strongly suggest that all new users turn off the Suggestions Bar to avoid unexpected problems ranging from massive memory usage to peculiar evaluation leaks.


4

For question 1, with Mathematica 10.0.2, as an example, let's get the current icebergs from Antartica, as reported by US National Ice Center.Graph all icebergs with remarks amerw* icebergs = Import["http://www.natice.noaa.gov/pub/icebergs/Iceberg_Tabular.\ csv"]; titles = First@icebergs; icebergs = Drop[Rest@icebergs, -1]; GeoGraphics[{Text[#[[1]], ...


1

The probability mass over a specified support (region on your map) can be visualized by using RegionFunction[] as such: {\[Mu]x, \[Mu]y} = {0, 0}; Plot3D[ PDF[MultinormalDistribution[{\[Mu]x, \[Mu]y}, {{1, 0}, {0, 1}}], {x, y}], {x, -2, 2}, {y, -2, 2}, RegionFunction -> Function[{x, y}, 1 < x < 2 && 0 < y < 1], PlotRange ...


1

Maybe something like: ClearAll[foo]; foo[t1_: 0.01, t2_: 0.05, t3_: 0.1] := Module[{m = Mean@#, p = Quiet@TTest[#, Automatic, "TestData"], s = {"", "*", "**", "***"}, c}, c = # <= p[[2]] < #2 & @@@ Partition[{0, t1, t2, t3, Infinity}, 2, 1]; {Which @@ Riffle[c, Superscript[ToString@m, #] & /@ s /. Superscript[x_, ""] :> x], ...


2

In a comment elsewhere, @Mr.Wizard suggested that associations might be useful for this question. Here is my attempt: normalizeAssoc[keys_][l_] := <| l, AssociationThread[keys -> Sort @ Lookup[l, keys, 0]] |> ... and here it is in action: Take[testdata, 2] (* {{h->0.074356,i->0.756409,f->0.456624,b->-0.0342208,c->0.634687, ...


0

The integral in your differential equation isn't the same as that in the post you linked, it can be easily eliminated if you D your equation: vdh = {x, t} \[Function] 1/(1 + h[x, t]) - h[x, t] + Log[h[x, t]/(1 + h[x, t])] λ = t \[Function] -(1/L) Integrate[vdh[x, t], {x, 0, L}] neqn = D[D[h[x, t], t] == D[h[x, t], {x, 2}] - vdh[x, t] - λ[t], x] Then ...


2

I realize I am basically asking "how to handle custom objects", but I thought giving the context would make it easier to point me in the right direction. It seems that you are. I believe the most natural way to do that in Mathematica is to use a custom head. I'll use obj for my examples. First you might define a pattern for your custom object: p0 = ...


0

Here is an approach that is much faster, but with potentially some loss of accuracy. Do away with NIntegrate and eval the function at preselected points in the region: nsamp = 200; pts = Select[ Table[{RandomReal[{0, gam1 + Re[gam2]}], RandomReal[{0, Im[gam2]}]}, {nsamp}] , # \[Element] \[ScriptCapitalR] & ]; ...


4

Analysis Function is left-associative as converting to StandardForm reveals: (((f[#1] &) /@ f[#1] &) /@ f[#1] &) /@ {a, b, c} You can see the result of the rather odd operation with: f = {#, "x"} &; f[#] & /@ f[#] & /@ f[#] & /@ {a, b, c} {{{{a, x}, {x, x}}, {{x, x}, {x, x}}}, {{{b, x}, {x, x}}, {{x, x}, {x, x}}}, {{{c, ...


0

I am not sure which from of the solutions you are looking at but you can try this: f = #+1&; Composition[Sequence @@ ConstantArray[f, 2]][f[#]] & /@ {a, b, c} (*{3 + a, 3 + b, 3 + c}*) how about this: Nest[Map[f, #,{-1}] &, f[#], 2] & /@ {a, b, c}


4

rrF = Ordering@Ordering@# &; crF = Transpose[rrF /@ Transpose[#]] &; mat = RandomInteger[{1, 10}, {10, 5}]; Row[MatrixForm /@ {mat, crF@mat}] Update: Using the function colMap suggested by @Mr.Wizard in the comments colMap[f_][m_?MatrixQ] := (f /@ (m\[Transpose]))\[Transpose] (* or Transpose[fn /@ Transpose[m]] *) colMap[rrF][mat] // ...


1

Ordering /@ Transpose[RandomVariate[UniformDistribution[{10}], {3, 4}]]


1

The expression x<>x evaluates to the STRING "xx", but the Save command only saves symbols, so Save["file",x] works fine and Save["file",x<>x] does not work. You can assign the result to an expression and "Save" this expression.


2

Inner[Greater, x, y, Boole @ {##} &] Inner[Boole@Greater@## &, x, y, List] Inner[Composition[Boole, Greater], x, y, List] Boole @ Inner[Greater, x, y, List] Block[{Greater = Subtract}, UnitStep[x > y]]


5

Boole@Thread[Greater[x, y]] == MapThread[Boole@Greater@## &, {x, y}]


1

Boole[#1 > #2 & @@@ Transpose[{x, y}]] Or Transpose[{x, y}] /. {x_, y_} :> Boole[x > y]


1

"Do" form, but I don't think you understand how to use semicolon instead of comma, even though your semicolon versus comma issues have been corrected a couple of times in other forums. Clear[T, A, CC, II, Y, z, a, b, d, e, m, n]; T = 3; A[0] = 2; CC[0] = 2; II[0] = 0.5; Y[0] = 5; z = 0.3; a = 0.2; b = 3; d = 0.6; e = 0.95; m = 0.2; n = 0.4; Do[{A[t] = (1 + ...


8

Basics To get it out of the way, for the specific example given you could use Unevaluated: Cases[Unevaluated[1 + 3], _, {-1}] {1, 3} To actually be able to modify a System function I recommend Internal`InheritedBlock: SetAttributes[cases, HoldAll] cases[args___] := Internal`InheritedBlock[{Cases}, SetAttributes[Cases, HoldFirst]; ...


1

Just for fun: Join @@@ ({{1, 2}, {3, 4, 5}, {6}} /.Last@Reap[Sow[{##}, #1] & @@@ testdat, _, Rule]) yields: {{{1, a11, b11}, {1, a12, b12}, {2, a21, b21}, {2, a22, b22}}, {{3, a31, b31}, {3, a32, b32}, {3, a33, b33}, {4, a41, b41}, {4, a42, b42}, {5, a51, b51}, {5, a52, b52}, {5, a53, b53}, {5, b54, b54}}, {{6, a61, b61}, {6, a62, b62}}} ...


2

We can MapIndexed over a list of group lists to create an indexing function g, then compose this with the test function fn: gatherInto1[dat_, fn_, groups__List] := Module[{g}, MapIndexed[(g[#] = #2[[1]]) &, {groups}, {2}]; GatherBy[dat, g @ fn @ # &] ] Example: gatherInto1[testdat, First, {1, 2}, {3, 4, 5}, {6}] { {{1, a11, b11}, ...


2

I'm a bit unclear as to why this works .... however, using FindPath instead of FindShortestPathyields a route through 71 of the 88 vertices which definitely doubles back on itself. HighlightGraph[lab = Graph[edges], PathGraph[s = Last@Sort@FindPath[lab, 1, 125]], VertexLabels -> "Name", ImagePadding -> 10, GraphHighlightStyle -> "Thick", ...



Top 50 recent answers are included