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.
253
questions
3
votes
1
answer
125
views
How do #1 and #2 work in a function defined by 2 variables?
Can someone please explain the function of #1 and #2 in a pure random function of two variables?
For example, if I had something ...
10
votes
2
answers
201
views
Evaluation leak in Derivative[]
Bug introduced in 4, persisting through 14.1.
I usually only report bugs, but in this case, two reason prompted me to post this:
Given that derivatives are computed internally by various commands, ...
4
votes
2
answers
119
views
How to call a list of pure functions/pure vector function? [closed]
In one dimension, pure functions work as usual:
f = Function[{x}, 2 x]
f[a]
However, with a list of pure functions / a vector pure function this does not work with ...
4
votes
4
answers
237
views
Using MapThread with pure function and variable number of elements
I have a variable size array of lists and would like to set up a MapThread of the lists with a variable pure function $\{\#1,\#2,...,\#n\}\&$ with $n$ being the number of rows in the array and am ...
0
votes
1
answer
84
views
How Can I Use an Element of a Tuple as an Argument for a Pure Function?
(If I should elaborate on the circumstances, tell me.)
I want to do the following: Suppose I have a tuple
tuple={x,4,something,...}
and I want to define a pure ...
0
votes
2
answers
86
views
Same code behaves differently as a pure function and a function, and insider another function
I have a rule-transformer that takes a list of rules (and a list generator = {σ[z], σ[-1], σ[1]})
...
0
votes
2
answers
59
views
How to interpret the solution and how to work with it further?
Please help me understand the solution to the following problem:
...
2
votes
3
answers
174
views
Define equations as a pure function with list as argument
I have these set of equations. The number of equations depends on $n,$ which I want to keep control of. The code that I have generates it using Table and equations generated by this does not take an ...
0
votes
3
answers
115
views
Generalize the code to more variables
I have this code it runs and gives me the solution. How can I make it more compact and If I want it to extend(generalize) it to more variables how can I do it. I had asked a similar question here
(...
1
vote
1
answer
125
views
A way to generalize my code for this non linear ODE problem
I am trying to solve the differential equation $y''(x) = y(x)+\sin[y'(x)]$ using "Fixed Point Iteration" over the interval $[0,1].$ Now using central difference method I arrived at the ...
1
vote
0
answers
74
views
Mapping two pure functions over different lists
Say if I have two lists of vectors of the same length, for example:
u = {{1,0,0},{0,1,0},{0,0,1}}
v = {{1,0,1},{1,1,0},{1,1,1}}
I want to add (modulo 2) ...
1
vote
0
answers
72
views
Why must the "&" symbol be placed inside function instead of outside? [closed]
The following code is taken from Wolfram Community "Quantum Error Correction: bit flip".
...
2
votes
0
answers
35
views
Use FoldList to successively ImageAdd regular polygons with between 3 and 8 sides, and with opacity 0.2 [closed]
This question is from Section 29 of "An Elementary Introduction to the Wolfram Language".
The objective is to successively add regular polygons with between 3 and 8 sides, and with opacity 0....
2
votes
0
answers
79
views
Memory leak with pure function and in the listable option of compiled function?
I'm using
13.0.0 for Microsoft Windows (64-bit) (December 3, 2021)
When I repeatedly run the command
...
5
votes
1
answer
261
views
Pure functions: currying for & notation
I looked up currying in Shifrin's book.
He points out that currying can be used for pure functions using nested functions as in the following example:
...
5
votes
1
answer
129
views
Condition inside pure function
Why condition /; not work inside pure function?
Suppose we have:
...
3
votes
1
answer
77
views
Pure function with pattern test
Is it possible to achieve pattern test inside a pure function?
I have tried
f = Function[{x_?NumericQ},
x
]
but the error Function::flpar occured.
11
votes
6
answers
1k
views
Pure function of lists
I need to create a function with two arguments f(s, l):
s is a list of three integer like:
s={1,-3,+5}
and l is a list of lists of naturals of same length:
...
3
votes
1
answer
192
views
Pure functions failing in algebra
f[equation_] := Module[{},
ContourPlot[equation[x, y] == 1, {x, -2, 2}, {y, -2, 2}]
]
equation = (#1^2 + #2^2) &;
f[equation]
The above code draws a circle....
3
votes
1
answer
82
views
How to define pure functions in matrix form?
I want to define pure functions x(#1,#2) and y(#1,#2) like
But they aren't proper pure functions in the current form. How to modify the code?
8
votes
4
answers
566
views
Pure function on pure function
Given
t1 = {2, 4, 8, 16};
t2 = {1, 5, 9};
First[Select[t1, # > 1 &]]
First[Select[t1, # > 5 &]]
First[Select[t1, # > 9 &]]
can somehow be ...
5
votes
2
answers
209
views
Slots inside nested pure functions
I'd like /@ instead of Table or Map.
But let we have it inside pure function:
...
3
votes
3
answers
84
views
Sort a list using a scoring list with Switch [duplicate]
I have a minimal working example (MWE) list called listToSort that is a list of string triplets:
...
0
votes
1
answer
57
views
Sorting a list by increasing order algorythm [closed]
Im new to loops on mathematica,was wondering how could I create a function to loop through a list which finds the index of the smallest value of that list, makes a sublist from index 1 to that ...
1
vote
1
answer
124
views
Drawing lines between coordinates in a list
Variable "l" is a list of coordinates and I am trying to draw a line from each coordinate to the next. But how? Apparently the last line isn't working.
...
3
votes
1
answer
103
views
MapAt with pure functions
Consider
expr = Hold[1 + a + x + b + c + y + z];
idxes = {{1, 3}, {1, 4}};
Q1. I don't understand why
...
5
votes
3
answers
593
views
Can a pure function be constructed whose argument list is a matrix?
Can a pure function be constructed whose argument list is a matrix?
Using Function, I know it's not possible, but I'd like to get something like:
...
5
votes
6
answers
1k
views
How to simplify a pure function?
I am dealing with the vibration of a multi degree of freedom system with driving force, and I need to find the relation between the amplitude and the frequency of the driving force.
I hope to get an ...
1
vote
2
answers
110
views
Nested pure function argument as table
I made a nested pure function
Nest[(x^2)/ ((2x+1) + #)&, x,n]
I would like that for each iteration the value of x increment of 1, to have something like
$$\...
3
votes
1
answer
161
views
How to replace a multiple iterator Table to speed up computation
I need to create an array of data which resembles to something like this
Table[{i, Sin[j^2*i]}, {j, 2000}, {i, 0., Pi, Pi/10000.}]
where each "row" of ...
0
votes
0
answers
21
views
How to select from a sum the terms containing minus? [duplicate]
In a sum
y = \[Beta] x v - \[Mu] y z - \[Gamma] y;
I would like to extract the terms which contain a minus, i.e. - [Mu] y z and - [Gamma] y
I can test when a term ...
2
votes
1
answer
149
views
Minimize Sum of Hamming Distances
Problem:
Given an $n \times m$ matrix with binary values $(0,1)$, find a binary vector that minimizes the sum of Hamming Distances to the rows.
Example:
If the matrix is {{$1,1,1$},{$0,0,0$},{$1,0,1$}}...
28
votes
1
answer
944
views
When is ##@@@##& more appropriate than ##@@##&?
When assigning attachment operators, I often run into confusing statements like ##@@##&, ##@@@##&, or even ...
9
votes
4
answers
475
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 ...
0
votes
2
answers
130
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 ...
5
votes
1
answer
69
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]
...
2
votes
2
answers
153
views
Evaluating a FittedModel over a list of values
Simplified version
Using the simpler version suggested by @ulrich-neumann :
...
3
votes
1
answer
118
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?
...
7
votes
5
answers
514
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:
...
0
votes
1
answer
52
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.
...
4
votes
1
answer
103
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:...
8
votes
1
answer
316
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.:
...
0
votes
0
answers
47
views
Why does this function only work with exact numbers?
First, I tried calculating A^k with this and got an error.
...
0
votes
3
answers
147
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.
0
votes
1
answer
64
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:
...
5
votes
2
answers
180
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 ...
1
vote
1
answer
116
views
pure function assignment
I do not understand this statement from the Introduction to Dynamic tutorial:
...
1
vote
1
answer
79
views
Apply function using level
The following use of Level is documented, without any example.
Let's try some simple codes :
...
0
votes
0
answers
186
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 ...
5
votes
1
answer
155
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, ...