Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

7
votes
2answers
122 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 ...
11
votes
1answer
711 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. ...
2
votes
1answer
21 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 ...
1
vote
0answers
29 views

Issues with Manipulate (or variable assignment)

I am working to take a large manipulator I have built and generalize it, but I keep running into some variable assignment issue. I made a short example of my issues below: Set up: ...
0
votes
0answers
34 views

List of function - basis

I would like to define list of function (make polynomial basis as a set of the functions), and then evaluate value of first/ second function in the set. I have made set of the fuctions that I have ...
0
votes
0answers
15 views

Evaluating part(s) of a function which outputs another function [duplicate]

I would like to define a function f(n) which outputs another function as follows: ...
0
votes
0answers
53 views

Commutator of differential operators

Let $P_x = \frac{\hbar}{i}\frac{d}{dx}$, after specifying the commutator relation symbolically $[X, P_x] = i\hbar$, I can ask Mathematica to calculate commutator algebra. My question: is there a way ...
3
votes
2answers
220 views

Outputting expressions in the order in which they're entered

Background For display purposes, I sometimes find it desirable to defeat Mathematica's canonical ordering of variables, and instead have it output terms in the order in which I type them. E.g., ...
3
votes
3answers
76 views

Looking for a better way use multiple pure functions to condense repetitive code

I have a very large 3 dimensional dataset (named pcapdata) that I have broken down for data analysis. There are 7 smaller datasets contained in the main set. My ...
5
votes
1answer
175 views

Pure function as a variable of another function

In a notebook, I have a function that basically needs to do something like calling a pure function within a function. I would say that what best describes would be an example like: ...
4
votes
1answer
46 views

Combination of Scan[] and MapIndexed[]. Using an expression with slots corresponding to different functions [duplicate]

