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

7
votes
2answers
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 ...
7
votes
1answer
131 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, ...
7
votes
3answers
619 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 ...
7
votes
2answers
226 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: ...
7
votes
1answer
1k views

How to find the smallest root

I have a continuous, differentiable, monotonic, bounded function called F[t]. If t -> Infinity then ...
7
votes
2answers
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 ...
7
votes
2answers
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 ...
7
votes
2answers
94 views

For loop works at top-level but not in a function

Why does this work: For[i = 1, i <= Dimensions[list][[1]], i++, list = Insert[list, i, {i, 3}]] But this doesn't: ...
7
votes
3answers
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 ...
7
votes
3answers
90 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 ...
7
votes
3answers
81 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: ...
7
votes
1answer
215 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 ...
7
votes
1answer
178 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 ...
7
votes
2answers
159 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 ...
7
votes
1answer
234 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 ...
7
votes
1answer
106 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 ...
7
votes
2answers
185 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] ...
6
votes
3answers
651 views

How can I rewrite this with NestList or FoldList?

The original code used For: ...
6
votes
2answers
158 views

Sets and sums of different elements

Consider the set S={w,x,y,z}. If the set of all possible sums of any three different elements from S is {-1,3,5,8}, then what is the set S? How could I solve this function using Mathematica?
6
votes
3answers
313 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]]}
6
votes
4answers
196 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 ...
6
votes
3answers
607 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; ...
6
votes
2answers
574 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 ...
6
votes
2answers
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 ...
6
votes
3answers
438 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 ...
6
votes
2answers
281 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 ...
6
votes
2answers
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?
6
votes
4answers
795 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: ...
6
votes
2answers
173 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 ...
6
votes
3answers
217 views

Using PatternTest and/or Condition to detect equal list lengths as well as list element formats

On searching Mathematica.SE for information about overloading functions, I came across several threads about PatternTest and ...
6
votes
2answers
242 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 ...
6
votes
3answers
263 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 ...
6
votes
3answers
138 views

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

I discovered a strange behavior of the Evaluate function. Case 1: ...
6
votes
2answers
289 views

More elegant function construction for blues loop

Is there a more elegant way to do this: ...
6
votes
2answers
154 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 ...
6
votes
2answers
294 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}} ...
6
votes
2answers
977 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 ...
6
votes
2answers
113 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 ...
6
votes
2answers
145 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: ...
6
votes
1answer
195 views

Parentheses for negative integers

Trying to standard create prime factors presentation, but can't seem to remove the parentheses: ...
6
votes
1answer
146 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: ...
6
votes
1answer
356 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 ...
6
votes
1answer
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 ...
6
votes
1answer
94 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 ...
6
votes
3answers
251 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 ...
6
votes
2answers
308 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. ...
6
votes
3answers
194 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? ...
6
votes
2answers
110 views

How to define a complicated function inside the body of Compile?

I want to compile a function in a way to keep its memory footprint down. In the example below, I am trying to compile a function f that makes three calls to ...
6
votes
1answer
305 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. ...
6
votes
2answers
1k views

Algorithm for parts integration

Sorry if this is a duplicate, I've searched how to do this to no avail. What I'd like to do is a function that integrates by parts $n$ times, i.e $$ \int u(x) v(x) dx = u ...