Tag Info

10

Composition[f, g, g, f, g] might be what you are looking for. Composition[f, g, g, f, g][x] yields f[g[g[f[g[x]]]]].

9

The code below replaces pile with a function avalanche that works a bit bit differently! Instead of calling itself, it outputs a list of things that we then want to recursively call avalanche on again. When the list is empty (there are no more things to compute), we stop. To implement the recursion, we use NestWhile[f, init, testfunction]. the avalanche ...

9

In my experience the two forms have equivalent functionality. The slot-based version is very compact. The named-variable version can be more readable, particularly if embedded inside another pure function that e.g. uses slots. @J.M. brought up a very handy feature of the Function construct with slots: it is possible to assign attributes to these ...

8

EDIT: Okay, I've had a little bit of time to look at the PDE as well as your iterative solution. I don't believe the code below is set up correctly as there are a few mistakes. First, if you want $c = 1/4$, then it should be $dt = 1/(4 n^2)$. Second, there's a $\pi$ missing in the definition of f. Thirdly, the array should be of size $1/dt \times 1/dx$ or $... 6 l = {1, {2, {{3, 4}, {5, 6}}, 7, 8}}; result=IntegerString[l] {"1",{"2",{{"3","4"},{"5","6"}},"7","8"}} Per your comment question, either map on result, or just combine into one map (here prefixing with "a" for example): Map["a" <> # &, result, {-1}] Map["a" <> IntegerString@# &, l, {-1}] Both give: {"a1",{"a2",{{"a3","a4"},{"a5",... 6 All the methods below basically create for each rule k -> g[a, b, c,...] a code of the form mem : k[t_] := mem = g[a[t], b[t], c[t],...] which is then executed. The usage is the same for all three: ClearAll[a, b, c, d, mem]; toMemo[t][rules1] They also work for multivariate definitions, such as toMemo[s, t][rules]. Method 1 toMemo[args___][rules_] :... 5 This is not a full answer to the question! I have a package which helps solve eigenvalue BVPs by calculating the Evans function, an analytic function whose roots correspond to the eigenvalues. Some details are available at these two questions, or this PDF. Or search for CompoundMatrixMethod to see my previous answers here. It comes much better than the ... 5 Extend the definition of your function to include all inputs: func2[n_?IntegerQ, m_?IntegerQ] := If[n==m,0,n-m] func2[n_, m_] := 0 Now when you input n and m integers the top definition applies, when n or m are not integers, the bottom applies. Thus func2[4, 7] is -3 while func2[54., 32] is 0. You could do the same for func1. 5 If I understand you right, you have names of variables, and have list of values and want to assign variables to values? In this case, one way could be Clear["Global`*"]; values = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; vars = {len, n, range, midrange, μ, median, mode, σavg, rms, σ2, σ, Em, Es, skew, kurt, mean}; Then do ... 5 Reference Replace, Apply: fList = {f1, f2, f3, f4, f5}; Or @@@ Replace[Evaluate[fList] &, f_ :> # === f@#2, {2}] #1 === f1[#2] || #1 === f2[#2] || #1 === f3[#2] || #1 === f4[#2] || #1 === f5[#2] & Similar: Replace[Function @@ {Or @@ fList}, f_ :> # === f@#2, {2}] make[{x__}] := Replace[Or[x] &, f_ :> # === f@#2, {2}] make[fList] ... 5 The integration limits can't be Row and there were some syntax issues I cleaned up. Hopefully the comments (* ... *) below explain everything. You can change the$x,R,n\$ in the With to different values if needed: (* Function to create the symbols r[1],r[2],...,r[n] *) LoC[n_] := Array[r,n] (* Functions to integrate *) f[x_] := x g[l_] := Sum[f[i], {i, l}] ...

5

It is a bit exotic, but #0 as a reference to the whole pure function is only possible with the slot notation, AFAIK. It makes possible to define recursive pure functions. There are other posts on this site which explain the details. Example: Function[If[#2 > 0, #0[#1 + #2, #2 - 1], #1]][0, 100]

4

