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

1
vote
0answers
43 views

Rescale large numerical factors in rational functions

Given a rational function $$ f(x_1,x_2) = \dfrac{r_1 x_1^2 + r_2 x_2}{r_3 x_1 + r_4 x_2}, $$ with $r_i$ arbitrary real or complex numbers, is there a built-in function to get Mathemtica to rewrite as ...
9
votes
1answer
150 views

How to make a function to analyse a sentence?

I have two lists: grammarNonterminal = {{VP,{V,NP}},{NP,{D,N}},{S,{NP,VP}}} sentence = {{D,"the"},{N,"man"},{V,"hit"},{D,"the"},{N,"table"}} I want to make a ...
2
votes
1answer
124 views

How to constrain the range of a plot of a function to satisfy an inequality?

Given the function f[x_] := x^2, how can I plot f[x] over the range where f[x] >= 10 and ...
0
votes
1answer
30 views

Solution of a function that is in a variable [duplicate]

If I want to get the solution for a simple function, e.g. a^x, I can use: a = 2; Function[x, a^x][2] However, if I have the function in a variable, I can't get ...
3
votes
1answer
91 views

Evaluate Limit only if both directions(sides) are equal?Ex: $\lim _{x\to 0}\left(\frac{1}{x}\right)=DNE$

I want the limit of a function to only evaluate if both the left and right hand side are equal. For example $\lim _{x\to 0}\left(\frac{1}{x}\right)$ doesn't exist(two sided limit). However, the right ...
6
votes
2answers
290 views
2
votes
2answers
46 views

Deciding whether the return value of a function is defined based on its input [duplicate]

I have been met with the following problem: I have defined the following: myFunc[e_Integer]; That means, if I call: ...
4
votes
2answers
89 views

Defining arbitary Derivatives of functions

If I have two functions like these f[a_,b_] and g[a_,b_]. Lets say ...
0
votes
1answer
86 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
95 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 ...
5
votes
3answers
212 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 ...
7
votes
3answers
82 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: ...
1
vote
1answer
95 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
39 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: ...
3
votes
4answers
191 views

Pack Solve results into a vector

I am currently using a really easy function to get the eigenvectors of a corresponding eigenspace: ...
2
votes
2answers
88 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 ...
1
vote
1answer
82 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} &=& ...
2
votes
1answer
1k views

Inverse gradient operator

I found a nice paper about inverse vector operators here. I have successfully implemented a Mathematica function for most of them, however I can't figure out how to do inverse gradient (page 7 in the ...
1
vote
1answer
64 views

Defining functions that depend upon other functions which depend upon other functions and so on

The problem is that I have to evaluate functions, that take output from previous functions, and combine them to form new functions, and do some operations and evaluate the value at new function. But, ...
6
votes
3answers
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]]}
3
votes
1answer
84 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 ...
6
votes
1answer
358 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 ...
1
vote
0answers
50 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)$ ...
5
votes
2answers
186 views

Getting Arg[z] to go from $0$ to $2\pi$

I'm defining branch cut functions, and I'm using $\arg(z)$ as a building block. So I just spent an hour at the whiteboard assuming that $\arg(z)$ goes from $0$ to $2\pi$, and then I implement the ...
4
votes
3answers
139 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 ...
2
votes
1answer
98 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
46 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 ...
6
votes
4answers
237 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 & ...
4
votes
2answers
80 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
61 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 ...
2
votes
1answer
117 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 ...
9
votes
1answer
149 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, ...
2
votes
0answers
68 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. ...
3
votes
1answer
98 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
214 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
55 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
138 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
58 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 ...
9
votes
2answers
224 views

How to assemble and disassemble a function into steps

I've watched many programmers (both novice and expert) write functions in Mathematica and have observed a common behavioral pattern. This pattern has many elements that would be automated and thus ...
0
votes
1answer
471 views

A problem with functions and Infinite expression 1/0 encountering

Following code (function) makes integrals and then replace f[x] with Cos functions and calculate the definite parametric ...
1
vote
3answers
291 views

Defining a function as a sum of several Gaussians

I have a function that is a summation of several Gaussians. Working with a 1D Gaussian here, there are 3 variables for each Gaussian: A, ...
1
vote
1answer
92 views

How can I avoid a scoping problem when differentiating?

I simplify my real problem as follows: I define a rule function as Clear[rule]; rule[i_] := tt -> i; the real rule function is not this simple. I define a ...
0
votes
1answer
150 views
10
votes
1answer
238 views

Question about Rules and Pattern (The operator patt/;test)

Maybe this question is so trivial but it has confused me. I'm studying the What the @#%^&*?! do all those funny signs mean? and in the Rules and patterns under ...
0
votes
0answers
47 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
74 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 ...
15
votes
4answers
425 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
101 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 ...
3
votes
2answers
197 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].