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

6
votes
1answer
190 views

Parentheses for negative integers

Trying to standard create prime factors presentation, but can't seem to remove the parentheses: ...
6
votes
1answer
134 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
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
202 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 ...
6
votes
3answers
220 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
242 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
1answer
261 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
1answer
112 views

A question about two ways to use Default

Usually we define a function with default values by the syntax x_:default, but there is another technique to do this by registering a global default value with ...
6
votes
1answer
189 views

Property assignment on a graph vertex using PropertyValue does not work inside a function

I created this function: AddStone[board_Graph, v_, s : white | black | empty] := PropertyValue[{board, v}, VertexState] = s This function should change the ...
6
votes
1answer
183 views

Functional-Style Partial Application and the Accumulator Pattern?

Following up on Functional-style Fixed-Length Queue Object?, I want to generalize it to a functional-style accumulator pattern. Lots of things look like accumulators, loosely defined as binary ...
6
votes
1answer
76 views

How to bail out from a non-applicable transformation function?

I provide a custom function in TransformationFunctions options of FullSimplify that can do a transformation only for some values ...
6
votes
2answers
263 views

How to implement a formal expectation operator over an unknown distribution?

I need to do some simplification of an expression involving averages over a stochastic variable (in order to verify a long analytical calculation). The easiest way to do that, I figured, were if I ...
6
votes
1answer
182 views

Creating a Block from a list of rules

How can I create a function which works like Block but takes a list of rules as its first argument, rather than a list of assignments? The function needs to be ...
6
votes
1answer
250 views

Pattern definition for replacing plus and subtract

Suppose I have two functions $h_1(x)$ and $h_2(x)$, for which we know their sum and their difference: $$ h_1(x)+h_2(x) = d(x) \qquad h_1(x)-h_2(x) = i f(x) $$ I have a complicated expression, in ...
6
votes
0answers
112 views

Fine tuning compiled code that computes dilogarithm function

As an exercise of writing a good Compile function, I want to do the simple task of coding a routine that outputs the real part of the dilogarithm function ...
5
votes
3answers
2k views

Make a differentiable smooth sawtooth waveform

So, I am trying to understand the effect of sawtooth waveform on a mechanical system model. For example, a system of a plate and a box on it. And then we try to oscillate the plate. In order to do ...
5
votes
4answers
926 views

Swap two entries in a matrix

Is there a nice, simple script one can use to swap two entires in a matrix? For example, suppose we have the matrix below: $A=\begin{pmatrix}2 & 4 & 1\\ 3 & 1 & 2\\ 4 & 3 & 1 ...
5
votes
3answers
216 views

Creating a nonperiodic function in mathematica

I want to create a non-periodic square wave with values of 1 and -1(not necessarily alternating). For e.g. I want to convert an arbitrary array like {1,-1,-1,1,-1,1,-1} into a function. I tried ...
5
votes
4answers
188 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 ...
5
votes
3answers
498 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; ...
5
votes
2answers
3k 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 ...
5
votes
4answers
504 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 ...
5
votes
2answers
556 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. ...
5
votes
2answers
588 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. ...
5
votes
3answers
422 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 ...
5
votes
3answers
485 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 ...
5
votes
2answers
270 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 ...
5
votes
3answers
252 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 ...
5
votes
3answers
322 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).) ...
5
votes
3answers
812 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 ...
5
votes
2answers
4k 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 ...
5
votes
2answers
105 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 ...
5
votes
2answers
191 views

A better way of handling Module output?

I am sure this has been asked before in a way or another. I need to Save or DumpSave some symbols that contain the result of ...
5
votes
3answers
129 views

Mapping a function of the form matrix[[row,column1;;column2]] over a list

I want to create a list of lists of values by mapping values[[row, col1;;col2]] over a list like {{r1,c1,c2},{r2,c2,c3},... } to ...
5
votes
2answers
179 views

How to optimize computing a product?

Problem description Write a function to caculate the formula My solutions Solution 1: ...
5
votes
2answers
176 views

How is OptionsPattern[] defined? Can I replicate this?

I am writing several functions which all take similar input parameters (a complicated nested list structure). Is there some way for me to define a pattern that can be used in multiple places and ...
5
votes
1answer
215 views

Procedural to functional style

The following code generates an iterative mapping of some trigonometric functions. I have a couple questions about it. The first question is how to optimize this iterative process using mathematica's ...
5
votes
2answers
503 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 ...
5
votes
2answers
237 views

Optimising coefficient finding functions

I'm seeking a generalisation to this question, where I am looking to list the coefficients (without repetition) to monomials of the form ...
5
votes
1answer
212 views

How to find the name of the current function

I would like to know the name of the current function from within that function. For example, consider the following code ...
5
votes
3answers
813 views

Define an 'inner product' with AngleBracket

I want to define my own little 'Inner Product' function satisfying properties of linearity and commutativity, and I'd like to use the "$\langle$" and "$\rangle$" symbols to output my results. For ...
5
votes
1answer
90 views

Is Sow/Reap compatible with Composition?

I'm trying to figure out why these two pieces of code return different results. As expected, f[Reap[Sow[x]]] gives ...
5
votes
2answers
282 views

Define a rule of derivative in NonCommutativeMultiply?

In tensor calculation, I need to do the following thing: Define a derivative operator Drv[fun,i], such that ...
5
votes
3answers
155 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? ...
5
votes
3answers
234 views

Applying a function with the HoldAll attribute inside NestList

I'm trying to write an update function, which can be applied to a list and then to NestList it. As the function has to manipulate the given variable I figured I ...
5
votes
4answers
214 views

Defining a function with parameters as variables

I have an expression V = (a[1] + a[2])b[1]. How would I define a function of a[1], a[2] and ...
5
votes
1answer
268 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 ...
5
votes
1answer
199 views

Implementing Bilinear Products

Consider a real, vector space $V$ with basis $B=\{v_1,v_2,\dots\}$, and let $\star:V\times V\to\mathbb R$ be a bilinear product on $V$. I would like to implement this product in Mathematica by ...
5
votes
1answer
315 views

Define a function with the condition that it must be positive

I have a function f(x) and I want to define a new function g(x) that is equal to f(x) only ...
5
votes
0answers
212 views

Strange behaviour of MMA in derivatives of some standard functions

There are some peculiar things to be discovered in derivatives of some standard functions in MMA: Strange behaviour Example 1: Abs We have ...