# Tag Info

19

As far as I know there is no way to do this with the named parameter form of Function but you can use destructuring methods with SlotSequence (##): f = {##} /. {u_: 1, v_: 0} :> body[u, v] &; f[] f[7] f[7, 8] body[1, 0] body[7, 0] body[7, 8] It is possible to give your pure function Attributes using an undocumented form. For Hold attributes ...

17

You can always string several anonymous functions together, but you'll also have to pay attention to operator precedence. In this case, you had to enclose the anonymous function in parentheses. Replace the corresponding line in your second example with the following and it works. ChartLabels -> (DateString[#, {"ShortDay", "/", "ShortMonth"}] & @@@ ...

15

You can always use Function to create anonymous functions: Function[{a},a^2] is equivalent to #^2& and can be used as such, but it is unambiguous. It can be used as: Function[{a},a^2][2] (* ==> 4 *)

13

Yes, this form exists, and was first shown to me by Leonid. It is: Function[Null, (* body with ## *), (* attributes *)] As always the Null may be implicit, so in your application: Function[, Length[Unevaluated@#1]{##2}, HoldFirst][1+2,2+3,3+1] {10, 8}

11

What rm-rf suggested is this: #[[2]]/#[[1]] & /@ (#[[2]] - #[[1]] & /@ Partition[data, 2, 1]) Or some version of it (see g3kk0's answer). This can also be written using Differences: #[[2]]/#[[1]] & /@ Differences[data] If the slope is calculated using the standard $\frac{\Delta y}{\Delta x}$ formula. But we don't have to use an anonymous ...

10

Two new methods and a comparison of performance. Conceptually I like my second one, but it's slow; similarly I like Anon's second Ratios one, but it's not as fast as the pure function version. The first solution, Anon's fourth, and g3kko's edit are worth looking at if you want to take advantage of Mathematica's efficiencies with vectorized functions and ...

9

Edit: Mr.Wizard helped to refine my old function to: SetAttributes[Through2, HoldFirst] Through2[head_[args___]] := Replace[head, s : _Function | _Symbol :> s[args], -1] This locates the most nested functions and symbols and evaluates their value for the parameter arguments. Below is my older, less robust function: SetAttributes[Through2, HoldFirst] ...

9

Here is one possibility to compute the slope between each pair of adjacent points. I create a list of random points first and use the Sort function sort them by their x-coordinate (First): list = SortBy[RandomReal[{0, 10}, {20, 2}], First] {{0.0612793, 5.82737}, {0.171386, 6.8975}, {0.704354, 8.53224}, {0.798152, 6.39703}, {0.967127, 8.35358}, ...

9

Let's read what the docs say: Root[{$f_1, f_2 ,\ldots$}, {$k_1, k_2 ,\ldots$}] represents the last coordinate of the exact vector {$a_1, a_2, \ldots$} such that $a_i$ is the $k_i$th root of the polynomial equation $f_i(a_1, \ldots, a_{i-1}, x)=0$. This, though a bit confusing, is a pretty accurate and straightforward description. Let's assume two ...

8

FWIW, I disagree with the answers which state that Function with named arguments and Function expressed using slots (#) are the same thing. Please see the first part of this answer of mine for a partial list of differences. The main difference I want to stress here is that Function-s with named arguments are true (albeit leaky) lexical scoping constructs, ...

8

For the first puzzle, I can only guess. The idea is that Function with named variables is a true lexical scoping construct, in that it cares about the possible name collisions inside the inner scoping constructs, including another Function-s (this is where it is different from Slot- based functions, which are not like that. The price to pay is that ...

8

Why is this happening? How can I speed up my routines without having to put the function explicitly inside Compile? It is happening because Compile has the attribute HoldAll Attributes[Compile] (* {HoldAll, Protected} *) This means, that no evaluation of the arguments will happen. In your case the arguments to your Compile call are {{list,_Real,1}} ...

7

In can be done in a terse way with nested pure functions: lists = RandomReal[{0, 10}, {3, 10}] {{3.35338, 2.82572, 0.152277, 1.19036, 9.88211, 6.55398, 8.11855, 0.793288, 9.04547, 6.42518}, {4.95417, 7.73982, 5.58323, 3.09912, 5.44546, 8.88474, 2.67437, 8.20605, 4.55918, 1.95303}, {2.53793, 6.67839, 8.71033, 8.4877, 0.634367, 7.99796, ...

6

Mapping First over your list will strip off the Function head. Then multiply them together with Apply[Times... and finally Apply[Function... makes the result a pure function. Use the final Apply so the argument to Function is evaluated first as Function has the attribute HoldAll. Apply[Function,{Apply[Times, Map[First,l]]}] Gives... (-1+#1) (#1+#1^2) ...

6

You can use the form Function[{x,y,z}, body] to define a pure function with formal parameters x,y,z ( see Documentation >> Function) Let lists = {ln125, ln126, ln127} = RandomReal[{0, 10}, {3, 10}] (* {{1.72286, 5.24912, 8.87257, 5.77593, 6.31276, 1.77914, 2.06393, 0.328725, 9.46436, 4.96257}, {1.71171, 2.54337, 5.93807, 9.46774, 6.99601, ...

6

I seem to recall an earlier question addressing this specific aspect (is there a ## equivalent for named parameters) but I cannot find it. Nevertheless... I echo Leonid's answer that there are important differences between pure functions using Slot and/or SlotSequence and named parameters. A primary one is the automatic renaming that occurs with the ...

5

How about this: g[n_]:=Function @@ {Slot /@ Join[#, Reverse[#]]&@Range[n]} For a random order: gr[n_] := Function @@ {Slot/@Array[RandomInteger[{1, n}] &, 2 n]} All in all you can replace Range[n] with your own order of integers depending what you want to accomplice.

5

But it doesn't work. Why is that? It becomes visible when you inspect the inner Map only. I replace the slot for the outer function with 1, because we don't need it to see the error b = {1, 2}; c = {1, 2, 3}; Map[f[# &, 1], b] (* {f[#1 &, 1][1], f[#1 &, 1][2]} *) This is not what you expect and when you look a bit closer, you instantly ...

5

I have left bottom part of this answer as a warning against not thinking :) Here is my improvement. It is not so handy but it allows us to specify which argument has to take it's optional value. edit - scoping. f = Module[{x, y, z}, Function[{u, v, g}, x^2 y^4 + z^4 /. {x -> (u /. Null -> 3), y -> (v ...

5

This is malformed code with some strange effects. In the first evaluation y is substituted for var1 in var1 := #1 therefore after the first evaluation the global Symbol y has the assigned value #1 (Slot[1]). When the expression is evaluated a second time this value is substituted into the inner function var1 + h*funk & by the main definition for diff ...

4

You seem to be on the right track but having some trouble with the anonomous function syntax. I highly recommend you read up on this as it's a very useful concept. Essentially #1+#2& is shorthand that translates into Function[{arg1,arg2},arg1+arg2]. So a good place to start is the documentation for Function. Now to the problem at hand. I understand it ...

4

Here is another option: l = {(#1 - 1) &, (#1^2 + #1) &, (#1^3 - 1) &}; Thread[Times @@ l, Function] (#1 - 1) (#1^3 - 1) (#1^2 + #1) & This has the benefit of not evaluating the body of the functions. For example: l = {(#1 - 1) &, (#1^2 + #1) &, (Print["!"]; #1^3 - 1) &}; Thread[Times @@ l, Function] (#1 - 1) ...

4

Perhaps you want something like this? apply = (# /. s_Symbol /; Context[s] =!= "System" :> s[##2]) &; apply[f*g + h, x] f[x] g[x] + h[x] This is a limited implementation but it can be extended if this is in fact the kind of operation you desire. The idea is to recognize any Symbol not belonging to the System context as a function to apply ...

4

First consider : Root[327680000000000000 - 1280000000 #1^8 + #1^16 &, 7] // ToRadicals // FullSimplify (-2 - 2 I) 5^(3/4) (10 - 2 Sqrt[5])^(1/8) now substitute #1 with this number in #1 + 25 #2 + 25 #2^5 & switching #2 to #1 and compare with your original Root object : Root[(-2 - 2 I) 5^(3/4) (10 - 2 Sqrt[5])^(1/8) + 25 #1 + 25 #1^5 &, ...

4

Witness the following: With[{n = 7}, Function[Evaluate[ArrayPad[Slot /@ Range[n], {0, n - 1}, "Reflected"]]]] {#1, #2, #3, #4, #5, #6, #7, #6, #5, #4, #3, #2, #1} & With[{n = 7}, Function[Evaluate[ArrayPad[Slot /@ Range[n], {0, n}, "Reversed"]]]] {#1, #2, #3, #4, #5, #6, #7, #7, #6, #5, #4, #3, #2, #1} &

4

You've almost got it: list1 = {{"a", 1}, {"b", 2}, {"c", 3}}; list2 = {"A", "b"}; test = StringMatchQ[#, Alternatives @@ list2, IgnoreCase -> True] &; Cases[list1, {_?test, _}] {{"a", 1}, {"b", 2}} A key element is clearly Alternatives. I used Cases rather than DeleteCases as that seemed simpler to me. The first part of my post is in ...

3

First, to evaluate the functions embedded in calcShapeFunctions[3], you could do the following: calcShapeFunctions[3] /. f_Function :> f[x, y] {1/2 - x/2, x/2 - y/2, 1/2 + y/2} But to find the derivatives, you need to alter how calcShapeFunctions function is defined so that the bodies of your Functions are evaluated and the part of the list of ...

2

There's a number of reasons your code doesn't work (all related to attributes; basically, Function has attribute HoldAll). I think this does what you want: MapThread[ Rule, Transpose@Table[ {f[n], With[{n = n}, c[[n]][#1] #2^n &]}, {n, 1, order} ] ] How does this work? With[{n=n},body] is what fixes the problem, as it literally inserts n ...

2

I'm not really sure about what are you trying to do with those rules, but the following works: c = Table[#/n! &, {n, 0, 10}]; f[order_] := ((Sum[c[[n + 1]][#1] #2^n, {n, 0, order}]) &) Plot3D[{f[5][x, y], f[10][x, y]}, {x, -10, 10}, {y, -10, 10}, PlotStyle -> {{Opacity[0.6], Red}, {Opacity[0.6], Blue}}] Please remember to start your ...

2

To clarify one of the answers. Note there is no way to distinguish both # inside # == Nearest[#, 551.748][[1]] in Select[#, # == Nearest[#, 551.748][[1]] &] & as you already noted. So, you need to give the outermost # a name and Function[...] is just for that. This one works: Function[{l}, Select[l, # == First[Nearest[l, 551.748]] &]] ...

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