Linked Questions

5
votes
2answers
240 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
2answers
102 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}...
570
votes
19answers
145k 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 ...
37
votes
9answers
4k 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: ...
41
votes
4answers
4k 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 ...
53
votes
3answers
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,...
12
votes
4answers
835 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: ...
16
votes
2answers
574 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
4answers
936 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
1answer
1k 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
2answers
198 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
3answers
267 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 ...
4
votes
2answers
286 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 ...
3
votes
1answer
291 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 ...
1
vote
2answers
64 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: ...

15 30 50 per page