# Tagged Questions

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.

122 views

### Force evaluation of the right-hand side of a local variable definition

One can do expression = a + 1; f[a_] := Evaluate[expression] but how would one do something like this ...
154 views

### Optional arguments with lists as function parameter

I'm developing a function f which should support optional arguments {x_, y_} in list form. A simple example looks like this: <...
197 views

### Parentheses for negative integers

Trying to standard create prime factors presentation, but can't seem to remove the parentheses: ...
151 views

### Graphics Function Definition Style (Using SubValues vs. DownValues)

I am going through some of the items in "Modern Differential Geometry of Curves and Surfaces with Mathematica, Third Edition". An example of the style used by these authors is: ...
363 views

### What does it mean when a function is defined with two pairs of brackets

I have noticed a function defined as such: f[x_][y_] := expr; How is the argument y in 2nd pair of brackets being used? What is the difference from defining a ...
1k views

### Nesting multiple functions of multiple variables

I was wondering how one would go about nesting multiple functions of, say, two variables. The problem comes from trying to implement the Chirikov standard map without using "for" cycles. I found a ...
100 views

### How to delay the definition of a function from the time of package initialization to when it is first called

I am writing a package where a couple defined functions take a very long time define at initialization time (because they are large CompiledFunctions). However ...
255 views

### How to define a nested function like this?

For example, I want to calculate the limit of $\frac{1}{1+\frac{2}{1+\frac{3}{1+\ldots}}}$. So I want to define: $f(1)=1$ $f(2)=\frac{1}{1+2}$ $f(3)=\frac{1}{1+\frac{2}{1+3}}$ Then I calculate ...
325 views

### How to write complex function definitions at run time?

(Posting as a self-answered question to share a technique I discovered) In a certain project, I have many function definitions in which all parameters should be optional and come with default values. ...
202 views

### What is the recommended way to define numeric function with special cases?

What is the recommended way to define a purely numeric function with special cases? Should I define many special cases with pattern matching? ...
323 views

### Compiling a function that takes lots of optional arguments

I have a function that takes lots of optional arguments (basically some design parameters, about 10 of them) and performs some basic algebraic calculations involving complex numbers with the options. ...
1k views

570 views

### Any rule of thumb for converting a simple mathematical expression into a pure function?

By simple mathematical expression I mean one with a few functions and few variables copied into MMA in a mechanical way from a math book. For example this one, giving the distance between two points ...
596 views

### Variable that changes itself

I am trying to store a boolean value into a variable with the additional property that the value changes each time the variable is called. ...
445 views

### What is the right way to write a function that includes other functions?

I want to include multiple sub functions: $b(x) = 10x$ $w(x) = s + b(x)$ $c(x) = x^2$ into two main functions: $ua(x) = w(x)-c(x)$ $ub(x) = w(x)-c(x)^2$ As a reaction to this answer, I wrote ...
1k views

### Generating list of alphabet letters

I want to create a list of length n with the letters of the alphabet. For example, if n = 3, by list would be ...
284 views

### Pattern matching repeated structures with named subexpression components

I am trying to write a function with a pattern that tests its arguments for a particular structure. But in that pattern I'd like to name a subpattern to make it easier to exract and operate on. The ...
129 views

### How to define a function whose parameter have a list with default-value

As we know,we can define a function with default-value like this: fun1[x_, n_: 1, a_: 2, b_: 3] := x + n + a + b In[6]:= fun1[4] Out[6]= 10 But in my ...
257 views

### Constructing a function with Flat and OneIdentity attribute with the property that otimes[a]:>a

I want to have a function (called otimes) with the following two properties: it has the attribute OneIdentity and ...
283 views

### difficulty abstracting the OptionValue pattern

I am building lots of functions that use Options and OptionValue and wanted to reduce the redundancy in the expressions that ...
380 views

### Prime factor counting function

Is there any way I can speed up this prime factor counting function? (I am looking for all numbers in a range with 3 prime factors (counted with multiplicity).) ...
1k views

### How to define the Poisson bracket in Mathematica

If $f,g$ are functions of the independent variables $\{q_1, q_2, ..., q_N, p_1, p_2, ..., p_N\}$, then the Poisson bracket is defined as: \begin{equation*} [f, g] = \sum_{i=1}^N \left(\frac{\partial ...
3k views

### Run entire notebook

I have created a notebook, with functions. The only thing that I want to change in the notebook later is the input file. I therefor want to have a button that runs every input step-by-step so that I ...
5k views

### How to find (numerical) value of a derivative at point?

I have the following function: f[0, 0] = 0 f[x_, y_] := Exp[-(x^2 + y^2)^(-1)] How do I find its partial derivatives at any given point, including $(0,0)$? This ...
280 views

### Simpler way to type Derivative[1, 0][u][0, x]

Though not unbearable, I always feel a little nervous when typing partial derivatives at a specified place e.g. $u^{(1,0)}(0,x)$, which happens a lot when setting initial/boundary conditions for PDEs. ...