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

0
votes
0answers
41 views

Improving numerical stability of the Goldstein-Price Function

Goldstein-Price Function is a two-dimensional test function for optimization, defined as $$f(y,z) = \left(\left(3 y^2+2 y (3 z-7)+z (3 z-14)+19\right) (y+z+1)^2+1\right) \times \left(\left(12 y^2-4 y ...
2
votes
2answers
71 views

Plot does not take range with OptionValue and PlotRange option

I am making a function that is to use Show to overlay a few plots of a function. I want the first Plot in ...
14
votes
4answers
386 views

Skipping “middle” Optional Arguments

Is there a way to call a function where the "middle" arguments should use their default value while specifying values for the "right-most" arguments? For example, given this function: ...
1
vote
1answer
99 views

How to write or extend operators like $*\!\!*$

I'm stuck on trying to define simplification rules for NonCommutativeMultiply, much like ExpandNCM found in Documentation Center, under Applications. For example, I'd like ...
11
votes
2answers
257 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[] ,...
3
votes
2answers
152 views

Pollard's Rho algorithm

I'm working in Mathematica and I'm trying to implement the [Pollard's Rho Algorithm for the Discrete Logartihm Problem][1].
0
votes
1answer
39 views

Construct a function whose definition depends on the values of its arguments and evaluates a Section

I have defined a function that returns different output according to the values of the axes of an ellipsoidal. If none of the Which statement cases is satisfied then the function evaluates the ...
6
votes
1answer
125 views
2
votes
1answer
44 views

Defining function [duplicate]

ver1 = 2 x verf[x_] := ver1 verf[3] Result : 2 x Expected Result : 6 In the above code ...
1
vote
1answer
23 views

Evaluate function entrywise on high dimensional array [closed]

I have defined a function $f$ from number to number. I want to tell Mathematica to apply that function to each entry of a high dimensional array $A$, to get a new array $f(A)$ in the same form of $A$. ...
1
vote
1answer
66 views

Defining function from table [closed]

I have a table of values of the form Table[{i1,j1}, {i2,j2}...] and I want to define a function like f[i] to take the values ...
5
votes
0answers
63 views
4
votes
3answers
198 views

How to tell Mathematica that the argument of a function is real?

I want to define the following function in mathematica: $$R(\psi)=\begin{bmatrix}1&0&0\\0&\cos 2\psi & \sin 2\psi\\0&-\sin 2\psi & \cos 2\psi\end{bmatrix}\qquad,\psi\in\mathbb ...
8
votes
1answer
320 views

Two identical functions give different answers

I am new to mathematica and I hope this is a simple problem. I have two identical functions that only differ by the variable names: ...
1
vote
2answers
52 views

Find function to relate two lists of values

I know Mathematica has the FindFit function to determine the parameters of a known function that fits a sequence of points, but what if you do not know the relationship (linear, log, quadratic, etc)? ...
6
votes
1answer
83 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 ...
3
votes
1answer
125 views

Programming a MaxMin Linear Optimization

I want to program a function for a two-player game. Basically it's like this: . Each player has an array of options, and the result of the game is based on both players choices. So heres my most ...
2
votes
1answer
122 views

How to define functions with a list of arguments

I wante to define several functions, all of them with the same arguments, for example: f[a_, b_, c_]:= ... g[a_, b_, c_]:= ... h[a_, b_, c_]:= ... How can I do ...
1
vote
1answer
87 views

How to define a periodic function from an interval to Play (monotonic) sound

I would like to define a function on an interval and then duplicate it on other intervals of the same length in order to define a (generally discontinuous) periodic function, from which I could get a ...
8
votes
2answers
147 views

Name rewriting for Function fails in closure

At its heart, Mathematica is a dynamically scoped language. While the choice of dynamic scope for Mathematica is very much a defensible one, lexical scope is too useful to do without, and Mathematica ...
1
vote
4answers
199 views

How to define a conditioned function that returns one when x is integer and zero otherwise? [closed]

What is the simplest way to define a conditioned function that returns one when x is integer and zero otherwise? I'm doing it wrong: ...
3
votes
1answer
73 views

Using functionals in Mathematica [duplicate]

I recently installed Mathematica and I wanted to define a functional in it. This is what I did: ...
21
votes
2answers
359 views

Kernel inexplicably hangs if I define a pure function without suppressing output

Bug introduced in 9.0 and fixed in 10.0.2 Save any unsaved work before trying as it may crash Mathematica and other programs by filling up the memory! In v9.0.1.0 on OSX 10.8.5, if I start a new ...
0
votes
1answer
43 views

Checking the solution of a differential equation with a boundary condition depending on an arbitrary function

I have the partial differential equation: $\qquad U_{x} - 4 x^{2} U_{t} = x U, \qquad U(x,0)=f(x), \qquad x \in (- \infty, \infty), \qquad t \in (0, \infty)$ Where $U_{x} = \frac{\partial ...
4
votes
1answer
65 views

Create a function for $\Pi^{-1}(n)$

Is it possible to create a function that gives the inverse of pP[x_] := Sum[PrimePi[x^(1/k)]/k, {k, 1, Floor[Log2[x]]}] i.e., a function that plots ...
6
votes
2answers
353 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 ...
0
votes
1answer
102 views

Element assignment in matrix function

I have to define a function of two variables that yields a high dimensional matrix, with each entry containing a scalar function, that uses its coordinates in the matrix as input. like this: ...
6
votes
2answers
272 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 ...
2
votes
3answers
168 views

data manipulation in log-log scale

Say that I have the following: y[t_] := 2 + (2/10)*(1 - Exp[-t/11]) timedat = N[Table[{t, y[t]}, {t, 0, 10, 1/1000}]]; How is it possible to construct an ...
0
votes
1answer
87 views

Optimization of a voxel projection function

I implemented a function to check if a voxel (3D pixel) is well projected on the space of a image (black and white silhouette) through a perspective camera. The function does the work but It need more ...
10
votes
3answers
375 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 ...
0
votes
1answer
70 views

a function that evaluates a Section

I have defined a function that returns different output according to the values of the axes of an ellipsoidal. The function is given below: ...
4
votes
2answers
105 views

Unsure how to nest / fold this power tower factorisation

Not quite sure how to nest this. What I would like is power tower of integer factored until all powers are primes. eg desired output for f[3*125^4] would be ...
2
votes
0answers
36 views

Unexpected behavior from Return called in a Block [duplicate]

I like to write functions that dispose of failure cases early, before they do the main calculation, but I don't like to write cascading If statements. Here is a toy ...
16
votes
1answer
659 views

Lazy lists of Tuples and Subsets

I'm trying to build a lazy list that evaluates the n'th m-tuple or subset of a given list using Mathematicas ordering without calculating all the Tuples. The purpose is to allow for example the ...
1
vote
1answer
64 views

Writing a function with two functions as arguments

I am trying to write a function of two functions. A function of one function works as in h[f___, x_, y_] := Integrate[f, x] h[x^2 + y, x, y] and this ...
9
votes
1answer
176 views

Finding large prime gaps

I am using basic searches for prime gaps as follows: ...
2
votes
3answers
87 views

Making a list of functions

Suppose I have the following equation system to solve: x(t_):={x1(t),x2(t)}; DSolve[x'(t)==x(t),x(t),t] This code works fine, but now suppose that instead of 2 ...
12
votes
4answers
208 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 ...
17
votes
3answers
466 views

Improving speed of code computing number of nonrepeating partitions

I need to answer the following for a number of parameters: How many ways can the integer $k$ be written as a sum of $n$ different integers ranging from $1$ to $m$? My initial attempt was the ...
2
votes
1answer
203 views

Issue with Coefficient command [closed]

I'm trying to used the Coefficient command to extract the numerical values in front of a Chebyshev polynomial. I know that there is a numerical way to do this, presented in numerical recipes, which I ...
0
votes
1answer
70 views

Working with functionals [closed]

I am trying to work with functionals in Mathematica, this means that I want to define a function of a function. I defined my function of a function as: ...
1
vote
2answers
43 views

Replace function with multiple functions while respecting the inputs

I want to replace a function with either one or two functions and also combine their arguments. Say I have an original expression expr = u[i + 1] r[i] r[i-1] ...
3
votes
5answers
305 views

Function involving For loop

I've written the following function (the code includes the output): ...
4
votes
2answers
233 views

Generate the Fibonacci sequence with Accumulate

Is there a way to generate the Fibonacci sequence with the Accumulate function?
3
votes
2answers
132 views

Symbolic multiplicative partitions

Let $p_n\#\equiv\prod_{k=1}^{n}p_k$ (primorial): p[n_] := Times @@ Prime[Range[n]] then the multiplicative partitions of $p_{1,2,3,4}\#$ are $$ \{\{2\}\},$$$$ ...
0
votes
0answers
56 views

Is there a IsDefinedQ function? [duplicate]

I have a function which is a bit expensive to calculate. I want to translate and integrate. I want to translate by values on a grid, store the values of the integral and refine the grid. That is, I ...