If I want to use slots corresponding to different function, how can I dod that. For example: Scan[MapIndexed[(#1+#2)*#3&,{"a","b","c"}],Range[5]] where I ...
1
vote
2answers
79 views

Reproducing Nest-function with recursive nesting does not work

In general, when I am learning a new programming language, it helps me to understand how builtin functions works. I am trying to reproduce the builtin Nest[f,exp,n] ...
1
vote
0answers
40 views

Generate a list of variable names [duplicate]

How can I generate a list of variable-names like {p0, p1, p2, p3} etc... I tried: mylist = p # &@Range[1, 4] But then the result is: {p, 2 p, 3 p, 4 p}
3
votes
0answers
102 views

How to overload and compile pure functions operating on associations

I am writing a small package for myself for operating on zonotopes. I am storing the generator matrix and the center of the zonotope in an association. I decided to write a small constructor: ...
5
votes
3answers
219 views

pure Function[ ] named formal parameters

I am trying to setup a bunch of pure Functions in particular with an identical rahter long parameter list. ...
1
vote
0answers
43 views

Mapping over array/matrix the efficient(compact) way

Update In short, I have myCAans[[1 ;; 5]] like this: {{0, 0, 0, True, 0, 0, 0, 0, True, 0, "T0", 39}, {0, 0, 0, True, 0, 0, 0, 1, True, 1, "B", 101500}, {...
0
votes
0answers
50 views

How to protect pure functions

I have some pattern functions and pure function in my package. If i set attributes Protected and ReadProtected. My pattern ...
4
votes
2answers
66 views

Pure Function passed as an option to a function, which uses other options of the same function as arguments

I am editing my original question, as I have figured out a method of doing what I want. Now my question is if there is a more elegant, efficient way to do the following: ...
1
vote
1answer
94 views

Difference between f[x] and f as arguments of functions

I'm new to Mathematica and trying to better understand the syntax. In the documentation, the function Limit has the signature: ...
3
votes
1answer
276 views

Defining pure function @ vs /@

Learning how to program I dont fully get the difference of /@ vs @ ...
1
vote
1answer
97 views

I don't really understand &

While I use the & all the time when doing a mapping, such as someFunction[#] &/@ somelist I don't really understand ...
2
votes
4answers
95 views

How to apply or Map a function to a 2 variables nested list

I tried to understand the mechanism of pure function tricks. Imaging I have a nested list generated by: list=Table[{p, q}, {p, 1, 9, 2}, {q, -p, p, 2}] Result: <...
0
votes
0answers
41 views

How to pass var to Select[(#colunm == value) &] construct [duplicate]

I'm using a construct (more complex in fact as the value_ should take a list) to select some entries from an association with criteria: ...
19
votes
2answers
552 views

Access last variable in a pure function

# is the first argument of a pure function, #n the nth, ## are all variables and ...
7
votes
1answer
169 views

Using the list as the argument of the pure function

I know some function is listable, such that Sin@{1,2,3,4,5} gives {Sin[1], Sin[2], Sin[3], Sin[4], Sin[5]} By reading some ...
2
votes
1answer
110 views

Two-slot pure function [closed]

I come across this problem in many different forms when doing arithmetic in MMA and have until now just suffered along doing it the long way – having given up trying to figure it out myself – but now ...
2
votes
2answers
84 views

Difference between (#1[[2]] < #2[[2]]&) and (#1[[2]] & < #2[[2]] &) [closed]

For example, if I have a list {{a, 2}, {c, 1}, {d, 3}} and I want to sort it w.r.t. the numbers: ...
0
votes
0answers
50 views

Max and Mean command produce the same result

I tried to find a point that is nearest to two other points, using both "Max" and "Mean". I wonder why both produce the same value. I tried changing the input, but the results by using "Max" and "Mean"...
1
vote
3answers
62 views

Replacing undefined variables

Here's a MWE of what I want to do: a = 1; If[ValueQ[#], #, "Null"] & /@ {a, b, c} (*Actual: {Null, Null, Null}*) (*Expected: {1, Null, Null}*) What am I not ...
0
votes
0answers
44 views

Pure function slot delivery [duplicate]

Another novice problem encountered. I am going to need a cardiology surgery. :( For a simple example, a = #1 &; b = #2 &; f = a^2 + b^2; f results in <...
1
vote
1answer
52 views

How to avoid multiple copies of a list when mapping a pure function to it?

Given this code: mylist = Tuples[{a, e}, 2]; Insert[mylist, 1, {#, 3}] & /@ Range[2] The output is this: {{{a, a, 1}, {a, e}, {e, a}, {e, e}}, {{a, a}, {a,...
0
votes
0answers
35 views

Error in storing the output of a module (subroutine) into an array

For a physics application, I have to compute components of a rank-4 tensor, the Riemann tensor. I do this using a module (subroutine) which looks like this ...
8
votes
5answers
328 views

Slot # corresponding to subset of a list in select

Is it possible to use # in the select function preserving the structure of the list? Here an example of what I mean. Let's say I want to select all the pairs where the first element is 1: ...
1
vote
1answer
83 views

Why doesn't Table work for list of pure functions? [duplicate]

I wrote a function InversionF[f__] := Table[Function[x, 1 - ff[x]], {ff, {f}}] I was thinking that for list of functions, it will create list of modified ...
2
votes
3answers
151 views

Replacing elements of a list using a pure function fails; why?

I have this list: {{5, 6, 7, 8, 9}, {10, 11, 12, 13, 14}, {15, 16, 17, 18, 19}, {20, 21, 22, 23, 24}, {25, 26, 27, 28, 29}} For learning purposes I try to replace one number (random) in each ...
1
vote
2answers
103 views

Defining an operator: Partially evaluate a Pure Function

I would like to define an operator that takes some parameters as input and returns a function. Here is an example: ...
2
votes
3answers
195 views

Pure function within another function

I am writing a function along the lines of the following: func[basis_,coordinates_,vars_]:=Table[Function[vars,monomial]@@@coordinates,{monomial,basis}] The idea ...
5
votes
3answers
417 views

Simplifying the body of a pure function

After solving a differential equation I obtain a pure function which I wish to simplify. I am able to simplify the corresponding expression ...
2
votes
0answers
348 views

What does the @ symbol do in Mathematica? [duplicate]

I have the folllowing array $$h = \left(\begin{array}{cccc}1 & 0 & 0 & 0\\ 0 & f^2 & 0 & f^2 \cos\theta\\ ...
1
vote
0answers
140 views

Problem in evaluating nested anonymous functions [duplicate]

It seems that I have found a bug (or a fundamental problem in the design of the programming language of Mathematica) with functions defined as rules. Define the function g as g(1)=2 and g(2)=1. We can ...
11
votes
1answer
215 views

Practice notebook for Pure Functions & Rules-Patterns

Are there practice notebooks or exercises for practising using pure functions and rule-patterns? I've completed "An Elementary Introduction to the Wolfram Language" and "Fast Introduction for ...
5
votes
1answer
136 views

AppendTo in Combination with pure function

spA = {}; spB = {}; spC = {}; AppendTo[#, {"Up", "Down"}] & /@ {spA, spB, spC} doesn't work in Mathematica 11.1.1. Why? What is an alternative?
9
votes
2answers
382 views

Performance of Nest vs. Do

When I am iterating the logistic function using a pure function, comparing Do and Nest: ...
4
votes
3answers
335 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
508 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
127 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
31 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
32 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
588 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
156 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 ...