Linked Questions
16 questions linked to/from A function that accepts a pair or a list of pairs
5 votes
2 answers
414 views
Function pattern to support listed and non-listed arguments [duplicate]
I often want to write functions that take as an argument either a) a rule or b) a list of rules. As an example, when using Replace with just one rule, it does not ...
0 votes
2 answers
158 views
Writing a function with variable arguments [duplicate]
I want to write a function which takes multiple arguments varying from 1 to n, where each argument is a triplet. sublistProduct[{$a_1,b_1,c_1$},{$a_2,b_2,c_2$},...] = { $\Pi_{i=1}^{i=n}a_i ,\Pi_{i=1}...
608 votes
19 answers
161k 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 ...
38 votes
9 answers
5k views
How to select minimal subsets?
I am a newbie, so please point me in the right direction if you feel this question has been answered somewhere else before. Here goes: Suppose I have a list like this: ...
45 votes
4 answers
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 ...
58 votes
3 answers
2k views
How is pattern specificity decided?
Mathematica has a notion of pattern specificity, which is a partial ordering on patterns. The rules (e.g. DownValues, SubValues,...
14 votes
4 answers
1k views
How to Thread with custom functions? Thread vs Map?
This function finds n given the nth prime: findPrime[n_] := If[PrimeQ[n], i = 1; While[Prime[i] < n, i = i + 1]; i, False]; This works: ...
17 votes
2 answers
795 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 ...
7 votes
4 answers
1k views
Composition of mappings not working as expected
I have two functions $f,g:\mathbb{R}^2 \to \mathbb{R}^2$ and I define a third one $h:\mathbb{R}^2 \to \mathbb{R}^2$ as the composition $$h(x,y) = g(f(x,y))$$ I'm trying to get this function into <...
12 votes
1 answer
2k views
Safe way to put a pattern on the right hand side of a rule
I'm interested in creating a function or rule that generates its own set of rules. For instance, it would take as an input the expression h[m, n] and the indices <...
2 votes
2 answers
255 views
make trace of a matrix listable?
I have a list of 2 by 2 matrices and I want the list of their traces. I have been using Map[Tr,mylistofmatrices] But I wonder if there is a way to make ...
1 vote
3 answers
355 views
Defining functions when there are a lot of parameters
Suppose I have two lists of parameters: $\delta=\{\delta1, \delta2, …\}$ and $\gamma = \{\gamma1, \gamma2, …\}$. The question is: how can one use $\delta$ and $\gamma$ (the parameter vectors) within ...
3 votes
1 answer
664 views
Replacement rules with Lists
first time user and not quite familiar with how to enter my code here properly, but this isn't lengthy.. I have a previously defined function that computes the total time it takes to ride a bike with ...
4 votes
2 answers
512 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 vote
3 answers
185 views
About composition of functions (I don't want to use a vector variable function)
f2[x_, y_] := x^2 + y^3; g[t_] := {t^2, 3*t+1} $g$ returns a list, but $f_2$ is a two variable function. So, we cannot compose these functions: ...