# 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.

2,247 questions
Filter by
Sorted by
Tagged with
1 vote
60 views

### How to define functions using $Z_2$ coefficients and $Z_N$ variables, e.g, realizing $1+1=0$?

A minimal example is to realize the following: $x+x = (1+1)x = 0$, and $x^{N+1}y^{N+1}=xy$, where $N$ is a given positive integer.
• 111
75 views

### Mathematica function defining problem

The "F" function is defined as mentioned, but when I call the function F inside the code, "F" can't compute value using the function F, just give F_0, F_1, etc. Can somebody guide ...
• 1
43 views

### How to double sum or triple sum up over the list of functions?

I'm dealing with a summation problem by now using a list of function $F(x; \theta_i)$. The function is defined as \begin{align*} F(x; \theta) = 1 - \Phi[\Phi^{-1}(1 - \frac{x}{2}) - \theta] + \Phi[-\...
• 101
43 views

### Simulate a binomial distribution experiment

I am trying to write a user-defined function to simulate a binomial experiment as following: ...
• 163
76 views

### How to create this function? [closed]

Let vowels:= {"A","E","I","O","U"} Let alphabet be some CharacterRange, i.e.: ...
• 9,447
266 views

### Declare a function with symmetric arguments

I have a package that contains a function f[a_,b_,c_] which is explicitly symmetric but only in its first two arguments. How do I make Mathematica simplify things ...
• 651
67 views

### Verbose Dynamic variable lists

Is there a way to achieve the following without listing all of the dynamic pieces so verbosely / manually listing all the dynamic variables? ...
• 8,005
127 views

### Display subscripts and superscripts using conditions on Format

I have a simple function Eps[a,b] that I want to display as a a particular symbol. Currently I have the code: Format[Eps[a_, b_]] = Superscript[\[Epsilon], ab] ...
• 651
58 views

### Syntax for nested If statements

This seems an awfully long-winded way to achieve the following: ...
• 8,005
41 views

### Unable to access Options from an outer function

Like most coders, I hate writing variables as strings. Symbol options among other Options assists its designed for, is also ...
• 2,009
71 views

### How to plot a function with different conditions (if or which or piecewise)

The given function is This function is plotted to be ...
• 664
170 views

### Trouble understanding function composition?

I made the following code: Cs = {{0, 1}}; f[x_] := (Clear[s]; s = (x[] - x[])/3; Splice[{{x[], x[] + s}, {x[] + 2 s, x[]}}]) Observe that we can ...
• 4,911
42 views

### How to find the sequence function $f(n,x)$ of the three given functions?

I have the three functions $f(2,x)$, $f(3,x)$ and $f(4,x)$ where $x>0$ ...
• 463
113 views

### When to use f:= x vs f[x_]:= x?

I have many compound functions, using the same variables. I can define them like this: f[x_,y_]:= ... g[x_,y_]:= ... h[x_,y_]:= f[x,y] + g[x,y] or like ...
133 views

### Best practices or hidden drawbacks to define multiple-argument functions

I was wondering if there were any important differences or stylistic considerations for deciding between different ways to define functions of multiple variables, e.g. ...
• 1,600
1 vote
42 views

### How to better display the data using ComplexListPlot?

I'm trying to plot the solution of the equation with ComplexListPlot but the plot of the data points are small and hard to see. Do you have any idea to display it better and easier to see the data? <...
• 2,484
167 views

### How to programmatically construct a function or a compiled function?

I'd like to programmatically construct a function from a long expression with many duplicate terms. The objective is to programmatically create a set of compiled functions with expressions that were ...
• 2,881
52 views

### Is it possible to name a pattern within a repeating pattern during function construction?

Question I would like to know if it is possible to have a named pattern within a repeated pattern during function desctiption. For example: f[(a_)..]:= Why I am ...
68 views

### Define functions collectively depending on an input parameter

I'm developing a package, and one of its functions is param, which reads a rather big file and extract one of the parameters in there. I have a few more functions, ...
• 185
38 views

### How to apply the map function to NMaximize to build a 2*2 table?

I have a single variable function, named globalprofitfuntion[beta]. I am using NMaximize to locate its maximizer, beta*. This function has four parameters: cv,cr,k and w. To perform sensitivity ...
208 views

### How to automatically interrupt Set with conditions

Maybe this is a bad idea: I want to inject some conditions into Set associated to specfic symbols, like ...
• 667
102 views

### Function Parameter Substitution Problem

I have this function ...
• 415
25 views

### how to set a function with a default value for some of its variable? [duplicate]

I wonder if there is a way to set a default value in a function definition. For example, I set up a function f[x_, y_] := (x + y)*2 I would like to set the default ...
• 163
88 views

