# 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.

634 views

### Define a function with a function for an input

I want to create the functions $even[f]$ and $odd[f]$ which will apply the formula $even[f(x)] = \frac{f(x)+f(-x)}{2}$ and $odd[f(x)]=\frac{f(x)-f(-x)}{2}$ for an arbitrary $f$. I tried using <...
593 views

### How can I shorten this code to rotate a line segment around its center?

I have a list of line segments stored in the form: { {{x11,y11},{x12,y12}} , {{x21,y21},{x22,y22}} , ... , {{xn1,yn1},{xn2,yn2}} } Now I want to rotate all of ...
891 views

### Composition of functions

I have a number of rotations computed by rot = RotationTransform[theta, point], and I would like to compose them to produce one function that is the composition of ...
103 views

### Problem with handling a factor of $1$ in pattern matching

I am trying to implement the creation and annihilation operators $\hat{a}$ and $\hat{a}^{\dagger}$ of quantum mechanics with the following code, but alas, there is a problem with the pattern matching ...
3k views

### Module with “local functions”

I am trying to use a Module having "local functions", i.e., those which I need to define only inside this module. So I tried this: ...
374 views

561 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 ...
2k views

### How to define a polynomial/function from an array of coefficients?

I have the coefficients of my desired polynomial in an array CoefArr (I'm new to mathematica, so I think of everything as arrays, it is actually a list I believe) starting with the constant at index 1....
133 views

### Can one specify a “computed optional value”?

I would like to write a function with an optional argument, which if absent should not be a default value, but rather a computed value. What I mean is the following [warning this code does not work, ...
644 views

### Problems using hand-made transformation functions

I experience difficulties when using custom transformation rules. Below I give two concrete problems. I am interested in fixing each of them and in understanding what is wrong with my attitude to the ...
235 views

### How to correct this, f[a_] := FindRoot[eq == 0, {x, 0.5}], where eq contains a parameter $a$, without using eq[a]?

I want to solve an equation with a parameter $a$, and define the result as a function of $a$. It is something like this: ...
1k views

### How to find the smallest root

I have a continuous, differentiable, monotonic, bounded function called F[t]. If t -> Infinity then ...
4k views

### Building matrices by attaching vectors (columns) and matrices

Having the need to attach a column to a matrix or to join matrices to make longer rows is an operation that I use very frequently and I find the Join function ideal ...
186 views

### Account for powers of 0 and 1 when defining function

I have a function that I am defining as (assume m,n,a,b are non-negative integers): myfcn[x_^m_ p_^n_, x_^a_ p_^b_] := basefcn[m, n, a, b]; and then extending to ...
169 views

### Checking through an expression

I am writing a code that manipulates expressions that contains functions with an arbitrary number of arguments. An example expression is ...
94 views

### How to organize expression by symbols (like Collect), but apply different functions to each coefficient

I want to mimic the functionality of Collect[expr, {vars}, func], but with the following modification: The function f that is ...
85 views

### TagSetDelayed on a function with a delayed value

I'm trying to automatically optimize cases when the user evaluates Length[returnsList[x_]] to instead use returnsLength: ...
217 views

### How can a function know in which function it was evaluated?

Hello my second question here might be very hard. I am asking for something "magical", if i define a function g and i call it inside a function f, how can g have information about the outer function ...
181 views

### Specifying optional arguments with variables

Perhaps this is poor programming on my part, but I would like to create a function that uses a variable as an optional parameter: var = 2; f[a_:var] := {a} The ...
160 views

### Change some option of a function when calling it [duplicate]

Say I have defined some function like f[p_] := Plot[Sin[p*x], {x, 0, 2 Pi}] Now in most cases, I just need to specify the value of ...
239 views

### FullDefinition and Module local variable

I have a question about FullDefinition and how it interacts with the local variables of Module. I've created a few functions (...
109 views

### Context-dependent functions

TL;DR warning! My question is not about getting the length of the ticks correct (although if someone has good suggestions, that would be welcomed). My question is about constructing functions that ...
187 views

### How to track Part of Symbol or How to symbolize Parts of Symbol without copying data

Let's create simple function: SetAttributes[h, HoldFirst]; h[data_] := Dynamic[Grid[data], TrackedSymbols :> {data}] test = RandomReal[1, {2, 2}]; h[test] ...
663 views

### How can I rewrite this with NestList or FoldList?

The original code used For: ...
5k views

### Creating custom functions with multiple arguments

I am hoping that this isn't a stupid question so feel free to vote it for closure. Google failed somehow. I want to define a custom function (more complicated than the usual ...
316 views

### How can I create currying functions using pure function syntax?

For example how can I write In: pureFunctionSyntax[myF] /@ {7, 3} Out: {myF[7, Log[7]], myF[3, Log[3]]}
198 views

### Make second argument same as first when only one argument passed to function [duplicate]

Clear[f] f[n_, k_: n] :={n, k} f[x] {x,n} Actually I would like to get: {x,x} The following is one wrong definition, I'm showing that I need the ...
624 views

### Defining functions with special cases

I'm trying to define a function myFunc with a special case: myFunc[x_, y_] = (x^2 - y) Log[x^2 - y]; myFunc[x_, x_^2] = 0; ...
592 views

### Passing unquoted strings (poor man's enumeration)

I would like to be able to pass an unquoted string as a parameter to a Mathematica function [that I am writing] and have it show up without evaluation if the string happens to be defined. Thus I would ...
356 views

### Calculating extreme spread with a Do loop [closed]

I am trying to model what is called the extreme spread of a series of shots at a target. Each shot is from a normal distribution with mean 0 and variance 1. For this example there are three shots per ...
287 views

### What evaluation mechanism is behind Mathematica support for multiple definitions of a function? What is the benefit of this?

I am studying the question What are the most common pitfalls awaiting new users? Up to now, I have fulfilled some of the parts but yet don't understand which this problem is related to? Consider the ...
462 views

### Using function with multiple definitions in Manipulate

I'm trying to use Manipulate to visually try out different values of lambda in a Box-Cox transformation. I've created a boxcox ...
2k views

### How to find the non-differentiable point(s) of a given continuous function?

For example, the non-differentiable point of the function $f(x)=|x|$ is at $x=0$. How to find the non-differentiable points of a continuous function that is defined numerically?
817 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: <...
175 views

### Using a held option

Woud there be an elegant way to use a held option ? For example if I need to pass by reference several variables but I don't always need all of them, it can be useful. For example if I want to append ...
243 views

### Recursive piecewise integral relation with piecewise base case?

How is this recursive formula $$f_{n+1}(z) = \int_0^1 f_{n}(z-y)\,{\rm d}y$$ implemented in Mathematica? The base case is  f_1(z) = \begin{cases} 1 & 0\leq z\leq 1 \\ 0 & \text{...
217 views

107 views

### Multiple output cells, suppressed by semicolon

I'd like to write a function that outputs multiple cells, as in Multiple output cells, but not when the function is called with a semicolon at the end. Any idea how?
253 views

### How to understand the process of ListCorrelate when it in two-dimensional condition?

Toady,I learn a function ListCorrelate,and I can understand process of execution his result in one-dimensional. However,the ...
280 views

### How to get “Signature” of each element of “Permutation” output

I need to sum over all possible permutations, multiplied by the Signature of each permutation of a given list of symbols. But, I don't know how to determine the <...
139 views

### Explicit use of Evaluate not behaving the way I expect it to

I discovered a strange behavior of the Evaluate function. Case 1: ...
290 views

### More elegant function construction for blues loop

Is there a more elegant way to do this: ...
158 views

### How to specify and use nested options for a function?

I want to write a function that takes options that are nested. For example, suppose the function has one main option, "method" which can take two values, "a" and "b". Also, when method is "a", it can ...
116 views

### Filters on image vs data

I was using GaussianFilter and realized that applying it to an array yields a different result from applying it to an image. For instance: ...
296 views

### How can I create a class of matrices programmatically?

I am trying to figure out how to create a function that can create the matrix pattern below, where: f[1] returns {{1}} ...
1k views

### Manipulation of InterpolatingFunction

Suppose that $q[t]$ is obtained by NDSolve as an InterpolatingFunction, and I want to define $Q[t]$ to be some function of $q[t]$...
119 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 ...
151 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: <...