Questions tagged [pure-function]

Questions about implementing or applying functions which: (1) always yield the same result for the same input, (2) are independent of outside information, and (3) produce no side effects.

Filter by
Sorted by
Tagged with
27 votes
1 answer
900 views

When is ##@@@##& more appropriate than ##@@##&?

When assigning attachment operators, I often run into confusing statements like ##@@##&, ##@@@##&, or even ...
user avatar
  • 35.6k
9 votes
3 answers
359 views

Construct a pure function containing built-in 'Sequence'

I guessed the output for (Sequence[#, 7]&)[3] should be Sequence[3,7] But my guess was wrong. See the screenshot: And I ...
user avatar
  • 2,451
0 votes
1 answer
95 views

Spurious renaming in nested Function construct

From the documentation of Function, I take this example (slightly simplified). Renaming the dummy or bound variable (y->y$ in the inner ...
user avatar
4 votes
1 answer
52 views

Splitting a list into sublists at provided markers

For a list of strings (or other elements): SeedRandom[3]; t1 = Table[RandomChoice[CharacterRange["A", "E"]], 20] ...
user avatar
  • 14.8k
2 votes
2 answers
91 views

Evaluating a FittedModel over a list of values

Simplified version Using the simpler version suggested by @ulrich-neumann : ...
user avatar
3 votes
1 answer
116 views

Why do these two calls to Apply return different results?

In the following example, why do these two calls to Apply return different results? ...
user avatar
  • 9,415
7 votes
5 answers
412 views

An anonymous function with constraints [duplicate]

I'm not very good at working with anonymous functions, a fact that has now come back to bite me. Suppose I have a function defined as follows: ...
user avatar
  • 347
0 votes
1 answer
42 views

MeshFunctions::invmeshf that resists other solutions

I'm trying to base mesh on the slope of the function. That's why there is a Cross of two partial derivatives. ...
user avatar
4 votes
1 answer
81 views

Apply function modification to curried function

This is based on A function that modifies functions - well-defined operation?, a previous question of mine. Lukas Lang's answer of course works very well, but now I encountered the following edge case:...
user avatar
  • 175
8 votes
1 answer
290 views

How to supply argument to pure function when slot is already defined?

Here's an example of a pure function: 3^# > 100 & I can supply any value I please to the function, by placing that value after the prefix. E.g.: ...
user avatar
  • 3,446
0 votes
0 answers
40 views

Why does this function only work with exact numbers?

First, I tried calculating A^k with this and got an error. ...
user avatar
  • 2,147
0 votes
3 answers
121 views

How to use a pure function to write following sum?

How to use a pure function to write following sum? I have tried to use FoldList, but it is still not working.
user avatar
  • 11
0 votes
1 answer
36 views

Maximize returns unevaluated even though there is a global maximum

I was trying to calculate the maximum of a gaussian plus a line, but Maximize return unevaluated: ...
user avatar
5 votes
2 answers
136 views

bind specific 'Slots' to specific 'Map'

Is it possible to construct a pure function using multiple slots, with the following concept :       Binding   specific slot    to    specific apply or map For example : In this example, To ...
user avatar
  • 2,451
1 vote
1 answer
100 views

pure function assignment

I do not understand this statement from the Introduction to Dynamic tutorial: ...
user avatar
  • 11
1 vote
1 answer
43 views

Apply function using level

The following use of Level is documented, without any example. Let's try some simple codes : ...
user avatar
  • 2,451
0 votes
0 answers
52 views

Numerical errors in compiled function involving real and complex numbers as output

I am trying to use the Compile command in Mathematica to reduce the computation time because of its fast execution within Mathematica and ability to use the ...
user avatar
  • 13
5 votes
1 answer
136 views

Reduce from two pure functions to one

Liking the headings and spacing allowed by Row[] and TableForm[] I set up the below with 3 TableForm[]'s to use as a model from which to generalize, using a pure function, to display a list of 3-ples, ...
user avatar
  • 614
1 vote
0 answers
63 views

Shorten Pure Function

My task is to write a pure function with 5 characters or less that can be called with a single number a or a list {a,b,c} and returns 1/a^3 or {1/a^3,1/b^3, 1/c^3,...}, depending on the input. I am ...
user avatar
0 votes
0 answers
25 views

Pure functions with derivatives and list elements [duplicate]

I have a list list = {x, y, z} and then the following line D[#, list[[2]]] & gives the output ...
user avatar
6 votes
1 answer
139 views

Using a pure function as an option of a function

Here's my code: Options[f] = {"Func" -> Exp}; f[x_, OptionsPattern[]] := OptionValue["Func"][x] f[2, "Func" -> (# + 2) &] ...
user avatar
4 votes
0 answers
44 views

`Function` does something bizarre with formal parameters

I think this may overlap with existing questions, including one I asked years ago, but I'm not sure how, because the behavior I'm seeing is confusing enough I can't even really describe it except to ...
user avatar
  • 17.8k
4 votes
3 answers
173 views

Function incorrectly renaming variables in Rule Pattern

Consider the simple pure Function containing a Rule with the named pattern q. ...
user avatar
0 votes
1 answer
60 views

How to write a function returning Chebyshev Polynominals of the first kind using Nest[]? [duplicate]

I want to create a function returning the Chebyshev polynomials of the first kind using the in build Nest[] function, preferably with a pure function. I know there exist an in-build function for the ...
user avatar
0 votes
1 answer
111 views

Pattern matching tuples as inputs to pure function

I've been having an issue with functions which are supposed to receive as input a list of tuples. My troubles are two-fold: The function doesn't properly identify the pattern test I laid out in the ...
user avatar
  • 1
0 votes
1 answer
46 views

Dot product two lists containing pure functions

Can i get something like this to work? {D[#1, t] & , D[#1, x] & } . {t, y} and my expected output be ...
user avatar
  • 556
1 vote
2 answers
109 views

How to flip the function f(x) about y-axis

Suppose I have a function W(x). I wanted to flip (reflect) this function about the y-axis. I tried plotting W(-x) but it is not ...
user avatar
  • 1,639
8 votes
0 answers
184 views

Performance of pure function - Best way to define a function?

In general, to define a function, we often use f[x_]. Another way is to use pure function (full form and shorthand). I want to test the performance of these ...
user avatar
  • 1,609
1 vote
1 answer
75 views

Array function and slot

I am trying to make an Array depending on a function, and I saw that Slotis useful for my situation. There are examples described in https://reference.wolfram.com/...
user avatar
  • 43
1 vote
0 answers
44 views

Why are nested anonymous pure functions shielded from evaluation?

I tried the following code (ignoring the warning messages): ...
user avatar
4 votes
2 answers
112 views

Using pure functions to assign a value to variable depending on whether it has been defined already

Let's say I want to assign to a variable the value $2020$ if it has not already been defined and do nothing if it has. In short, for a variable $a$, I want to achieve the functionality of ...
user avatar
  • 471
0 votes
2 answers
142 views

How to constrain parameters of a pure function?

Consider the following definition of splitList: ...
user avatar
  • 6,902
5 votes
2 answers
127 views

How to apply the multiple composite functions to a list?

A minimum example is this: ls = {1 + I, 2 - I}; Max@Re[ls] Min@Im[ls] #[ls] & /@ {Max@Re, Min@Im} (*want {2, -1}*) The result is: ...
user avatar
0 votes
1 answer
46 views

Plot any parametrization (pure function, numbered variables)

I'd like to write a function plot[reg_,f_] that takes as input any product of intervals (list of pairs of reals) reg$\subseteq\!\...
user avatar
  • 1,095
7 votes
0 answers
185 views

Can Wolfram Language enforce pure functions?

Is there a feature in Wolfram Language that supports or enforces pure functions? WL has something that it calls "pure functions" in its documentation, but these are most emphatically not pure ...
user avatar
  • 12.7k
1 vote
0 answers
75 views

Slot numbers, pure functions [closed]

For the first line of code my function works fine on the list, but when trying to use Map I just keep getting an error (Function::slotn) basically saying there are not enough arguments for it to work, ...
user avatar
  • 199
5 votes
4 answers
459 views

How to Force Evaluation of Derivative in a Pure Function Definition

Let's say I have a function f1 = #^2 &; I want to calculate its derivative, perform some manipulations and make the result a pure function. I try ...
user avatar
  • 338
0 votes
0 answers
32 views

Behavior of ## changes inside Position function?

I don't understand the behavior of this following simple(?) example: ##&/@ {{0,1},{1,2}} Position[%,{1,2}] gives {{2}} ...
user avatar
  • 207
3 votes
1 answer
106 views

Soft question: Is it feasible to have argument names with the shorthand (&) pure function syntax?

Nested pure functions can be messy, code length wise and syntax wise. It seems that the ampersand (&) in pure functions is always either proceeded by nothing: ...
user avatar
12 votes
2 answers
216 views

The use of SlotSequence in If[#1 > #2, ##] &

The below function when applied to a list of two elements, will output the maximum between the two: If[#1 > #2, ##] & For example: ...
user avatar
6 votes
1 answer
104 views

How to use a function as a value in an association? [closed]

I just started learning Mathematica. I was wondering if it is possible to use a function as an value in an association. Ideally, it would be something like this: ...
user avatar
  • 181
2 votes
2 answers
510 views

How to convert list of variables x1, x2, x3 ,x4 ... x10 to list of x1_,x2_,x3_..x10_?

I'm writing a function where I need to give many inputs. I created a list of variables from Table function. My aim is to get ...
user avatar
1 vote
0 answers
97 views

Is there a good reason why `Slot[]` is not an atomic expression?

Here's a good reason why I think it should be, Map[ If[AtomQ@#, #->"is an atom", #->"not an atom"]& , {"hello",Range@3, # &} , {-1} ] ...
user avatar
  • 1,970
5 votes
2 answers
380 views

How to condition arguments of a pure function?

I would like to exclude the point {x=0,y=0} in the function definition ...
user avatar
6 votes
2 answers
439 views

Pure functions instead of For loop

How to get following loop output using pure functions such as NestList? For[i = 0; v = 0, i < 4 && v < 4, i++; v++, Print[x^i + b^v]] and the ...
user avatar
2 votes
1 answer
77 views

Construction of symmetric function / matching function argument patterns

[Edited for clarification after comment] Suppose that the function f is defined as the linear combination of several translated/reflected functions starting from the primitive f0[x,y,z] ...
user avatar
1 vote
1 answer
235 views

Pure functions vs normal functions [duplicate]

I am trying to understand the true difference/usage of a pure function vs a "normal" function. Besides the argument that a pure function is particularly handy when it comes to a single usage, are ...
user avatar
  • 743
7 votes
2 answers
498 views

Replacement Rule for Functions

Attempting to write a rule that will substitute an undetermined function of several variables by some other undeterminate functions (also, of the same several variables). I can do it in an inelegant ...
user avatar
11 votes
1 answer
789 views

What's the meaning of #0? [closed]

Here's a line of code from a handbook written by Stephen Wolfram, which turns out to be very complicated for me. ...
user avatar
  • 113
3 votes
1 answer
56 views

Applying pure function to rotate the label in a BoxWhiskerChart

Data is available from here https://www.ocr.org.uk/Images/400305-large-data-set-lds_1-h630-2018-h640-2018-2019-pre-release-data-mei.xlsx ...
user avatar
  • 1,252

1
2 3 4 5