Questions tagged [function-construction]

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.

Filter by
Sorted by
Tagged with
27
votes
1answer
10k views

Module with "local functions"

I am trying to use a Module having "local functions", i.e., those which I need to define only inside this module. So I tried this: ...
17
votes
6answers
2k views

How can I plot this spirally surface?

I would like to plot a surface that looks something like this sand spiral. But I really have no idea how. Ideally I would like to be able to express this as a function in cartesian coordinates. I'm ...
10
votes
1answer
928 views

define a function with optional arguments

I have defined a function as follows: ...
7
votes
3answers
1k 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 ...
24
votes
3answers
1k views

Modeling the spread of an infection in networked computers

I would like to show the iterative stages of infection for problem 2 in this link. In case the link dies, I have copied the text for the problem below: One hundred computers are connected in a ...
18
votes
1answer
273 views

Why Function is not identical to `&` in autocompilation?

Here is the example, copied from here square = Function[x, x^2]; square1 = #^2 &; the timing and unpacking status shows ...
21
votes
2answers
654 views

Should I always be using Module in functions unless I explicitly want global variables?

I define functions a ton. In the past, I've mostly just defined my functions like: myFunc[var1_]:=( var2=var1^2; Return@var2; ) And I haven't run into too ...
18
votes
2answers
4k views

Stereographic Projection

Say I want to represent points of the complex plane in the sphere $\Bbb S^2$ using stereographic projection. That is, the Riemann sphere: Specifically, it would be nice to be able to: Given the ...
16
votes
2answers
3k 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. ...
13
votes
7answers
4k views

Recursive function with if-statement

I am trying to represent the following function definition in Mathematica: $$\begin{align*} f(1)&=1 \\ f(2n)&= \begin{cases}f(n) & \text{if}\space n\equiv0\pmod{2} \\ 2f(n) & \text{if}...
12
votes
2answers
566 views

multiple reflections of a laser beam in a triangle

