For questions on writing functions (pure or using Set/SetDelayed) for any purpose, including the features that may be incorporated in those functions, such as options, patterns and conditions.

learn more… | top users | synonyms

9
votes
2answers
239 views

Defining a ForEach function

Consider the following function definition: ForEach[iterators__] := Table[#, iterators] &; Two questions about it: I. It works perfectly if you call it like ...
7
votes
3answers
287 views

Inverting a function in a certain region

InverseFunction works well for globally invertible functions, like f = 2*# + 2 &; InverseFunction[f] ...
12
votes
1answer
196 views

How to make a function like Set, but with a Block construct for the pattern names

How can we define a function that works like f[x_]=ComputeSomething[x] and treats x as a variable that does not have a value? ...
10
votes
3answers
463 views

Passing down arguments

In R, one can use ... to pass arguments down to another function. For example ...
10
votes
5answers
317 views

define a function from a list

I would like to create a mapping from a list and I am wondering if there is a slick way of doing it. For example, consider the list ...
26
votes
6answers
973 views

How can I create a function with optional arguments and options?

Is it possible to create a function with optional arguments that also takes options? Here is a simple example. I have a function f with option "g". It also has ...
28
votes
6answers
923 views

Functions vs. patterns

Every time I define a new function, I wonder which construct I should use: true functions obtained by using Function, or rule-based syntax. For example, these are ...
47
votes
4answers
2k views

Functions with Options

Suppose you want to create a function which has optional arguments. Maeder's book "Programming in Mathematica" covers the topic extensively up to version 3, but some things have changed/evolved since ...
10
votes
2answers
295 views

Constructing functions aware of the number of output arguments

In Mathematica, you can construct a function f to have different definitions based on its input arguments. For example, ...
13
votes
2answers
298 views

SetAttributes[f,Flat]: Why the order dependence?

I'm experiencing a strange phenomenon. Here's a (quite artificial) test case: The following definition work just fine: ...
21
votes
3answers
658 views

Sum over n variables

What is the most painless way to sum over n variables, for example, if the range of summation is $i_1 < i_2 < i_3 < \dots < i_n$, where $n$ is an argument of the function in question? Is ...
21
votes
1answer
271 views

which is better, using Assert[] or manual checks on arguments and other computations?

I never used Asserts in Mathematica, but trying to see what advantage they have over just argument check and additional definitions of the function to capture unwanted input. For example, which one ...

1 2 3 4 5