13 questions linked to/from Alternatives pattern in a function definition
156k views

### Where can I find examples of good Mathematica programming practice?

I consider myself a pretty good Mathematica programmer, but I'm always looking out for ways to either improve my way of doing things in Mathematica, or to see if there's something nifty that I haven't ...
5k views

### Can a function be made to accept a variable amount of inputs?

I have a function that takes two inputs and processes them for a single output. What I need is one that can take a varying number of inputs. and process them to a single output. Is this possible in ...
• 4,528
563 views

### List manipulation - adding last element of sublist to each sublist

If I have the following list: {{}, {1, 2}, {}, {2, 4, 3}, {5, 4, 3, 2}, {}} How can I add the last element of each sublist to each sublist? The results should ...
• 2,396
2k views

### How can a power of 0 or 1 be replaced?

Consider this given example for ReplaceAll 1 + x^2 + x^4 /. x^p_ -> f[p] This returns ...
• 15.9k
466 views

### How to distinguish between lists and values?

I have a (hopefully small) problem with some numerical integration algorithm, more specifically I want to integrate the imaginary part of a complex valued function, e.g. ...
• 377
2k views

### Function argument to default under certain condition

Inspired by this and this question (and how I handle this in practice), what is the best way to default a function value when a certain condition is met? For example, if a function is defined as: <...
• 10.9k
688 views

### f[arg1, arg2,...,argN] vs. f[{arg1, arg2,...,argN}]

I am trying to reproduce the API of a function (written in R) that accepts an arbitrary number of arguments and handles it the same way as it would handle a single argument that is a list of different ...
• 4,426
2k views

### Function with zero or one arguments

Is there a neat way to define a function with a single optional argument that defaults to an empty sequence? For example, suppose I want to define a wrapper for ...
• 1,091
760 views

### Generalization to AllTrue, AnyTrue and NoneTrue

I am wondering if there is a natural Mathematica way to generalize those functions. To be specific, All three functions AllTrue, ...
• 2,305
324 views

### How to get Cases to wrap results in an arbitrary head?

A number of functions provide for results to be wrapped in an arbitrary head. This is very important in cases where the results should not be evaluated. Take Level...
• 272k
397 views

### Generating all permutations of labels in an expression

I have some very long and complex expressions which involve a set of $n$ variables, and I want to be able to permute the labels of the variables. I will give a simple example, instead of my awful ...
• 1,278
188 views

### Optional argument as a correction of a main argument

Let's consider a function f[a_,b___] I want that if I input only one argument I get f[a]=a but if I input 2 arguments I ...
• 712