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

15
votes
1answer
412 views

Multiple templates for a single user-defined function

How do I use the ::usage tag to allow the Mathematica v9 front end to know that there are multiple ways to call an overloaded function (a function that can have ...
15
votes
1answer
360 views

Modules that initialize themselves on first call

I use a lot of functions that extract a specific data item from a file with many data items. I want these functions to load data (slow) and return the item (fast) on first call, but just return the ...
15
votes
1answer
308 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? ...
14
votes
7answers
830 views

A function that accepts a pair or a list of pairs

Probably a duplicate, but it's not easy to search on "lists". I have a function that accepts a list of lists, say {{a1, b1}, {a2, b2}, {a3, b3}} and performs some ...
14
votes
3answers
267 views

What's wrong with this pattern of the form Except[Repeated[…]]?

I'm trying to build a head that recognizes molecules. Here's the code ...
14
votes
3answers
1k views

Passing down arguments

In R, one can use ... to pass arguments down to another function. For example ...
13
votes
2answers
300 views

Named patterns in Except

Bug introduced in 8 or earlier and fixed in 10.4.0 Bug isn't present in version 5.2 I don't think I quite understand how Except works. I want to define $f(a,b) ...
13
votes
2answers
537 views

How do I get Mathematica to return a function call unevaluated?

How do I get Mathematica to return a function call (conditionally) unevaluated? I expect this may use the slightly-mysterious Hold function. As a toy example, ...
13
votes
1answer
372 views

Is it possible to Clear all variables (but not functions)?

I have written a Mathematica script in which I define functions and variables. Here is a vastly simplified example: ...
13
votes
3answers
571 views

Is it possible to get the order of inputs when “overloading” an orderless function?

When working with symbolic matrix operations or other objects which don't have commutative multiplication, it would be nice to not have to constantly switch out times for ...
13
votes
0answers
176 views

func::usage needs to be run twice? [duplicate]

There is an annoying problem that I can't figure out why it's happening. I have defined a usage for a my function tensorA: ...
12
votes
5answers
1k 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 ...
12
votes
2answers
1k views

NestList on a function of several arguments

I'd like to know if it is possible to use NestList on a regular function of several arguments, such as: ...
12
votes
4answers
489 views

How to pass a list of arguments into HoldAll

I have a list of arguments (which in reality is lengthy): arguments = {a, b, c} arguments2 = {a_, b_, c_} f[Sequence@@arguments2] := a + b + c Note: It seems ...
12
votes
4answers
214 views

Distributing function arguments with function compositions. How to compute $(f + g^2)(x) = f(x) + g(x)^2$?

Suppose I have two functions f and g. To compute $f + g$ evaluated at a point $x$, I know that one can use ...
12
votes
4answers
197 views

How to define a function that is Listable for some but not all arguments?

I want to define a function f[x,y] that is Listable for only the first argument, not the second. If we use ...
12
votes
4answers
408 views

Equivalent Nested Loop Structure

Consider the following examples: ...
12
votes
2answers
1k views

Converting ConditionalExpression to Piecewise

I have Solve[] return a list like ...
12
votes
1answer
569 views

How can I define a Listable function only apply to a vector?

Consider the function f which should behave as f[{{1, 2}, {3, 4}, {5, 6, 7}}] ...
12
votes
3answers
636 views

Pass by reference for an option argument

Pass by reference can be faked by using HoldAll or something similar in the definition of a "Function". But can pass by reference be faked for symbols passed as ...
12
votes
2answers
846 views

How to plot a barycentric line

I want to plot a barycentric function on an equilateral triangle (ternary plot). For example f1 = {Abs[Sin[x]], Mod[x, 2], Abs[Cos[x]]}; At the moment I evaluate ...
12
votes
2answers
196 views

How can one write a robust ListableQ function?

I would like to have a test that determines if a particular function is Listable. In the case of Symbols this is merely a matter of checking Attributes. ...
11
votes
4answers
386 views

Is there a concise form of Which or similar conditional?

Suppose that I want to write a function fun that takes an Integer num as input, and returns ...
11
votes
7answers
3k views

Recursive function with if-statement

I am trying to represent the following function definition in Mathematica: $$\begin{align*} f(1)&=1 \\ f(2n)&= \begin{cases}f(n) & \text{if}\space n\equiv0\pmod{2} \\ 2f(n) & ...
11
votes
4answers
662 views

Assessing argument type in set delayed function definitions

I'm wondering how to properly assess the type of arguments passed to functions defined with :=. I want my functions to be the most efficient, unambiguous and clear ...
11
votes
5answers
853 views

Reverse Polish Notation

Is there a way to evaluate a string containing RPN in Mathematica? SE thinks this question is too short, so let me expand on it. Do you know of any function, that provides the following ...
11
votes
2answers
270 views

How to build a function object like the built-ins such as Interpolation?

There are many built-in functions that return a function object, such as Interpolation[], BSplineFunction[] ,...
11
votes
4answers
296 views

Recognizing special cases of a defined function for permuted arguments

I have a number of ugly ways to implement the following pattern recognition task, but I'm looking for something elegant to keep my notebook tidy. I need to define a function of six variables ...
11
votes
1answer
187 views

Copy Built-in function to new name

It is know that some built-in functions are system function and changing their properties may cause trouble. Suppose for example I want to change features of Cases: ...
11
votes
2answers
276 views

can a ColorFunction have side effects?

Can anyone explain this (to me) odd behavior. If I run this code once (fresh kernel) it works as intended. ...
10
votes
5answers
330 views

Function for a series of joined slopes

I need a function for a series of joined slopes and my solution feels a bit kludgy. Is there a better way? A list of pairs of transition points and slopes: ...
10
votes
2answers
924 views

Number of digits for factorial of 12345678987654321

What is the number of digits (IntegerLength) of the factorial of 12 345 678 987 654 321? The number of zeros at the end of this factorial was calculated and it is huge: exactly 3 086 419 746 913 569 ...
10
votes
4answers
336 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: ...
10
votes
4answers
2k views

Turn expression into function

I'm new to Mathematica and I spent the last hour trying to make the expression ...
10
votes
3answers
455 views

Where in the documentation can I find a list of function argument types?

Recently, I learned that it is possible to assign "types" to function arguments in the definition of a function. Suppose I have a function stringFun that does some ...
10
votes
2answers
330 views

Manipulating OptionsPatterns[]

How do you combine OptionsPatterns? For example, I would like to have a function that can take both options that I specify, and options for all graphs: ...
10
votes
2answers
400 views

Functions that remember their values

Can someone explain what is going on with the following ... func[y_] := func[y] = (Print["Hello world !!!"];) func[1] Hello world !!! ...
10
votes
3answers
389 views

How to set a TraditionalForm output for a symbol

How do I set a TraditionalForm output for a particular symbol/function? In particular I would like my user-defined symbol ...
10
votes
4answers
412 views

How to check the style and number of arguments like the built-in functions?

Given that I have a function sqrt[x] that I must check the style and number of argument x. I can do like this: ...
10
votes
1answer
238 views

Question about Rules and Pattern (The operator patt/;test)

Maybe this question is so trivial but it has confused me. I'm studying the What the @#%^&*?! do all those funny signs mean? and in the Rules and patterns under ...
10
votes
2answers
649 views

Proper way to add vertices to an adjacency matrix

I'm looking for a robust way to add vertices to a graph by modifying its AdjacencyMatrix. Here's what I have so far: ...
10
votes
1answer
344 views

Is there a function to generate a minimal clique cover?

This is a graph problem known as the "Clique Edge Cover" or "Intersection Number" problem, and the goal is to find, from a given graph like $E=\{\{a,b\},\{a,c\},\{b,c\},\{b,d\},\{c,d\}\}$ and ...
9
votes
3answers
888 views

Inverting a function in a certain region

InverseFunction works well for globally invertible functions, like f = 2*# + 2 &; InverseFunction[f] ...
9
votes
2answers
909 views

How to create 'help' `?` for a function (as documentation)

I have defined a function foo in some complicated way. But now I would like to add a little documentation that gives the user information on how it is to be used. ...
9
votes
2answers
5k views

How to define a differential operator?

I have a linear differential operator, for instance, $L\left (\partial _{t} \right )=\partial _{tt} - 3\partial _{t} + 2$. I use it in 2 different ways: apply the operator to a function: $L\left ...
9
votes
3answers
305 views
9
votes
2answers
348 views

Using up-values and down-values

I have a problem I'm trying hard to find a solution for, although I don't know if a solution exists. The following rules produce the result e[2].f[] ...
9
votes
2answers
379 views

Define Log so that negative reals evaluate on lower edge of branch

I need to get Mathematica to evaluate the logarithm of a negative real number using the lower branch instead of the upper branch, so that while ...
9
votes
2answers
473 views

How can I get the right hand side of a delayed expression?

Imagine there is a given function f, defined with SetDelayed, say f[x_] := Sin[x]^2 + Cos[x]^2. Is it possible to get rhs of ...
9
votes
1answer
169 views

Map at a subsequence of a list

I'm trying to construct a function somewhat similar to MapAt. A basic usage example should look like this: ...