Skip to main content

Questions tagged [functional-style]

Questions about Mathematica's functional programming style, including the use of pure functions (Function[], #, &) and functions such as Map, Apply, Nest, and Through.

Filter by
Sorted by
Tagged with
4 votes
2 answers
131 views

How to use functional iteration with changing function?

I recently came across this problem on reddit. The problem is supposed to be read like a snake, and the result of each succesive operation is sent to the next operation: There are 9 blank boxes and ...
ydd's user avatar
  • 4,572
2 votes
2 answers
68 views

How to implement this zip function? zip[g,{f[{a,b}],f[{c,d}]}] to f[{g[{a,c}],g[{b,d}]}]

I have a function that returns an expression in the form f[{a,b}], and I use this function twice and assemble the expression ...
Tapiocaweasel's user avatar
4 votes
2 answers
319 views

Get cumulative sums in a list without using a for loop [closed]

I want a list of the kind list = {a,a+b,a+b+c} for list plotting. The quantities of the list come from a function. Let us say this is ...
QFTheorist's user avatar
4 votes
1 answer
82 views

PatternTest in Function

This is probably trivial and already asked but I did not find it. How to use PatternTest inside Function (...
azerbajdzan's user avatar
  • 20.1k
2 votes
1 answer
72 views

Expression as parameter to function that evaluates it, best way to do it?

I need a function fSum that computes the angular sum for some expression, like this: fSum[3 Sin[2 x], 0, 2 π] Or from a predefined function, like this: f[x] = 2 Sin[3 x]; fSum[f, 0, 2 π] Or plots it ...
Mikl's user avatar
  • 101
0 votes
2 answers
62 views

Problem with $ sign in temporary function parameter

I am having a problem with $ sign in the name of a parameter to a temporary function. The functions I have should compute the angular sum (or turning angle) for some expression generated by function <...
Mikl's user avatar
  • 101
3 votes
1 answer
94 views

Quickly processing long lists using SequencePosition

Overview SequencePosition does exactly what I need; however, it seems to be very slow for long lists. Is there an input option that would allow it to process faster? Or, perhaps an alternative ...
user6546's user avatar
  • 1,069
4 votes
3 answers
657 views

Concise version of MATLAB code

Please see this MATLAB code: k=0:8; wk=2 * pi * k/17; ak=double(wk<0.5*pi) It generates ak with such a simple expression. To ...
metroidman's user avatar
5 votes
1 answer
81 views

Undocumented behavior of Fold for arbitrary heads [closed]

Edit: I'm apparently blind. Sorry! And thanks to those who answered anyway. Apologies in advance if this is sort of a nitpicky question, but I'm generally very paranoid that I don't fully understand ...
max's user avatar
  • 111
4 votes
1 answer
98 views

How to shorten the plotting code using slots and table? [closed]

Background Information I have the following Mathematica code: ...
codebpr's user avatar
  • 2,433
0 votes
1 answer
60 views

How to evaluate expressions which involve derivatives and evaluations of functions in a specific case, via a condition?

I have an expression involving the derivative of an arbitrary function r_0 c0 = Derivative[1][Subscript[r, 0]] Later I must replace r_0 by several specific ...
florin's user avatar
  • 1,900
6 votes
4 answers
429 views

Subdivide a sequence

Yesterday I couldn't solve a problem which seemed to be rather simple. I tried RepleaceRepeated, SequenceReplace and similar ...
eldo's user avatar
  • 81.7k
0 votes
1 answer
91 views

How can I apply Residue in a functional-style way? [closed]

Let me suppose that I have got some expression, for example f_{1}(x)+f_{2}(x) and also a list of a form: {1,2} How can I apply ...
George's user avatar
  • 69
6 votes
8 answers
405 views

Removing duplicates between sublists

I want do delete from each sublist those elements which are contained in one or more of the other sublists. The surviving elements must maintain their original order of appearance. ...
eldo's user avatar
  • 81.7k
8 votes
6 answers
448 views

Accumulate up and down

I want to accumulate positive and negative values separately. dt = RandomSample[Range[-10, 10]] {3, -5, 2, -9, 1, -1, 0, 8, 10, 4, -7, 7, -8, -3, 6, -4, -2, 5, -10,...
eldo's user avatar
  • 81.7k
9 votes
9 answers
674 views

Partition a random sequence composed of three numbers

I want to partition an array composed of 1s, 2s and 3s in such a way that, going from left ...
eldo's user avatar
  • 81.7k
3 votes
4 answers
220 views

Add missing column keys

Example data: data = {{"1", 50}, {"1", 60}, {"1", 40}, {"2", 15}, {"3", 18}, {"3", 18}}; The first ...
eldo's user avatar
  • 81.7k
9 votes
4 answers
695 views

Smooth a list of integer values

I want to smooth a list of Integers so that outlying values become less spiky. The result vector must again contain Integer ...
eldo's user avatar
  • 81.7k
7 votes
6 answers
377 views

Replace duplicate list values with unique elements

My request is a follow-up on this question: Replacing duplicates instead of deleting them What if we want to replace duplicated values not with a single value but with newly created values distinct ...
eldo's user avatar
  • 81.7k
5 votes
2 answers
271 views

Create unique position values

Given a list of integers I want to create unique position-values. The following rules apply: Take first number. If first number exceeds list length, take 1. Take next number. If next number is lower ...
eldo's user avatar
  • 81.7k
4 votes
2 answers
171 views

Two simple vector partition rules

We have a vector of zeros and other numbers, f.e: vector = {0, 0, 0, 9, 0, 2, 0, 5, 0, 4, 0, 5, 6, 2, 0}; The two rules: Partition the vector in such a way that (1)...
eldo's user avatar
  • 81.7k
4 votes
1 answer
104 views

Function[0], Function[,0], and Function[{},0]: Is there any inherent distinction in the "academic" sense?

Note that Function[,0] is by default interpreted as Function[Null,0]. The following three calls all give ...
user688486's user avatar
4 votes
2 answers
265 views

Enumeration of a nested list

My problem is consisting of following: I have a nested list of following form: {{B, v ** v, b ** B}, {b, B ** b}, {b ** B, v ** v}, {}} And I want to enumerate ...
George's user avatar
  • 69
0 votes
1 answer
43 views

My three demo script is too slow switching tabs

My three demo stripped down script runs fine but takes 6 sec to switch from demo 2 or 3 to demo 1. Actually I need nKs = 7 which takes 30 sec. I can't find examples of slow switching tabs. ...
L. Gorham's user avatar
5 votes
2 answers
247 views

How to Vectorize this Function?

I have been puzzling over how to vectorize the following function: ...
MMAUser's user avatar
  • 409
2 votes
0 answers
168 views

How to write 2D finite volume solver in a functional approach?

I'm solving 2D poisson problem of a finite volume problem using TDMA. The solution of the problem is given: \[Phi][x_, y_] := x^2 y + x y^2 + 1 The definitions are ...
kpaz's user avatar
  • 41
3 votes
1 answer
120 views

MapApply Operator Form

I have a list of matrix sequences (list of matrices) that I want to multiply, something like ...
Aaron's user avatar
  • 153
2 votes
2 answers
84 views

Would it be slower to place function definitions inside another function definition?

Will it be slower to place functions' defintions inside a main function? I usually do that if the subfunctions are short. However, with a long subfunction I usually place it outside as I think that ...
internet's user avatar
  • 785
4 votes
4 answers
177 views

How to I know how many nested functions have been called?

I have two functions, and the 1st calls the 2nd: f[x_]:=Block[{},x^2] g[y_]:=Block[{},Sin[f[y]]] I would like to know, in each ...
Sébastien Neukirch's user avatar
3 votes
3 answers
415 views

Better way to do such iteration

Consider such an iteration function ...
matrix42's user avatar
  • 7,148
0 votes
1 answer
44 views

How to fix accuracy error?

When I run the code this type of error appears: ...
Mam Mam's user avatar
  • 1,883
3 votes
3 answers
461 views

How to make a function take another function as an input?

I am quite new to mathematica, and I simply want to know how to make a custom function, take another arbitrary function as an input. I have attached code that shows what I am trying to accomplish (...
Robin Hansen's user avatar
0 votes
1 answer
230 views

Create a function that can assign values to different variables in Mathematica

Here I am trying to create a function that can assign values to different variables in Mathematica. Assume the function f[a _, b _, c _]; x1 = a; x2 = b; x3 = c; ...
Math View's user avatar
2 votes
1 answer
165 views

Can it be rewritten in a less abbreviated version? [closed]

I am trying to understand this code ...
Mam Mam's user avatar
  • 1,883
10 votes
7 answers
393 views

Map function to the list and keep the previous

Give the following list, list = {{a1, a2, a3}, {b1, b2, b3, b4}, {c1, c2, c3, c4, c5}}; I want to get ...
expression's user avatar
  • 5,662
1 vote
0 answers
40 views

How to create a Sort function that does not differentiate between String rule keys and Symbol rule keys

Im trying to Sort Rule-s according to Key-s (or names) but the normal ...
Jules Manson's user avatar
  • 2,467
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 ...
Meclassic's user avatar
  • 1,035
2 votes
0 answers
32 views

Attributes for functions with currying [duplicate]

Is there a way to set attributes for a function which holds for all arguments when currying? When evaluating, e.g., ...
Gert's user avatar
  • 1,620
1 vote
1 answer
160 views

Cannot find DockedCells with Cells or NotebookFind

Request 1: A workaround for Cells or NotebookFind needs to be found for locating and returning the ...
Jules Manson's user avatar
  • 2,467
10 votes
1 answer
243 views

How to programmatically construct a function or a compiled function?

I'd like to programmatically construct a function from a long expression with many duplicate terms. The objective is to programmatically create a set of compiled functions with expressions that were ...
Craig Carter's user avatar
  • 4,625
23 votes
3 answers
1k views

What is the fastest method to set the diagonals of a matrix to 1?

I am trying to replace the zeros along diagonal of a distance matrix to a list of constants. In this case, a diagonal of 1's. It appears that the LinearAlgebra ...
Craig Carter's user avatar
  • 4,625
4 votes
1 answer
85 views

Accumulate[] on an ordered HashTable

Hopefully this is a trivial question, but I keep getting lost looking for an answer in the documentation. I have an ordered HashTable containing indexed real numbers: ...
davidcl's user avatar
  • 143
1 vote
0 answers
97 views

Numerical solution for multi-step PDEs

I am trying to numerically solve the following PDEs in MMA (v12.0) $$\partial_u f(x,u) = \left\{\begin{matrix}&-a f(x,u) + b \, \partial_x^2 f(x,u), & 0<u<T\\ &r(x;\alpha_1) \, f(...
SaMaSo's user avatar
  • 231
1 vote
1 answer
104 views

While inside a function body does not work as expected [closed]

I'm trying to write a function that return a randomly created rational polynomial function that has a discontinuities points. I already have a function that generates random rational polynomial (...
cgcg's user avatar
  • 69
7 votes
4 answers
442 views

How can I efficiently define functions with different names but doing the same stuff?

Recently I have met with with this interesting question:How can I efficiently define functions with different names but doing the same stuff? Somebody may want to ask me why I want to do things like ...
AlbertLew's user avatar
  • 483
0 votes
0 answers
56 views

Algorithm consisting of modules returning multiple errors

I'm trying to write a genetic algorithm that will take a list of lists of reals in interval [0,1], my "starting population", "score" them according to a fitness function, append ...
J0ta's user avatar
  • 115
6 votes
1 answer
174 views

K-Mean Clustering Algorithm

I have implemented the k-Means Clustering Algorithm using the Wolfram Language. However, I think it can be more efficient. Do you have any idea how to make it more efficient (e.g., by removing the ...
LambdaHaskell's user avatar
3 votes
5 answers
284 views

How can I get a list of all distinct unordered pairs of numbers in a specific range? [duplicate]

If I do e.g. Flatten[Outer[List, Range[3], Range[3]], 1] I get ...
user236343's user avatar
2 votes
1 answer
398 views

What's a functional alternative to nested Do loops?

How can I use functional code to achieve the same effect as the following procedural code Do[ Do[ f[i,j], {j,1,m} ], {i,1,n} ]
user236343's user avatar
7 votes
1 answer
221 views

How can I do "Map" on a binary function?

If there is a one-variable function, we can f/@{a,b,c} to get {f[a],f[b],f[c]} What to do with it if it comes to a binary ...
Patchouli Nine's user avatar

1
2 3 4 5
10