If the application does not demand explicit use of code generation and Or then we might consider something like this: test[fns___][x_, y_] := AnyTrue[{fns}, x === #[y] &] This would allow us to define functions that test against any number of target functions. For example: f = test[Sin, Cos, Tan]; or equivalently: f = test @@ {Sin, Cos, Tan}; f ...

4

Nest[f@*g, f@g@##, 2] &[] f[g[f[g[f[g[]]]]]] Nest[f@*g, f@g@##, 2] &[x] f[g[f[g[f[g[x]]]]]] Also flist = {f, g}; Nest[Last[flist = RotateLeft[flist]]@# &, g[], 5] f[g[f[g[f[g[]]]]]] and (Composition @@ ConstantArray[f@*g, 3])[] f[g[f[g[f[g[]]]]]]

4

Why not use function? ClearAll[g, x, y, H]; g[x_, y_] := x^2 + y^3; H[x_, y_] := Eigenvalues[{{0, g[x, y]}, {g[x, y], 0}}] And now H[2, 3] gives (-31,31} And to avoid having to call g[x,y] twice, you could optimize it a little ClearAll[g, x, y, H]; g[x_, y_] := x^2 + y^3; H[x_, y_] := Module[{z = g[x, y]}, Eigenvalues[{{0, z}, {z, 0}}]] it is ...

4

Some Hold[] trickery can be used for this: fList = {f1, f2, f3, f4, f5}; Hold[Function][Or @@ Thread[Hold[SameQ][#1, Through[fList[#2]]]]] // ReleaseHold #1 === f1[#2] || #1 === f2[#2] || #1 === f3[#2] || #1 === f4[#2] || #1 === f5[#2] &

4

Does this do what you want? {a[t] -> Sin[t], b[t] -> (a[t]*Cos[t])/(2 + a[t]), c[t] -> 1 + b[t]^2 + a[t]*Sin[t], d[t] -> (-b[t])*c[t] + c[t]^2 + b[t]*Cos[t]} /. {(name_[arg_] -> val_) :> With[{name = name}, (name[dummy_] := name[dummy] = With[{arg = dummy}, val])]} After executing this, we can call a[1]; ...

3

I'm sorry i don't have enough reputation to add a comment. One things you may look at: I think you should write x[0] = list1 instead of x[n_] := list1 /; n = 0 Another detail about Condition: When using a Condition /;, you must use a test. In your statement you write n = 0 which is an assignment. Use n == 0 instead.

3

Update If I understand your two-step string prefix concatenation correctly then this should work s1 = Map[ToString, l, {-1}]; prefix = "EGME "; Map[prefix <> # &, s1, {-1}] (* {"EGME 1", {"EGME 2", {{"EGME 3", "EGME 4"}, {"EGME 5", "EGME 6"}}, "EGME 7", "EGME 8"}} *) Map[ToString, l, {-1}] (* {"1", {"2", {{"3", "4"}, {"5", "6"}}, "7", "8"}} *)...

3

Those 2 are Pure Functions with 1 parameter. This is the 3rd in MMA Help you did not show. # + 3 & [x]. To answer your question, I think? (1) Pure functions are anonymous ones, like Scheme. (2) (Why do this?) This means you can use inline and unnamed. (3A) What was special about your above observations? The parameters can be unnamed, too. (3B) That ...

3

I would do it this way: aaronF[n_Integer?Positive, k_, z_] := HypergeometricPFQ[Prepend[ConstantArray[2, n - 1], 1 - k], ConstantArray[1, n - 1], z] For example: aaronF[5, 4, z] 1 - 48 z + 243 z^2 - 256 z^3

3

You may use Sequence and Nothing to build the Integrate variable list with Formal Symbols to prevent naming conflicts with variables outside of your function. With foo[x_, y_, z_] := x + 2 y + 3 z then f[x_, y_, z_] := Integrate[ foo[\[FormalX], \[FormalY], \[FormalZ]], Sequence @@ { If[x != 0, {\[FormalX], 0, x}, Nothing], If[y != 0, {\...

3

the problem was in the comparator operator i.e. the IF statement. I have used the difference and Chop as the fix: If[Chop[ptTri[[1]] - source, 10^-8] == {0., 0., 0.}...] rather than If[ptTri[[1]] == source...] the code below now works. ClearAll@surfaceGrad; With[{epcc = 0.7, epco = 1.}, surfaceGrad = Compile[{{point, _Real, 1}, {opentr, _Real, 3}, {...

2

If your list consisted of a mixture of integers and non-integers, then it might be better to use Replace: l = {1, {2, {{3, 4}, {5, 6.1}}, 7, 8}}; Replace[ l, i_Integer :> "a" <> IntegerString[i], {-1} ] {"a1", {"a2", {{"a3", "a4"}, {"a5", 6.1}}, "a7", "a8"}}

2

The outer function needs to hold also: SetAttributes[{makeRow1, makeRow2}, HoldFirst] test = 1; makeRow1[test, " : "] makeRow2[test, " ~ "] test : 1 test ~ 1

2

Such a downvalue definition is impossible. The way values work is that a symbol serves as a tag to trigger the pattern matching mechanism. You have no tag. Also note that if you could do this, it would have massive, presumably unintended, consequences. Every head with two arguments that came up anywhere within Mathematica's internal calculations would be ...

2

This has to do with how Plot evaluates its arguments and the difference in how the arguments evaluate. Clearly the OP knows there is a difference between a pattern t_ and a literal symbol t. ifun2[t] = ifun[t] ifun3[t_] = ifun[t] The two codes below show the difference in evaluation. On the one hand ifun2[t] is defined only when the argument is literally ...

2

Intro I assume in the following that we have a multiplication operation mytimes which is non-commutative, an addition operation Plus which is abelian and scalar multiplication Times. We will want our operation to be distributive and linear this is the tricky thing in my eyes. Let's code it My favorite Mathematica book is Power Programming with ...

2

In versions 11.1 through 12.0, Curry can be used to access the separate ("curried") arguments: F[X][Y] /. F -> Curry[F1[#2][#] + F2[#2][#] &] (* F1[X][Y] + F2[X][Y] *) For historical reasons, the default form of Curry expects two arguments which it reverses. So we have to write F1[#2][#] etc. We can also explicitly specify the argument order, but ...

1

Define your function using the following (note also the :=): eq[t_, {lambdas__, β_}] := (* rest of your code *) then inside the code change λs in MapIndexed[summand[#, #2[[1]]] &, λs]] to MapIndexed[summand[#, #2[[1]]]&, List[lambdas]]]

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