I want to make a animation that multiple reflections of a laser beam in a triangle looked like, I have tried following, but it's not a good way, I'm looking for a better way(Nested solution). ...
5
votes
2answers
413 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 $$(x_{1}^{3}+x_{2}^{3}+x_{3}^{3}+x_{4}^{3})^4\cdot(x_{1}^{2}+x_{...
10
votes
3answers
5k views

Better solution than returning a list of 3 values?

I have a function (using SetDelayed) that currently returns 3 values in a list. Later on I use the result of this list along with ...
4
votes
2answers
308 views

Improve timings for calculating coeficients

Is there a quicker way of finding ther coefficients of something like $$(x_{1}^{2}+x_{2}^{2}+x_{3}^{2}+x_{4}^{2}+x_{5}^{2}+x_{6}^{2}+x_{7}^{2}+x_{8}^{2}+x_{9}^{2}+x_{10}^{2})\times\\ (x_{1}+x_{2}+x_{...
21
votes
5answers
743 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 ...
19
votes
1answer
198 views

Why does OperatorApplied by default reverse the arguments?

Perhaps this is just one of those Mathematica curiosities, but I'd like to know why by default OperatorApplied[f][x][y] === f[y,x] versus the more usual way one ...
17
votes
3answers
397 views

Is it possible to change the order that symbol definitions are checked? (DownValues vs UpValues)

Suppose I have the function f with the following definition assigned to it: ...
15
votes
1answer
346 views

Why do subsequent evaluations seem to be stopped by a False Condition (/;)?

Exploring another problem and this example of @halirutan, I came across an evaluation behavior I don't understand. When g[] below is evaluated the first time, <...
15
votes
2answers
2k views

Converting ConditionalExpression to Piecewise

I have Solve[] return a list like ...
14
votes
2answers
515 views

How I can define this operator in Mathematica?

I want to define an operator $G$ such that $$G(f):=\begin{cases}f(\{x\}),&\lfloor x\rfloor\text{ is even}\\\frac1{f(\{x\})},&\lfloor x\rfloor\text{ is odd}\end{cases}$$ for any function $f$, ...
13
votes
4answers
829 views

How to make a custom Echo

I usually check my intermediate variable by Echo.Like Table[Echo@x, {x, 10}] But when the intermediate variable have too much ...
12
votes
2answers
1k views

How to plot a barycentric line

I want to plot a barycentric function on an equilateral triangle (ternary plot). For example f1 = {Abs[Sin[x]], Mod[x, 2], Abs[Cos[x]]}; At the moment I evaluate ...
12
votes
1answer
229 views

Is there a built-in function to inverse Thread?

Namely, while Thread @ f[{a, b}, {x, y}] gives {f[a, x], f[b, y]} the InverseThread (if it existed) should reverse the ...
11
votes
1answer
200 views

Is there a way to TagSet a HoldAllComplete function?

Let's say I want to overload the built-in function Lookup, to handle my data wrapped in myObject. One would use ...
10
votes
2answers
117 views

Extract DownValues used in function evaluation

Is there a convenient way to find out which DownValues is used when calling a function? As an example, consider the following function: ...
8
votes
3answers
122 views

Make an InterpolatingFunction periodic

Interpolation can make an InterpolatingFunction periodic with the option ...
8
votes
4answers
3k views

Force function to make assumptions about its input variables in Mathematica

I'm trying to force a Mathematica function to make an assumption about its input variables. In my case, I'm trying to define a function to return the pdf of a Gaussian. But let's use a simple toy case:...
8
votes
1answer
193 views

Position-based Inline syntax for Curry

I would like to update an existing pseudo-curry function: • /: h_[pre___, •, post___] := Function[expr, h[pre, expr, post]]; that I use inline like this - the ...
8
votes
3answers
522 views

Why these ways of defining a function with variable number of arguments fail?

First: I know about the correct way to define a function with variable number of arguments: Clear[f] f[s__] := Plus[s] f[1, 2, 3, 4] (* 10 *) BUT I ...
7
votes
1answer
199 views

Extract Jacobian from FindRoot?

I have to solve a system of fixed point equations and then calculate the eigenvalues of the Jacobian at the fixed point. There are about 50 equations with 50 variables and they contain a lot of ...
7
votes
1answer
3k views

How to find the smallest root

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

Calculating commutators in quantum mechanics symbolically with the help of Mathematica

Say I have a hamiltonian $H=p_x^2+p_y^2+x^2+y^2+x^4+y^4$, and I want to calculate the commutator $\left[A,B\right] \equiv AB-BA$ of arbitrary operators $\mathcal{O}=x^ap_x^by^cp_y^d$, where $a,b,c,d\...
6
votes
2answers
847 views

Function with Return

A simple function that adds 3 to all input and return a list containing the input and the output: test[h_] := { g = h + 3; Return[{h,g}]; }; When I ...
5
votes
3answers
936 views

Returning a function built with local (block) variables

I would like to build a block that outputs a pure function. Something like: ...
5
votes
1answer
413 views

Define a distributive function?

Here is a related question, but not quite what I'm after. I would like to define a function f[x_] that has the distributive attribute, such that: ...
4
votes
1answer
278 views

How to Override the Head of a Symbol

I was wondering if it is possible to override the head of symbols/operators. As a motivating example if I run: Then Ket will be returned. But lets say I want Bob to be returned instead of Ket. Is ...
3
votes
1answer
2k views

Piecewise smooth rectangle pulse

I am wondering how to create a single rectangle pulse with nonlinear riding and falling edges on both sides. I don't know whether it is better to do it piecewise or to mathematically transform the ...
3
votes
1answer
415 views

How can I automatically generate usage messages?

When programming, I try and name my functions and arguments clearly and document them with usage:: so I can keep track of everything; e.g., ...
11
votes
3answers
275 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 ...
10
votes
2answers
1k views

Defining the Moyal Product in Mathematica

What would be the best way to define the Moyal Product, $f(x,p)\star g(x,p)$, in Mathematica? $f(x,p)\star g(x,p)$ may be written as $f\star g=\sum\limits_{n=0}^{\infty}\frac{1}{n!}\left(\frac{i\hbar}...
9
votes
1answer
382 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[] ,...
8
votes
1answer
2k views

What is -(-2,3,4)?

I was trying to define a function f such that, when its second argument is negative, we have $$ f(a,b,c,d,\dots,n)=f(a,-b,-c,-d,\dots,-n) $$ i.e., we reverse the ...
8
votes
1answer
212 views

How does a construct like `i : func[arg_] := i = an expression using arg` work? [duplicate]

Occasionally I run into a Pattern that looks like i:func[arg_]:=i= an expression using arg I have two questions on this: Can ...
6
votes
2answers
1k views

Write a function that returns the logarithmic derivative

How can we write a function that if we input an expression f, it returns the log derivative $\frac{1}{f} \frac{df}{dx}$? We have to use a conditional or pattern ...
6
votes
2answers
411 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
195 views

Select with test function that depends on # [duplicate]

I'm trying to teach myself to use pure functions and slots as much as possible. Here is my problem. I have a list: ...
6
votes
3answers
298 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
3answers
647 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 ...
4
votes
2answers
788 views

Force Mathematica not to reduce a fraction

I am interested in a function which would stop Mathematica reducing fraction. If my input is 50/100, Mathematica outputs 1/2, I ...

1
3 4
5
6 7
9