### How to get $(1-x^2)^{1/2}\theta(1-x)$ continuous?

The function $f(x)=(1-x^2)^{1/2}\theta(1-x),$ where $\theta(x)$ is the Heaviside function, should be a continuous function for $x\geq-1$. Another way to define the same function is,  g(x)= \begin{...
• 33
1 vote
38 views

### How to define a function as an operation on the other option

I would like to define the function g as the Laplacian of the function f as follows: ...
• 31
201 views

### How to define functions of functions

I'm trying to define a function, call it "L", which itself is a function of other functions, such as: L[x[t], y[t]]:= x'[t]+y'[t] Then, I want to take a derivative of L[x[t], y[t]] with ...
96 views

### How to make my function to thread over additions?

I have a function f[x]. Can I make it to thread over sums automatically? I mean I want to declare it distributive against addition.
• 2,940
1 vote
92 views

### Apply function to the second level if the first level is integral from zero to infinity, otherwise, not evaluate

How can I make a function, say, R[expression] such that it works on arguments like this Integrate[f[x],{x,0,Infinity}], applying ...
• 2,940
85 views

### OptionValue not inserting correct option

I am using OptionsPattern and OptionValue to define some switches for my custom function. Here is a minimal example: ...
• 557
86 views

### How to print the function of $f[n]==f[n]$?

The funciton is as follow,and n can be 1,2,3... f[n_] := n + n^2 + n*a + (2*n + 1)*b; f f ...
• 19
56 views

The function ...
• 19
1 vote
148 views

I use ...
• 885
267 views

### How to create a sequence of functions defined recursively?

I'm trying to create a Table of functions where each new element is defined recursively via indefinite integration of the previous one. I've tried doing: ...
• 49
160 views

### Find first position satisfying condition, which is true after a certain element

Can we find the first position of elements of a list satisfying a condition, with good performance, if the conditions is a tail condition of the list. What is tail condition ? (the term is informal, ...
• 3,231
1 vote
53 views

### How do I construct and plot functions properly using NestList?

I am trying to construct a function using NestList and plot the values. Although the individually used and defined functions in my notebook seem to do the right ...
314 views

### Define functions by ‘List’

Let's say I now have a set of variables {x,y,z}，I define functions in terms of x,y, and z，I can do that f[x_, y_, z_] := x + y + z The downside of this definition ...
• 885
122 views

### How to give a property to a list of functions?

For example, I have defined a bunch of binary functions Sum1, Sum2, Prod1, ...
• 123
116 views

### Constructing a RootSum object changes existing internal syntax

Bug introduced in 9.0 or earlier and persisting through 13.0.1 or later I noticed that initially valid syntax becomes invalid within nested RootSum objects. ...
• 2,979
657 views

### What is the best method to search pattern for 2D list?

What is the best method to search patterns for 2D list ? Let L be following 2D-list : ...
• 3,231
90 views

...
• 97
1 vote
66 views

### Why does my simple string function fail to grab the only element in a List?

The idea is to normalize a string before processing it to extract information from it. Strings may come with embedded outer brackets which are easily stripped or as elements of a true list. This is ...
• 2,009
59 views

### Can substitution rules be used in function definitions?

Simple input like this f[x_]:=2x f has the predicted output of 4. However, this r={a->2x}; f[x_] := a/.r f Produces ...
• 187
72 views

### Piecewise based on the length of the list

I want to create a piecewise function that depends on the length of the list. Let's say I have a list, and a corresponding piecewise function: ...
• 857
64 views

### Create list with integer partitions satisfying some conditions

I want to create a function PartSet[N_,M_] of two positive integer variables which outputs a list of all pairs of integer partitions for all integers up to $M$ ...
• 1,307
133 views

### How to add ticks for mesh?

I'm trying to reprocedue this plot with Mathematica and got some problems. ...
• 2,484
260 views

### Association vs Pair - Creation performance, Selection performance

Suppose that we ...
• 3,231
75 views

### Replacement, PatternTest, Module related Question

Let's see how MMA handles following type of expression : Replace[L, {pattTest1->rhs1, pattTest2->rhs2, pattTest3->rhs3}, {1}] Specific example : ...
• 3,231
81 views

### Classifying elements, by the value of a fixed function, using Replace

Let L be a list of several lists. And the length of each list is less than 1000. You can create L by ...
• 3,231
1 vote
93 views

### While inside a function body does not work as expected [closed]

I'm trying to write a function that return a randomly created rational polynomial function that has a discontinuities points. I already have a function that generates random rational polynomial (...
• 69