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
1 vote
1 answer
79 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 ...
Learner's user avatar
  • 143
1 vote
0 answers
65 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) ...
Aislin_367's user avatar
1 vote
0 answers
71 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". ...
Aislin_367's user avatar
2 votes
0 answers
33 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....
Aislin_367's user avatar
2 votes
0 answers
71 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 ...
dzsoga's user avatar
  • 341
5 votes
1 answer
228 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: ...
Michel's user avatar
  • 1,596
3 votes
1 answer
82 views

Condition inside pure function

Why condition /; not work inside pure function? Suppose we have: ...
lesobrod's user avatar
  • 1,085
3 votes
1 answer
69 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.
Cheng Tao's user avatar
  • 151
10 votes
5 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: ...
Giovanni Russo's user avatar
3 votes
1 answer
189 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....
feynman's user avatar
  • 229
3 votes
1 answer
81 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?
feynman's user avatar
  • 229
7 votes
2 answers
466 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 ...
user57467's user avatar
  • 2,304
4 votes
2 answers
150 views

Slots inside nested pure functions

I'd like /@ instead of Table or Map. But let we have it inside pure function: ...
lesobrod's user avatar
  • 1,085
3 votes
3 answers
72 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: ...
Andrew's user avatar
  • 10.2k
0 votes
1 answer
51 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 ...
SkySystem's user avatar
1 vote
1 answer
80 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. ...
GambitSquared's user avatar
3 votes
1 answer
81 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 ...
Nicola's user avatar
  • 572
4 votes
3 answers
573 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: ...
E. Chan-López's user avatar
5 votes
5 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 ...
houzw's user avatar
  • 328
1 vote
2 answers
101 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 $$\...
epselonzero's user avatar
3 votes
1 answer
154 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 ...
Meclassic's user avatar
  • 953
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 ...
florin's user avatar
  • 1,582
2 votes
1 answer
126 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$}}...
B A's user avatar
  • 35
28 votes
1 answer
937 views

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

When assigning attachment operators, I often run into confusing statements like ##@@##&, ##@@@##&, or even ...
Roman's user avatar
  • 44.9k
9 votes
4 answers
449 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 ...
imida k's user avatar
  • 4,037
0 votes
2 answers
124 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 ...
Pierre ALBARÈDE's user avatar
5 votes
1 answer
64 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] ...
Syed's user avatar
  • 37.8k
2 votes
2 answers
133 views

Evaluating a FittedModel over a list of values

Simplified version Using the simpler version suggested by @ulrich-neumann : ...
Martin42's user avatar
3 votes
1 answer
117 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? ...
Andrew's user avatar
  • 10.2k
7 votes
5 answers
477 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: ...
user26718's user avatar
  • 346
0 votes
1 answer
50 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. ...
user6130990's user avatar
4 votes
1 answer
92 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:...
Jo Mo's user avatar
  • 175
8 votes
1 answer
310 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.: ...
theorist's user avatar
  • 3,548
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. ...
emnha's user avatar
  • 2,361
0 votes
3 answers
141 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.
Udf Hx's user avatar
  • 11
0 votes
1 answer
56 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: ...
Daniel H.'s user avatar
5 votes
2 answers
161 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 ...
imida k's user avatar
  • 4,037
1 vote
1 answer
114 views

pure function assignment

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

Apply function using level

The following use of Level is documented, without any example. Let's try some simple codes : ...
imida k's user avatar
  • 4,037
0 votes
0 answers
119 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 ...
Rohan's user avatar
  • 13
5 votes
1 answer
148 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, ...
Rabbit's user avatar
  • 624
1 vote
0 answers
69 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 ...
noob3000's 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 ...
user108687's user avatar
6 votes
1 answer
151 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) &] ...
Lucas Amaral's user avatar
4 votes
0 answers
45 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 ...
Pillsy's user avatar
  • 18.2k
5 votes
3 answers
192 views

Function incorrectly renaming variables in Rule Pattern

Consider the simple pure Function containing a Rule with the named pattern q. ...
Anti Earth's user avatar
  • 1,151
0 votes
1 answer
70 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 ...
Gordon J. Köhn's user avatar
1 vote
2 answers
201 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 ...
Ben's user avatar
  • 21
0 votes
1 answer
72 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 ...
Irtiza's user avatar
  • 576
1 vote
2 answers
163 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 ...
acoustics's user avatar
  • 1,699

1
2 3 4 5