Tag Info

Hot answers tagged

16

What's happening This is not simple by any means. You have encountered another instance of a general situation with lexical scope leaks / emulation / over-protection by symbol renaming. The case at hand is pretty similar to the one discussed here, so you can read the detailed explanation of this behavior in my answer there. Roughly speaking, outer lexical ...


15

fibSequences[n_?EvenQ] := Nest[Accumulate[Join[{1, 0}, #]] &, {}, n/2] fibSequences[n_?OddQ] := Most@Nest[Accumulate[Join[{1, 0}, #]] &, {}, (n + 1)/2] fibSequences[10] {1, 1, 2, 3, 5, 8, 13, 21, 34, 55} fibSequences[9] {1, 1, 2, 3, 5, 8, 13, 21, 34}


14

There are subtle differences between #& and Identity. If you pass more than one argument, Identity will complain and remain unevaluated, #& will just return the first argument. Identity[x, y] (* Identity::argx: Identity called with 2 arguments; 1 argument is expected. >> *) (* Identity[x, y] *) #&[x, y] (* x *) Also Identity is ...


10

Let's start by looking at the code for SequenceCases: << GeneralUtilities` PrintDefinitions[SequenceCases] We can see in this code that three different conditions determine whether the expression will be evaluated by sequenceCasesSublist or sequenceCasesPattern. Let's evaluate the two tests that matter on {a_?PrimeQ, b_, c_?PrimeQ} and {a_?PrimeQ, ...


8

It is sometimes beneficial to first work with functions (in mathematical sense) as symbols and apply to them some pointwise operations. Then, just at the end, convert resulting expression to pure function (in Mathematica sense) and pass some arguments. This can be automated using something like this: ClearAll[purify] Options[purify] = {"FunctionPattern" ...


7

Similar comments on recursion. The expression you create looks like this: TreeForm[orig, VertexLabeling -> None] Building it, e.g., like this gives you a flat structure: new = Table[ Rotate[ Line[AnglePath[{{-0.7, -0.7}, (-Pi/6)}, {{2, (7 \[Pi])/8}, {2, \[Pi]/8}, {2, (7 \[Pi])/8}, {2, \[Pi]/8}}]], i*Pi/5, {0, 0}], {i, 10}]; Such ...


7

First@Sort[{Times @@ #, #} & /@ Transpose[{list1, Reverse@list2}]] (* {15, {15, 1}} *) The first number is the minimum of the multiplications (15), which was obtained from multiplying (15) from list1 by (1) from list2. The following: Sort[{Times@##, ##} & @@@ Transpose[{list1, Reverse@list2}]] // First also performs the same thing. If you want ...


7

I have used a method similar to your "dirty code" myself and I don't see an apparent alternative as Pattern requires a true Symbol for its first parameter. I will note that your Unique Symbols should be made Temporary, or you can generate them with Module to add this attribute automatically. And since you are trying things for fun you could use Map in ...


6

I notice no one is using an association to map to positions nor allowing the sort to use a one-element heap instead of building up an entire sorted array ... PositionIndex[#] @@ TakeSmallestBy[#, Times @@ # &, 1] & [ Transpose[{list1, Reverse@list2}] ] In the example, the result is {14}, the index of 15 in list1 that gets paired with 1 ...


6

Here is a somewhat verbose and strange way of getting your answer: Function[{x}, {#, list1[[#]], x[[#]]} & @@ Ordering[x, 1]] @ Times[list1, Reverse@list2] Times[list1, Reverse@list2] gets your third list. This function: Function[{x}, {#, list1[[#]], x[[#]]} & @@ Ordering[x, 1]] takes Times[list1, Reverse@list2] as the argument x. Ordering[x, ...


6

You can achieve this defining an UpValue for g: g/:Power[g,2]:=g[#]^2& Or more generally: g/:Power[g,n_Integer]:=g[#]^n& Using Through now works as wanted: Through[(f + g^2)[x]] (*Out=f[x]+g[x]^2*)


6

The first thing I came up with was: Permutations[l] The second thing I came up with was an inductive answer: perms[l_] := Flatten[With[{p = perms[Rest@l]}, Function[{n}, Insert[#, First[l], n] & /@ p] /@ Range[Length[l]]], 1] perms[{a_}] := {{a}} I can't think of a pattern-based one at the moment, but I'm sure there's a clever one.


6

To get the polynomial, the easiest way is res = M /. Rationalize@Solve[a == 0, M]; poly = res[[1, 1]][M] 900 + 14400 M^5 + M^4 (50400 - 8944 z) - 1909 z + M^3 (65700 - 27760 z - 14612 z^2) + M (9900 - 13690 z + 98 z^2) + M^2 (38700 - 30597 z - 14514 z^2 + 147 z^3) Now I don't know what Solve does, but I did the following. Take the numerator ...


6

This is pitched at the "how to understand" level, not the "how is it implemented" level. I've basically presented how I think of these things, without really caring about how Mathematica carries them out. The object h[2][x] is just another expression-with-head. The head is h[2], which just happens to have a more complicated form than is perhaps usual. In ...


5

Well... I did use Accumulate! First /@ NestList[{Last@#, Last@*Accumulate@#} &, list, 10] {0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55} EDIT Of course I forgot to mention that list = {0, 1}


5

Possibly you want the Import format "HeldExpressions": Import["test", "HeldExpressions"] {HoldComplete[A = {1, 2, 3, 4, 5}], HoldComplete[B = {1, 4, 9, 16, 25}], HoldComplete[Attributes[C] = {NHoldAll, Protected}]} The last expression may not be as you expect until you remember that C is a reserved System Symbol. Sorry, I overlooked the fact ...


4

In your examples you choose one color to begin with and then you pass that color onwards to each of the following leaves through recursion. Just like you have to apply Rotate in each step to change the angle, you also have to apply an operator that changes the color. For example: next[{_, leaf_}] := {RandomColor[], Rotate[leaf, Pi/5, {0, 0}]} g1 = ...


4

see comment below by OleskandrR If he posts I will up vote. Original post This is not ideal (and done with little time) but may motivate better answers. Note this manually (visually tries to minimize the discontinuity at "breakpoint") using Manipulate. The NonlinearModelFit treats the breakpoint as fixed so you can play and chose better way. I look forward ...


4

The local i does not evaluate in the first table of functions; e.g. Table[# f[i] &, {i, 1, 10}] gives... {#1 f[i] &, #1 f[i] &, #1 f[i] &, #1 f[i] &, #1 f[i] &, #1 f[i] &, #1 f[i] &, #1 f[i] &, #1 f[i] &, #1 f[i] &} Use With to force the evaluation of i... Table[With[{i = i}, # f[i] &], {i, 1, 10}] to ...


4

Not that I would recommend this, but anyway: positionPayload = 2. x + 3. t^2; variableList = Variables[positionPayload]; positionPayload = Function[Evaluate@variableList, Evaluate@positionPayload]; positionPayload[q, r] (* 3. q^2 + 2. r *) Edit positionPayload = 2. x[1] + 3. t[3]^2; variableList = Variables[positionPayload]; ul = ...


4

There was a wrong bracket in F. Also do not use { as a normal bracket, and second you wrote Exp{[ which has to be either {Exp[ or (Exp[ . F[k1_, k2_, λ1_, λ2_, δ_, w1_, w2_] := (1 - Exp[-(w1/λ1)^k1]) (1 - Exp[-(w2/λ2)^k2]) (Exp[(1 - (1 - Exp[-(w1/λ1)^k1]))^(-δ) + (1 + (1 - Exp[-(w2/λ2)^k2]))^-δ]^(1/-δ))


4

I can illustrate what is going on with a simpler function than yours. Consider f[x_, y_] := Sum[y[[i]], {i, Length[x]} Then f[{"a", "b"}, {3, 4}] gives 7 as expected, but f[x, y] /. {x -> {"a", "b"}, y -> {3, 4}} gives 0, just as your function did. Now /. is infix operator version of ReplaceAll, so the above is equivalent of ReplaceAll[f[x, y], ...


4

The given above answer is correct, however if you are not against using undocumented functions, then Internal`StringToDouble["9.0E-03"] is much faster. To demonstrate the speedup, first generate some fake data heads = ToString /@ RandomReal[{1.0000, 9.9999}, 100000, WorkingPrecision -> 5] // Quiet; exp = ConstantArray["E-", 100000]; tails = ...


4

Everything in Mathematica has a precedence, including semicolon! That is similar to 3+4*5 meaning the multiplication is done before the addition. So the way you have it written above says the definition of your calc function ends with your first semicolon and then your second Module will calculate a result without waiting for you to later use your calc ...


4

Here are a number of one-liners that will define f ci = {X1, X2, X3, X4}; This first one is perhaps the easiest for_Mathematica_ newcomers to understand. Clear[f, t]; f[t_] := Evaluate[Sum[ci[[i]][t], {i, Length[ci]}]]; Definition[f] This one is pretty easy to understand too. Clear[f, t]; f[t_] := Evaluate[Plus @@ Through[ci[t]]]; Definition[f] ...


4

I'm probably missing an important point, but what is wrong with (f[#] + g[#]^2)&[x] f[x]+g[x]^2


3

Your code can be much simplified. The following rewrite of your code works. wlines = {427.397, 431.958, 450.235, 557.029, 587.092, 605.613, 645.629, 665.223, 669.923, 681.311, 690.468}; wcal = {4.1989123474370302*^02, -5.3957450948852408*^-02, 6.7152505835315814*^-04, -8.6698204011228679*^-07, 5.5523712684399200*^-10}; g[x_] = ...


3

You might try something like this, which makes pure functions, which means the variables used int the expression to converted only have to be clear at time expToF is called. expToF[exp_, vars : {_Symbol ..}] := With[{body = exp /. Thread[Rule[vars, Slot /@ Range @ Length[vars]]]}, Function[body]] Clear[x,t] f = expToF[2. x + 3. t^2, {x, t}]; f[x,t] ...


3

You should correct the typo in your code. I'm not even sure how this can happen, when you copied the example from your notebook. Additionally, you shouldn't use $\psi$ for both, a function and a variable. Once this is fixed: energyFunctional[ψ_] := Integrate[ 1/2 (D[ψ[x], x])^2 + 1/2*x^2*(ψ[x])^2, {x, -∞, ∞}]; ψ1 = (1/π)^(1/4) Exp[-(#^2/2)] ...


3

I think it is best to approach these kinds of problems with pure functions, which minimizes nasty surprises from variable scoping issues. Let's see how well this idea works out when applied to your problem. Generate test data. With[{u = Range[-180., 180., 6.] Degree}, data = With[{x = #[[1]], y = #[[2]]}, {x, y, Sin[x] Cos[ y]}] & /@ ...



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