# Tag Info

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}

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

The problem is that the function name f is substituted into Function (&) which is HoldAll. This means f[t] will not be evaluated until the Function is evaluated, such as in the OP's example problem[one][17]. So the trick is to evaluate the integrand before inserting it into the Function. Here is one way. one[x_] := 1 (*the argument*) problem[f_] := ...

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

Here is a partial answer. I believe for Method -> {opts}, opts may be any of the following, and they will have whatever effect they have: InternalInequalitySolverOptions[] InternalReduceOptions[] InternalNSolveOptions[] (* {"ARSDecision" -> False, "BrownProjection" -> True, "CAD" -> True, "CADAlgebraicCoefficients" -> True, ...

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 ...

5

The following I hope will assist you: b = 0; blub[x_, p_] := Do[b += ((-1)^k/x^k), {k, 1, p}] blub[1, 100] b or achieving the output: f[x_, p_] := Module[{v = 0}, Do[v += ((-1)^k/x^k), {k, 1, p}]; v] There are many ways to achieve this result, e.g. s[x_, p_] := Sum[(-1)^k/x^k, {k, 1, p}] r[x_, p_] := Total[(-1)^#/x^# & /@ Range[p]]

5

As Guess says, List @@ expr should work. A weirder way: First@Replace[{a || b || c || d}, Or -> List, {2}, Heads -> True] A less weird way: ReplacePart[a || b || c || d, 0 -> List]

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

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

You can take advantage of how the rules associated with theta are stored behind the scenes by doing Length @ DownValues[theta][[1, 2, 1]] (* 40 *) This isn't robust, however. In the sense that if you have defined beforehand theta[10] = 5, for instance, then it won't find the right DownValue. Alternatively, you can build the checking into the function: ...

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

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

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

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

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

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] ...

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