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
1answer
76 views

High-order functions and interactive objects

I have been developing Mathematica code to interpret LaTeX code, extract formulas and apply various Mathematica operations (e.g. Simplify, ...
1
vote
1answer
80 views

ClickPane and InputField entry in a Manipulate

By clicking at a point on the slope field for an ode, a solution is plotted through that point. Additional clicks produce more plots; the plots are saved in graph ...
7
votes
3answers
77 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: ...
4
votes
2answers
81 views

Defining arbitary Derivatives of functions

If I have two functions like these f[a_,b_] and g[a_,b_]. Lets say ...
1
vote
1answer
92 views

Using a table as a variable in a function

I have a bunch of arrays and need to extract specific rows and columns of these that obey certain criteria. So for one table I'd do like this ...
1
vote
1answer
38 views

Please explain this pattern/Message related issue and help with finding workaround

I attempted to clean up my code and also add messages to functions if they are called with wrong arguments. This introduced some error which I tracked down to the following: ...
2
votes
1answer
47 views

How to make Simplify (and FullSimplify) give a default output if $Assumptions are contradictory

I have a function (deep inside my package) that looks like ...
2
votes
2answers
74 views

Function where variable is integration bound

Lets say I have a function of the form: $f(T) = T^4 \int_0^{1/T} \frac{x^3 dx}{e^x-1}$ This is an example of the situation I am describing not a unique problem. I want to define it as a numerical ...
2
votes
1answer
46 views

combine two definitions into one [closed]

Suppose I have two definitions of a function such as ...
4
votes
3answers
189 views

Make own rule for multiplication

I'm new to mathematica, so I still cannot use it properly. I want to do symbolic programming. My question is : is there any way to define our own multiplication. Suppose $a,b$ are arbitrary ...
1
vote
1answer
72 views

Define recursive function over two indices

I have $x_i=(i-1)/50$ for $i=1,\ldots,51 $. Given a function $ f(x) = x^2 $, I want to find $y_i^k$ given by \begin{eqnarray} y_i^1 &=& f(x_i) \\ y_i^{k+1} &=& ...
6
votes
3answers
301 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]]}
3
votes
1answer
73 views

InverseFunction: how can I extract the function inside?

This is probably a trivial question. But I could not find an answer. Given the explicit expression for ft in te assignment ...
1
vote
0answers
48 views

Mapping over two indices with a condition

How can I use Map over two indices with a condition? I am trying to calculate second derivative of an eigenvalue, $\lambda_i(x)$, of $n \times n$ matrix $M(x)$ ...
2
votes
1answer
83 views

Why must I specify the Global context for a symbol in a private package?

I have written two versions of the function below in a package. When I load the package and call the function, Why does the first version where the variable s has no explicit Context return the long ...
1
vote
0answers
32 views

Piecewise in 2D with quadrants as conditions

I am trying to define a piecewise vector field. Can the condition be of the form I am using (i.e. a quadrant)? I can't also make Piecewise work with more than two ...
4
votes
3answers
132 views

List of functions to list of parameters

I am working in a Dataset and I need to create a function for a ChartStyle option of a ...
4
votes
2answers
75 views

Assigning function values in a memoized recursive module

I have a complicated recursive module which I have memoized. I realised that certain isomorphs of inputs to this function will give the same output. Therefore I decided to memoize all such ...
3
votes
1answer
53 views

Prevent expression from evaluation in function when using `ToString `

I defined function fhold Clear[fhold] SetAttributes[fhold, HoldAll] fhold[para_List] := ToString[para]; What I want is that ...
5
votes
4answers
203 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 & ...
2
votes
1answer
107 views

How to properly define this function of function

I encountered real problem at first, and here is the toy example variable argument stored some expression that is generated from other parts of the program. I ...
2
votes
0answers
67 views

Automating game simulation, using previous results [closed]

Based on this question on MSE, I would like to simulate a game whereby player $2$ enters a number, so as to test the strategy. The basis for this is the factor game online here. ...
9
votes
1answer
146 views

Why there is only `HoldFirst` but no `HoldSecond`, `HoldThird`, …`HoldN`?

I feel that this must be a duplicate. But I just can't find such a post. Mathematica has HoldFirst, HoldRest, ...
3
votes
1answer
84 views

How to get name of “named row” in Dataset for function applied to each row

After some calculations and a GroupBy I have a Dataset that is structured like this. ...
4
votes
1answer
207 views

How to tame the wild MMA?

MMA is a free country. But TOO free sometimes. Just as an example: Clear[a] a[x_] == a[y_] ^:= Round[x] == Round[y] {a[3] == a[3.01], a[3] == a[4]} is {True, ...
3
votes
0answers
54 views

Memoized function with function parameter [duplicate]

I have some code that takes a function and uses the function to do some complicated computation. Thus, I wish to store this so I don't have to repeat this. Below, it is more simple than my real ...
1
vote
1answer
103 views

How to find period of a discrete sequence?

For example, {1,2,3,4,5,6,7,8} has period 1, the base is {1} {1,2,4,5,7,8,10,11} has period 3, the base is {1,2} I ...
3
votes
1answer
54 views

Setting a One Dimensional Function Equal to a Constant over a Specific Range

I am trying to take the output from NDSolve and create a variable equal to the solution except for a certain range of values. Here is the code I am trying to use ...
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 ...
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 ...
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 ...
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].
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[] ,...
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

Does Compilation option “InlineExternalDefinitions”/“InlineCompiledFunctions” lead to unnecessarily complicated nesting?

Question: When an external (possibly complicated) definition f is used inside a compiled function with ...
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)? ...
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 ...
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 ...
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 ...
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 ...
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: ...
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: ...
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 ...
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 ...
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 ...