The Stack Overflow podcast is back! Listen to an interview with our new CEO.

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
4
votes
3answers
425 views

Subtracting elements in a nested list

I have a list containing 500 rows, each of 37 elements; i.e., a 500 x 37 matrix. My goal is to substract in each row each element from the first. My approach in mind is to call each list by a ...
14
votes
3answers
541 views

Combining # & pure functions with post-fix notation

I can't find the link anymore but there was a talk at a Wolfram conference (referencing v7 I think) where the authors highlight that stringing together functions like this: ...
1
vote
2answers
134 views

Change the coordinates from $(x,y)$ to $(\mu,\theta)$?

Suppose I have the following definition: $$ x=\cosh \mu\cos\theta, \quad y=\sinh\mu\sin\theta $$ I want to change the coordinates $(x,y)$ to $(\mu,\theta)$, suppose $x\in(-1,1), y\in(-1,1)$. Here ...
2
votes
0answers
34 views

Bug with LinearRecurrence and Slot/SlotSequence?

The code below runs without error: LinearRecurrence[{##}, {1}, 5] Each term is the previous term squared times the index of the previous term. If the ...
0
votes
1answer
36 views

How to deal with common arguments for several helper functions in a module?

Suppose a module exports functions like Calculate1[...,T], and Calculate2[...,T] which have several arguments in addition to <...
5
votes
1answer
661 views

Writing lambda expressions in terms of pure functions

I am trying to create some functions that I want to comfortably compose together using the pure functions. The functions were originally written in $\lambda$-expressions so I would like to keep the ...
3
votes
0answers
171 views

Making pure functions listable [closed]

I have a very large number of functions defined in the following manner: f[1] = Function[{x}, x^2]; f[2] = Function[{x}, x^3]; I would like to make all of these ...
4
votes
2answers
136 views

Defining pure function using module

I am creating a function fun which takes a parameter p and returns a pure function: ...
11
votes
2answers
474 views

Combining pure functions

Is there an easy way to combine pure functions into a single pure function? For example, say I have f = #1^2 &; g = #1 - 2 &; and I want to define a new ...
2
votes
1answer
117 views

Vector of Pure Functions

I'm trying to make a function that takes in a vector of pure functions (representing a parametric equation) and returns another vector of pure functions representing an offset curve. Here's what I ...
3
votes
1answer
149 views

Replace all the elements in a list at a given level with the level number

How can I change the function replace in a pure function way? replace[list_List, n_] := (g[a_] := n; Map[g, list, {n}]); I ...
2
votes
3answers
134 views

How to pre-evaluate parts of a lambda or a delayed-assigned function?

I have an expression which I'd like to evaluate and use as a subexpression of a delayed-assigned expression. But I need it to be evaluated before actual assignment, while other parts of the full ...
2
votes
1answer
82 views

function on parts of lists works only in pure function?

For example, I'd like to take the first four upper left values of a matrix, i.e. f[x_] := f[x] = x[[;; 2, ;; 2]]; This works nicely, e.g. ...
8
votes
2answers
545 views

Creating a pure function that does operations with pure functions

Suppose I have two functions defined as follows: a = #+3& b = #^2 + 3& So when I call a[2] and ...
1
vote
0answers
87 views

Time-stepping an equation in Mathematica

I have an iterative integral equation of the form $$n(k,t+dt)=n(k,0)+dt\int_{0}^{\infty}dp\int_{0}^{\pi}d\alpha\int_{0}^{2\pi}d\beta\int_{0}^{\pi}da\,p^{2}q^{2}\sin(\alpha)\sin(a)\frac{(n(p,t)+1)}{\...
2
votes
3answers
229 views

How to convert numbers to currency values?

I am trying to create a way to convert some values of numbers into currency (dollars). The example I display below takes in three random numbers between -4 and 4, and rounds them to the second decimal ...
1
vote
2answers
133 views

How to produce this list of pure function pairs

I need to produce this ...
14
votes
1answer
114 views

Bug when combining Dataset with a scoping construct and Map

The following fails in v11: ...
14
votes
2answers
238 views

Is anonymous pure function a scoping construct?

In recent thread was raised the question: why anonymous pure functions Function[body] (or body &) do not rename symbols in ...
11
votes
4answers
533 views

Using predefined expressions as free variables in a pure function [duplicate]

I want to have a function value of an expression where some variables are solutions to some set of equations, with some values of parameters. I had an idea to use pure functions for that. However, ...
8
votes
2answers
325 views

Unexpected variable renaming depending on form of a pure function

I encountered unexpected difference in variable renaming behavior depending on whether a pure function with a named parameter, or a pure function with a slot is used: ...
8
votes
1answer
808 views

Pure functions are not functions?

According to the manual: "HoldAll is an attribute which specifies that all arguments to a function are to be maintained in an unevaluated form." The following code yields, as expected, ...
0
votes
0answers
37 views

How to use OptionValue inside a pure function [duplicate]

I want to define a function with OptionsPattern as argument which returns a pure function using OptionValue: ...
1
vote
1answer
176 views

Optimization of the solution to an ODE

Apologies if this is obvious -- I'm very new to Mathematica. I'm trying to minimize the solution to an ODE with respect to a variable. The following code generates the solution to the ODE, ...
1
vote
1answer
154 views

Evaluate pure function through round brackets

In have some matrices of pure functions or numbers which I multiply by other matrices or vectors. I would like the functions to be evaluated in the result, but all that I get is something like this: <...
1
vote
2answers
502 views

Solving a pure function to obtain function definition and then map that function

I've been trying to use Solve with pure functions to receive function definition and then use it with Map , to obtain results from mapping function to some data. Here is an example ...
4
votes
2answers
194 views

Automatic generation of pure function

My main problem consist in the creation of a sort function which wolud be used inside SortBy. Introduction The principle of the sort to apply is the following (...
15
votes
4answers
2k views

Can you plot pure function without specifying variable?

Given a pure function, e.g. f=Sin[#]&, it is possible to plot it with introducing an arbitrary local variable, e.g., ...
1
vote
0answers
88 views

Matching All Functions [duplicate]

Is there any way to match all functions? There's an existing answer for matching functions of one parameter (_@_) but it doesn't work for trying to test if a symbol ...
2
votes
1answer
351 views

How to force evaluation inside the body of a pure function?

For example, I'm writing a function to scale my list of numbers into a specific range. It goes like ...
6
votes
1answer
161 views

Select with test function that depends on # [duplicate]

I'm trying to teach myself to use pure functions and slots as much as possible. Here is my problem. I have a list: ...
1
vote
4answers
349 views

How to use Select according to a list of criteria to produce a list of subsets of another list obeying those criteria [duplicate]

The code Print[Select[{1, 4, 3, 9}, Mod[#, 3] == 0 &]]; Print[Select[{1, 4, 3, 9}, Mod[#, 3] == 1 &]]; Print[Select[{1, 4, 3, 9}, Mod[#, 3] == 2 &]] ...
1
vote
0answers
253 views

Variable naming conventions for nested pure functions [closed]

My understanding is that for nesting pure functions, one should use the explicit version of pure functions. I would like to know the best way of making it readable for people. I currently have ...
4
votes
1answer
125 views

Creating a list of unevaluated pure functions with Apply

Suppose I have a list list with length L and a function ...
0
votes
1answer
58 views

Deciphering Coding Shortcuts [duplicate]

I'm having trouble deciphering why the following couple of expressions evaluate the way they do. #&@@ This returns the first value of a list. Why? Naïvely I ...
2
votes
1answer
133 views

How to write this code as a pure function? [duplicate]

The code has three places with same variable ls. So I think it would be possible to write it as a pure function. ...
1
vote
0answers
37 views

Why is `g[#1, #2] &` different from `g[##] &` for a function with two variables in this example? [duplicate]

If we only consider functions with two variables, I expect g[#1, #2] & is the same as g[##] &. But in the following ...
1
vote
2answers
90 views

Translating syntax of a pure function

I have a matrix in complex exponential form. I want to factor out the complex exponential form and then express the inside in the a+i*b form. For example: \begin{bmatrix} e^{i \frac{\pi}{4}} ...
1
vote
0answers
87 views

How to get the values out of Alternatitves? [closed]

Saying we have something like: SomeFunction[ ... { Alternatives[1, 2, 3] :> "something" } ... ] How can I know if ...
8
votes
3answers
477 views

How can I create currying functions using pure function syntax?

For example how can I write In: pureFunctionSyntax[myF] /@ {7, 3} Out: {myF[7, Log[7]], myF[3, Log[3]]}
5
votes
1answer
511 views

Why does #0[[1]] &[] output 0?

When a pure function accesses the first element of itself, 0 is output. What is the logic behind this, or is there any?
3
votes
3answers
85 views

How to use pattern matching to apply a function to specific elements of a matrix

I have a 3x3 matrix testweightedgraph = {{{}, {p, q, pq, pq}, {}}, {{p, q, pq, pq}, {q}, {}}, {{}, {}, {}}}; whose elements are (possibly empty) lists. I would ...
1
vote
1answer
146 views

Replace variable with # and pass function body as an argument to a function

I have a very simple problem, which I would like to make a bit general, but none of my naive approaches have worked yet. This is what I want to do step by step: Define a parameter: ...
26
votes
1answer
1k views

Is it possible to reference “self” from a pure function?

This would calculate the factorial of 6, if the "self" was the function itself. Is there something that could allow this? ...
1
vote
2answers
1k views

“Nonatomic expression” error when identifying certain positions in a list [duplicate]

Please can anyone help ? I have a list of sets, and I am trying to find the positions in that list of those sets which are subsets of a given set. (Here I am ignoring the distinction between lists and ...
0
votes
1answer
177 views

Working with functionals [closed]

I am trying to work with functionals in Mathematica, this means that I want to define a function of a function. I defined my function of a function as: ...
3
votes
4answers
1k views

Cases vs Select, Pattern vs Pure function [duplicate]

What is the difference between having Cases with selection condition presented by pattern and a with pure function as condition? ...
4
votes
1answer
243 views

Substitute a function's definition for its name

I'm having trouble with a function which is supposed to work on another function. Simplified version: ...
1
vote
1answer
315 views

Pure “Select” function over matrix

I have several lists of tuples in the following shape: l1 = {{1, 134}, {2, 314}, {3, 164}} I do want to select, from each list, list elements fulfilling a ...
4
votes
5answers
212 views

Using Through with SlotSequence

I have an expression consisting of a few pure functions added together like so: f+g+h I want to add the bodies of these functions together and make that a pure ...